summaryrefslogtreecommitdiff
path: root/themes/src/shader_fetcher.cpp
blob: 19ef98315489ff1f68c284a789fc812693699ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "shader_fetcher.hpp"
#include "types.h"
#include <cstdio>
#include <emscripten/fetch.h>

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;
}