add webgl support

This commit is contained in:
2019-10-05 21:08:40 +02:00
parent a1c0dcb007
commit f2a73a905d
19 changed files with 370 additions and 11 deletions

93
webgl/src/main.cpp Normal file
View File

@@ -0,0 +1,93 @@
#include <pch.h>
#include <stdio.h>
#include <emscripten.h>
#include <thread>
#include <chrono>
#include <app.h>
#include <fstream>
App app;
GLFWwindow* wnd;
float theta = 0;
glm::vec2 g_cursor_pos;
void main_loop()
{
app.render_thread_tick();
app.ui_thread_tick();
// theta += 0.1f;
// float r = sinf(theta);
// glClearColor(r, 0.9f, 0.9f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT);
// app.tick(0);
// app.update(0);
// app.draw(0);
// SDL_GL_SwapWindow(wnd);
}
int main()
{
if (glfwInit() != GL_TRUE)
printf("Failed to init GLFW");
wnd = glfwCreateWindow(800, 600, "hello", nullptr, nullptr);
glfwMakeContextCurrent(wnd);
glfwSetCursorPosCallback(wnd, [](GLFWwindow* wnd, double x, double y){
g_cursor_pos = glm::vec2(x, y);
app.ui_task_async([=]{
app.mouse_move(x, y, 1.f, kEventSource::Mouse, false);
});
});
glfwSetMouseButtonCallback(wnd, [](GLFWwindow* wnd, int button, int action, int mods){
app.ui_task_async([=]{
if (action == GLFW_PRESS)
app.mouse_down(button, g_cursor_pos.x, g_cursor_pos.y, 1.f, kEventSource::Mouse, false);
else if (action == GLFW_RELEASE)
app.mouse_up(button, g_cursor_pos.x, g_cursor_pos.y, kEventSource::Mouse, false);
});
});
glfwSetWindowSizeCallback(wnd, [](GLFWwindow* wnd, int width, int height){
app.ui_task_async([=]{
app.resize(width, height);
});
});
glfwSetWindowCloseCallback(wnd, [](GLFWwindow* wnd){
app.ui_task([] {
if (!app.request_close())
glfwSetWindowShouldClose(app.glfw_window, GLFW_FALSE);
});
});
glfwSetWindowRefreshCallback(wnd, [](GLFWwindow* wnd){
app.ui_task_async([=]{
app.redraw = true;
}, true);
});
printf("GL version: %s\n", glGetString(GL_VERSION));
printf("GL vendor: %s\n", glGetString(GL_VENDOR));
printf("GL renderer: %s\n", glGetString(GL_RENDERER));
printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
App::I = &app;
app.initLog();
app.create();
app.width = 800;
app.height = 600;
app.glfw_window = wnd;
// app.render_thread_tick();
// app.ui_thread_tick();
app.ui_task_async([]{
app.init();
});
LOG("start threads");
//app.render_thread_start();
// app.ui_thread_start();
emscripten_set_main_loop(main_loop, 0, true);
printf("hello world 003\n");
return 0;
}