diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/library_gl.js | 69 | ||||
-rw-r--r-- | src/library_sdl.js | 67 |
2 files changed, 100 insertions, 36 deletions
diff --git a/src/library_gl.js b/src/library_gl.js index 9dd29bb7..68f8248b 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -114,7 +114,7 @@ var LibraryGL = { var ext = Module.ctx.getExtension('WEBGL_compressed_texture_s3tc') || Module.ctx.getExtension('MOZ_WEBGL_compressed_texture_s3tc') || Module.ctx.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc'); - assert(ext, 'Failed to get texture compression WebGL extension'); + if (!ext) Module.printErr('Failed to get texture compression WebGL extension, if compressed textures are used they will fail'); } }, @@ -906,7 +906,14 @@ var LibraryGL = { $GLEmulation__postset: 'GLEmulation.init();', $GLEmulation: { + // Fog support. Partial, we assume shaders are used that implement fog. We just pass them uniforms + fogStart: 0, + fogEnd: 1, + fogColor: null, + init: function() { + GLEmulation.fogColor = new Float32Array(4); + // Add some emulation workarounds Module.printErr('WARNING: using emscripten GL emulation. This is a collection of limited workarounds, do not expect it to work'); @@ -1006,7 +1013,7 @@ var LibraryGL = { source = source.replace(/gl_ProjectionMatrix/g, 'u_projection'); if (old != source) need_pm = 1; old = source; - source = source.replace(/gl_ModelViewMatrixTranspose\[2\]/g, 'vec4(u_modelView[0][0], u_modelView[1][0], u_modelView[2][0], u_modelView[3][0])'); // XXX extremely inefficient + source = source.replace(/gl_ModelViewMatrixTranspose\[2\]/g, 'vec4(u_modelView[0][2], u_modelView[1][2], u_modelView[2][2], u_modelView[3][2])'); // XXX extremely inefficient if (old != source) need_mm = 1; old = source; source = source.replace(/gl_ModelViewMatrix/g, 'u_modelView'); @@ -1071,16 +1078,16 @@ var LibraryGL = { source = 'varying vec4 v_color; \n' + source.replace(/gl_Color/g, 'v_color'); } if (source.indexOf('gl_Fog.color') >= 0) { - source = 'uniform vec4 a_fogColor; \n' + - source.replace(/gl_Fog.color/g, 'a_fogColor'); + source = 'uniform vec4 u_fogColor; \n' + + source.replace(/gl_Fog.color/g, 'u_fogColor'); } if (source.indexOf('gl_Fog.end') >= 0) { - source = 'uniform float a_fogEnd; \n' + - source.replace(/gl_Fog.end/g, 'a_fogEnd'); + source = 'uniform float u_fogEnd; \n' + + source.replace(/gl_Fog.end/g, 'u_fogEnd'); } if (source.indexOf('gl_Fog.scale') >= 0) { - source = 'uniform float a_fogScale; \n' + - source.replace(/gl_Fog.scale/g, 'a_fogScale'); + source = 'uniform float u_fogScale; \n' + + source.replace(/gl_Fog.scale/g, 'u_fogScale'); } if (source.indexOf('gl_FogFragCoord') >= 0) { source = 'varying float v_fogFragCoord; \n' + @@ -1170,11 +1177,11 @@ var LibraryGL = { } else if (pname == 0x0BA8) { // GL_TEXTURE_MATRIX HEAPF32.set(GL.immediate.matrix['t' + GL.immediate.clientActiveTexture], params >> 2); } else if (pname == 0x0B66) { // GL_FOG_COLOR - {{{ makeSetValue('params', '0', '0', 'float') }}}; + HEAPF32.set(GLEmulation.fogColor, params >> 2); } else if (pname == 0x0B63) { // GL_FOG_START - {{{ makeSetValue('params', '0', '0', 'float') }}}; + {{{ makeSetValue('params', '0', 'GLEmulation.fogStart', 'float') }}}; } else if (pname == 0x0B64) { // GL_FOG_END - {{{ makeSetValue('params', '0', '0', 'float') }}}; + {{{ makeSetValue('params', '0', 'GLEmulation.fogEnd', 'float') }}}; } else { glGetFloatv(pname, params); } @@ -1580,6 +1587,11 @@ var LibraryGL = { this.hasNormal = normalSize > 0 && this.normalLocation >= 0; this.floatType = Module.ctx.FLOAT; // minor optimization + + this.fogColorLocation = Module.ctx.getUniformLocation(this.program, 'u_fogColor'); + this.fogEndLocation = Module.ctx.getUniformLocation(this.program, 'u_fogEnd'); + this.fogScaleLocation = Module.ctx.getUniformLocation(this.program, 'u_fogScale'); + this.hasFog = !!(this.fogColorLocation || this.fogEndLocation || this.fogScaleLocation); }, prepare: function() { @@ -1590,6 +1602,7 @@ var LibraryGL = { var end = GL.immediate.lastVertex*GL.immediate.stride; assert(end <= GL.immediate.MAX_TEMP_BUFFER_SIZE, 'too much vertex data'); arrayBuffer = GL.immediate.tempVertexBuffers[GL.immediate.tempBufferIndexLookup[end]]; + // TODO: consider using the last buffer we bound, if it was larger. downside is larger buffer, but we might avoid rebinding and preparing } else { arrayBuffer = GL.currArrayBuffer; } @@ -1661,6 +1674,11 @@ var LibraryGL = { Module.ctx.bindTexture(Module.ctx.TEXTURE_2D, texture); Module.ctx.uniform1i(this.textureLocation, 0); } + if (this.hasFog) { + if (this.fogColorLocation) Module.ctx.uniform4fv(this.fogColorLocation, GLEmulation.fogColor); + if (this.fogEndLocation) Module.ctx.uniform1f(this.fogEndLocation, GLEmulation.fogEnd); + if (this.fogScaleLocation) Module.ctx.uniform1f(this.fogScaleLocation, 1/(GLEmulation.fogEnd - GLEmulation.fogStart)); + } }, cleanup: function() { @@ -1873,9 +1891,14 @@ var LibraryGL = { } } else if (GL.immediate.mode > 6) { // above GL_TRIANGLE_FAN are the non-GL ES modes if (GL.immediate.mode != 7) throw 'unsupported immediate mode ' + GL.immediate.mode; // GL_QUADS + // GL.immediate.firstVertex is the first vertex we want. Quad indexes are in the pattern + // 0 1 2, 0 2 3, 4 5 6, 4 6 7, so we need to look at index firstVertex * 1.5 to see it. + // Then since indexes are 2 bytes each, that means 3 + assert(GL.immediate.firstVertex % 4 == 0); + ptr = GL.immediate.firstVertex*3; var numQuads = numVertexes / 4; - var numIndexes = numQuads * 6; // 0 1 2, 0 2 3 pattern - assert(numIndexes << 1 <= GL.immediate.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (b)'); + numIndexes = numQuads * 6; // 0 1 2, 0 2 3 pattern + assert(ptr + (numIndexes << 1) <= GL.immediate.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (b)'); Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.tempQuadIndexBuffer); emulatedElementArrayBuffer = true; } @@ -2018,10 +2041,26 @@ var LibraryGL = { _glColor4f({{{ makeGetValue('p', '0', 'float') }}}, {{{ makeGetValue('p', '4', 'float') }}}, {{{ makeGetValue('p', '8', 'float') }}}, {{{ makeGetValue('p', '12', 'float') }}}); }, - glFogf: function(){}, // TODO + glFogf: function(pname, param) { // partial support, TODO + switch(pname) { + case 0x0B63: // GL_FOG_START + GLEmulation.fogStart = param; break; + case 0x0B64: // GL_FOG_END + GLEmulation.fogEnd = param; break; + } + }, glFogi: function(){}, // TODO glFogx: function(){}, // TODO - glFogfv: function(){}, // TODO + glFogfv: function(pname, param) { // partial support, TODO + switch(pname) { + case 0x0B66: // GL_FOG_COLOR + GLEmulation.fogColor[0] = {{{ makeGetValue('param', '0', 'float') }}}; + GLEmulation.fogColor[1] = {{{ makeGetValue('param', '4', 'float') }}}; + GLEmulation.fogColor[2] = {{{ makeGetValue('param', '8', 'float') }}}; + GLEmulation.fogColor[3] = {{{ makeGetValue('param', '12', 'float') }}}; + break; + } + }, glPolygonMode: function(){}, // TODO diff --git a/src/library_sdl.js b/src/library_sdl.js index e940cffa..41898056 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -16,9 +16,14 @@ var LibrarySDL = { surfaces: {}, events: [], - audios: [null], fonts: [null], + audios: [null], + music: { + audio: null, + volume: 1.0 + }, + keyboardState: null, shiftKey: false, ctrlKey: false, @@ -498,6 +503,16 @@ var LibrarySDL = { } }, + setGetVolume: function(info, volume) { + if (!info) return 0; + var ret = info.volume * 128; // MIX_MAX_VOLUME + if (volume != -1) { + info.volume = volume / 128; + if (info.audio) info.audio.volume = info.volume; + } + return ret; + }, + // Debugging debugSurface: function(surfData) { @@ -600,6 +615,10 @@ var LibrarySDL = { return SDL.screen = SDL.makeSurface(width, height, flags, true, 'screen'); }, + SDL_GetVideoSurface: function() { + return SDL.screen; + }, + SDL_QuitSubSystem: function(flags) { Module.print('SDL_QuitSubSystem called (and ignored)'); }, @@ -1039,6 +1058,9 @@ var LibrarySDL = { SDL_CondWait: function() {}, SDL_DestroyCond: function() {}, + SDL_StartTextInput: function() {}, // TODO + SDL_StopTextInput: function() {}, // TODO + // SDL Mixer Mix_OpenAudio: function(frequency, format, channels, chunksize) { @@ -1058,11 +1080,13 @@ var LibrarySDL = { }, Mix_Volume: function(channel, volume) { - var info = SDL.channels[channel]; - var ret = info.volume * 128; - info.volume = volume / 128; - if (info.audio) info.audio.volume = info.volume; - return ret; + if (channel == -1) { + for (var i = 0; i < SDL.numChannels-1; i++) { + _Mix_Volume(i, volume); + } + return _Mix_Volume(SDL.numChannels-1, volume); + } + return SDL.setGetVolume(SDL.channels[channel], volume); }, Mix_SetPanning: function() { @@ -1133,13 +1157,13 @@ var LibrarySDL = { Mix_HookMusicFinished__deps: ['Mix_HaltMusic'], Mix_HookMusicFinished: function(func) { SDL.hookMusicFinished = func; - if (SDL.music) { // ensure the callback will be called, if a music is already playing - SDL.music['onended'] = _Mix_HaltMusic; + if (SDL.music.audio) { // ensure the callback will be called, if a music is already playing + SDL.music.audio['onended'] = _Mix_HaltMusic; } }, - Mix_VolumeMusic: function(func) { - return 0; // TODO + Mix_VolumeMusic: function(volume) { + return SDL.setGetVolume(SDL.music, volume); }, Mix_LoadMUS: 'Mix_LoadWAV_RW', @@ -1153,31 +1177,32 @@ var LibrarySDL = { if (!audio) return 0; audio.loop = loops != 1; // TODO: handle N loops for finite N audio.play(); + audio.volume = SDL.music.volume; audio['onended'] = _Mix_HaltMusic; // will send callback - SDL.music = audio; + SDL.music.audio = audio; return 0; }, - Mix_PauseMusic: function(id) { - var audio = SDL.audios[id]; + Mix_PauseMusic: function() { + var audio = SDL.music.audio; if (!audio) return 0; - audio.audio.pause(); + audio.pause(); return 0; }, - Mix_ResumeMusic: function(id) { - var audio = SDL.audios[id]; + Mix_ResumeMusic: function() { + var audio = SDL.music.audio; if (!audio) return 0; - audio.audio.play(); + audio.play(); return 0; }, Mix_HaltMusic: function() { - var audio = SDL.music; + var audio = SDL.music.audio; if (!audio) return 0; audio.src = audio.src; // rewind audio.pause(); - SDL.music = null; + SDL.music.audio = null; if (SDL.hookMusicFinished) { FUNCTION_TABLE[SDL.hookMusicFinished](); } @@ -1189,11 +1214,11 @@ var LibrarySDL = { Mix_FadeOutMusic: 'Mix_HaltMusic', // XXX ignore fading out effect Mix_PlayingMusic: function() { - return (SDL.music && !SDL.music.paused) ? 1 : 0; + return (SDL.music.audio && !SDL.music.audio.paused) ? 1 : 0; }, Mix_PausedMusic: function() { - return (SDL.music && SDL.music.paused) ? 1 : 0; + return (SDL.music.audio && SDL.music.audio.paused) ? 1 : 0; }, // SDL TTF |