Browse Source

Setup emscripten canvas.

Branimir Karadzic 13 years ago
parent
commit
4bc42d9f97
5 changed files with 81 additions and 12 deletions
  1. 45 0
      examples/common/entry_emscripten.cpp
  2. 12 3
      premake/premake4.lua
  3. 8 1
      src/bgfx.cpp
  4. 12 8
      src/renderer_gl.cpp
  5. 4 0
      src/renderer_gl.h

+ 45 - 0
examples/common/entry_emscripten.cpp

@@ -0,0 +1,45 @@
+/*
+ * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include <bx/bx.h>
+
+#if BX_PLATFORM_EMSCRIPTEN
+
+#include <emscripten/emscripten.h>
+#include <pthread.h>
+
+extern int _main_(int _argc, char** _argv);
+
+#include <setjmp.h>
+jmp_buf s_main;
+jmp_buf s_loop;
+
+void emscripten_yield()
+{
+	if (!setjmp(s_main) )
+	{
+		longjmp(s_loop, 1);
+	}
+}
+
+void loop()
+{
+	if (!setjmp(s_loop) )
+	{
+		longjmp(s_main, 1);
+	}
+}
+
+int main(int _argc, char** _argv)
+{
+	if (!setjmp(s_loop) )
+	{
+		_main_(_argc, _argv);
+	}
+
+	emscripten_set_main_loop(loop, 10, true);
+}
+
+#endif // BX_PLATFORM_EMSCRIPTEN

+ 12 - 3
premake/premake4.lua

@@ -59,9 +59,14 @@ if _ACTION == "gmake" then
 	}
 	}
 
 
 	if "emscripten" == _OPTIONS["gcc"] then
 	if "emscripten" == _OPTIONS["gcc"] then
-		premake.gcc.cc = "emcc"
-		premake.gcc.cxx = "em++"
-		premake.gcc.ar = "emar"
+
+		if not os.getenv("EMSCRIPTEN") then 
+			print("Set EMSCRIPTEN enviroment variables.")
+		end
+
+		premake.gcc.cc = "$(EMSCRIPTEN)/emcc"
+		premake.gcc.cxx = "$(EMSCRIPTEN)/em++"
+		premake.gcc.ar = "$(EMSCRIPTEN)/emar"
 		location (BGFX_BUILD_DIR .. "projects/" .. _ACTION .. "-emscripten")
 		location (BGFX_BUILD_DIR .. "projects/" .. _ACTION .. "-emscripten")
 	end
 	end
 
 
@@ -203,6 +208,10 @@ configuration { "emscripten" }
 	targetdir (BGFX_BUILD_DIR .. "emscripten" .. "/bin")
 	targetdir (BGFX_BUILD_DIR .. "emscripten" .. "/bin")
 	objdir (BGFX_BUILD_DIR .. "emscripten" .. "/obj")
 	objdir (BGFX_BUILD_DIR .. "emscripten" .. "/obj")
 	libdirs { BGFX_THIRD_PARTY_DIR .. "lib/emscripten" }
 	libdirs { BGFX_THIRD_PARTY_DIR .. "lib/emscripten" }
+	includedirs { "$(EMSCRIPTEN)/system/include" }
+	buildoptions {
+		"-pthread",
+	}
 
 
 configuration { "nacl" }
 configuration { "nacl" }
 	defines { "_BSD_SOURCE=1", "_POSIX_C_SOURCE=199506", "_XOPEN_SOURCE=600" }
 	defines { "_BSD_SOURCE=1", "_POSIX_C_SOURCE=199506", "_XOPEN_SOURCE=600" }

+ 8 - 1
src/bgfx.cpp

@@ -215,7 +215,14 @@ namespace bgfx
 		uint8_t* rgba = mem->data;
 		uint8_t* rgba = mem->data;
 		charsetFillTexture(vga8x8, rgba, 8, pitch, bpp);
 		charsetFillTexture(vga8x8, rgba, 8, pitch, bpp);
 		charsetFillTexture(vga8x16, &rgba[8*pitch], 16, pitch, bpp);
 		charsetFillTexture(vga8x16, &rgba[8*pitch], 16, pitch, bpp);
-		m_texture = createTexture2D(2048, 24, 1, TextureFormat::L8, BGFX_TEXTURE_MIN_POINT|BGFX_TEXTURE_MAG_POINT|BGFX_TEXTURE_MIP_POINT, mem);
+		m_texture = createTexture2D(2048, 24, 1, TextureFormat::L8
+						, BGFX_TEXTURE_MIN_POINT
+						| BGFX_TEXTURE_MAG_POINT
+						| BGFX_TEXTURE_MIP_POINT
+						| BGFX_TEXTURE_U_CLAMP
+						| BGFX_TEXTURE_V_CLAMP
+						, mem
+						);
 
 
 #if BGFX_CONFIG_RENDERER_DIRECT3D9
 #if BGFX_CONFIG_RENDERER_DIRECT3D9
 		mem = makeRef(vs_debugfont_dx9, sizeof(vs_debugfont_dx9) );
 		mem = makeRef(vs_debugfont_dx9, sizeof(vs_debugfont_dx9) );

+ 12 - 8
src/renderer_gl.cpp

@@ -391,14 +391,18 @@ namespace bgfx
 					success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
 					success = eglMakeCurrent(m_display, m_surface, m_surface, m_context);
 					BGFX_FATAL(success, Fatal::OPENGL_UnableToCreateContext, "Failed to set context.");
 					BGFX_FATAL(success, Fatal::OPENGL_UnableToCreateContext, "Failed to set context.");
 
 
-#	define GL_IMPORT(_optional, _proto, _func) \
-				{ \
-					_func = (_proto)eglGetProcAddress(#_func); \
-					BX_TRACE(#_func " 0x%08x", _func); \
-					BGFX_FATAL(_optional || NULL != _func, Fatal::OPENGL_UnableToCreateContext, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_func); \
-				}
-#	include "glimports.h"
-#	undef GL_IMPORT
+#	if BX_PLATFORM_EMSCRIPTEN
+					emscripten_set_canvas_size(_width, _height);
+#	else
+#		define GL_IMPORT(_optional, _proto, _func) \
+					{ \
+						_func = (_proto)eglGetProcAddress(#_func); \
+						BX_TRACE(#_func " 0x%08x", _func); \
+						BGFX_FATAL(_optional || NULL != _func, Fatal::OPENGL_UnableToCreateContext, "Failed to create OpenGLES context. eglGetProcAddress(\"%s\")", #_func); \
+					}
+#		include "glimports.h"
+#		undef GL_IMPORT
+#	endif // !BX_PLATFORM_EMSCRIPTEN
 				}
 				}
 #endif // BX_PLATFORM_
 #endif // BX_PLATFORM_
 			}
 			}

+ 4 - 0
src/renderer_gl.h

@@ -95,6 +95,10 @@
 #		define BGFX_USE_EGL 1
 #		define BGFX_USE_EGL 1
 #	endif // BX_PLATFORM_
 #	endif // BX_PLATFORM_
 
 
+#	if BX_PLATFORM_EMSCRIPTEN
+#		include <emscripten/emscripten.h>
+#	endif // BX_PLATFORM_EMSCRIPTEN
+
 #	ifndef GL_BGRA_EXT
 #	ifndef GL_BGRA_EXT
 #		define GL_BGRA_EXT 0x80E1
 #		define GL_BGRA_EXT 0x80E1
 #	endif // GL_BGRA_EXT
 #	endif // GL_BGRA_EXT