@easywasm/gl 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/package.json +35 -0
- package/src/example.c +58 -0
- package/src/glfw3.h +6577 -0
- package/web/index.js +4 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @easywasm/gl
|
|
2
|
+
|
|
3
|
+
The idea with this is easy GLFW/GL shim-layer for wasm.
|
|
4
|
+
|
|
5
|
+
It should make existing projects that use those libs easier to compile for wasm, without emscripten, but it's also an API you can link against in any wasm.
|
|
6
|
+
|
|
7
|
+
Think of it as a platform-layer. You have WASI, which covers a lot of "OS things" like filesystem, time, etc, and you have this covers all the basic stuff you need to make a game (window, graphics, input, sound.)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## basic usage
|
|
11
|
+
|
|
12
|
+
You can assume GLFW (and some of GL) is exposed, and then you can use it normally. You can see an example in [index.html](./web/index.html). The example is in [example.c](./src/example.c)
|
|
13
|
+
|
|
14
|
+
- `npm start` - compile example for web - requires wasi-sdk
|
|
15
|
+
- `npm run example:mac` - compiles same example for native mac. This is what I have for testing. Might add more build commands, later
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@easywasm/gl",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Web host interface exposing GLFW 3.5 to WebAssembly modules compiled with wasi-sdk",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "web/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./web/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src/"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"webassembly",
|
|
15
|
+
"wasm",
|
|
16
|
+
"glfw",
|
|
17
|
+
"wasi",
|
|
18
|
+
"wasi-sdk",
|
|
19
|
+
"webgl"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/easywasm/glfw"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"start": "npx -y live-server web",
|
|
31
|
+
"prestart": "npm run example:web",
|
|
32
|
+
"example:web": " /opt/wasi-sdk/bin/clang -DGLFW_INCLUDE_NONE -Wl,--allow-undefined -O3 src/example.c -o web/example.wasm",
|
|
33
|
+
"example:mac": "clang $(pkg-config --cflags --libs glfw3) -isysroot $(xcrun --show-sdk-path) -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo src/example.c -o example"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/example.c
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// this is an exmaple of wasm/native code
|
|
2
|
+
|
|
3
|
+
#include "glfw3.h"
|
|
4
|
+
#include <math.h>
|
|
5
|
+
|
|
6
|
+
#ifdef __wasm__
|
|
7
|
+
void glClearColor(float r, float g, float b, float a);
|
|
8
|
+
void glClear(unsigned int mask);
|
|
9
|
+
# define GL_COLOR_BUFFER_BIT 0x4000
|
|
10
|
+
#elif defined(__APPLE__)
|
|
11
|
+
# include <OpenGL/gl.h>
|
|
12
|
+
#else
|
|
13
|
+
# include <GL/gl.h>
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
static GLFWwindow* window = NULL;
|
|
17
|
+
|
|
18
|
+
static void key_callback(GLFWwindow* w, int key, int scancode, int action, int mods) {
|
|
19
|
+
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
20
|
+
glfwSetWindowShouldClose(w, GLFW_TRUE);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// On wasm: exported and called each tick by JS requestAnimationFrame.
|
|
24
|
+
// On native: called in the main loop below.
|
|
25
|
+
// Returns 1 to keep going, 0 to stop.
|
|
26
|
+
__attribute__((export_name("frame")))
|
|
27
|
+
int frame(void) {
|
|
28
|
+
if (!window || glfwWindowShouldClose(window)) return 0;
|
|
29
|
+
|
|
30
|
+
float t = (float)glfwGetTime();
|
|
31
|
+
glClearColor(
|
|
32
|
+
0.5f + 0.5f * sinf(t),
|
|
33
|
+
0.5f + 0.5f * sinf(t + 2.094f),
|
|
34
|
+
0.5f + 0.5f * sinf(t + 4.189f),
|
|
35
|
+
1.0f
|
|
36
|
+
);
|
|
37
|
+
glClear(GL_COLOR_BUFFER_BIT);
|
|
38
|
+
glfwSwapBuffers(window);
|
|
39
|
+
glfwPollEvents();
|
|
40
|
+
return 1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
int main(void) {
|
|
44
|
+
if (!glfwInit()) return -1;
|
|
45
|
+
|
|
46
|
+
window = glfwCreateWindow(640, 480, "GLFW Demo", NULL, NULL);
|
|
47
|
+
if (!window) { glfwTerminate(); return -1; }
|
|
48
|
+
|
|
49
|
+
glfwMakeContextCurrent(window);
|
|
50
|
+
glfwSetKeyCallback(window, key_callback);
|
|
51
|
+
|
|
52
|
+
#ifndef __wasm__
|
|
53
|
+
while (frame()) {}
|
|
54
|
+
glfwTerminate();
|
|
55
|
+
#endif
|
|
56
|
+
|
|
57
|
+
return 0;
|
|
58
|
+
}
|