فهرست منبع

Merge pull request #521 from blackberry-gaming/next-setaylor

Next setaylor
Sean Paul Taylor 13 سال پیش
والد
کامیت
67aa34f38b

+ 2 - 2
gameplay/src/Game.cpp

@@ -72,9 +72,9 @@ bool Game::isMouseCaptured()
     return Platform::isMouseCaptured();
 }
 
-void Game::setMouseCapture(bool captured)
+void Game::setMouseCaptured(bool captured)
 {
-    Platform::setMouseCapture(captured);
+    Platform::setMouseCaptured(captured);
 }
 
 void Game::setCursorVisible(bool visible)

+ 2 - 2
gameplay/src/Game.h

@@ -100,9 +100,9 @@ public:
      *
      * @param captured true to enable mouse capture mode, false to disable it.
      *
-     * @see Platform::setMouseCapture(bool)
+     * @see Platform::setMouseCaptured(bool)
      */
-    static void setMouseCapture(bool captured);
+    static void setMouseCaptured(bool captured);
 
     /**
      * Sets the visibility of the platform cursor.

+ 2 - 2
gameplay/src/Joystick.cpp

@@ -105,9 +105,9 @@ bool Joystick::touchEvent(Touch::TouchEvent touchEvent, int x, int y, unsigned i
 
                 notifyListeners(Listener::PRESS);
 
-                _displacement.set(0.0f, 0.0f);
+                _displacement.set(dx, dy);
                 
-                Vector2 value(0.0f, 0.0f);
+                Vector2 value(dx, dy);
                 if (_value != value)
                 {
                     _value.set(value);

+ 6 - 2
gameplay/src/Platform.h

@@ -26,10 +26,11 @@ public:
      * Creates a platform for the specified game which is will interacte with.
      *
      * @param game The game to create a platform for.
+     * @param attachToWindow The native window handle to optionally attach to.
      * 
      * @return The created platform interface.
      */
-    static Platform* create(Game* game);
+    static Platform* create(Game* game, void* attachToWindow = NULL);
 
     /**
      * Begins processing the platform messages.
@@ -37,6 +38,9 @@ public:
      * This method handles all OS window messages and drives the game loop.
      * It normally does not return until the application is closed.
      * 
+     * If a attachToWindow is passed to Platform::create the message pump will instead attach
+     * to or allow the attachToWindow to drive the game loop on the platform.
+     *
      * @return The platform message pump return code.
      */
     int enterMessagePump();
@@ -127,7 +131,7 @@ public:
      *
      * @param captured True to enable mouse capture, false to disable it.
      */
