Skip to content

Commit 452cac3

Browse files
[examples] Add core_monitor_change (#5215)
* Add core monitor change example * Add monitor drawing and more information * Update monitor information every frame * Show info and window position inside the rectangle
1 parent 14a0a4d commit 452cac3

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [core] example - monitor change
4+
*
5+
* Example complexity rating: [★☆☆☆] 1/4
6+
*
7+
* Example originally created with raylib 5.5, last time updated with raylib 5.6
8+
*
9+
* Example contributed by Maicon Santana (@maiconpintoabreu) and reviewed by Ramon Santamaria (@raysan5)
10+
*
11+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+
* BSD-like license that allows static linking with closed source software
13+
*
14+
* Copyright (c) 2025-2025 Maicon Santana (@maiconpintoabreu)
15+
*
16+
********************************************************************************************/
17+
18+
#include "raylib.h"
19+
20+
#define MAX_MONITORS 10
21+
22+
// Monitor Details
23+
typedef struct Monitor {
24+
Vector2 position;
25+
char *name;
26+
int width;
27+
int height;
28+
int physicalWidth;
29+
int physicalHeight;
30+
int refreshRate;
31+
} Monitor;
32+
33+
//------------------------------------------------------------------------------------
34+
// Program main entry point
35+
//------------------------------------------------------------------------------------
36+
int main(void)
37+
{
38+
// Initialization
39+
//--------------------------------------------------------------------------------------
40+
const int screenWidth = 800;
41+
const int screenHeight = 450;
42+
43+
Monitor monitors[MAX_MONITORS] = { 0 };
44+
45+
InitWindow(screenWidth, screenHeight, "raylib [core] example - monitor change");
46+
47+
int currentMonitorIndex = GetCurrentMonitor();
48+
int monitorCount = 0;
49+
50+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
51+
//--------------------------------------------------------------------------------------
52+
53+
// Main game loop
54+
while (!WindowShouldClose()) // Detect window close button or ESC key
55+
{
56+
// Update
57+
//----------------------------------------------------------------------------------
58+
59+
// Variables to find the max x and Y to calculate the scale
60+
int maxWidth = 1;
61+
int maxHeight = 1;
62+
63+
// Monitor offset is to fix when monitor position x is negative
64+
int monitorOffsetX = 0;
65+
66+
// Rebuild monitors array every frame
67+
monitorCount = GetMonitorCount();
68+
for (int i = 0; i < monitorCount; i++)
69+
{
70+
monitors[i] = (Monitor){
71+
GetMonitorPosition(i),
72+
GetMonitorName(i),
73+
GetMonitorWidth(i),
74+
GetMonitorHeight(i),
75+
GetMonitorPhysicalWidth(i),
76+
GetMonitorPhysicalHeight(i),
77+
GetMonitorRefreshRate(i)
78+
};
79+
if (monitors[i].position.x < monitorOffsetX) monitorOffsetX = monitors[i].position.x*-1;
80+
81+
const int width = monitors[i].position.x + monitors[i].width;
82+
const int height = monitors[i].position.y + monitors[i].height;
83+
84+
if (maxWidth < width) maxWidth = width;
85+
if (maxHeight < height) maxHeight = height;
86+
}
87+
88+
if (IsKeyPressed(KEY_ENTER) && monitorCount > 1)
89+
{
90+
currentMonitorIndex += 1;
91+
92+
// Set index to 0 if the last one
93+
if(currentMonitorIndex == monitorCount) currentMonitorIndex = 0;
94+
95+
SetWindowMonitor(currentMonitorIndex); // Move window to currentMonitorIndex
96+
}
97+
else
98+
{
99+
// Get currentMonitorIndex if manually moved
100+
currentMonitorIndex = GetCurrentMonitor();
101+
}
102+
const Monitor currentMonitor = monitors[currentMonitorIndex];
103+
104+
float monitorScale = 0.6;
105+
106+
if(maxHeight > maxWidth + monitorOffsetX) monitorScale *= ((float)screenHeight/(float)maxHeight);
107+
else monitorScale *= ((float)screenWidth/(float)(maxWidth + monitorOffsetX));
108+
109+
// Draw
110+
//----------------------------------------------------------------------------------
111+
BeginDrawing();
112+
113+
ClearBackground(RAYWHITE);
114+
115+
DrawText("Press [Enter] to move window to next monitor available", 20, 20, 20, DARKGRAY);
116+
117+
DrawRectangleLines(20, 60, screenWidth - 40, screenHeight - 100, DARKGRAY);
118+
119+
// Draw Monitor Rectangles with information inside
120+
for (int i = 0; i < monitorCount; i++)
121+
{
122+
// Calculate retangle position and size using monitorScale
123+
const Rectangle rec = (Rectangle){
124+
(monitors[i].position.x + monitorOffsetX) * monitorScale + 140,
125+
monitors[i].position.y * monitorScale + 80,
126+
monitors[i].width * monitorScale,
127+
monitors[i].height * monitorScale
128+
};
129+
130+
// Draw monitor name and information inside the rectangle
131+
DrawText(TextFormat("[%i] %s", i, monitors[i].name), rec.x + 10, rec.y + (int)(100*monitorScale), (int)(120*monitorScale), BLUE);
132+
DrawText(
133+
TextFormat("Resolution: [%ipx x %ipx]\nRefreshRate: [%ihz]\nPhysical Size: [%imm x %imm]\nPosition: %3.0f x %3.0f",
134+
monitors[i].width,
135+
monitors[i].height,
136+
monitors[i].refreshRate,
137+
monitors[i].physicalWidth,
138+
monitors[i].physicalHeight,
139+
monitors[i].position.x,
140+
monitors[i].position.y
141+
), rec.x + 10, rec.y + (int)(200*monitorScale), (int)(120*monitorScale), DARKGRAY);
142+
143+
// Highlight current monitor
144+
if (i == currentMonitorIndex)
145+
{
146+
DrawRectangleLinesEx(rec, 5, RED);
147+
Vector2 windowPosition = (Vector2){ (GetWindowPosition().x + monitorOffsetX)*monitorScale + 140, GetWindowPosition().y*monitorScale + 80 };
148+
149+
// Draw window position based on monitors
150+
DrawRectangleV(windowPosition, (Vector2){screenWidth * monitorScale, screenHeight * monitorScale}, Fade(GREEN, 0.5));
151+
}
152+
else
153+
{
154+
DrawRectangleLinesEx(rec, 5, GRAY);
155+
}
156+
157+
}
158+
159+
160+
EndDrawing();
161+
//----------------------------------------------------------------------------------
162+
}
163+
164+
// De-Initialization
165+
//--------------------------------------------------------------------------------------
166+
CloseWindow(); // Close window and OpenGL context
167+
//--------------------------------------------------------------------------------------
168+
169+
return 0;
170+
}
17 KB
Loading

0 commit comments

Comments
 (0)