1 | /******************************************************************************************* |
2 | * |
3 | * raylib [core] example - window scale letterbox (and virtual mouse) |
4 | * |
5 | * This example has been created using raylib 2.5 (www.raylib.com) |
6 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
7 | * |
8 | * Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) |
9 | * |
10 | * Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5) |
11 | * |
12 | ********************************************************************************************/ |
13 | |
14 | #include "raylib.h" |
15 | |
16 | #define max(a, b) ((a)>(b)? (a) : (b)) |
17 | #define min(a, b) ((a)<(b)? (a) : (b)) |
18 | |
19 | // Clamp Vector2 value with min and max and return a new vector2 |
20 | // NOTE: Required for virtual mouse, to clamp inside virtual game size |
21 | Vector2 ClampValue(Vector2 value, Vector2 min, Vector2 max) |
22 | { |
23 | Vector2 result = value; |
24 | result.x = (result.x > max.x)? max.x : result.x; |
25 | result.x = (result.x < min.x)? min.x : result.x; |
26 | result.y = (result.y > max.y)? max.y : result.y; |
27 | result.y = (result.y < min.y)? min.y : result.y; |
28 | return result; |
29 | } |
30 | |
31 | int main(void) |
32 | { |
33 | const int windowWidth = 800; |
34 | const int windowHeight = 450; |
35 | |
36 | // Enable config flags for resizable window and vertical synchro |
37 | SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT); |
38 | InitWindow(windowWidth, windowHeight, "raylib [core] example - window scale letterbox" ); |
39 | SetWindowMinSize(320, 240); |
40 | |
41 | int gameScreenWidth = 640; |
42 | int gameScreenHeight = 480; |
43 | |
44 | // Render texture initialization, used to hold the rendering result so we can easily resize it |
45 | RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight); |
46 | SetTextureFilter(target.texture, FILTER_BILINEAR); // Texture scale filter to use |
47 | |
48 | Color colors[10] = { 0 }; |
49 | for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 }; |
50 | |
51 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
52 | //-------------------------------------------------------------------------------------- |
53 | |
54 | // Main game loop |
55 | while (!WindowShouldClose()) // Detect window close button or ESC key |
56 | { |
57 | // Update |
58 | //---------------------------------------------------------------------------------- |
59 | // Compute required framebuffer scaling |
60 | float scale = min((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight); |
61 | |
62 | if (IsKeyPressed(KEY_SPACE)) |
63 | { |
64 | // Recalculate random colors for the bars |
65 | for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 }; |
66 | } |
67 | |
68 | // Update virtual mouse (clamped mouse value behind game screen) |
69 | Vector2 mouse = GetMousePosition(); |
70 | Vector2 virtualMouse = { 0 }; |
71 | virtualMouse.x = (mouse.x - (GetScreenWidth() - (gameScreenWidth*scale))*0.5f)/scale; |
72 | virtualMouse.y = (mouse.y - (GetScreenHeight() - (gameScreenHeight*scale))*0.5f)/scale; |
73 | virtualMouse = ClampValue(virtualMouse, (Vector2){ 0, 0 }, (Vector2){ gameScreenWidth, gameScreenHeight }); |
74 | //---------------------------------------------------------------------------------- |
75 | |
76 | // Draw |
77 | //---------------------------------------------------------------------------------- |
78 | BeginDrawing(); |
79 | ClearBackground(BLACK); |
80 | |
81 | // Draw everything in the render texture, note this will not be rendered on screen, yet |
82 | BeginTextureMode(target); |
83 | |
84 | ClearBackground(RAYWHITE); // Clear render texture background color |
85 | |
86 | for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]); |
87 | |
88 | DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!" , 10, 25, 20, WHITE); |
89 | |
90 | DrawText(TextFormat("Default Mouse: [%i , %i]" , (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN); |
91 | DrawText(TextFormat("Virtual Mouse: [%i , %i]" , (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW); |
92 | |
93 | EndTextureMode(); |
94 | |
95 | // Draw RenderTexture2D to window, properly scaled |
96 | DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height }, |
97 | (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5, |
98 | (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE); |
99 | |
100 | EndDrawing(); |
101 | //-------------------------------------------------------------------------------------- |
102 | } |
103 | |
104 | // De-Initialization |
105 | //-------------------------------------------------------------------------------------- |
106 | UnloadRenderTexture(target); // Unload render texture |
107 | |
108 | CloseWindow(); // Close window and OpenGL context |
109 | //-------------------------------------------------------------------------------------- |
110 | |
111 | return 0; |
112 | } |
113 | |