diff --git a/examples/common/entry/entry_glfw.cpp b/examples/common/entry/entry_glfw.cpp new file mode 100644 index 00000000..fe83e9bf --- /dev/null +++ b/examples/common/entry/entry_glfw.cpp @@ -0,0 +1,86 @@ +/* + * Copyright 2011-2015 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include "entry_p.h" + +#if ENTRY_CONFIG_USE_GLFW + +#define GLFW_DLL +#include +#include +#include "dbg.h" + +namespace entry +{ + const Event* poll() + { + return NULL; + } + + const Event* poll(WindowHandle _handle) + { + BX_UNUSED(_handle); + return NULL; + } + + void release(const Event* _event) + { + BX_UNUSED(_event); + } + + WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title) + { + BX_UNUSED(_x, _y, _width, _height, _flags, _title); + WindowHandle handle = { UINT16_MAX }; + return handle; + } + + void destroyWindow(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + + void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y) + { + BX_UNUSED(_handle, _x, _y); + } + + void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height) + { + BX_UNUSED(_handle, _width, _height); + } + + void setWindowTitle(WindowHandle _handle, const char* _title) + { + BX_UNUSED(_handle, _title); + } + + void toggleWindowFrame(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + + void toggleFullscreen(WindowHandle _handle) + { + BX_UNUSED(_handle); + } + + void setMouseLock(WindowHandle _handle, bool _lock) + { + BX_UNUSED(_handle, _lock); + } +} + +int main(int _argc, char** _argv) +{ + glfwInit(); + GLFWwindow *window = glfwCreateWindow(1280, 720, "bgfx", NULL, NULL); + glfwMakeContextCurrent(window); + + bgfx::glfwSetWindow(window); + return entry::main(_argc, _argv); +} + +#endif // ENTRY_CONFIG_USE_GLFW diff --git a/examples/common/entry/entry_p.h b/examples/common/entry/entry_p.h index 74d4756b..d41ab568 100644 --- a/examples/common/entry/entry_p.h +++ b/examples/common/entry/entry_p.h @@ -17,8 +17,13 @@ # define ENTRY_CONFIG_USE_SDL 0 #endif // ENTRY_CONFIG_USE_SDL -#if !ENTRY_CONFIG_USE_SDL && \ - !defined(ENTRY_CONFIG_USE_NATIVE) +#ifndef ENTRY_CONFIG_USE_GLFW +# define ENTRY_CONFIG_USE_GLFW 0 +#endif // ENTRY_CONFIG_USE_GLFW + +#if !defined(ENTRY_CONFIG_USE_NATIVE) \ + && !ENTRY_CONFIG_USE_SDL \ + && !ENTRY_CONFIG_USE_GLFW # define ENTRY_CONFIG_USE_NATIVE 1 #else # define ENTRY_CONFIG_USE_NATIVE 0 diff --git a/include/bgfxplatform.h b/include/bgfxplatform.h index 8544e8ee..77284f57 100755 --- a/include/bgfxplatform.h +++ b/include/bgfxplatform.h @@ -76,7 +76,7 @@ namespace bgfx namespace bgfx { /// - void osxSetNSWindow(void* _window); + void osxSetNSWindow(void* _window, void* _nsgl = NULL); } // namespace bgfx @@ -155,11 +155,12 @@ namespace bgfx { # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD ::Display* display = glfwGetX11Display(); - ::Window window = glfwGetX11Window(_window); + ::Window window = glfwGetX11Window(_window); x11SetDisplayWindow(display, window); # elif BX_PLATFORM_OSX - void* id = glfwGetCocoaWindow(_window); - osxSetNSWindow(id); + void* window = glfwGetCocoaWindow(_window); + void* nsgl = glfwGetNSGLContext(_window); + osxSetNSWindow(window, nsgl); # elif BX_PLATFORM_WINDOWS HWND hwnd = glfwGetWin32Window(_window); winSetHwnd(hwnd); diff --git a/scripts/example-common.lua b/scripts/example-common.lua index 6ab71ab9..d408224e 100644 --- a/scripts/example-common.lua +++ b/scripts/example-common.lua @@ -31,6 +31,12 @@ project ("example-common") } end + if _OPTIONS["with-glfw"] then + defines { + "ENTRY_CONFIG_USE_GLFW=1", + } + end + configuration { "mingw* or vs2008" } includedirs { "$(DXSDK_DIR)/include", diff --git a/scripts/genie.lua b/scripts/genie.lua index 540b2281..102913b9 100644 --- a/scripts/genie.lua +++ b/scripts/genie.lua @@ -18,6 +18,11 @@ newoption { description = "Enable SDL entry.", } +newoption { + trigger = "with-glfw", + description = "Enable GLFW entry.", +} + newoption { trigger = "with-shared-lib", description = "Enable building shared library.", @@ -123,6 +128,21 @@ function exampleProject(_name) configuration {} end + if _OPTIONS["with-glfw"] then + defines { "ENTRY_CONFIG_USE_GLFW=1" } + links { + "glfw3" + } + + configuration { "osx" } + linkoptions { + "-framework CoreVideo", + "-framework IOKit", + } + + configuration {} + end + if _OPTIONS["with-ovr"] then links { "winmm", diff --git a/src/bgfx.cpp b/src/bgfx.cpp index defc7a21..86dd064d 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -52,10 +52,12 @@ namespace bgfx } #elif BX_PLATFORM_OSX void* g_bgfxNSWindow = NULL; + void* g_bgfxNSGL = NULL; - void osxSetNSWindow(void* _window) + void osxSetNSWindow(void* _window, void* _nsgl) { g_bgfxNSWindow = _window; + g_bgfxNSGL = _nsgl; } #elif BX_PLATFORM_WINDOWS ::HWND g_bgfxHwnd = NULL; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 94ff16b4..0e765682 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -206,6 +206,7 @@ namespace bgfx extern uint32_t g_bgfxX11Window; #elif BX_PLATFORM_OSX extern void* g_bgfxNSWindow; + extern void* g_bgfxNSGL; #elif BX_PLATFORM_WINDOWS extern ::HWND g_bgfxHwnd; #elif BX_PLATFORM_WINRT diff --git a/src/glcontext_nsgl.mm b/src/glcontext_nsgl.mm index 688232b8..77b6c24f 100644 --- a/src/glcontext_nsgl.mm +++ b/src/glcontext_nsgl.mm @@ -35,7 +35,7 @@ namespace bgfx { namespace gl { } }; - + static void* s_opengl = NULL; void GlContext::create(uint32_t _width, uint32_t _height) @@ -46,45 +46,49 @@ namespace bgfx { namespace gl BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!"); NSWindow* nsWindow = (NSWindow*)g_bgfxNSWindow; + m_context = g_bgfxNSGL; - NSOpenGLPixelFormatAttribute profile = + if (NULL == m_context) + { + NSOpenGLPixelFormatAttribute profile = #if BGFX_CONFIG_RENDERER_OPENGL >= 31 - NSOpenGLProfileVersion3_2Core + NSOpenGLProfileVersion3_2Core #else - NSOpenGLProfileVersionLegacy + NSOpenGLProfileVersionLegacy #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31 - ; + ; - NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = { - NSOpenGLPFAOpenGLProfile, profile, - NSOpenGLPFAColorSize, 24, - NSOpenGLPFAAlphaSize, 8, - NSOpenGLPFADepthSize, 24, - NSOpenGLPFAStencilSize, 8, - NSOpenGLPFADoubleBuffer, true, - NSOpenGLPFAAccelerated, true, - NSOpenGLPFANoRecovery, true, - 0, 0, - }; + NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = { + NSOpenGLPFAOpenGLProfile, profile, + NSOpenGLPFAColorSize, 24, + NSOpenGLPFAAlphaSize, 8, + NSOpenGLPFADepthSize, 24, + NSOpenGLPFAStencilSize, 8, + NSOpenGLPFADoubleBuffer, true, + NSOpenGLPFAAccelerated, true, + NSOpenGLPFANoRecovery, true, + 0, 0, + }; - NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes]; - BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format."); + NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes]; + BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format."); - NSRect glViewRect = [[nsWindow contentView] bounds]; - NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat]; - - [pixelFormat release]; - [nsWindow setContentView:glView]; - - NSOpenGLContext* glContext = [glView openGLContext]; - BGFX_FATAL(NULL != glContext, Fatal::UnableToInitialize, "Failed to initialize GL context."); + NSRect glViewRect = [[nsWindow contentView] bounds]; + NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat]; - [glContext makeCurrentContext]; - GLint interval = 0; - [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval]; - - m_view = glView; - m_context = glContext; + [pixelFormat release]; + [nsWindow setContentView:glView]; + + NSOpenGLContext* glContext = [glView openGLContext]; + BGFX_FATAL(NULL != glContext, Fatal::UnableToInitialize, "Failed to initialize GL context."); + + [glContext makeCurrentContext]; + GLint interval = 0; + [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval]; + + m_view = glView; + m_context = glContext; + } import(); }