Skip to content

Commit

Permalink
Add minimal emscripten support
Browse files Browse the repository at this point in the history
  • Loading branch information
zturtleman authored and mgerhardy committed Jun 9, 2024
1 parent 265d9db commit c459736
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
16 changes: 16 additions & 0 deletions code/qcommon/q_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#endif

//================================================================== EMSCRIPTEN ===

#ifdef __EMSCRIPTEN__

#define OS_STRING "emscripten"
#define ID_INLINE inline
#define PATH_SEP '/'

#define ARCH_STRING "wasm32"

#define Q3_LITTLE_ENDIAN

#define DLL_EXT ".wasm"

#endif

//================================================================== Q3VM ===

#ifdef Q3_VM
Expand Down
17 changes: 16 additions & 1 deletion code/sdl/sdl_glimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder, qbool
int profileMask;
int majorVersion;
int minorVersion;
} contexts[3];
} contexts[4];
int numContexts, type;
const char *glstring;
int perChannelColorBits;
Expand Down Expand Up @@ -502,6 +502,14 @@ static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder, qbool
(r_preferOpenGLES->integer == -1 && profileMask == SDL_GL_CONTEXT_PROFILE_ES));

if (preferOpenGLES) {
#ifdef __EMSCRIPTEN__
// WebGL 2.0 isn't fully backward compatible so you have to ask for it specifically
contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
contexts[numContexts].majorVersion = 3;
contexts[numContexts].minorVersion = 0;
numContexts++;
#endif

contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
contexts[numContexts].majorVersion = 2;
contexts[numContexts].minorVersion = 0;
Expand All @@ -519,6 +527,13 @@ static int GLimp_SetMode(int mode, qboolean fullscreen, qboolean noborder, qbool
numContexts++;

if (!preferOpenGLES) {
#ifdef __EMSCRIPTEN__
contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
contexts[numContexts].majorVersion = 3;
contexts[numContexts].minorVersion = 0;
numContexts++;
#endif

contexts[numContexts].profileMask = SDL_GL_CONTEXT_PROFILE_ES;
contexts[numContexts].majorVersion = 2;
contexts[numContexts].minorVersion = 0;
Expand Down
8 changes: 8 additions & 0 deletions code/sys/sys_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include <ctype.h>
#include <errno.h>

#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif

#ifndef DEDICATED
#ifdef USE_LOCAL_HEADERS
#include "SDL.h"
Expand Down Expand Up @@ -787,9 +791,13 @@ int main(int argc, char **argv) {
signal(SIGTERM, Sys_SigHandler);
signal(SIGINT, Sys_SigHandler);

#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(Com_Frame, 0, 1);
#else
while (1) {
Com_Frame();
}
#endif

return 0;
}

0 comments on commit c459736

Please sign in to comment.