Add web support

This commit is contained in:
2023-11-21 06:09:37 +01:00
parent 9fac356492
commit 133a9740c8
4 changed files with 45 additions and 20 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
*~
.idea/
build-web/
cmake-build-debug/
cmake-build-release/
tiled/PixelDefense.tiled-session

View File

@@ -30,3 +30,8 @@ add_executable(PixelDefense
target_link_libraries(PixelDefense LINK_PRIVATE Breeze)
if (EMSCRIPTEN)
set_target_properties(PixelDefense
PROPERTIES SUFFIX ".html"
LINK_FLAGS " --bind -s WASM=1 -s STACK_SIZE=512kb -s ASSERTIONS=2 -s MIN_WEBGL_VERSION=1 --preload-file ../assets -g2 -gseparate-dwarf -gsource-map -s USE_GLFW=3")
endif()

View File

@@ -3,7 +3,11 @@ project(Breeze C)
set(CMAKE_C_STANDARD 11)
if (EMSCRIPTEN)
add_compile_options(-Wall -Wextra -Wpedantic)
else()
add_compile_options(-Wall -Wextra -Wpedantic -std=c11)
endif()
add_compile_definitions(DEBUG_MODE)
@@ -92,6 +96,6 @@ file(COPY ${BreezeHeaders} DESTINATION "include")
if (${BUILD_BREEZE_TESTS})
MESSAGE(STATUS "Building breeze tests is enabled")
add_subdirectory(tests)
#add_subdirectory(tests)
endif()

View File

@@ -28,6 +28,10 @@ extern bool bzMain(BzAppDesc *appDesc, int argc, const char **argv);
#ifdef BZ_ENTRYPOINT
#include <rlImGui.h>
#ifdef PLATFORM_WEB
#include <emscripten/emscripten.h>
#endif
// https://www.raylib.com/examples/core/loader.html?name=core_custom_logging
static void bzRaylibLogger(int msgType, const char *text, va_list args) {
BzLoggerLevel level = BZ_LOG_TRACE;
@@ -57,6 +61,29 @@ static void bzRaylibLogger(int msgType, const char *text, va_list args) {
bzLoggerOnlyLogV(level, text, args);
}
static BzAppDesc appDesc = {0, 0};
static void bzGameLoopTick() {
float dt = GetFrameTime();
if (appDesc.update)
appDesc.update(dt, appDesc.userData);
//if (ECS)
// ecs_progress(ECS, dt);
BeginDrawing();
if (appDesc.render)
appDesc.render(dt, appDesc.userData);
if (appDesc.imguiRender) {
rlImGuiBegin();
appDesc.imguiRender(dt, appDesc.userData);
rlImGuiEnd();
}
EndDrawing();
}
int main(int argc, const char **argv) {
if (!bzLoggerInit())
return 1;
@@ -65,7 +92,7 @@ int main(int argc, const char **argv) {
SetTraceLogCallback(bzRaylibLogger);
BzAppDesc appDesc = {
appDesc = (BzAppDesc){
1280,
720,
"Breeze Engine",
@@ -94,24 +121,11 @@ int main(int argc, const char **argv) {
return 1;
}
#ifdef PLATFORM_WEB
emscripten_set_main_loop(bzGameLoopTick, 0, 1);
#else
while (!WindowShouldClose()) {
float dt = GetFrameTime();
if (appDesc.update)
appDesc.update(dt, appDesc.userData);
//if (ECS)
// ecs_progress(ECS, dt);
BeginDrawing();
if (appDesc.render)
appDesc.render(dt, appDesc.userData);
if (appDesc.imguiRender) {
rlImGuiBegin();
appDesc.imguiRender(dt, appDesc.userData);
rlImGuiEnd();
}
EndDrawing();
bzGameLoopTick();
}
// User deinitialize
@@ -124,6 +138,7 @@ int main(int argc, const char **argv) {
CloseWindow();
bzLoggerDeinit();
#endif
return 0;
}