Skip to content

Commit

Permalink
emscripten: first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Mar 10, 2019
1 parent 839c22c commit 40b37e6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/build/
**/embuild/
.vscode
.vs
CMakeCache.txt
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions cmake/GTest.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions cmake/SDL2.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (EMSCRIPTEN)
return()
endif()
find_package(SDL2 REQUIRED)
17 changes: 17 additions & 0 deletions cmake/emscripten.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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("-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\"")
12 changes: 12 additions & 0 deletions pcmockup/pcmockup.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,19 @@ void pcmockup_update(PCMockup *me)
}
}

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
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();
Expand All @@ -188,6 +199,7 @@ void pcmockup_mainLoop(PCMockup *me)
if (delay > 0)
SDL_Delay(delay);
}
#endif
}

#undef main
Expand Down
2 changes: 1 addition & 1 deletion pcmockup/texturemanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 12 additions & 4 deletions pcmockup/windowcontainer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <emscripten.h>
#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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
###################################################################
# tests
###################################################################
if (EMSCRIPTEN)
return()
endif()

set(sources_test_podrenderer
fixtures.h
Expand Down
4 changes: 4 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# Tools
###################################################################

if (EMSCRIPTEN)
return()
endif()

add_executable(texgencli
"texgencli.c"
)
Expand Down

0 comments on commit 40b37e6

Please sign in to comment.