Skip to content

Commit

Permalink
Fix wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-haerinck committed Sep 24, 2019
1 parent 18f57c4 commit 4dc363f
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 106 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
cmake_minimum_required(VERSION 3.10)
project(opengl-playground C CXX)
set(CMAKE_CXX_STANDARD 17)
set(default_build_type "Release")

# Setup Conan
if (EMSCRIPTEN)
Expand Down
128 changes: 103 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,119 @@
#include <stdio.h>
#include <SDL2/SDL.h>
#include <glad/glad.h>
#include "app.h"
#include <entt/entt.hpp>
#include <spdlog/spdlog.h>
#include <debug_break/debug_break.h>
#include <imgui.h>
#include <imgui/imgui_impl_sdl.h>
#include <imgui/imgui_impl_opengl3.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif

void gameLoop(void* data);
struct position {
float x;
float y;
};

int main(int argc, char *argv[]) {
#ifdef _WIN32 // Check memory leaks
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
void gameLoop(void* window) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

App* app = new App();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame((SDL_Window*) window);
ImGui::NewFrame();
ImGui::Begin("Main debug window");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();

#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg(gameLoop, (void *) app, 0, 0);
#else
while (app->isRunning()) {
gameLoop((void *) app);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_KEYDOWN:
printf("Key down ! \n");
break;

case SDL_MOUSEBUTTONDOWN:
printf("Button down ! \n");
break;
}
#endif
}

delete app;
return 0;
SDL_GL_SwapWindow((SDL_Window*) window);
}

void gameLoop(void* data) {
// TODO handle deltatime and sleep
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int main(int argc, char *argv[]) {
spdlog::info("It works");

App* app = static_cast<App*>(data);
// Init SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
{
printf("Cannot init SDL2 \n");
return 1;
}
SDL_GL_LoadLibrary(NULL);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
SDL_GL_SetSwapInterval(1);

app->update();
SDL_Window *window = SDL_CreateWindow(
"Roll Goal",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
500, 500,
SDL_WINDOW_OPENGL);
if (window == nullptr)
{
printf("Cannot create window \n");
}

SDL_GL_SwapWindow(app->getWindow());
SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == nullptr) {
printf("Context is null %s\n", SDL_GetError());
debug_break();
}

if (!gladLoadGLES2Loader(SDL_GL_GetProcAddress)) {
printf("Glad not init ! \n");
debug_break();
}

// ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
ImGui_ImplSDL2_InitForOpenGL(window, context);
ImGui_ImplOpenGL3_Init("#version 300 es");
ImGui::StyleColorsDark();

// Test entt
entt::registry registry;
auto myentity = registry.create();

registry.assign<position>(myentity, 0.0f, 0.0f);
registry.view<position>().each([](auto& pos) {
printf("Position x : %f \n", pos.x);
pos.x += 5;
printf("Position x : %f \n", pos.x);
});

// Test opengl
glClearColor(1, 0, 0, 1);

#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg(gameLoop, (void *) window, 0, 0);
#else
while (true) {
gameLoop(window);
}
#endif

return 0;
}
2 changes: 1 addition & 1 deletion src/systems/render-system.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "i-system.h"
#include "core/Context.h"
#include "core/context.h"

