|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [core] example - window scale letterbox (and virtual mouse) |
| 4 | +* |
| 5 | +* Example originally created with raylib 2.5, last time updated with raylib 4.0 |
| 6 | +* |
| 7 | +* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) |
| 8 | +* |
| 9 | +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 10 | +* BSD-like license that allows static linking with closed source software |
| 11 | +* |
| 12 | +* Copyright (c) 2019-2023 Anata (@anatagawa) and Ramon Santamaria (@raysan5) |
| 13 | +* |
| 14 | +********************************************************************************************/ |
| 15 | + |
| 16 | +#include "raylib-cpp.hpp" |
| 17 | + |
| 18 | +#include "raymath.hpp" // Required for: Vector2Clamp() |
| 19 | + |
| 20 | +#define MAX(a, b) ((a)>(b)? (a) : (b)) |
| 21 | +#define MIN(a, b) ((a)<(b)? (a) : (b)) |
| 22 | + |
| 23 | +//------------------------------------------------------------------------------------ |
| 24 | +// Program main entry point |
| 25 | +//------------------------------------------------------------------------------------ |
| 26 | +int main(void) |
| 27 | +{ |
| 28 | + const int windowWidth = 800; |
| 29 | + const int windowHeight = 450; |
| 30 | + |
| 31 | + // Enable config flags for resizable window and vertical synchro |
| 32 | + raylib::Window window(windowWidth, windowHeight, |
| 33 | + "raylib [core] example - window scale letterbox", |
| 34 | + FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT); |
| 35 | + window.SetMinSize(320, 240); |
| 36 | + |
| 37 | + int gameScreenWidth = 640; |
| 38 | + int gameScreenHeight = 480; |
| 39 | + |
| 40 | + // Render texture initialization, used to hold the rendering result so we can easily resize it |
| 41 | + raylib::RenderTexture2D target(gameScreenWidth, gameScreenHeight); |
| 42 | + target.GetTexture().SetFilter(TEXTURE_FILTER_BILINEAR); // Texture scale filter to use |
| 43 | + |
| 44 | + raylib::Color colors[10] = { 0 }; |
| 45 | + for (int i = 0; i < 10; i++) { |
| 46 | + colors[i] = raylib::Color((unsigned char)GetRandomValue(100, 250), (unsigned char)GetRandomValue(50, 150), (unsigned char)GetRandomValue(10, 100), 255); |
| 47 | + } |
| 48 | + |
| 49 | + window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 50 | + //-------------------------------------------------------------------------------------- |
| 51 | + |
| 52 | + // Main game loop |
| 53 | + while (!window.ShouldClose()) // Detect window close button or ESC key |
| 54 | + { |
| 55 | + // Update |
| 56 | + //---------------------------------------------------------------------------------- |
| 57 | + // Compute required framebuffer scaling |
| 58 | + float scale = MIN((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight); |
| 59 | + |
| 60 | + if (IsKeyPressed(KEY_SPACE)) |
| 61 | + { |
| 62 | + // Recalculate random colors for the bars |
| 63 | + 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 }; |
| 64 | + } |
| 65 | + |
| 66 | + // Update virtual mouse (clamped mouse value behind game screen) |
| 67 | + raylib::Vector2 mouse = raylib::Mouse::GetPosition(); |
| 68 | + raylib::Vector2 virtualMouse( |
| 69 | + (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale, |
| 70 | + (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale |
| 71 | + ); |
| 72 | + virtualMouse = virtualMouse.Clamp(raylib::Vector2::Zero(), raylib::Vector2(gameScreenWidth, gameScreenHeight)); |
| 73 | + |
| 74 | + // Apply the same transformation as the virtual mouse to the real mouse (i.e. to work with raygui) |
| 75 | + //SetMouseOffset(-(GetScreenWidth() - (gameScreenWidth*scale))*0.5f, -(GetScreenHeight() - (gameScreenHeight*scale))*0.5f); |
| 76 | + //SetMouseScale(1/scale, 1/scale); |
| 77 | + //---------------------------------------------------------------------------------- |
| 78 | + |
| 79 | + // Draw |
| 80 | + //---------------------------------------------------------------------------------- |
| 81 | + // Draw everything in the render texture, note this will not be rendered on screen, yet |
| 82 | + target.BeginMode(); |
| 83 | + ClearBackground(RAYWHITE); // Clear render texture background color |
| 84 | + |
| 85 | + for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]); |
| 86 | + |
| 87 | + DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE); |
| 88 | + DrawText(TextFormat("Default Mouse: [%i , %i]", (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN); |
| 89 | + DrawText(TextFormat("Virtual Mouse: [%i , %i]", (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW); |
| 90 | + target.EndMode(); |
| 91 | + |
| 92 | + BeginDrawing(); |
| 93 | + ClearBackground(BLACK); // Clear screen background |
| 94 | + |
| 95 | + // Draw render texture to screen, properly scaled |
| 96 | + target.GetTexture().Draw(raylib::Rectangle(0.0f, 0.0f, target.texture.width, -target.texture.height), |
| 97 | + raylib::Rectangle( |
| 98 | + (GetScreenWidth() - (gameScreenWidth*scale))*0.5f, |
| 99 | + (GetScreenHeight() - (gameScreenHeight*scale))*0.5f, |
| 100 | + gameScreenWidth*scale, gameScreenHeight*scale |
| 101 | + ), |
| 102 | + raylib::Vector2::Zero(), 0.0f, WHITE); |
| 103 | + EndDrawing(); |
| 104 | + //-------------------------------------------------------------------------------------- |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
0 commit comments