Skip to content

Commit b722cfa

Browse files
committed
Add flags and SetConfigFlag to Window
1 parent 7133302 commit b722cfa

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 int)GetRandomValue(100, 250), (unsigned int)GetRandomValue(50, 150), (unsigned int)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 int)GetRandomValue(100, 250), (unsigned int)GetRandomValue(50, 150), (unsigned int)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((Vector2){ 0, 0 }, (Vector2){ (float)gameScreenWidth, (float)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((Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height },
97+
(Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5f, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5f,
98+
(float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
99+
EndDrawing();
100+
//--------------------------------------------------------------------------------------
101+
}
102+
103+
return 0;
104+
}

include/Window.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Window {
2727
*
2828
* @throws raylib::RaylibException Thrown if the window failed to initiate.
2929
*/
30-
Window(int width, int height, const std::string& title = "raylib") {
31-
Init(width, height, title);
30+
Window(int width, int height, const std::string& title = "raylib", unsigned int flags = 0) {
31+
Init(width, height, title, flags);
3232
}
3333

3434
/**
@@ -43,7 +43,10 @@ class Window {
4343
*
4444
* @throws raylib::RaylibException Thrown if the window failed to initiate.
4545
*/
46-
inline void Init(int width = 800, int height = 450, const std::string& title = "raylib") {
46+
inline void Init(int width = 800, int height = 450, const std::string& title = "raylib", unsigned int flags = 0) {
47+
if (flags != 0) {
48+
SetConfigFlags(flags);
49+
}
4750
::InitWindow(width, height, title.c_str());
4851
if (!::IsWindowReady()) {
4952
throw RaylibException("Failed to create Window");
@@ -401,6 +404,10 @@ class Window {
401404
inline static bool IsReady() {
402405
return ::IsWindowReady();
403406
}
407+
408+
inline void SetConfigFlags(unsigned int flags) {
409+
::SetConfigFlags(flags);
410+
}
404411
};
405412
} // namespace raylib
406413

0 commit comments

Comments
 (0)