class RenderSystem : public ISystem {
public:
Expand Down
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,6 @@
};
};
</script>
<script async type="text/javascript" src="opengl-playground.js></script>
<script async type="text/javascript" src="opengl-playground.js"></script>
</body>
</html>
92 changes: 13 additions & 79 deletions www/opengl-playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,11 +1223,11 @@ function updateGlobalBufferViews() {


var STATIC_BASE = 1024,
STACK_BASE = 98432,
STACK_BASE = 99200,
STACKTOP = STACK_BASE,
STACK_MAX = 5341312,
DYNAMIC_BASE = 5341312,
DYNAMICTOP_PTR = 98176;
STACK_MAX = 5342080,
DYNAMIC_BASE = 5342080,
DYNAMICTOP_PTR = 98944;

assert(STACK_BASE % 16 === 0, 'stack must start aligned');
assert(DYNAMIC_BASE % 16 === 0, 'heap must start aligned');
Expand Down Expand Up @@ -1679,8 +1679,8 @@ Module['asm'] = function(global, env, providedBuffer) {
;
// import table
env['table'] = wasmTable = new WebAssembly.Table({
'initial': 57344,
'maximum': 57344,
'initial': 60416,
'maximum': 60416,
'element': 'anyfunc'
});
env['__memory_base'] = 1024; // tell the memory segments where to place themselves
Expand Down Expand Up @@ -1708,8 +1708,7 @@ var ASM_CONSTS = [function() { return screen.width; },
function($0, $1, $2, $3) { var SDL2 = Module['SDL2']; SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0); SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) { if ((SDL2 === undefined) || (SDL2.audio === undefined)) { return; } SDL2.audio.currentOutputBuffer = e['outputBuffer']; dynCall('vi', $2, [$3]); }; SDL2.audio.scriptProcessorNode['connect'](SDL2.audioContext['destination']); },
function($0) { var SDL2 = Module['SDL2']; if ($0) { if (SDL2.capture.silenceTimer !== undefined) { clearTimeout(SDL2.capture.silenceTimer); } if (SDL2.capture.stream !== undefined) { var tracks = SDL2.capture.stream.getAudioTracks(); for (var i = 0; i < tracks.length; i++) { SDL2.capture.stream.removeTrack(tracks[i]); } SDL2.capture.stream = undefined; } if (SDL2.capture.scriptProcessorNode !== undefined) { SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {}; SDL2.capture.scriptProcessorNode.disconnect(); SDL2.capture.scriptProcessorNode = undefined; } if (SDL2.capture.mediaStreamNode !== undefined) { SDL2.capture.mediaStreamNode.disconnect(); SDL2.capture.mediaStreamNode = undefined; } if (SDL2.capture.silenceBuffer !== undefined) { SDL2.capture.silenceBuffer = undefined } SDL2.capture = undefined; } else { if (SDL2.audio.scriptProcessorNode != undefined) { SDL2.audio.scriptProcessorNode.disconnect(); SDL2.audio.scriptProcessorNode = undefined; } SDL2.audio = undefined; } if ((SDL2.audioContext !== undefined) && (SDL2.audio === undefined) && (SDL2.capture === undefined)) { SDL2.audioContext.close(); SDL2.audioContext = undefined; } },
function($0, $1) { var SDL2 = Module['SDL2']; var numChannels = SDL2.capture.currentCaptureBuffer.numberOfChannels; for (var c = 0; c < numChannels; ++c) { var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(c); if (channelData.length != $1) { throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!'; } if (numChannels == 1) { for (var j = 0; j < $1; ++j) { setValue($0 + (j * 4), channelData[j], 'float'); } } else { for (var j = 0; j < $1; ++j) { setValue($0 + (((j * numChannels) + c) * 4), channelData[j], 'float'); } } } },
function($0, $1) { var SDL2 = Module['SDL2']; var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels']; for (var c = 0; c < numChannels; ++c) { var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c); if (channelData.length != $1) { throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!'; } for (var j = 0; j < $1; ++j) { channelData[j] = HEAPF32[$0 + ((j*numChannels + c) << 2) >> 2]; } } },
function($0) { var str = UTF8ToString($0) + '\n\n' + 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :'; var reply = window.prompt(str, "i"); if (reply === null) { reply = "i"; } return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL); }];
function($0, $1) { var SDL2 = Module['SDL2']; var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels']; for (var c = 0; c < numChannels; ++c) { var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c); if (channelData.length != $1) { throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!'; } for (var j = 0; j < $1; ++j) { channelData[j] = HEAPF32[$0 + ((j*numChannels + c) << 2) >> 2]; } } }];

