Skip to content

Commit 32daf4e

Browse files
authored
Merge pull request #238 from RobLoach/SetConfigFlags
Add flags and SetConfigFlag to Window
2 parents b2d3787 + 5292404 commit 32daf4e

File tree

2 files changed

+141
-3
lines changed

2 files changed

+141
-3
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

include/Window.hpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ class Window {
2525
/**
2626
* Initialize window and OpenGL context.
2727
*
28+
* @param width The width of the window.
29+
* @param height The height of the window.
30+
* @param title The desired title of the window.
31+
* @param flags The ConfigFlags to set prior to initializing the window. See SetConfigFlags for more details.
32+
*
33+
* @see ::SetConfigFlags()
34+
* @see ConfigFlags
35+
*
2836
* @throws raylib::RaylibException Thrown if the window failed to initiate.
2937
*/
30-
Window(int width, int height, const std::string& title = "raylib") {
31-
Init(width, height, title);
38+
Window(int width, int height, const std::string& title = "raylib", unsigned int flags = 0) {
39+
Init(width, height, title, flags);
3240
}
3341

3442
/**
@@ -41,9 +49,20 @@ class Window {
4149
/**
4250
* Initializes the window.
4351
*
52+
* @param width The width of the window.
53+
* @param height The height of the window.
54+
* @param title The desired title of the window.
55+
* @param flags The ConfigFlags to set prior to initializing the window. See SetConfigFlags for more details.
56+
*
57+
* @see ::SetConfigFlags()
58+
* @see ConfigFlags
59+
*
4460
* @throws raylib::RaylibException Thrown if the window failed to initiate.
4561
*/
46-
inline void Init(int width = 800, int height = 450, const std::string& title = "raylib") {
62+
inline void Init(int width = 800, int height = 450, const std::string& title = "raylib", unsigned int flags = 0) {
63+
if (flags != 0) {
64+
::SetConfigFlags(flags);
65+
}
4766
::InitWindow(width, height, title.c_str());
4867
if (!::IsWindowReady()) {
4968
throw RaylibException("Failed to create Window");
@@ -401,6 +420,17 @@ class Window {
401420
inline static bool IsReady() {
402421
return ::IsWindowReady();
403422
}
423+
424+
/**
425+
* Sets the configuration flags for raylib.
426+
*
427+
* @param flags The ConfigFlags to apply to the configuration.
428+
*
429+
* @see ::SetConfigFlags
430+
*/
431+
inline void SetConfigFlags(unsigned int flags) {
432+
::SetConfigFlags(flags);
433+
}
404434
};
405435
} // namespace raylib
406436

0 commit comments

Comments
 (0)