From: DeaDDooMER Date: Fri, 3 Apr 2020 07:52:40 +0000 (+0300) Subject: ports: ported to emscripten X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=b047ed3a09b3738defa4f0c323dbe698ff6831ce;p=flatwaifu.git ports: ported to emscripten --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e496d8c..d395262 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,10 @@ cmake_minimum_required(VERSION 3.6) project(doom2d C) +string(COMPARE EQUAL "${CMAKE_SYSTEM_NAME}" "Emscripten" D2D_FOR_EMSCRIPTEN) +string(COMPARE EQUAL "${CMAKE_SYSTEM_NAME}" "Darwin" D2D_FOR_DARWIN) +string(COMPARE EQUAL "${CMAKE_SYSTEM_PROCESSOR}" "powerpc" D2D_FOR_POWERPC) + option(SYSTEM_DRIVER "Build with selected system driver" "SDL") option(RENDER_DRIVER "Build with selected render driver" "OpenGL") option(SOUND_DRIVER "Build with selected sound driver" "OpenAL") @@ -28,9 +32,6 @@ set(D2D_SDLMIXER_ROOT ${D2D_GAME_ROOT}/sdlmixer) set(D2D_OPENAL_ROOT ${D2D_GAME_ROOT}/openal) set(D2D_STUBSOUND_ROOT ${D2D_GAME_ROOT}/stubsnd) -string(COMPARE EQUAL "${CMAKE_SYSTEM_NAME}" "Darwin" D2D_FOR_DARWIN) -string(COMPARE EQUAL "${CMAKE_SYSTEM_PROCESSOR}" "powerpc" D2D_FOR_POWERPC) - aux_source_directory(${D2D_GAME_ROOT} D2D_GAME_SRC) aux_source_directory(${D2D_SDL_ROOT} D2D_SDL_SRC) aux_source_directory(${D2D_SDL2_ROOT} D2D_SDL2_SRC) @@ -43,44 +44,65 @@ aux_source_directory(${D2D_OPENAL_ROOT} D2D_OPENAL_SRC) aux_source_directory(${D2D_STUBSOUND_ROOT} D2D_STUBSOUND_SRC) if(WITH_SDL) - find_package(SDL REQUIRED) - set(D2D_SYSTEM_SRC "${D2D_SDL_SRC}") - set(D2D_SYSTEM_INCLUDE_DIR "${SDL_INCLUDE_DIR}") - set(D2D_SYSTEM_LIBRARY "${SDL_LIBRARY}") + if(D2D_FOR_EMSCRIPTEN) + set(D2D_SYSTEM_SRC "${D2D_SDL_SRC}") + set(D2D_SYSTEM_INCLUDE_DIR "") + set(D2D_SYSTEM_LIBRARY "-lSDL") + set(D2D_SYSTEM_LINKFLAGS "-s USE_SDL=1") + else() + find_package(SDL REQUIRED) + set(D2D_SYSTEM_SRC "${D2D_SDL_SRC}") + set(D2D_SYSTEM_INCLUDE_DIR "${SDL_INCLUDE_DIR}") + set(D2D_SYSTEM_LIBRARY "${SDL_LIBRARY}") + endif() elseif(WITH_SDL2) - if(D2D_FOR_DARWIN AND D2D_FOR_POWERPC) - # tigerbrew did not provide sdl2 module for cmake - find_package(SDL2) - if(NOT SDL2_FOUND) - set(SDL2_INCLUDE_DIRS "/usr/local/include/SDL2") - set(SDL2_LIBRARIES "-lSDL2") - endif(NOT SDL2_FOUND) - else(D2D_FOR_DARWIN AND D2D_FOR_POWERPC) - find_package(SDL2 REQUIRED) - endif(D2D_FOR_DARWIN AND D2D_FOR_POWERPC) - set(D2D_SYSTEM_SRC "${D2D_SDL2_SRC}") - set(D2D_SYSTEM_INCLUDE_DIR "${SDL2_INCLUDE_DIRS}") - set(D2D_SYSTEM_LIBRARY "${SDL2_LIBRARIES}") -else(WITH_SDL) + if(D2D_FOR_EMSCRIPTEN) + set(D2D_SYSTEM_SRC "${D2D_SDL_SRC}") + set(D2D_SYSTEM_INCLUDE_DIR "") + set(D2D_SYSTEM_LIBRARY "-lSDL2") + set(D2D_SYSTEM_LINKFLAGS "-s USE_SDL=2") + else() + if(D2D_FOR_DARWIN AND D2D_FOR_POWERPC) + # tigerbrew did not provide sdl2 module for cmake + find_package(SDL2) + if(NOT SDL2_FOUND) + set(SDL2_INCLUDE_DIRS "/usr/local/include/SDL2") + set(SDL2_LIBRARIES "-lSDL2") + endif(NOT SDL2_FOUND) + else() + find_package(SDL2 REQUIRED) + endif() + set(D2D_SYSTEM_SRC "${D2D_SDL2_SRC}") + set(D2D_SYSTEM_INCLUDE_DIR "${SDL2_INCLUDE_DIRS}") + set(D2D_SYSTEM_LIBRARY "${SDL2_LIBRARIES}") + endif() +else() message(FATAL_ERROR "Select SYSTEM_DRIVER as 'SDL' or 'SDL2'") -endif(WITH_SDL) +endif() if(WITH_STUBRENDER) set(D2D_RENDER_SRC "${D2D_STUBRENDER_SRC}") set(D2D_RENDER_INCLUDE_DIR "") set(D2D_RENDER_LIBRARY "") elseif(WITH_OPENGL) - find_package(OpenGL REQUIRED) - set(D2D_RENDER_SRC "${D2D_OPENGL_SRC}") - set(D2D_RENDER_INCLUDE_DIR "${OPENGL_INCLUDE_DIR}") - set(D2D_RENDER_LIBRARY "${OPENGL_LIBRARY}") + if(D2D_FOR_EMSCRIPTEN) + set(D2D_RENDER_SRC "${D2D_OPENGL_SRC}") + set(D2D_RENDER_INCLUDE_DIR "") + set(D2D_RENDER_LIBRARY "-lGL") + set(D2D_RENDER_LINKFLAGS "-s LEGACY_GL_EMULATION=1") + else() + find_package(OpenGL REQUIRED) + set(D2D_RENDER_SRC "${D2D_OPENGL_SRC}") + set(D2D_RENDER_INCLUDE_DIR "${OPENGL_INCLUDE_DIR}") + set(D2D_RENDER_LIBRARY "${OPENGL_LIBRARY}") + endif() elseif(WITH_SOFTWARE) set(D2D_RENDER_SRC "${D2D_SOFTWARE_SRC}") set(D2D_RENDER_INCLUDE_DIR "") set(D2D_RENDER_LIBRARY "") -else(WITH_STUBRENDER) +else() message(FATAL_ERROR "Select RENDER_DRIVER as 'OPENGL' or 'SOFTWARE' or 'STUB'") -endif(WITH_STUBRENDER) +endif() if(WITH_STUBSOUND) set(D2D_SOUND_SRC "${D2D_STUBSOUND_SRC}") @@ -119,4 +141,11 @@ message(STATUS "SOUND: " "${SOUND_DRIVER}") add_executable(doom2d ${D2D_GAME_SRC} ${D2D_SYSTEM_SRC} ${D2D_RENDER_SRC} ${D2D_SOUND_SRC}) target_include_directories(doom2d PRIVATE "${D2D_GAME_ROOT}" "${D2D_SYSTEM_INCLUDE_DIR}" "${D2D_RENDER_INCLUDE_DIR}" "${D2D_SOUND_INCLUDE_DIR}") -target_link_libraries(doom2d "${D2D_SYSTEM_LIBRARY}" "${D2D_RENDER_LIBRARY}" "${D2D_SOUND_LIBRARY}") +if(D2D_FOR_EMSCRIPTEN) + set(CMAKE_EXECUTABLE_SUFFIX ".html") + configure_file(${D2D_GAME_ROOT}/doom2d.wad doom2d.wad COPYONLY) + set(D2D_DOOM2D_WAD doom2d.wad) + set_target_properties(doom2d PROPERTIES LINK_FLAGS "-s WASM=0 -s DISABLE_EXCEPTION_CATCHING=1 -s SAFE_HEAP=1 -s ASSERTIONS=1 -s GL_ASSERTIONS=1 ${D2D_SYSTEM_LINKFLAGS} ${D2D_RENDER_LINKFLAGS} ${D2D_SOUND_LINKFLAGS} --preload-file ${D2D_DOOM2D_WAD}") +else() + target_link_libraries(doom2d "${D2D_SYSTEM_LIBRARY}" "${D2D_RENDER_LIBRARY}" "${D2D_SOUND_LIBRARY}") +endif() diff --git a/src/sdl/main.c b/src/sdl/main.c index db74674..b3627fc 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -20,7 +20,11 @@ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "SDL.h" +#ifdef __EMSCRIPTEN__ +# include +#endif + +#include #include #include #include // srand exit @@ -46,6 +50,7 @@ #define MODE_OPENGL 1 #define MODE_SOFTWARE 2 +static Uint32 ticks; static int quit = 0; static SDL_Surface *surf = NULL; static int mode = MODE_NONE; @@ -113,6 +118,8 @@ int Y_set_videomode_opengl (int w, int h, int fullscreen) { if (s != NULL) { mode = MODE_OPENGL; surf = s; + } else { + logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError()); } } return s != NULL; @@ -159,8 +166,10 @@ int Y_videomode_setted (void) { void Y_unset_videomode (void) { surf = NULL; mode = MODE_NONE; +#ifndef __EMSCRIPTEN__ SDL_QuitSubSystem(SDL_INIT_VIDEO); SDL_InitSubSystem(SDL_INIT_VIDEO); +#endif } void Y_set_fullscreen (int fullscreen) { @@ -358,9 +367,19 @@ static void poll_events (void (*h)(int key, int down)) { } } +static void step (void) { + poll_events(&G_keyf); + S_updatemusic(); + Uint32 t = SDL_GetTicks(); + if (t - ticks > DELAY) { + ticks = t; + G_act(); + } + R_draw(); +} + int main (int argc, char *argv[]) { char *pw; - Uint32 t, ticks; logo("main: initialize SDL\n"); if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { logo("main: failed to init SDL: %s\n", SDL_GetError()); @@ -409,16 +428,13 @@ int main (int argc, char *argv[]) { R_init(); G_init(); ticks = SDL_GetTicks(); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(step, 0, 1); +#else while (!quit) { - poll_events(&G_keyf); - S_updatemusic(); - t = SDL_GetTicks(); - if (t - ticks > DELAY) { - ticks = t; - G_act(); - } - R_draw(); + step(); } +#endif CFG_save(); R_done(); S_donemusic(); diff --git a/src/sdl2/main.c b/src/sdl2/main.c index dd9de83..1375a29 100644 --- a/src/sdl2/main.c +++ b/src/sdl2/main.c @@ -1,4 +1,8 @@ -#include "SDL.h" +#ifdef __EMSCRIPTEN__ +# include +#endif + +#include #include #include #include // srand exit @@ -22,6 +26,7 @@ #define TITLE_STR "DooM 2D (SDL2)" +static Uint32 ticks; static int quit = 0; static SDL_Window *window; static SDL_GLContext context; @@ -105,8 +110,14 @@ int Y_set_videomode_opengl (int w, int h, int fullscreen) { flags = flags | SDL_WINDOW_FULLSCREEN; } // TODO set context version and type +#ifdef __EMSCRIPTEN__ + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#else SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); +#endif SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); if (win != NULL) { @@ -122,6 +133,9 @@ int Y_set_videomode_opengl (int w, int h, int fullscreen) { } } } + if (win == NULL) { + logo("Y_set_videomode_opengl: error: %s\n", SDL_GetError()); + } return win != NULL; } @@ -152,6 +166,9 @@ int Y_set_videomode_software (int w, int h, int fullscreen) { } } } + if (win == NULL) { + logo("Y_set_videomode_software: error: %s\n", SDL_GetError()); + } return win != NULL; } @@ -392,15 +409,24 @@ static void poll_events (void (*h)(int key, int down)) { } } +static void step (void) { + poll_events(&G_keyf); + S_updatemusic(); + Uint32 t = SDL_GetTicks(); + if (t - ticks > DELAY) { + ticks = t; + G_act(); + } + R_draw(); +} + int main (int argc, char **argv) { char *pw; - Uint32 t, ticks; logo("system: initialize SDL2\n"); if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) { logo("system: failed to init SDL2: %s\n", SDL_GetError()); return 1; } - //SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D"); // Player 1 defaults pl1.ku = KEY_KP_8; pl1.kd = KEY_KP_5; @@ -443,16 +469,13 @@ int main (int argc, char **argv) { R_init(); G_init(); ticks = SDL_GetTicks(); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(step, 0, 1); +#else while (!quit) { - poll_events(&G_keyf); - S_updatemusic(); - t = SDL_GetTicks(); - if (t - ticks > DELAY) { - ticks = t; - G_act(); - } - R_draw(); + step(); } +#endif CFG_save(); R_done(); S_donemusic(); diff --git a/src/sdlmixer/sound.c b/src/sdlmixer/sound.c index 92334c7..f8ef47e 100644 --- a/src/sdlmixer/sound.c +++ b/src/sdlmixer/sound.c @@ -9,6 +9,7 @@ #include "SDL.h" #include "SDL_mixer.h" #include +#include #define TAG_MIX1 0x4d495831