From 0e2b3d08120cb42b70f85556bf14997d2d003962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 7 Nov 2013 02:32:27 -0300 Subject: [PATCH] Working on emscripten support ( compiles but doesn't render anything ). Fixed a bug with a shader in OpenGL ES 2 ( i haven't seen this before, it seems that it new ). --- include/eepp/declares.hpp | 26 ++++++----- premake4.lua | 44 ++++++++++++++++--- projects/android-project/jni/Application.mk | 6 +-- projects/emscripten/make.sh | 6 +++ src/eepp/graphics/renderer/cgl.cpp | 6 +-- .../graphics/renderer/shaders/clipped.frag | 5 +++ .../graphics/renderer/shaders/clipped.vert | 9 +++- src/eepp/helper/haikuttf/hkbase.hpp | 2 +- src/eepp/window/backend/SDL/cinputsdl.cpp | 16 +++++++ src/eepp/window/backend/SDL/cwindowsdl.cpp | 3 ++ 10 files changed, 95 insertions(+), 28 deletions(-) create mode 100755 projects/emscripten/make.sh diff --git a/include/eepp/declares.hpp b/include/eepp/declares.hpp index 4cf4c3dcc..0cc9b4a9a 100644 --- a/include/eepp/declares.hpp +++ b/include/eepp/declares.hpp @@ -4,14 +4,15 @@ #include #include -#define EE_PLATFORM_WIN 1 -#define EE_PLATFORM_LINUX 2 -#define EE_PLATFORM_MACOSX 3 -#define EE_PLATFORM_BSD 4 -#define EE_PLATFORM_SOLARIS 5 -#define EE_PLATFORM_HAIKU 6 -#define EE_PLATFORM_ANDROID 7 -#define EE_PLATFORM_IOS 8 +#define EE_PLATFORM_WIN 1 +#define EE_PLATFORM_LINUX 2 +#define EE_PLATFORM_MACOSX 3 +#define EE_PLATFORM_BSD 4 +#define EE_PLATFORM_SOLARIS 5 +#define EE_PLATFORM_HAIKU 6 +#define EE_PLATFORM_ANDROID 7 +#define EE_PLATFORM_IOS 8 +#define EE_PLATFORM_EMSCRIPTEN 9 #if defined( __WIN32__ ) || defined( _WIN32 ) || defined( _WIN64 ) #define EE_PLATFORM EE_PLATFORM_WIN @@ -43,7 +44,8 @@ #else #define EE_PLATFORM EE_PLATFORM_MACOSX #endif - +#elif defined( __emscripten__ ) || defined( EMSCRIPTEN ) + #define EE_PLATFORM EE_PLATFORM_EMSCRIPTEN #elif defined( __ANDROID__ ) || defined( ANDROID ) #define EE_PLATFORM EE_PLATFORM_ANDROID #elif defined ( linux ) || defined( __linux__ ) @@ -56,12 +58,12 @@ #define EE_PLATFORM EE_PLATFORM_HAIKU #endif -#if EE_PLATFORM == EE_PLATFORM_ANDROID || EE_PLATFORM == EE_PLATFORM_IOS +#if EE_PLATFORM == EE_PLATFORM_ANDROID || EE_PLATFORM == EE_PLATFORM_IOS || EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN #if !defined( EE_GLES1 ) && !defined( EE_GLES2 ) #define EE_GLES2 #endif - #ifndef EE_PLATFORM_TOUCH + #if !defined( EE_PLATFORM_TOUCH ) && EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN #define EE_PLATFORM_TOUCH #endif #endif @@ -77,7 +79,7 @@ #endif //! Since EE just use basic POSIX stuff, declare as POSIX some OS that are mostly POSIX-compliant -#if defined ( linux ) || defined( __linux__ ) || defined( __FreeBSD__ ) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined( __DragonFly__ ) || defined( __SVR4 ) || defined( __sun ) || defined( __APPLE_CC__ ) || defined ( __APPLE__ ) || defined( __HAIKU__ ) || defined( __BEOS__ ) +#if defined ( linux ) || defined( __linux__ ) || defined( __FreeBSD__ ) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined( __DragonFly__ ) || defined( __SVR4 ) || defined( __sun ) || defined( __APPLE_CC__ ) || defined ( __APPLE__ ) || defined( __HAIKU__ ) || defined( __BEOS__ ) || defined( __emscripten__ ) || defined( EMSCRIPTEN ) #define EE_PLATFORM_POSIX #endif diff --git a/premake4.lua b/premake4.lua index ad77d3130..64272cde5 100644 --- a/premake4.lua +++ b/premake4.lua @@ -40,6 +40,17 @@ newplatform { } } +newplatform { + name = "emscripten", + description = "Emscripten", + gcc = { + cc = "emcc", + cxx = "em++", + ar = "emar", + cppflags = "-MMD -D__emscripten__" + } +} + newgcctoolchain { name = "mingw32", description = "Mingw32 to cross-compile windows binaries from *nix", @@ -121,6 +132,10 @@ function os.get_real() if _OPTIONS.platform == "mingw32" then return _OPTIONS.platform end + + if _OPTIONS.platform == "emscripten" then + return _OPTIONS.platform + end return os.get() end @@ -244,6 +259,8 @@ end function build_link_configuration( package_name, use_ee_icon ) includedirs { "include", "src" } + local extension = ""; + if package_name == "eepp" then defines { "EE_EXPORTS" } elseif package_name == "eepp-static" then @@ -265,8 +282,12 @@ function build_link_configuration( package_name, use_ee_icon ) linkoptions { "../../assets/icon/ee.res" } end end - end - + + if os.is_real("emscripten") then + extension = ".html" + end + end + configuration "debug" defines { "DEBUG", "EE_DEBUG", "EE_MEMORY_MANAGER" } flags { "Symbols" } @@ -275,7 +296,7 @@ function build_link_configuration( package_name, use_ee_icon ) buildoptions{ "-Wall -Wno-long-long" } end - targetname ( package_name .. "-debug" ) + targetname ( package_name .. "-debug" .. extension ) configuration "release" defines { "NDEBUG" } @@ -285,11 +306,20 @@ function build_link_configuration( package_name, use_ee_icon ) buildoptions { "-fno-strict-aliasing -O3 -s -ffast-math" } end - targetname ( package_name ) + targetname ( package_name .. extension ) configuration "windows" add_cross_config_links() + configuration "emscripten" + if _OPTIONS["with-gles1"] then + linkoptions{ "-s LEGACY_GL_EMULATION=1" } + end + + if _OPTIONS["with-gles2"] then + linkoptions{ "-s FULL_ES2=1" } + end + set_ios_config() end @@ -351,7 +381,7 @@ function add_static_links() "imageresampler-static" } - if not os.is_real("haiku") and not os.is_real("ios") and not os.is_real("android") then + if not os.is_real("haiku") and not os.is_real("ios") and not os.is_real("android") and not os.is_real("emscripten") then links{ "glew-static" } end end @@ -453,7 +483,7 @@ function backend_is( name ) local ret_val = os_findlib( name ) and backend_sel - if os.is_real("mingw32") then + if os.is_real("mingw32") or os.is_real("emscripten") then ret_val = backend_sel end @@ -585,7 +615,7 @@ solution "eepp" kind "StaticLib" language "C" targetdir("libs/" .. os.get_real() .. "/helpers/") - if not os.is_real("haiku") and not os.is_real("ios") and not os.is_real("android") then + if not os.is_real("haiku") and not os.is_real("ios") and not os.is_real("android") and not os.is_real("emscripten") then files { "src/eepp/helper/glew/*.c" } end includedirs { "include/eepp/helper/glew" } diff --git a/projects/android-project/jni/Application.mk b/projects/android-project/jni/Application.mk index 9c18c65e0..5c3bdc436 100644 --- a/projects/android-project/jni/Application.mk +++ b/projects/android-project/jni/Application.mk @@ -14,11 +14,11 @@ APP_LDLIBS := -llog $(EE_GLES_LINK) -lm -lz -lOpenSLES #Debug Build # arm-linux-androideabi-4.4.3 crashes in -O0 mode on SDL sources -#APP_CFLAGS := -g -DDEBUG -DEE_DEBUG -DEE_MEMORY_MANAGER -#APP_OPTIM :=debug +APP_CFLAGS := -g -DDEBUG -DEE_DEBUG -DEE_MEMORY_MANAGER +APP_OPTIM :=debug #Release Build -APP_CFLAGS := -fno-strict-aliasing -O3 -s -DNDEBUG -ffast-math +#APP_CFLAGS := -fno-strict-aliasing -O3 -s -DNDEBUG -ffast-math APP_PLATFORM := android-9 APP_MODULES := main diff --git a/projects/emscripten/make.sh b/projects/emscripten/make.sh new file mode 100755 index 000000000..46ea26d47 --- /dev/null +++ b/projects/emscripten/make.sh @@ -0,0 +1,6 @@ +#!/bin/sh +cd $(dirname "$0") +premake4 --file=../../premake4.lua --with-gles2 --with-static-eepp --with-static-freetype --platform=emscripten --with-backend=SDL gmake +cd ../../make/emscripten/ +sed -i 's/-rcs/rcs/g' *.make +emmake make $@ diff --git a/src/eepp/graphics/renderer/cgl.cpp b/src/eepp/graphics/renderer/cgl.cpp index 68aae05af..2b4d5cb15 100644 --- a/src/eepp/graphics/renderer/cgl.cpp +++ b/src/eepp/graphics/renderer/cgl.cpp @@ -587,7 +587,7 @@ const int& cGL::QuadVertexs() const { } void cGL::BindVertexArray ( GLuint array ) { -#ifndef EE_GLES +#if !defined( EE_GLES ) || EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN if ( mCurVAO != array ) { glBindVertexArray( array ); @@ -597,13 +597,13 @@ void cGL::BindVertexArray ( GLuint array ) { } void cGL::DeleteVertexArrays ( GLsizei n, const GLuint *arrays ) { -#ifndef EE_GLES +#if !defined( EE_GLES ) || EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN glDeleteVertexArrays( n, arrays ); #endif } void cGL::GenVertexArrays ( GLsizei n, GLuint *arrays ) { -#ifndef EE_GLES +#if !defined( EE_GLES ) || EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN glGenVertexArrays( n, arrays ); #endif } diff --git a/src/eepp/graphics/renderer/shaders/clipped.frag b/src/eepp/graphics/renderer/shaders/clipped.frag index 43f838da2..625e7a173 100644 --- a/src/eepp/graphics/renderer/shaders/clipped.frag +++ b/src/eepp/graphics/renderer/shaders/clipped.frag @@ -7,8 +7,13 @@ "#define MAX_CLIP_PLANES 6\n\ uniform sampler2D textureUnit0;\n\ uniform int dgl_TexActive;\n\ +#ifndef GL_ES\n\ uniform int dgl_ClippingEnabled;\n\ uniform int dgl_ClipEnabled[ MAX_CLIP_PLANES ];\n\ +#else\n\ +uniform lowp int dgl_ClippingEnabled;\n\ +uniform lowp int dgl_ClipEnabled[ MAX_CLIP_PLANES ];\n\ +#endif\n\ uniform vec4 dgl_ClipPlane[ MAX_CLIP_PLANES ];\n\ varying vec4 dgl_Color;\n\ #ifndef GL_ES\n\ diff --git a/src/eepp/graphics/renderer/shaders/clipped.vert b/src/eepp/graphics/renderer/shaders/clipped.vert index 3652e575e..cd5bdbc53 100644 --- a/src/eepp/graphics/renderer/shaders/clipped.vert +++ b/src/eepp/graphics/renderer/shaders/clipped.vert @@ -7,8 +7,13 @@ "#define MAX_CLIP_PLANES 6\n\ uniform mat4 dgl_ProjectionMatrix;\n\ uniform mat4 dgl_ModelViewMatrix;\n\ -uniform int dgl_ClippingEnabled;\n\ -uniform int dgl_ClipEnabled[ MAX_CLIP_PLANES ];\n\ +#ifndef GL_ES\n\ +uniform int dgl_ClippingEnabled;\n\ +uniform int dgl_ClipEnabled[ MAX_CLIP_PLANES ];\n\ +#else\n\ +uniform lowp int dgl_ClippingEnabled;\n\ +uniform lowp int dgl_ClipEnabled[ MAX_CLIP_PLANES ];\n\ +#endif\n\ uniform vec4 dgl_ClipPlane[ MAX_CLIP_PLANES ];\n\ attribute vec4 dgl_Vertex;\n\ attribute vec4 dgl_FrontColor;\n\ diff --git a/src/eepp/helper/haikuttf/hkbase.hpp b/src/eepp/helper/haikuttf/hkbase.hpp index 1340629c2..071f80420 100644 --- a/src/eepp/helper/haikuttf/hkbase.hpp +++ b/src/eepp/helper/haikuttf/hkbase.hpp @@ -35,7 +35,7 @@ typedef SOPHIST_uint32 u32; #define HK_PLATFORM HK_PLATFORM_HAIKU #endif -#if defined ( linux ) || defined( __linux__ ) || defined( __FreeBSD__ ) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined( __DragonFly__ ) || defined( __SVR4 ) || defined( __APPLE_CC__ ) || defined ( __APPLE__ ) || defined( __HAIKU__ ) || defined( __BEOS__ ) +#if defined ( linux ) || defined( __linux__ ) || defined( __FreeBSD__ ) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined( __DragonFly__ ) || defined( __SVR4 ) || defined( __APPLE_CC__ ) || defined ( __APPLE__ ) || defined( __HAIKU__ ) || defined( __BEOS__ ) || defined( __emscripten__ ) || defined( EMSCRIPTEN ) #define HK_PLATFORM_POSIX #endif diff --git a/src/eepp/window/backend/SDL/cinputsdl.cpp b/src/eepp/window/backend/SDL/cinputsdl.cpp index 26dedc961..60e28ff8d 100644 --- a/src/eepp/window/backend/SDL/cinputsdl.cpp +++ b/src/eepp/window/backend/SDL/cinputsdl.cpp @@ -16,6 +16,12 @@ #endif #endif +#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN +#ifndef SDL_NUMEVENTS +#define SDL_NUMEVENTS SDL_LASTEVENT +#endif +#endif + #include #include #include @@ -60,7 +66,9 @@ void cInputSDL::Update() { EEEvent.Type = InputEvent::KeyDown; EEEvent.key.state = SDLEvent.key.state; + #if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN EEEvent.key.which = SDLEvent.key.which; + #endif EEEvent.key.keysym.sym = SDLEvent.key.keysym.sym; EEEvent.key.keysym.mod = SDLEvent.key.keysym.mod; EEEvent.key.keysym.unicode = SDLEvent.key.keysym.unicode; @@ -70,7 +78,9 @@ void cInputSDL::Update() { { EEEvent.Type = InputEvent::KeyUp; EEEvent.key.state = SDLEvent.key.state; + #if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN EEEvent.key.which = SDLEvent.key.which; + #endif EEEvent.key.keysym.sym = SDLEvent.key.keysym.sym; EEEvent.key.keysym.mod = SDLEvent.key.keysym.mod; EEEvent.key.keysym.unicode = SDLEvent.key.keysym.unicode; @@ -79,7 +89,9 @@ void cInputSDL::Update() { case SDL_MOUSEMOTION: { EEEvent.Type = InputEvent::MouseMotion; + #if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN EEEvent.motion.which = SDLEvent.motion.which; + #endif EEEvent.motion.state = SDLEvent.motion.state; EEEvent.motion.x = SDLEvent.motion.x; EEEvent.motion.y = SDLEvent.motion.y; @@ -91,7 +103,9 @@ void cInputSDL::Update() { { EEEvent.Type = InputEvent::MouseButtonDown; EEEvent.button.button = SDLEvent.button.button; + #if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN EEEvent.button.which = SDLEvent.button.which; + #endif EEEvent.button.state = SDLEvent.button.state; EEEvent.button.x = SDLEvent.button.x; EEEvent.button.y = SDLEvent.button.y; @@ -101,7 +115,9 @@ void cInputSDL::Update() { { EEEvent.Type = InputEvent::MouseButtonUp; EEEvent.button.button = SDLEvent.button.button; + #if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN EEEvent.button.which = SDLEvent.button.which; + #endif EEEvent.button.state = SDLEvent.button.state; EEEvent.button.x = SDLEvent.button.x; EEEvent.button.y = SDLEvent.button.y; diff --git a/src/eepp/window/backend/SDL/cwindowsdl.cpp b/src/eepp/window/backend/SDL/cwindowsdl.cpp index 3107dcc2b..c754dd45b 100644 --- a/src/eepp/window/backend/SDL/cwindowsdl.cpp +++ b/src/eepp/window/backend/SDL/cwindowsdl.cpp @@ -192,7 +192,10 @@ void cWindowSDL::SetGLConfig() { SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE , mWindow.ContextConfig.DepthBufferSize ); // Depth SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, ( mWindow.ContextConfig.DoubleBuffering ? 1 : 0 ) ); // Double Buffering SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, mWindow.ContextConfig.StencilBufferSize ); + + #if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, ( mWindow.ContextConfig.VSync ? 1 : 0 ) ); // VSync + #endif } void cWindowSDL::ToggleFullscreen() {