summaryrefslogtreecommitdiff
path: root/frontend/shared_cpp/WebglContext.h
blob: 017498f022cde0ab9aef22d9c36480a7bc76c6df (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
#pragma once
#include "types.h"
#include <emscripten.h>
#include <emscripten/html5.h>
#include <GLES2/gl2.h>
#include <EGL/egl.h>

struct WebglContext {
	EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context;
	int width = 640;
	int height = 480;

	void init(const char* query, int inWidth = 640, int inHeight = 480) {
		width = inWidth;
		height = inHeight;
		emscripten_set_canvas_element_size( query, width, height);
		
		EmscriptenWebGLContextAttributes attrs;
		emscripten_webgl_init_context_attributes(&attrs);

		attrs.enableExtensionsByDefault = 1;
		attrs.majorVersion = 3;
		attrs.minorVersion = 0;

		context = emscripten_webgl_create_context(query, &attrs);
		makeCurrentContext();

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);	
	};
	
	void makeCurrentContext() {
		emscripten_webgl_make_context_current(context);
	};
	
	void destroy() {
		emscripten_webgl_destroy_context(context);
	}
};