From b722cfa8bfbac51e1cbdf6fe92a29906328d4177 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 22 Jun 2023 12:04:55 -0400 Subject: [PATCH 1/3] Add flags and SetConfigFlag to Window --- examples/core/core_window_letterbox.cpp | 104 ++++++++++++++++++++++++ include/Window.hpp | 13 ++- 2 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 examples/core/core_window_letterbox.cpp diff --git a/examples/core/core_window_letterbox.cpp b/examples/core/core_window_letterbox.cpp new file mode 100644 index 00000000..bdf2951d --- /dev/null +++ b/examples/core/core_window_letterbox.cpp @@ -0,0 +1,104 @@ +/******************************************************************************************* +* +* raylib [core] example - window scale letterbox (and virtual mouse) +* +* Example originally created with raylib 2.5, last time updated with raylib 4.0 +* +* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2019-2023 Anata (@anatagawa) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib-cpp.hpp" + +#include "raymath.hpp" // Required for: Vector2Clamp() + +#define MAX(a, b) ((a)>(b)? (a) : (b)) +#define MIN(a, b) ((a)<(b)? (a) : (b)) + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + const int windowWidth = 800; + const int windowHeight = 450; + + // Enable config flags for resizable window and vertical synchro + raylib::Window window(windowWidth, windowHeight, + "raylib [core] example - window scale letterbox", + FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT); + window.SetMinSize(320, 240); + + int gameScreenWidth = 640; + int gameScreenHeight = 480; + + // Render texture initialization, used to hold the rendering result so we can easily resize it + raylib::RenderTexture2D target(gameScreenWidth, gameScreenHeight); + target.GetTexture().SetFilter(TEXTURE_FILTER_BILINEAR); // Texture scale filter to use + + raylib::Color colors[10] = { 0 }; + for (int i = 0; i < 10; i++) { + colors[i] = raylib::Color((unsigned int)GetRandomValue(100, 250), (unsigned int)GetRandomValue(50, 150), (unsigned int)GetRandomValue(10, 100), 255); + } + + window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!window.ShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // Compute required framebuffer scaling + float scale = MIN((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight); + + if (IsKeyPressed(KEY_SPACE)) + { + // Recalculate random colors for the bars + for (int i = 0; i < 10; i++) colors[i] = (Color){ (unsigned int)GetRandomValue(100, 250), (unsigned int)GetRandomValue(50, 150), (unsigned int)GetRandomValue(10, 100), 255 }; + } + + // Update virtual mouse (clamped mouse value behind game screen) + raylib::Vector2 mouse = raylib::Mouse::GetPosition(); + raylib::Vector2 virtualMouse( + (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale, + (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale + ); + virtualMouse = virtualMouse.Clamp((Vector2){ 0, 0 }, (Vector2){ (float)gameScreenWidth, (float)gameScreenHeight }); + + // Apply the same transformation as the virtual mouse to the real mouse (i.e. to work with raygui) + //SetMouseOffset(-(GetScreenWidth() - (gameScreenWidth*scale))*0.5f, -(GetScreenHeight() - (gameScreenHeight*scale))*0.5f); + //SetMouseScale(1/scale, 1/scale); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + // Draw everything in the render texture, note this will not be rendered on screen, yet + target.BeginMode(); + ClearBackground(RAYWHITE); // Clear render texture background color + + for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]); + + DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE); + DrawText(TextFormat("Default Mouse: [%i , %i]", (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN); + DrawText(TextFormat("Virtual Mouse: [%i , %i]", (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW); + target.EndMode(); + + BeginDrawing(); + ClearBackground(BLACK); // Clear screen background + + // Draw render texture to screen, properly scaled + target.GetTexture().Draw((Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height }, + (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5f, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5f, + (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE); + EndDrawing(); + //-------------------------------------------------------------------------------------- + } + + return 0; +} diff --git a/include/Window.hpp b/include/Window.hpp index 8ee78e11..45244554 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -27,8 +27,8 @@ class Window { * * @throws raylib::RaylibException Thrown if the window failed to initiate. */ - Window(int width, int height, const std::string& title = "raylib") { - Init(width, height, title); + Window(int width, int height, const std::string& title = "raylib", unsigned int flags = 0) { + Init(width, height, title, flags); } /** @@ -43,7 +43,10 @@ class Window { * * @throws raylib::RaylibException Thrown if the window failed to initiate. */ - inline void Init(int width = 800, int height = 450, const std::string& title = "raylib") { + inline void Init(int width = 800, int height = 450, const std::string& title = "raylib", unsigned int flags = 0) { + if (flags != 0) { + SetConfigFlags(flags); + } ::InitWindow(width, height, title.c_str()); if (!::IsWindowReady()) { throw RaylibException("Failed to create Window"); @@ -401,6 +404,10 @@ class Window { inline static bool IsReady() { return ::IsWindowReady(); } + + inline void SetConfigFlags(unsigned int flags) { + ::SetConfigFlags(flags); + } }; } // namespace raylib From 3677be2b331492297e72b3095575813dd32a4ed2 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 22 Jun 2023 12:13:26 -0400 Subject: [PATCH 2/3] Add doxygen comments --- include/Window.hpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/include/Window.hpp b/include/Window.hpp index 45244554..2693de63 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -25,6 +25,14 @@ class Window { /** * Initialize window and OpenGL context. * + * @param width The width of the window. + * @param height The height of the window. + * @param title The desired title of the window. + * @param flags The ConfigFlags to set prior to initializing the window. See SetConfigFlags for more details. + * + * @see ::SetConfigFlags() + * @see ConfigFlags + * * @throws raylib::RaylibException Thrown if the window failed to initiate. */ Window(int width, int height, const std::string& title = "raylib", unsigned int flags = 0) { @@ -41,11 +49,19 @@ class Window { /** * Initializes the window. * + * @param width The width of the window. + * @param height The height of the window. + * @param title The desired title of the window. + * @param flags The ConfigFlags to set prior to initializing the window. See SetConfigFlags for more details. + * + * @see ::SetConfigFlags() + * @see ConfigFlags + * * @throws raylib::RaylibException Thrown if the window failed to initiate. */ inline void Init(int width = 800, int height = 450, const std::string& title = "raylib", unsigned int flags = 0) { if (flags != 0) { - SetConfigFlags(flags); + ::SetConfigFlags(flags); } ::InitWindow(width, height, title.c_str()); if (!::IsWindowReady()) { @@ -405,6 +421,13 @@ class Window { return ::IsWindowReady(); } + /** + * Sets the configuration flags for raylib. + * + * @param flags The ConfigFlags to apply to the configuration. + * + * @see ::SetConfigFlags + */ inline void SetConfigFlags(unsigned int flags) { ::SetConfigFlags(flags); } From 7ec0e3e82c591ed04b38fdbbf8ffdfb054119a8c Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 22 Jun 2023 12:22:33 -0400 Subject: [PATCH 3/3] Update casting --- examples/core/core_window_letterbox.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/core/core_window_letterbox.cpp b/examples/core/core_window_letterbox.cpp index bdf2951d..aeb166fe 100644 --- a/examples/core/core_window_letterbox.cpp +++ b/examples/core/core_window_letterbox.cpp @@ -43,7 +43,7 @@ int main(void) raylib::Color colors[10] = { 0 }; for (int i = 0; i < 10; i++) { - colors[i] = raylib::Color((unsigned int)GetRandomValue(100, 250), (unsigned int)GetRandomValue(50, 150), (unsigned int)GetRandomValue(10, 100), 255); + colors[i] = raylib::Color((unsigned char)GetRandomValue(100, 250), (unsigned char)GetRandomValue(50, 150), (unsigned char)GetRandomValue(10, 100), 255); } window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second @@ -60,7 +60,7 @@ int main(void) if (IsKeyPressed(KEY_SPACE)) { // Recalculate random colors for the bars - for (int i = 0; i < 10; i++) colors[i] = (Color){ (unsigned int)GetRandomValue(100, 250), (unsigned int)GetRandomValue(50, 150), (unsigned int)GetRandomValue(10, 100), 255 }; + for (int i = 0; i < 10; i++) colors[i] = (Color){ (unsigned char)GetRandomValue(100, 250), (unsigned char)GetRandomValue(50, 150), (unsigned char)GetRandomValue(10, 100), 255 }; } // Update virtual mouse (clamped mouse value behind game screen) @@ -69,7 +69,7 @@ int main(void) (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale, (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale ); - virtualMouse = virtualMouse.Clamp((Vector2){ 0, 0 }, (Vector2){ (float)gameScreenWidth, (float)gameScreenHeight }); + virtualMouse = virtualMouse.Clamp(raylib::Vector2::Zero(), raylib::Vector2(gameScreenWidth, gameScreenHeight)); // Apply the same transformation as the virtual mouse to the real mouse (i.e. to work with raygui) //SetMouseOffset(-(GetScreenWidth() - (gameScreenWidth*scale))*0.5f, -(GetScreenHeight() - (gameScreenHeight*scale))*0.5f); @@ -93,9 +93,13 @@ int main(void) ClearBackground(BLACK); // Clear screen background // Draw render texture to screen, properly scaled - target.GetTexture().Draw((Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height }, - (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5f, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5f, - (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE); + target.GetTexture().Draw(raylib::Rectangle(0.0f, 0.0f, target.texture.width, -target.texture.height), + raylib::Rectangle( + (GetScreenWidth() - (gameScreenWidth*scale))*0.5f, + (GetScreenHeight() - (gameScreenHeight*scale))*0.5f, + gameScreenWidth*scale, gameScreenHeight*scale + ), + raylib::Vector2::Zero(), 0.0f, WHITE); EndDrawing(); //-------------------------------------------------------------------------------------- }