From d97a163011f1949e30cf04bb380c391b92de97bc Mon Sep 17 00:00:00 2001 From: Spoike Date: Tue, 14 May 2013 18:38:42 +0000 Subject: [PATCH] Playing around trying to make the emscriptem port more usable/nicer. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4371 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/Makefile | 4 +- engine/gl/gl_vidcommon.c | 4 +- engine/gl/gl_vidsdl.c | 5 +-- engine/http/httpclient.c | 44 +++++++++++++++++----- engine/web/ftejslib.h | 2 + engine/web/ftejslib.js | 43 ++++++++++++++++++++++ engine/web/fteshell.html | 79 ++++++++++++++++++++++++++++++++++++++++ engine/web/prejs.js | 4 +- 8 files changed, 165 insertions(+), 20 deletions(-) create mode 100644 engine/web/ftejslib.h create mode 100644 engine/web/ftejslib.js create mode 100644 engine/web/fteshell.html diff --git a/engine/Makefile b/engine/Makefile index 7e9e43dbb..770fd9e19 100644 --- a/engine/Makefile +++ b/engine/Makefile @@ -1105,8 +1105,8 @@ ifeq ($(FTE_TARGET),web) WEB_MEMORY?=402653184 RELEASE_CFLAGS=-DOMIT_QCC -DGL_STATIC -DFTE_TARGET_WEB DEBUG_CFLAGS=-g --jcache -DOMIT_QCC -DGL_STATIC -DFTE_TARGET_WEB - RELEASE_LDFLAGS=-O1 $(WEB_PREJS) -s TOTAL_MEMORY=$(WEB_MEMORY) - DEBUG_LDLAGS=-O0 $(WEB_PREJS) -s TOTAL_MEMORY=$(WEB_MEMORY) + RELEASE_LDFLAGS=-O1 $(WEB_PREJS) -s TOTAL_MEMORY=$(WEB_MEMORY) --js-library web/ftejslib.js --shell-file web/fteshell.html + DEBUG_LDLAGS=-O0 $(WEB_PREJS) -s TOTAL_MEMORY=$(WEB_MEMORY) --js-library web/ftejslib.js CC=emcc #-s ASM_JS=1 #BASELDFLAGS= diff --git a/engine/gl/gl_vidcommon.c b/engine/gl/gl_vidcommon.c index 97121a9ec..98903cee5 100644 --- a/engine/gl/gl_vidcommon.c +++ b/engine/gl/gl_vidcommon.c @@ -28,6 +28,7 @@ void (APIENTRY *qglGetFloatv) (GLenum pname, GLfloat *params); void (APIENTRY *qglGetIntegerv) (GLenum pname, GLint *params); const GLubyte * (APIENTRY *qglGetString) (GLenum name); void (APIENTRY *qglHint) (GLenum target, GLenum mode); +GLboolean (APIENTRY *qglIsEnabled) (GLenum cap); void (APIENTRY *qglPolygonOffset) (GLfloat factor, GLfloat units); void (APIENTRY *qglReadPixels) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); void (APIENTRY *qglTexImage2D) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); @@ -110,7 +111,6 @@ void (APIENTRY *qglEnd) (void); void (APIENTRY *qglEndList) (void); void (APIENTRY *qglFrustum) (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar); GLuint (APIENTRY *qglGenLists) (GLsizei range); -GLboolean (APIENTRY *qglIsEnabled) (GLenum cap); void (APIENTRY *qglLoadIdentity) (void); void (APIENTRY *qglLoadMatrixf) (const GLfloat *m); void (APIENTRY *qglNormal3f) (GLfloat nx, GLfloat ny, GLfloat nz); @@ -1507,6 +1507,7 @@ void GL_Init(void *(*getglfunction) (char *name)) qglGetIntegerv = (void *)getglcore("glGetIntegerv"); qglGetString = (void *)getglcore("glGetString"); qglHint = (void *)getglcore("glHint"); + qglIsEnabled = (void *)getglext("glIsEnabled"); qglReadPixels = (void *)getglcore("glReadPixels"); qglTexImage2D = (void *)getglcore("glTexImage2D"); qglTexSubImage2D = (void *)getglcore("glTexSubImage2D"); @@ -1596,7 +1597,6 @@ void GL_Init(void *(*getglfunction) (char *name)) qglGetTexEnviv = (void *)getglext("glGetTexEnviv"); qglGetPointerv = (void *)getglext("glGetPointerv"); - qglIsEnabled = (void *)getglext("glIsEnabled"); qglGetStringi = (void *)getglext("glGetStringi"); diff --git a/engine/gl/gl_vidsdl.c b/engine/gl/gl_vidsdl.c index 581510b2e..87df6e6b9 100644 --- a/engine/gl/gl_vidsdl.c +++ b/engine/gl/gl_vidsdl.c @@ -31,8 +31,6 @@ qboolean GLVID_Init (rendererstate_t *info, unsigned char *palette) { int flags; - Con_Printf("SDL GLVID_Init\n"); - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE); #ifndef FTE_TARGET_WEB SDL_SetVideoMode( 0, 0, 0, 0 ); //to get around some SDL bugs @@ -89,7 +87,6 @@ void GLVID_DeInit (void) ActiveApp = false; IN_DeactivateMouse(); - Con_Printf("Restoring gamma\n"); SDL_SetGammaRamp (intitialgammaramps[0], intitialgammaramps[1], intitialgammaramps[2]); SDL_QuitSubSystem(SDL_INIT_VIDEO); @@ -149,7 +146,7 @@ qboolean GLVID_ApplyGammaRamps (unsigned short *ramps) if (gammaworks) { //we have hardware gamma applied - if we're doing a BF, we don't want to reset to the default gamma (yuck) SDL_SetGammaRamp (&ramps[0], &ramps[256], &ramps[512]); - return; + return true; } gammaworks = !SDL_SetGammaRamp (&ramps[0], &ramps[256], &ramps[512]); } diff --git a/engine/http/httpclient.c b/engine/http/httpclient.c index 5d5b6f32d..bb632764d 100644 --- a/engine/http/httpclient.c +++ b/engine/http/httpclient.c @@ -7,7 +7,14 @@ #if defined(WEBCLIENT) #if defined(FTE_TARGET_WEB) + + +#define MYJS 1 +#if MYJS +#include "web/ftejslib.h" +#else #include +#endif typedef struct { @@ -56,11 +63,11 @@ static void VFSMEM_Flush(struct vfsfile_s *file) } static vfsfile_t *VFSMEM_File(void *data, unsigned int datasize) { - /*create a file which is already unlinked*/ - mfile_t *f; - f = malloc(sizeof(*f) + datasize); - if (!f) - return NULL; + /*create a file which is already unlinked*/ + mfile_t *f; + f = malloc(sizeof(*f) + datasize); + if (!f) + return NULL; f->funcs.ReadBytes = VFSMEM_ReadBytes; f->funcs.WriteBytes = VFSMEM_WriteBytes; f->funcs.Seek = VFSMEM_Seek; @@ -82,43 +89,55 @@ static void DL_Abort(struct dl_download *dl) static void DL_OnLoad(void *c, void *data, int datasize) { struct dl_download *dl = c; - Con_Printf("download %p: success\n", dl); //make sure the file is 'open'. if (!dl->file) { if (*dl->localname) { - Con_Printf("create file\n"); FS_CreatePath(dl->localname, FS_GAME); dl->file = FS_OpenVFS(dl->localname, "w+b", FS_GAME); } else { //emscripten does not close the file. plus we seem to end up with infinite loops. - Con_Printf("temp file\n"); dl->file = VFSMEM_File(data, datasize); } } if (dl->file) { - Con_Printf("writing to file\n"); VFS_WRITE(dl->file, data, datasize); } dl->replycode = 200; +#if !MYJS dl->completed += datasize; +#endif dl->status = DL_FINISHED; } +#if MYJS +static void DL_OnError(void *c, int ecode) +#else static void DL_OnError(void *c) +#endif { struct dl_download *dl = c; Con_Printf("download %p: error\n", dl); +#if MYJS + dl->replycode = ecode; +#else dl->replycode = 404; //we don't actually know. should we not do this? +#endif dl->status = DL_FAILED; } +static void DL_OnProgress(void *c, int position, int totalsize) +{ + struct dl_download *dl = c; + dl->completed = position; + dl->totalsize = totalsize; +} //this becomes a poll function. the main thread will call this once a frame or so. qboolean HTTPDL_Decide(struct dl_download *dl) @@ -141,8 +160,13 @@ qboolean HTTPDL_Decide(struct dl_download *dl) dl->abort = DL_Abort; dl->ctx = dl; - Con_Printf("Sending %p request for %s\n", dl, url); +#if MYJS + emscriptenfte_async_wget_data2(url, dl, DL_OnLoad, DL_OnError, DL_OnProgress); +#else + //annoyingly, emscripten doesn't provide an onprogress callback, unlike firefox etc, so we can't actually tell how far its got. + //we'd need to provide our own js library to fix this. it can be done, I'm just lazy. emscripten_async_wget_data(url, dl, DL_OnLoad, DL_OnError); +#endif } return true; diff --git a/engine/web/ftejslib.h b/engine/web/ftejslib.h new file mode 100644 index 000000000..e1edec506 --- /dev/null +++ b/engine/web/ftejslib.h @@ -0,0 +1,2 @@ +void emscriptenfte_async_wget_data2(const char *url, void *ctx, void (*onload)(void*ctx,void*buf,int sz), void (*onerror)(void*ctx,int code), void (*onprogress)(void*ctx,int prog,int total)); + diff --git a/engine/web/ftejslib.js b/engine/web/ftejslib.js new file mode 100644 index 000000000..97e2c3491 --- /dev/null +++ b/engine/web/ftejslib.js @@ -0,0 +1,43 @@ + +mergeInto(LibraryManager.library, +{ + emscriptenfte_async_wget_data2 : function(url, ctx, onload, onerror, onprogress) + { + var _url = Pointer_stringify(url); + + var http = new XMLHttpRequest(); + http.open('GET', _url, true); + http.responseType = 'arraybuffer'; + + http.onload = function(e) + { + if (http.status == 200) + { + var bar = new Uint8Array(http.response); + var buf = _malloc(bar.length); + HEAPU8.set(bar, buf); + if (onload) + Runtime.dynCall('viii', onload, [ctx, buf, bar.length]); + } + else + { + if (onerror) + Runtime.dynCall('vii', onerror, [ctx, http.status]); + } + }; + + http.onerror = function(e) + { + if (onerror) + Runtime.dynCall('vii', onerror, [ctx, http.status]); + }; + + http.onprogress = function(e) + { + if (onprogress) + Runtime.dynCall('viii', onprogress, [ctx, e.loaded, e.total]); + }; + + http.send(null); + } +}); diff --git a/engine/web/fteshell.html b/engine/web/fteshell.html new file mode 100644 index 000000000..52f99994d --- /dev/null +++ b/engine/web/fteshell.html @@ -0,0 +1,79 @@ + + + + + + FTE QuakeWorld + + + +
Is javascript enabled?
+
+ +
+
+ +
+
+ Resize canvas + Lock/hide mouse pointer +     + +
+ + + + + diff --git a/engine/web/prejs.js b/engine/web/prejs.js index 493da28dd..e862aed3f 100644 --- a/engine/web/prejs.js +++ b/engine/web/prejs.js @@ -12,9 +12,9 @@ Module['preRun'] = function() //FS.createPreloadedFile('/id1/', 'pak3.pak', '/pak3.pak', true, false); }; -Module['arguments'] = ['-nohome'];//, '+connect', 'tcp://127.0.0.1:80'];//, '-manifest', document.location + '.fmf']; +Module['arguments'] = ['-nohome', '-manifest', document.location + '.fmf']; // use query string in URL as command line if (!document.referrer) { qstring = decodeURIComponent(window.location.search.substring(1)).split(" "); Module['arguments'] = Module['arguments'].concat(qstring); -} \ No newline at end of file +}