function _emscripten_asm_const_i(code) {
return ASM_CONSTS[code]();
Expand Down Expand Up @@ -1738,7 +1737,7 @@ function _emscripten_asm_const_iiii(code, a0, a1, a2) {



// STATICTOP = STATIC_BASE + 97408;
// STATICTOP = STATIC_BASE + 98176;
/* global initializers */ __ATINIT__.push({ func: function() { globalCtors() } });


Expand All @@ -1749,7 +1748,7 @@ function _emscripten_asm_const_iiii(code, a0, a1, a2) {


/* no memory initializer */
var tempDoublePtr = 98416
var tempDoublePtr = 99184
assert(tempDoublePtr % 8 == 0);

function copyTempFloat(ptr) { // functions, because inlining this code increases code size too much
Expand Down Expand Up @@ -3230,11 +3229,11 @@ function copyTempDouble(ptr) {

var ERRNO_CODES={EPERM:1,ENOENT:2,ESRCH:3,EINTR:4,EIO:5,ENXIO:6,E2BIG:7,ENOEXEC:8,EBADF:9,ECHILD:10,EAGAIN:11,EWOULDBLOCK:11,ENOMEM:12,EACCES:13,EFAULT:14,ENOTBLK:15,EBUSY:16,EEXIST:17,EXDEV:18,ENODEV:19,ENOTDIR:20,EISDIR:21,EINVAL:22,ENFILE:23,EMFILE:24,ENOTTY:25,ETXTBSY:26,EFBIG:27,ENOSPC:28,ESPIPE:29,EROFS:30,EMLINK:31,EPIPE:32,EDOM:33,ERANGE:34,ENOMSG:42,EIDRM:43,ECHRNG:44,EL2NSYNC:45,EL3HLT:46,EL3RST:47,ELNRNG:48,EUNATCH:49,ENOCSI:50,EL2HLT:51,EDEADLK:35,ENOLCK:37,EBADE:52,EBADR:53,EXFULL:54,ENOANO:55,EBADRQC:56,EBADSLT:57,EDEADLOCK:35,EBFONT:59,ENOSTR:60,ENODATA:61,ETIME:62,ENOSR:63,ENONET:64,ENOPKG:65,EREMOTE:66,ENOLINK:67,EADV:68,ESRMNT:69,ECOMM:70,EPROTO:71,EMULTIHOP:72,EDOTDOT:73,EBADMSG:74,ENOTUNIQ:76,EBADFD:77,EREMCHG:78,ELIBACC:79,ELIBBAD:80,ELIBSCN:81,ELIBMAX:82,ELIBEXEC:83,ENOSYS:38,ENOTEMPTY:39,ENAMETOOLONG:36,ELOOP:40,EOPNOTSUPP:95,EPFNOSUPPORT:96,ECONNRESET:104,ENOBUFS:105,EAFNOSUPPORT:97,EPROTOTYPE:91,ENOTSOCK:88,ENOPROTOOPT:92,ESHUTDOWN:108,ECONNREFUSED:111,EADDRINUSE:98,ECONNABORTED:103,ENETUNREACH:101,ENETDOWN:100,ETIMEDOUT:110,EHOSTDOWN:112,EHOSTUNREACH:113,EINPROGRESS:115,EALREADY:114,EDESTADDRREQ:89,EMSGSIZE:90,EPROTONOSUPPORT:93,ESOCKTNOSUPPORT:94,EADDRNOTAVAIL:99,ENETRESET:102,EISCONN:106,ENOTCONN:107,ETOOMANYREFS:109,EUSERS:87,EDQUOT:122,ESTALE:116,ENOTSUP:95,ENOMEDIUM:123,EILSEQ:84,EOVERFLOW:75,ECANCELED:125,ENOTRECOVERABLE:131,EOWNERDEAD:130,ESTRPIPE:86};

var _stdin=98192;
var _stdin=98960;

var _stdout=98208;
var _stdout=98976;

var _stderr=98224;var FS={root:null,mounts:[],devices:{},streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,trackingDelegate:{},tracking:{openFlags:{READ:1,WRITE:2}},ErrnoError:null,genericErrors:{},filesystems:null,syncFSRequests:0,handleFSError:function(e) {
var _stderr=98992;var FS={root:null,mounts:[],devices:{},streams:[],nextInode:1,nameTable:null,currentPath:"/",initialized:false,ignorePermissions:true,trackingDelegate:{},tracking:{openFlags:{READ:1,WRITE:2}},ErrnoError:null,genericErrors:{},filesystems:null,syncFSRequests:0,handleFSError:function(e) {
if (!(e instanceof FS.ErrnoError)) throw e + ' : ' + stackTrace();
return ___setErrNo(e.errno);
},lookupPath:function(path, opts) {
Expand Down Expand Up @@ -10282,66 +10281,6 @@ function copyTempDouble(ptr) {
return id;
}

function _glDeleteBuffers(n, buffers) {
for (var i = 0; i < n; i++) {
var id = HEAP32[(((buffers)+(i*4))>>2)];
var buffer = GL.buffers[id];

// From spec: "glDeleteBuffers silently ignores 0's and names that do not
// correspond to existing buffer objects."
if (!buffer) continue;

GLctx.deleteBuffer(buffer);
buffer.name = 0;
GL.buffers[id] = null;

if (id == GL.currArrayBuffer) GL.currArrayBuffer = 0;
if (id == GL.currElementArrayBuffer) GL.currElementArrayBuffer = 0;
if (id == GLctx.currentPixelPackBufferBinding) GLctx.currentPixelPackBufferBinding = 0;
if (id == GLctx.currentPixelUnpackBufferBinding) GLctx.currentPixelUnpackBufferBinding = 0;
}
}

function _glDeleteProgram(id) {
if (!id) return;
var program = GL.programs[id];
if (!program) { // glDeleteProgram actually signals an error when deleting a nonexisting object, unlike some other GL delete functions.
GL.recordError(0x0501 /* GL_INVALID_VALUE */);
return;
}
GLctx.deleteProgram(program);
program.name = 0;
GL.programs[id] = null;
GL.programInfos[id] = null;
}

function _glDeleteShader(id) {
if (!id) return;
var shader = GL.shaders[id];
if (!shader) { // glDeleteShader actually signals an error when deleting a nonexisting object, unlike some other GL delete functions.
GL.recordError(0x0501 /* GL_INVALID_VALUE */);
return;
}
GLctx.deleteShader(shader);
GL.shaders[id] = null;
}

function _glDeleteTextures(n, textures) {
for (var i = 0; i < n; i++) {
var id = HEAP32[(((textures)+(i*4))>>2)];
var texture = GL.textures[id];
if (!texture) continue; // GL spec: "glDeleteTextures silently ignores 0s and names that do not correspond to existing textures".
GLctx.deleteTexture(texture);
texture.name = 0;
GL.textures[id] = null;
}
}

function _glDetachShader(program, shader) {
GLctx.detachShader(GL.programs[program],
GL.shaders[shader]);
}

function _glDisable(x0) { GLctx['disable'](x0) }


Expand Down Expand Up @@ -10566,7 +10505,7 @@ function copyTempDouble(ptr) {
function _glViewport(x0, x1, x2, x3) { GLctx['viewport'](x0, x1, x2, x3) }


var ___tm_timezone=(stringToUTF8("GMT", 98320, 4), 98320);function _gmtime_r(time, tmPtr) {
var ___tm_timezone=(stringToUTF8("GMT", 99088, 4), 99088);function _gmtime_r(time, tmPtr) {
var date = new Date(HEAP32[((time)>>2)]*1000);
HEAP32[((tmPtr)>>2)]=date.getUTCSeconds();
HEAP32[(((tmPtr)+(4))>>2)]=date.getUTCMinutes();
Expand Down Expand Up @@ -11694,11 +11633,6 @@ var asmLibraryArg = {
"_glCompileShader": _glCompileShader,
"_glCreateProgram": _glCreateProgram,
"_glCreateShader": _glCreateShader,
"_glDeleteBuffers": _glDeleteBuffers,
"_glDeleteProgram": _glDeleteProgram,
"_glDeleteShader": _glDeleteShader,
"_glDeleteTextures": _glDeleteTextures,
"_glDetachShader": _glDetachShader,
"_glDisable": _glDisable,
"_glDrawElements": _glDrawElements,
"_glEnable": _glEnable,
Expand Down
Binary file modified www/opengl-playground.wasm
Binary file not shown.

0 comments on commit 4dc363f

Please sign in to comment.