Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emscripten: first working version #84

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 23 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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)
18 changes: 18 additions & 0 deletions cmake/emscripten.cmake
Original file line number Diff line number Diff line change
@@ -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\"")
14 changes: 13 additions & 1 deletion pcmockup/pcmockup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down 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
8 changes: 8 additions & 0 deletions travis/emscripten_script.sh
Original file line number Diff line number Diff line change
@@ -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 ."