From ff33e12ee5d31d405b6372ee2cd40e58cac8fd38 Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Fri, 26 Apr 2019 19:25:23 +0100 Subject: [PATCH] Tested some desktop stuff and added projection matrix updates for window resizing. --- examples/core/core_basic_window.cpp | 2 + projects/VS2017.UWP/raylib.App.UWP/App.cpp | 2 +- projects/VS2017.UWP/raylib.App.UWP/BaseApp.h | 21 ++- src/core.c | 156 +++++++++++-------- src/utils.h | 2 + 5 files changed, 117 insertions(+), 66 deletions(-) diff --git a/examples/core/core_basic_window.cpp b/examples/core/core_basic_window.cpp index fa12026a0..a0c56f6fe 100644 --- a/examples/core/core_basic_window.cpp +++ b/examples/core/core_basic_window.cpp @@ -28,6 +28,8 @@ int main(int argc, char* argv[]) int screenWidth = 800; int screenHeight = 450; + SetConfigFlags(FLAG_WINDOW_RESIZABLE); + InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); SetTargetFPS(60); diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.cpp b/projects/VS2017.UWP/raylib.App.UWP/App.cpp index a8d0f7cf3..1e5ff6466 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.cpp +++ b/projects/VS2017.UWP/raylib.App.UWP/App.cpp @@ -18,7 +18,7 @@ int main(Platform::Array^) App::App() { //This does not work... need to fix this. - SetConfigFlags(FLAG_SHOW_LOGO); + SetConfigFlags(0); } static int posX = 100; diff --git a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h index be93d9d59..e7efd5ec1 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h +++ b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h @@ -123,6 +123,16 @@ public: virtual void Run() { + //Get display dimensions + DisplayInformation^ dInfo = DisplayInformation::GetForCurrentView(); + Vector2 screenSize = { dInfo->ScreenWidthInRawPixels, dInfo->ScreenHeightInRawPixels }; + + //Send display dimensions + UWPMessage* msg = CreateUWPMessage(); + msg->Type = SetDisplayDims; + msg->Vector0 = screenSize; + UWPSendMessage(msg); + while (!mWindowClosed) { if (mWindowVisible) @@ -285,7 +295,12 @@ protected: void OnResuming(Platform::Object^ sender, Platform::Object^ args) {} // Window event handlers. - void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args) {} + void OnWindowSizeChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::WindowSizeChangedEventArgs^ args) + { + UWPMessage* msg = CreateUWPMessage(); + msg->Type = HandleResize; + UWPSendMessage(msg); + } void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args) { @@ -416,8 +431,8 @@ private: bool mWindowClosed = false; bool mWindowVisible = true; - int width = 800; - int height = 450; + int width = 640; + int height = 480; }; //Application source for creating the program diff --git a/src/core.c b/src/core.c index e49584e03..ff43ae08d 100644 --- a/src/core.c +++ b/src/core.c @@ -626,13 +626,11 @@ void InitWindow(int width, int height, const char *title) mousePosition.y = (float)screenHeight/2.0f; // raylib logo appearing animation (if enabled) -#if !defined(PLATFORM_UWP) if (showLogo) { SetTargetFPS(60); LogoAnimation(); } -#endif //defined(PLATFORM_UWP) #endif // defined(PLATFORM_ANDROID) } @@ -887,7 +885,6 @@ int GetScreenHeight(void) // Get native window handle void *GetWindowHandle(void) { - //TODO: See if we can do UWP? #ifdef PLATFORM_DESKTOP // NOTE: Returned handle is: void *HWND (windows.h) return glfwGetWin32Window(window); @@ -2764,6 +2761,8 @@ static bool InitGraphicsDevice(int width, int height) eglQuerySurface(display, surface, EGL_WIDTH, &screenWidth); eglQuerySurface(display, surface, EGL_HEIGHT, &screenHeight); + //SetupFramebuffer(displayWidth, displayHeight); //Borked + #else // PLATFORM_ANDROID, PLATFORM_RPI EGLint numConfigs; @@ -2928,72 +2927,72 @@ static void SetupViewport(void) // NOTE: Global variables renderWidth/renderHeight and renderOffsetX/renderOffsetY can be modified static void SetupFramebuffer(int width, int height) { - // Calculate renderWidth and renderHeight, we have the display size (input params) and the desired screen size (global var) - if ((screenWidth > displayWidth) || (screenHeight > displayHeight)) - { - TraceLog(LOG_WARNING, "DOWNSCALING: Required screen size (%ix%i) is bigger than display size (%ix%i)", screenWidth, screenHeight, displayWidth, displayHeight); + // Calculate renderWidth and renderHeight, we have the display size (input params) and the desired screen size (global var) + if ((screenWidth > displayWidth) || (screenHeight > displayHeight)) + { + TraceLog(LOG_WARNING, "DOWNSCALING: Required screen size (%ix%i) is bigger than display size (%ix%i)", screenWidth, screenHeight, displayWidth, displayHeight); - // Downscaling to fit display with border-bars - float widthRatio = (float)displayWidth/(float)screenWidth; - float heightRatio = (float)displayHeight/(float)screenHeight; + // Downscaling to fit display with border-bars + float widthRatio = (float)displayWidth / (float)screenWidth; + float heightRatio = (float)displayHeight / (float)screenHeight; - if (widthRatio <= heightRatio) - { - renderWidth = displayWidth; - renderHeight = (int)round((float)screenHeight*widthRatio); - renderOffsetX = 0; - renderOffsetY = (displayHeight - renderHeight); - } - else - { - renderWidth = (int)round((float)screenWidth*heightRatio); - renderHeight = displayHeight; - renderOffsetX = (displayWidth - renderWidth); - renderOffsetY = 0; - } + if (widthRatio <= heightRatio) + { + renderWidth = displayWidth; + renderHeight = (int)round((float)screenHeight*widthRatio); + renderOffsetX = 0; + renderOffsetY = (displayHeight - renderHeight); + } + else + { + renderWidth = (int)round((float)screenWidth*heightRatio); + renderHeight = displayHeight; + renderOffsetX = (displayWidth - renderWidth); + renderOffsetY = 0; + } - // Screen scaling required - float scaleRatio = (float)renderWidth/(float)screenWidth; - screenScaling = MatrixScale(scaleRatio, scaleRatio, scaleRatio); + // Screen scaling required + float scaleRatio = (float)renderWidth / (float)screenWidth; + screenScaling = MatrixScale(scaleRatio, scaleRatio, scaleRatio); - // NOTE: We render to full display resolution! - // We just need to calculate above parameters for downscale matrix and offsets - renderWidth = displayWidth; - renderHeight = displayHeight; + // NOTE: We render to full display resolution! + // We just need to calculate above parameters for downscale matrix and offsets + renderWidth = displayWidth; + renderHeight = displayHeight; - TraceLog(LOG_WARNING, "Downscale matrix generated, content will be rendered at: %i x %i", renderWidth, renderHeight); - } - else if ((screenWidth < displayWidth) || (screenHeight < displayHeight)) - { - // Required screen size is smaller than display size - TraceLog(LOG_INFO, "UPSCALING: Required screen size: %i x %i -> Display size: %i x %i", screenWidth, screenHeight, displayWidth, displayHeight); + TraceLog(LOG_WARNING, "Downscale matrix generated, content will be rendered at: %i x %i", renderWidth, renderHeight); + } + else if ((screenWidth < displayWidth) || (screenHeight < displayHeight)) + { + // Required screen size is smaller than display size + TraceLog(LOG_INFO, "UPSCALING: Required screen size: %i x %i -> Display size: %i x %i", screenWidth, screenHeight, displayWidth, displayHeight); - // Upscaling to fit display with border-bars - float displayRatio = (float)displayWidth/(float)displayHeight; - float screenRatio = (float)screenWidth/(float)screenHeight; + // Upscaling to fit display with border-bars + float displayRatio = (float)displayWidth / (float)displayHeight; + float screenRatio = (float)screenWidth / (float)screenHeight; - if (displayRatio <= screenRatio) - { - renderWidth = screenWidth; - renderHeight = (int)round((float)screenWidth/displayRatio); - renderOffsetX = 0; - renderOffsetY = (renderHeight - screenHeight); - } - else - { - renderWidth = (int)round((float)screenHeight*displayRatio); - renderHeight = screenHeight; - renderOffsetX = (renderWidth - screenWidth); - renderOffsetY = 0; - } - } - else // screen == display - { - renderWidth = screenWidth; - renderHeight = screenHeight; - renderOffsetX = 0; - renderOffsetY = 0; - } + if (displayRatio <= screenRatio) + { + renderWidth = screenWidth; + renderHeight = (int)round((float)screenWidth / displayRatio); + renderOffsetX = 0; + renderOffsetY = (renderHeight - screenHeight); + } + else + { + renderWidth = (int)round((float)screenHeight*displayRatio); + renderHeight = screenHeight; + renderOffsetX = (renderWidth - screenWidth); + renderOffsetY = 0; + } + } + else // screen == display + { + renderWidth = screenWidth; + renderHeight = screenHeight; + renderOffsetX = 0; + renderOffsetY = 0; + } } // Initialize hi-resolution timer @@ -3263,6 +3262,39 @@ static void PollInputEvents(void) break; } + case SetDisplayDims: + { + displayWidth = msg->Vector0.x; + displayHeight = msg->Vector0.y; + break; + } + + case HandleResize: + { + eglQuerySurface(display, surface, EGL_WIDTH, &screenWidth); + eglQuerySurface(display, surface, EGL_HEIGHT, &screenHeight); + + // If window is resized, viewport and projection matrix needs to be re-calculated + rlViewport(0, 0, screenWidth, screenHeight); // Set viewport width and height + rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix + rlLoadIdentity(); // Reset current matrix (PROJECTION) + rlOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f); // Orthographic projection mode with top-left corner at (0,0) + rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix + rlLoadIdentity(); // Reset current matrix (MODELVIEW) + rlClearScreenBuffers(); // Clear screen buffers (color and depth) + + // Window size must be updated to be used on 3D mode to get new aspect ratio (BeginMode3D()) + // NOTE: Be careful! GLFW3 will choose the closest fullscreen resolution supported by current monitor, + // for example, if reescaling back to 800x450 (desired), it could set 720x480 (closest fullscreen supported) + currentWidth = screenWidth; + currentHeight = screenHeight; + + // NOTE: Postprocessing texture is not scaled to new size + + windowResized = true; + break; + } + } DeleteUWPMessage(msg); //Delete, we are done diff --git a/src/utils.h b/src/utils.h index 336773682..9ea92dde4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -82,6 +82,8 @@ typedef enum MarkGamepadActive, //Int0 (gamepad), Bool0 (active or not) MarkGamepadButton, //Int0 (gamepad), Int1 (button), Char0 (status) MarkGamepadAxis, //Int0 (gamepad), int1 (axis), Float0 (value) + SetDisplayDims, //Vector0 (display dimensions) + HandleResize, //Vector0 (new dimensions) - Onresized event } UWPMessageType; typedef struct UWPMessage {