diff --git a/.gitignore b/.gitignore index ca4137a..59d4063 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ **/build/ +**/embuild/ .vscode .vs CMakeCache.txt diff --git a/.travis.yml b/.travis.yml index a31febc..1651bf5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,21 +6,29 @@ branches: jobs: include: - - stage: Build&Test - name: Linux PCMockup - script: - - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 90 - - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 90 - - bash ./travis/cmake_script.sh - - stage: Build&Test - name: Pebble - before_script: bash ./travis/pebble_before_script.sh - script: bash ./travis/pebble_script.sh - cache: - directories: - - $HOME/pebble-dev - - $HOME/.cache/pip - - $HOME/.pebble-sdk +# - stage: Build&Test +# name: Linux PCMockup +# script: +# - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 90 +# - sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 90 +# - bash ./travis/cmake_script.sh +# - stage: Build&Test +# name: Pebble +# before_script: bash ./travis/pebble_before_script.sh +# script: bash ./travis/pebble_script.sh +# cache: +# directories: +# - $HOME/pebble-dev +# - $HOME/.cache/pip +# - $HOME/.pebble-sdk + - stage: Deploy + name: Emscripten + sudo: required + services: + - docker + before_install: + - docker run -dit --name emscripten -v `pwd`:`pwd` -w `pwd` trzeci/emscripten:sdk-incoming-64bit bash + script: bash ./travis/emscripten_script.sh addons: apt: diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e6b940..70b4a6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,6 @@ endfunction(enable_warnings) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/" ${CMAKE_MODULE_PATH}) -find_package(SDL2 REQUIRED) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 11) @@ -49,10 +48,12 @@ endif() include(CTest) include(GoogleTest) +include("${CMAKE_SOURCE_DIR}/cmake/SDL2.cmake") include("${CMAKE_SOURCE_DIR}/cmake/GTest.cmake") include("${CMAKE_SOURCE_DIR}/cmake/stb.cmake") include("${CMAKE_SOURCE_DIR}/cmake/glad.cmake") include("${CMAKE_SOURCE_DIR}/cmake/cimgui.cmake") +include("${CMAKE_SOURCE_DIR}/cmake/emscripten.cmake") if (MSVC AND CMAKE_C_COMPILER_ID STREQUAL "MSVC") message(FATAL_ERROR " Microsoft C Compiler is not supported, please use GCC or Clang") diff --git a/cmake/GTest.cmake b/cmake/GTest.cmake index 4214569..d4330a2 100644 --- a/cmake/GTest.cmake +++ b/cmake/GTest.cmake @@ -1,5 +1,9 @@ # Configures googletest for use in PebbleOfDoom +if (EMSCRIPTEN) + return() +endif() + configure_file(external/GTest.CMakeLists.txt.in googletest-download/CMakeLists.txt) execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result diff --git a/cmake/SDL2.cmake b/cmake/SDL2.cmake new file mode 100644 index 0000000..832cd49 --- /dev/null +++ b/cmake/SDL2.cmake @@ -0,0 +1,4 @@ +if (EMSCRIPTEN) + return() +endif() +find_package(SDL2 REQUIRED) diff --git a/cmake/emscripten.cmake b/cmake/emscripten.cmake new file mode 100644 index 0000000..97fd7c6 --- /dev/null +++ b/cmake/emscripten.cmake @@ -0,0 +1,18 @@ +if (NOT EMSCRIPTEN) + return() +endif() + +function(add_flag flag) + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE) +endfunction() + +set(CMAKE_EXECUTABLE_SUFFIX ".html") +add_flag("-s USE_SDL=2") +add_flag("-s USE_WEBGL2=1") +math(EXPR MEMORY_SIZE "1024 * 1024 * 256") +add_flag("-s TOTAL_MEMORY=${MEMORY_SIZE}") +add_flag("-s \"BINARYEN_TRAP_MODE='clamp'\"") +add_flag("-Wno-unknown-warning-option") +add_flag("-D'glBindSampler(a,b)='") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --preload-file \"${CMAKE_CURRENT_SOURCE_DIR}/resources/@resources\"") diff --git a/pcmockup/pcmockup.c b/pcmockup/pcmockup.c index 093aff3..163f2b0 100644 --- a/pcmockup/pcmockup.c +++ b/pcmockup/pcmockup.c @@ -96,7 +96,7 @@ PCMockup *pcmockup_init() me->windowContainer, windowGrid_getSingleBounds(&windowGrid, 0), GSize(RENDERER_WIDTH, RENDERER_HEIGHT), - RendererColorFormat_1BitBW, + RendererColorFormat_8BitColor, me->renderer ); if (pebbleWindow == NULL) @@ -175,8 +175,19 @@ void pcmockup_update(PCMockup *me) } } +#ifdef __EMSCRIPTEN__ +#include +void pcmockup_emscripten_main_loop(void* userdata) { + PCMockup* me = (PCMockup*)userdata; + pcmockup_update(me); +} +#endif + void pcmockup_mainLoop(PCMockup *me) { +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop_arg(pcmockup_emscripten_main_loop, me, MAX_FRAMERATE, 1); +#else while (me->isRunning) { const uint32_t frameStart = SDL_GetTicks(); @@ -188,6 +199,7 @@ void pcmockup_mainLoop(PCMockup *me) if (delay > 0) SDL_Delay(delay); } +#endif } #undef main diff --git a/pcmockup/texturemanager.c b/pcmockup/texturemanager.c index c39d4cf..e39c708 100644 --- a/pcmockup/texturemanager.c +++ b/pcmockup/texturemanager.c @@ -110,7 +110,7 @@ TextureId textureManager_registerFile(TextureManager* me, const char* filename) uint8_t* rgbaPixels = stbi_load(pathBuffer, &width, &height, NULL, 4); if (rgbaPixels == NULL) { - fprintf(stderr, "Could not register texture file: %s\n", pathBuffer); + fprintf(stderr, "stbi_load(%s): %s\n", pathBuffer, stbi_failure_reason()); return INVALID_TEXTURE_ID; } diff --git a/pcmockup/windowcontainer.c b/pcmockup/windowcontainer.c index 46af982..e41beb9 100644 --- a/pcmockup/windowcontainer.c +++ b/pcmockup/windowcontainer.c @@ -15,6 +15,14 @@ extern void ImGui_ImplOpenGL3_Shutdown(); extern void ImGui_ImplOpenGL3_NewFrame(); extern void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data); +#ifdef __EMSCRIPTEN__ +#include +#define POD_GL_LOAD_PROC emscripten_GetProcAddress +extern void* emscripten_GetProcAddress(const char *name_); +#else +#define POD_GL_LOAD_PROC SDL_GL_GetProcAddress +#endif + #define WINDOW_CONTAINER_CHUNK 16 typedef struct MenubarHandler @@ -60,9 +68,9 @@ WindowContainer* windowContainer_init(GSize windowSize) SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); + //SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetSwapInterval(0); me->glContext = SDL_GL_CreateContext(me->window); @@ -74,7 +82,7 @@ WindowContainer* windowContainer_init(GSize windowSize) } SDL_GL_MakeCurrent(me->window, me->glContext); - if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) + if (!gladLoadGLLoader(POD_GL_LOAD_PROC)) { fprintf(stderr, "gladLoadGLLoader: %s\n", SDL_GetError()); windowContainer_free(me); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 01c9fc2..24093ec 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,9 @@ ################################################################### # tests ################################################################### +if (EMSCRIPTEN) + return() +endif() set(sources_test_podrenderer fixtures.h diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 7515a51..d8fb82d 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -2,6 +2,10 @@ # Tools ################################################################### +if (EMSCRIPTEN) + return() +endif() + add_executable(texgencli "texgencli.c" ) diff --git a/travis/emscripten_script.sh b/travis/emscripten_script.sh new file mode 100644 index 0000000..c6c87fa --- /dev/null +++ b/travis/emscripten_script.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e +mkdir -p build +cd build + +docker exec -it emscripten bash -c "cd build && emconfigure cmake .." +docker exec -it emscripten bash -c "cd build && cmake --build ." +