From 5d3ff36045bfb11af0f373d5ecee7c9b54e4c4f9 Mon Sep 17 00:00:00 2001 From: mattkae Date: Mon, 3 Apr 2023 06:35:27 -0400 Subject: Folderized all of the seasons and loading the shaders from a remote source --- themes/src/shader_fetcher.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 themes/src/shader_fetcher.cpp (limited to 'themes/src/shader_fetcher.cpp') diff --git a/themes/src/shader_fetcher.cpp b/themes/src/shader_fetcher.cpp new file mode 100644 index 0000000..19ef983 --- /dev/null +++ b/themes/src/shader_fetcher.cpp @@ -0,0 +1,69 @@ +#include "shader_fetcher.hpp" +#include "types.h" +#include +#include + +struct FetchtimeData { + ShaderFetchResult result; + ShaderFetchPaths paths_data; + void (*cb)(ShaderFetchResult*); +}; + +void on_failure(emscripten_fetch_t *fetch) { + FetchtimeData* ftd = (FetchtimeData*)fetch->userData; + printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status); + emscripten_fetch_close(fetch); // Also free data on failure. + ftd->cb(nullptr); +} + +void on_fragment_shader(emscripten_fetch_t *fetch) { + FetchtimeData* ftd = (FetchtimeData*)fetch->userData; + printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); + const i32 len = fetch->numBytes; + char* data = (char*)fetch->data; + data[len - 1] = '\0'; + ftd->result.fragment = data; + emscripten_fetch_close(fetch); // Free data associated with the fetch. + + ftd->cb(&ftd->result); + delete ftd; +} + +void on_vertex_shader(emscripten_fetch_t *fetch) { + + FetchtimeData* ftd = (FetchtimeData*)fetch->userData; + printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url); + const i32 len = fetch->numBytes; + char* data = (char*)fetch->data; + data[len - 1] = '\0'; + + ftd->result.vertex = data; + emscripten_fetch_close(fetch); // Free data associated with the fetch. + + // Fetch fragment shader next + emscripten_fetch_attr_t attr; + emscripten_fetch_attr_init(&attr); + strcpy(attr.requestMethod, "GET"); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; + attr.onsuccess = on_fragment_shader; + attr.onerror = on_failure; + auto* request = emscripten_fetch(&attr, ftd->paths_data.fragment); + request->userData = ftd; +} + +void fetch_shader(ShaderFetchPaths paths, void (*cb)(ShaderFetchResult*), void* user_data) { + FetchtimeData* ftd = new FetchtimeData(); + ftd->cb = cb; + ftd->paths_data = paths; + ftd->result.user_data = user_data; + + // Fetch vertex shader + emscripten_fetch_attr_t attr; + emscripten_fetch_attr_init(&attr); + strcpy(attr.requestMethod, "GET"); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; + attr.onsuccess = on_vertex_shader; + attr.onerror = on_failure; + auto* request = emscripten_fetch(&attr, paths.vertex); + request->userData = ftd; +} \ No newline at end of file -- cgit v1.2.1