-    static void setMouseCapture(bool captured);
+    static void setMouseCaptured(bool captured);
 
     /**
      * Determines if mouse capture is currently enabled.

+ 2 - 3
gameplay/src/PlatformAndroid.cpp

@@ -715,10 +715,9 @@ Platform::~Platform()
 {
 }
 
-Platform* Platform::create(Game* game)
+Platform* Platform::create(Game* game, void* attachToWindow)
 {
     Platform* platform = new Platform(game);
-    
     return platform;
 }
 
@@ -930,7 +929,7 @@ bool Platform::hasMouse()
     return false;
 }
 
-void Platform::setMouseCapture(bool captured)
+void Platform::setMouseCaptured(bool captured)
 {
     // not supported
 }

+ 4 - 2
gameplay/src/PlatformMacOSX.mm

@@ -35,6 +35,7 @@ static bool __otherMouseDown = false;
 static bool __shiftDown = false;
 static char* __title = NULL;
 static bool __fullscreen = false;
+static void* __attachToWindow = NULL;
 
 
 double getMachTimeInMilliseconds()
@@ -617,8 +618,9 @@ Platform::~Platform()
 {
 }
 
-Platform* Platform::create(Game* game)
+Platform* Platform::create(Game* game, void* attachToWindow)
 {
+	__attachToWindow = attachToWindow;
     Platform* platform = new Platform(game);
     
     return platform;
@@ -761,7 +763,7 @@ bool Platform::hasMouse()
     return true;
 }
 
-void Platform::setMouseCapture(bool captured)
+void Platform::setMouseCaptured(bool captured)
 {
     // TODO: not implemented
 }

+ 2 - 3
gameplay/src/PlatformQNX.cpp

@@ -488,10 +488,9 @@ Platform::~Platform()
     }
 }
 
-Platform* Platform::create(Game* game)
+Platform* Platform::create(Game* game, void* attachToWindow)
 {
     FileSystem::setResourcePath("./app/native/");
-
     Platform* platform = new Platform(game);
 
     bps_initialize();
@@ -1126,7 +1125,7 @@ bool Platform::hasMouse()
     return false;
 }
 
-void Platform::setMouseCapture(bool captured)
+void Platform::setMouseCaptured(bool captured)
 {
     // not supported
 }

+ 138 - 114
gameplay/src/PlatformWin32.cpp

@@ -21,6 +21,7 @@ static bool __vsync = WINDOW_VSYNC;
 static float __roll;
 static float __pitch;
 static HINSTANCE __hinstance = 0;
+static HWND __attachToWindow = 0;
 static HWND __hwnd = 0;
 static HDC __hdc = 0;
 static HGLRC __hrc = 0;
@@ -477,112 +478,8 @@ Platform::~Platform()
     }
 }
 
-Platform* Platform::create(Game* game)
+bool initializeGL()
 {
-    GP_ASSERT(game);
-
-    FileSystem::setResourcePath("./");
-
-    Platform* platform = new Platform(game);
-
-    // Get the application module handle.
-    __hinstance = ::GetModuleHandle(NULL);
-
-    LPCTSTR windowClass = L"gameplay";
-    std::wstring windowName;
-    bool fullscreen = false;
-    
-    // Read window settings from config.
-    if (game->getConfig())
-    {
-        Properties* config = game->getConfig()->getNamespace("window", true);
-        if (config)
-        {
-            // Read window title.
-            const char* title = config->getString("title");
-            if (title)
-            {
-                int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
-                wchar_t* wtitle = new wchar_t[len];
-                MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
-                windowName = wtitle;
-                SAFE_DELETE_ARRAY(wtitle);
-            }
-
-            // Read window size.
-            int width = config->getInt("width");
-            if (width != 0)
-                __width = width;
-            int height = config->getInt("height");
-            if (height != 0)
-                __height = height;
-
-            // Read fullscreen state.
-            fullscreen = config->getBool("fullscreen");
-        }
-    }
-
-    RECT rect = { 0, 0, __width, __height };
-
-    // Register our window class.
-    WNDCLASSEX wc;
-    wc.cbSize = sizeof(WNDCLASSEX);
-    wc.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
-    wc.lpfnWndProc    = (WNDPROC)__WndProc;
-    wc.cbClsExtra     = 0;
-    wc.cbWndExtra     = 0;
-    wc.hInstance      = __hinstance;
-    wc.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
-    wc.hIconSm        = NULL;
-    wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
-    wc.hbrBackground  = NULL;  // No brush - we are going to paint our own background
-    wc.lpszMenuName   = NULL;  // No default menu
-    wc.lpszClassName  = windowClass;
-
-    if (!::RegisterClassEx(&wc))
-    {
-        GP_ERROR("Failed to register window class.");
-        goto error;
-    }
-
-    if (fullscreen)
-    {
-        DEVMODE dm;
-        memset(&dm, 0, sizeof(dm));
-        dm.dmSize= sizeof(dm);
-        dm.dmPelsWidth	= __width;
-        dm.dmPelsHeight	= __height;
-        dm.dmBitsPerPel	= 32;
-        dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
-
-        // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
-        if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
-        {
-            fullscreen = false;
-            GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", __width, __height);
-            goto error;
-        }
-    }
-
-    // Set the window style.
-    DWORD style   = (fullscreen ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
-    //DWORD style   = (fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
-    DWORD styleEx = (fullscreen ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
-
-    // Adjust the window rectangle so the client size is the requested size.
-    AdjustWindowRectEx(&rect, style, FALSE, styleEx);
-
-    // Create the native Windows window.
-    __hwnd = CreateWindowEx(styleEx, windowClass, windowName.c_str(), style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
-
-    // Get the drawing context.
-    __hdc = GetDC(__hwnd);
-
-    // Center the window
-    const int screenX = (GetSystemMetrics(SM_CXSCREEN) - __width) / 2;
-    const int screenY = (GetSystemMetrics(SM_CYSCREEN) - __height) / 2;
-    ::SetWindowPos(__hwnd, __hwnd, screenX, screenY, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
-
     // Choose pixel format. 32-bit. RGBA.
     PIXELFORMATDESCRIPTOR pfd;
     memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
@@ -599,12 +496,12 @@ Platform* Platform::create(Game* game)
     if (pixelFormat == 0)
     {
         GP_ERROR("Failed to choose a pixel format.");
-        goto error;
+        return false;
     }
     if (!SetPixelFormat (__hdc, pixelFormat, &pfd))
     {
         GP_ERROR("Failed to set the pixel format.");
-        goto error;
+        return false;
     }
 
     HGLRC tempContext = wglCreateContext(__hdc);
@@ -613,7 +510,7 @@ Platform* Platform::create(Game* game)
     if (GLEW_OK != glewInit())
     {
         GP_ERROR("Failed to initialize GLEW.");
-        goto error;
+        return false;
     }
 
     int attribs[] =
@@ -627,21 +524,146 @@ Platform* Platform::create(Game* game)
     {
         wglDeleteContext(tempContext);
         GP_ERROR("Failed to create OpenGL context.");
-        goto error;
+        return false;
     }
     wglDeleteContext(tempContext);
 
     if (!wglMakeCurrent(__hdc, __hrc) || !__hrc)
     {
         GP_ERROR("Failed to make the window current.");
-        goto error;
+        return false;
     }
 
     // Vertical sync.
     wglSwapIntervalEXT(__vsync ? 1 : 0);
 
-    // Show the window.
-    ShowWindow(__hwnd, SW_SHOW);
+    return true;
+}
+
+Platform* Platform::create(Game* game, void* attachToWindow)
+{
+    GP_ASSERT(game);
+
+    FileSystem::setResourcePath("./");
+    Platform* platform = new Platform(game);
+
+    // Get the application module handle.
+    __hinstance = ::GetModuleHandle(NULL);
+
+    __attachToWindow = (HWND)attachToWindow;
+    if (!__attachToWindow)
+    {
+        LPCTSTR windowClass = L"gameplay";
+        std::wstring windowName;
+        bool fullscreen = false;
+    
+        // Read window settings from config.
+        if (game->getConfig())
+        {
+            Properties* config = game->getConfig()->getNamespace("window", true);
+            if (config)
+            {
+                // Read window title.
+                const char* title = config->getString("title");
+                if (title)
+                {
+                    int len = MultiByteToWideChar(CP_ACP, 0, title, -1, NULL, 0);
+                    wchar_t* wtitle = new wchar_t[len];
+                    MultiByteToWideChar(CP_ACP, 0, title, -1, wtitle, len);
+                    windowName = wtitle;
+                    SAFE_DELETE_ARRAY(wtitle);
+                }
+
+                // Read window size.
+                int width = config->getInt("width");
+                if (width != 0)
+                    __width = width;
+                int height = config->getInt("height");
+                if (height != 0)
+                    __height = height;
+
+                // Read fullscreen state.
+                fullscreen = config->getBool("fullscreen");
+            }
+        }
+
+        RECT rect = { 0, 0, __width, __height };
+
+        // Register our window class.
+        WNDCLASSEX wc;
+        wc.cbSize = sizeof(WNDCLASSEX);
+        wc.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+        wc.lpfnWndProc    = (WNDPROC)__WndProc;
+        wc.cbClsExtra     = 0;
+        wc.cbWndExtra     = 0;
+        wc.hInstance      = __hinstance;
+        wc.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
+        wc.hIconSm        = NULL;
+        wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
+        wc.hbrBackground  = NULL;  // No brush - we are going to paint our own background
+        wc.lpszMenuName   = NULL;  // No default menu
+        wc.lpszClassName  = windowClass;
+
+        if (!::RegisterClassEx(&wc))
+        {
+            GP_ERROR("Failed to register window class.");
+            goto error;
+        }
+
+        if (fullscreen)
+        {
+            DEVMODE dm;
+            memset(&dm, 0, sizeof(dm));
+            dm.dmSize= sizeof(dm);
+            dm.dmPelsWidth	= __width;
+            dm.dmPelsHeight	= __height;
+            dm.dmBitsPerPel	= 32;
+            dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
+
+            // Try to set selected mode and get results. NOTE: CDS_FULLSCREEN gets rid of start bar.
+            if (ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
+            {
+                fullscreen = false;
+                GP_ERROR("Failed to start game in full-screen mode with resolution %dx%d.", __width, __height);
+                goto error;
+            }
+        }
+
+        // Set the window style.
+        DWORD style   = (fullscreen ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
+        DWORD styleEx = (fullscreen ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
+
+        // Adjust the window rectangle so the client size is the requested size.
+        AdjustWindowRectEx(&rect, style, FALSE, styleEx);
+
+        // Create the native Windows window.
+        __hwnd = CreateWindowEx(styleEx, windowClass, windowName.c_str(), style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL);
+
+        // Get the drawing context.
+        __hdc = GetDC(__hwnd);
+
+        // Center the window
+        const int screenX = (GetSystemMetrics(SM_CXSCREEN) - __width) / 2;
+        const int screenY = (GetSystemMetrics(SM_CYSCREEN) - __height) / 2;
+        ::SetWindowPos(__hwnd, __hwnd, screenX, screenY, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
+
+        if (!initializeGL())
+            goto error;
+
+        // Show the window.
+        ShowWindow(__hwnd, SW_SHOW);
+    }
+    else
+    {
+        // Attach to a previous windows
+        __hwnd = (HWND)__attachToWindow;
+        __hdc = GetDC(__hwnd);
+
+        SetWindowLongPtr(__hwnd, GWL_WNDPROC, (LONG)(WNDPROC)__WndProc);
+        
+        if (!initializeGL())
+            goto error;
+    }
 
     return platform;
 
@@ -674,6 +696,9 @@ int Platform::enterMessagePump()
     if (_game->getState() != Game::RUNNING)
         _game->run();
 
+    if (__attachToWindow)
+        return 0;
+
     // Enter event dispatch loop.
     MSG msg;
     while (true)
@@ -699,7 +724,6 @@ int Platform::enterMessagePump()
         if (_game->getState() == Game::UNINITIALIZED)
             break;
     }
-
     return msg.wParam;
 }
 
@@ -768,7 +792,7 @@ bool Platform::hasMouse()
     return true;
 }
 
-void Platform::setMouseCapture(bool captured)
+void Platform::setMouseCaptured(bool captured)
 {
     if (captured != __mouseCaptured)
     {

+ 2 - 2
gameplay/src/PlatformiOS.mm

@@ -833,7 +833,7 @@ Platform::~Platform()
 {
 }
 
-Platform* Platform::create(Game* game)
+Platform* Platform::create(Game* game, void* attachToWindow)
 {
     Platform* platform = new Platform(game);
     return platform;
@@ -900,7 +900,7 @@ bool Platform::hasMouse()
     return false;
 }
 
-void Platform::setMouseCapture(bool captured)
+void Platform::setMouseCaptured(bool captured)
 {
     // not supported
 }