diff --git a/examples/00-helloworld/helloworld.cpp b/examples/00-helloworld/helloworld.cpp index 7819b810..263105ae 100644 --- a/examples/00-helloworld/helloworld.cpp +++ b/examples/00-helloworld/helloworld.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include "../common/entry.h" #include "../common/dbg.h" #include "../common/processevents.h" diff --git a/examples/01-cubes/cubes.cpp b/examples/01-cubes/cubes.cpp index 94e15bbb..a205b43a 100644 --- a/examples/01-cubes/cubes.cpp +++ b/examples/01-cubes/cubes.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include "../common/entry.h" #include "../common/dbg.h" diff --git a/examples/02-metaballs/metaballs.cpp b/examples/02-metaballs/metaballs.cpp index c948aa31..76d7ef88 100644 --- a/examples/02-metaballs/metaballs.cpp +++ b/examples/02-metaballs/metaballs.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include "../common/entry.h" #include "../common/dbg.h" diff --git a/examples/03-raymarch/raymarch.cpp b/examples/03-raymarch/raymarch.cpp index 3a871033..23d49832 100644 --- a/examples/03-raymarch/raymarch.cpp +++ b/examples/03-raymarch/raymarch.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include "../common/entry.h" #include "../common/dbg.h" diff --git a/examples/04-mesh/mesh.cpp b/examples/04-mesh/mesh.cpp index db5ca6af..edb92bb6 100644 --- a/examples/04-mesh/mesh.cpp +++ b/examples/04-mesh/mesh.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include "../common/entry.h" diff --git a/examples/05-instancing/instancing.cpp b/examples/05-instancing/instancing.cpp index 30c037a6..120f32b9 100644 --- a/examples/05-instancing/instancing.cpp +++ b/examples/05-instancing/instancing.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include "../common/entry.h" #include "../common/dbg.h" diff --git a/examples/06-bump/bump.cpp b/examples/06-bump/bump.cpp index eac8d410..5c7cef24 100644 --- a/examples/06-bump/bump.cpp +++ b/examples/06-bump/bump.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include "../common/entry.h" diff --git a/examples/07-callback/callback.cpp b/examples/07-callback/callback.cpp index e74750f0..7493dfd4 100644 --- a/examples/07-callback/callback.cpp +++ b/examples/07-callback/callback.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp index 0a133fad..23034a9c 100644 --- a/examples/08-update/update.cpp +++ b/examples/08-update/update.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include "../common/entry.h" diff --git a/examples/09-hdr/hdr.cpp b/examples/09-hdr/hdr.cpp index 9c3843d0..e5db5a4e 100644 --- a/examples/09-hdr/hdr.cpp +++ b/examples/09-hdr/hdr.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include diff --git a/examples/10-font/font.cpp b/examples/10-font/font.cpp index 3db40df0..9f97dabe 100644 --- a/examples/10-font/font.cpp +++ b/examples/10-font/font.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include diff --git a/examples/11-fontsdf/fontsdf.cpp b/examples/11-fontsdf/fontsdf.cpp index 0430aa4c..73874b43 100644 --- a/examples/11-fontsdf/fontsdf.cpp +++ b/examples/11-fontsdf/fontsdf.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include "../common/entry.h" #include "../common/dbg.h" diff --git a/examples/12-lod/lod.cpp b/examples/12-lod/lod.cpp index 76dc3ecf..9021c305 100644 --- a/examples/12-lod/lod.cpp +++ b/examples/12-lod/lod.cpp @@ -3,8 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#include "../common/common.h" + #include -#include #include #include #include "../common/entry.h" @@ -28,7 +29,8 @@ struct KnightPos int32_t m_y; }; -KnightPos knightTour[8*4] = { +KnightPos knightTour[8*4] = +{ {0,0}, {1,2}, {3,3}, {4,1}, {5,3}, {7,2}, {6,0}, {5,2}, {7,3}, {6,1}, {4,0}, {3,2}, {2,0}, {0,1}, {1,3}, {2,1}, {0,2}, {1,0}, {2,2}, {0,3}, {1,1}, {3,0}, {4,2}, {5,0}, diff --git a/examples/common/common.h b/examples/common/common.h new file mode 100644 index 00000000..3a5f819e --- /dev/null +++ b/examples/common/common.h @@ -0,0 +1,38 @@ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __COMMON_H__ +#define __COMMON_H__ + +#include "dbg.h" + +#if 0 +# define BX_TRACE(_format, ...) \ + do { \ + DBG(BX_FILE_LINE_LITERAL "BGFX " _format "\n", ##__VA_ARGS__); \ + } while(0) + +# define BX_WARN(_condition, _format, ...) \ + do { \ + if (!(_condition) ) \ + { \ + DBG("WARN " _format, ##__VA_ARGS__); \ + } \ + } while(0) + +# define BX_CHECK(_condition, _format, ...) \ + do { \ + if (!(_condition) ) \ + { \ + DBG("CHECK " _format, ##__VA_ARGS__); \ + bx::debugBreak(); \ + } \ + } while(0) +#endif // 0 + +#include +#include + +#endif // __COMMON_H__ diff --git a/examples/common/cube_atlas.cpp b/examples/common/cube_atlas.cpp index 712b56b3..84481dcd 100644 --- a/examples/common/cube_atlas.cpp +++ b/examples/common/cube_atlas.cpp @@ -3,7 +3,7 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #include #include // INT_MAX diff --git a/examples/common/dbg.cpp b/examples/common/dbg.cpp index bae0edb0..f3e017e4 100755 --- a/examples/common/dbg.cpp +++ b/examples/common/dbg.cpp @@ -3,7 +3,7 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #include #include diff --git a/examples/common/dbg.h b/examples/common/dbg.h index 370cf663..5f20de84 100644 --- a/examples/common/dbg.h +++ b/examples/common/dbg.h @@ -14,7 +14,6 @@ #define DBG_FILE_LINE_LITERAL "" __FILE__ "(" DBG_STRINGIZE(__LINE__) "): " #define DBG(_format, ...) dbgPrintf(DBG_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__) -extern void dbgOutput(const char* _out); extern void dbgPrintfVargs(const char* _format, va_list _argList); extern void dbgPrintf(const char* _format, ...); extern void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...); diff --git a/examples/common/entry_android.cpp b/examples/common/entry_android.cpp index 480534d4..13743183 100644 --- a/examples/common/entry_android.cpp +++ b/examples/common/entry_android.cpp @@ -3,7 +3,7 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #if BX_PLATFORM_ANDROID diff --git a/examples/common/entry_linux.cpp b/examples/common/entry_linux.cpp index d136e054..37422305 100644 --- a/examples/common/entry_linux.cpp +++ b/examples/common/entry_linux.cpp @@ -3,7 +3,7 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #if BX_PLATFORM_LINUX diff --git a/examples/common/entry_nacl.cpp b/examples/common/entry_nacl.cpp index dc78395d..99429548 100644 --- a/examples/common/entry_nacl.cpp +++ b/examples/common/entry_nacl.cpp @@ -3,30 +3,49 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #if BX_PLATFORM_NACL #include #include #include +#include #include #include #include +#include #include #include +#include +#include +#include +#include +#include #include #include #include #include -#include "dbg.h" +#include #include "entry.h" +extern int _main_(int _argc, char** _argv); + namespace entry { + const PPB_Core* g_coreInterface; + const PPB_Instance* g_instInterface; + const PPB_Graphics3D* g_graphicsInterface; + const PPB_MessageLoop* g_messageLoopInterface; + const PPB_URLLoader* g_urlLoaderInterface; + const PPB_URLRequestInfo* g_urlRequestInterface; + const PPB_URLResponseInfo* g_urlResponseInterface; + const PPB_Var* g_varInterface; + PP_Instance g_instance; + const Event* poll() { return NULL; @@ -48,64 +67,83 @@ namespace entry { } + template + bool initializeInterface(PPB_GetInterface _interface, const char* _name, const Type*& _result) + { + _result = reinterpret_cast(_interface(_name) ); + return NULL != _result; + } + + struct MainThreadEntry + { + int m_argc; + char** m_argv; + + static int32_t threadFunc(void* _userData); + }; + + struct NaclContext + { + NaclContext() + { + const char* argv[1] = { "nacl.nexe" }; + m_mte.m_argc = 1; + m_mte.m_argv = const_cast(argv); + + bgfx::naclSetIntefraces(g_instance, g_instInterface, g_graphicsInterface, NULL); + m_thread.init(MainThreadEntry::threadFunc, &m_mte); + } + + ~NaclContext() + { + m_thread.shutdown(); + } + + MainThreadEntry m_mte; + bx::Thread m_thread; + }; + + static NaclContext* s_ctx; + + int32_t MainThreadEntry::threadFunc(void* _userData) + { + MainThreadEntry* self = (MainThreadEntry*)_userData; + + PP_Resource resource = g_messageLoopInterface->Create(g_instance); + g_messageLoopInterface->AttachToCurrentThread(resource); + + int32_t result = _main_(self->m_argc, self->m_argv); + return result; + } + + static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t /*_argc*/, const char* /*_argn*/[], const char* /*_argv*/[]) + { + g_instance = _instance; // one instance only! + s_ctx = new NaclContext; + return PP_TRUE; + } + + static void naclInstanceDidDestroy(PP_Instance _instance) + { + delete s_ctx; + } + + static void naclInstanceDidChangeView(PP_Instance /*_instance*/, PP_Resource /*_view*/) + { + } + + static void naclInstanceDidChangeFocus(PP_Instance /*_instance*/, PP_Bool /*_focus*/) + { + } + + static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance /*_instance*/, PP_Resource /*_urlLoader*/) + { + return PP_FALSE; + } + } // namespace entry -extern int _main_(int _argc, char** _argv); - -static void* _entry_(void*) -{ - const char* argv[1] = { "nacl.nexe" }; - _main_(1, const_cast(argv) ); - return NULL; -} - -struct NaclContext -{ - NaclContext() - : m_thread(0) - { - } - - const PPB_Instance* m_instInterface; - const PPB_Graphics3D* m_graphicsInterface; - - pthread_t m_thread; - PP_Module m_module; - PP_Instance m_instance; -}; - -static NaclContext s_ctx; - -static PP_Bool naclInstanceDidCreate(PP_Instance _instance, uint32_t _argc, const char* _argn[], const char* _argv[]) -{ - s_ctx.m_instance = _instance; - - bgfx::naclSetIntefraces(s_ctx.m_instance, s_ctx.m_instInterface, s_ctx.m_graphicsInterface, NULL); - pthread_create(&s_ctx.m_thread, NULL, _entry_, NULL); - - return PP_TRUE; -} - -static void naclInstanceDidDestroy(PP_Instance _instance) -{ - if (s_ctx.m_thread) - { - pthread_join(s_ctx.m_thread, NULL); - } -} - -static void naclInstanceDidChangeView(PP_Instance _instance, PP_Resource _view) -{ -} - -static void naclInstanceDidChangeFocus(PP_Instance _instance, PP_Bool _focus) -{ -} - -static PP_Bool naclInstanceHandleDocumentLoad(PP_Instance _instance, PP_Resource _urlLoader) -{ - return PP_FALSE; -} +using namespace entry; PP_EXPORT const void* PPP_GetInterface(const char* _name) { @@ -126,20 +164,17 @@ PP_EXPORT const void* PPP_GetInterface(const char* _name) return NULL; } -template -bool initializeInterface(const Type*& _result, PPB_GetInterface _interface, const char* _name) -{ - _result = reinterpret_cast(_interface(_name) ); - return NULL != _result; -} - PP_EXPORT int32_t PPP_InitializeModule(PP_Module _module, PPB_GetInterface _interface) { - s_ctx.m_module = _module; - bool result = true; - result &= initializeInterface(s_ctx.m_instInterface, _interface, PPB_INSTANCE_INTERFACE); - result &= initializeInterface(s_ctx.m_graphicsInterface, _interface, PPB_GRAPHICS_3D_INTERFACE); + result &= initializeInterface(_interface, PPB_CORE_INTERFACE, g_coreInterface); + result &= initializeInterface(_interface, PPB_GRAPHICS_3D_INTERFACE, g_graphicsInterface); + result &= initializeInterface(_interface, PPB_INSTANCE_INTERFACE, g_instInterface); + result &= initializeInterface(_interface, PPB_MESSAGELOOP_INTERFACE, g_messageLoopInterface); + result &= initializeInterface(_interface, PPB_URLLOADER_INTERFACE, g_urlLoaderInterface); + result &= initializeInterface(_interface, PPB_URLREQUESTINFO_INTERFACE, g_urlRequestInterface); + result &= initializeInterface(_interface, PPB_URLRESPONSEINFO_INTERFACE, g_urlResponseInterface); + result &= initializeInterface(_interface, PPB_VAR_INTERFACE, g_varInterface); result &= glInitializePPAPI(_interface); return result ? PP_OK : PP_ERROR_NOINTERFACE; diff --git a/examples/common/entry_qnx.cpp b/examples/common/entry_qnx.cpp index 2f91d00a..442dff38 100644 --- a/examples/common/entry_qnx.cpp +++ b/examples/common/entry_qnx.cpp @@ -3,7 +3,7 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #if BX_PLATFORM_QNX diff --git a/examples/common/entry_windows.cpp b/examples/common/entry_windows.cpp index ce9583b7..edf7d99d 100644 --- a/examples/common/entry_windows.cpp +++ b/examples/common/entry_windows.cpp @@ -3,7 +3,7 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "common.h" #if BX_PLATFORM_WINDOWS diff --git a/examples/common/font/font_manager.cpp b/examples/common/font/font_manager.cpp index 705a9cd8..68d0ae4a 100644 --- a/examples/common/font/font_manager.cpp +++ b/examples/common/font/font_manager.cpp @@ -3,7 +3,8 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include +#include "../common.h" + #include #include #include diff --git a/examples/common/font/text_buffer_manager.cpp b/examples/common/font/text_buffer_manager.cpp index 89475e2e..985ec4f2 100644 --- a/examples/common/font/text_buffer_manager.cpp +++ b/examples/common/font/text_buffer_manager.cpp @@ -3,9 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include -#include +#include "../common.h" +#include #include // offsetof #include // memcpy #include // wcslen