From 50d0da2604f79c4246312681c9e1cb0a5e5402d5 Mon Sep 17 00:00:00 2001 From: maiconpintoabreu Date: Tue, 30 Sep 2025 11:16:16 +0100 Subject: [PATCH] Add monitor drawing and more information --- examples/core/core_monitor_change.c | 113 +++++++++++++++++++++----- examples/core/core_monitor_change.png | Bin 6062 -> 16852 bytes 2 files changed, 94 insertions(+), 19 deletions(-) diff --git a/examples/core/core_monitor_change.c b/examples/core/core_monitor_change.c index f9c9e3040..bcbb7c990 100644 --- a/examples/core/core_monitor_change.c +++ b/examples/core/core_monitor_change.c @@ -17,6 +17,19 @@ #include "raylib.h" +#define MAX_MONITORS 10 + +// Monitor Details +typedef struct Monitor { + Vector2 position; + char *name; + int width; + int height; + int physicalWidth; + int physicalHeight; + int refreshRate; +} Monitor; + //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ @@ -27,9 +40,12 @@ int main(void) const int screenWidth = 800; const int screenHeight = 450; + Monitor monitors[MAX_MONITORS] = { 0 }; + InitWindow(screenWidth, screenHeight, "raylib [core] example - monitor change"); - int currentMonitor = GetCurrentMonitor(); + int currentMonitorIndex = GetCurrentMonitor(); + int monitorCount = 0; SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- @@ -40,18 +56,43 @@ int main(void) // Update //---------------------------------------------------------------------------------- - if (IsKeyPressed(KEY_ENTER)) { - currentMonitor += 1; - if(currentMonitor == GetMonitorCount()) { - currentMonitor = 0; + // Rebuild monitors array with the new monitor count + if (monitorCount != GetMonitorCount()) + { + monitorCount = GetMonitorCount(); + for (int i = 0; i < monitorCount; i++) + { + monitors[i] = (Monitor){ + GetMonitorPosition(i), + GetMonitorName(i), + GetMonitorWidth(i), + GetMonitorHeight(i), + GetMonitorPhysicalWidth(i), + GetMonitorPhysicalHeight(i), + GetMonitorRefreshRate(i) + }; } - SetWindowMonitor(currentMonitor); - } else { - currentMonitor = GetCurrentMonitor(); } - - const Vector2 resolution = (Vector2){(float)GetMonitorWidth(currentMonitor), (float)GetMonitorHeight(currentMonitor)}; - const int refreshRate = GetMonitorRefreshRate(currentMonitor); + + if (IsKeyPressed(KEY_ENTER) && monitorCount > 1) + { + currentMonitorIndex += 1; + + // Set index to 0 if the last one + if(currentMonitorIndex == GetMonitorCount()) + { + currentMonitorIndex = 0; + } + SetWindowMonitor(currentMonitorIndex); // Move window to currentMonitorIndex + } + else + { + // Get currentMonitorIndex if manually moved + currentMonitorIndex = GetCurrentMonitor(); + } + const Monitor currentMonitor = monitors[currentMonitorIndex]; + + const float monitorScale = 0.2 / monitorCount; // Draw //---------------------------------------------------------------------------------- @@ -59,16 +100,50 @@ int main(void) ClearBackground(RAYWHITE); - DrawText("[Enter] Move to the next monitor available", 20, 20, 20, DARKGRAY); + DrawText("Press [Enter] to move window to next monitor available", 20, 20, 20, DARKGRAY); DrawText( - TextFormat("%3.0f x %3.0f [%ihz]", - resolution.x, - resolution.y, - refreshRate - ), 20, 70, 20, GRAY); - DrawText(TextFormat("%i", currentMonitor), screenWidth * 0.5, screenHeight * 0.5, 70, GRAY); - DrawRectangleLines(20, 100, screenWidth - 40, screenHeight - 120, DARKGRAY); + TextFormat("Resolution: [%ipx x %ipx]\nRefreshRate: [%ihz]\nPhysical Size: [%imm x %imm]\nPosition: %3.2f x %3.2f", + currentMonitor.width, + currentMonitor.height, + currentMonitor.refreshRate, + currentMonitor.physicalWidth, + currentMonitor.physicalHeight, + currentMonitor.position.x, + currentMonitor.position.y + ), 30, 80, 20, GRAY); + + // List available Monitors + for (int i = 0; i < monitorCount; i++) + { + DrawText(TextFormat("%s", monitors[i].name), 40, 180 + 20*i, 20, GRAY); + if (i == currentMonitorIndex) + { + DrawCircle(30, 190 + 20*i, 5, RED); + } + } + DrawRectangleLines(20, 60, screenWidth - 40, screenHeight - 100, DARKGRAY); + + // Draw Monitors + for (int i = 0; i < monitorCount; i++) + { + const Rectangle rec = (Rectangle){ + monitors[i].position.x * monitorScale + 140, + monitors[i].position.y * monitorScale + 180, + monitors[i].width * monitorScale, + monitors[i].height * monitorScale + }; + if (i == currentMonitorIndex) + { + DrawRectangleLinesEx(rec, 5, RED); + } + else + { + DrawRectangleLinesEx(rec, 5, GRAY); + } + DrawText(TextFormat("%i", i), rec.x + rec.width * 0.5 - 10, rec.y + rec.height * 0.5 - 25, 50, GRAY); + + } EndDrawing(); diff --git a/examples/core/core_monitor_change.png b/examples/core/core_monitor_change.png index ceb984ccb10910ff48681ad568e058f339d6eb32..5b8d1aedbd658c271c540ce2dba40dc61eca28a7 100644 GIT binary patch literal 16852 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYU_8XZ#=yWJp1k%11B2siPZ!6Kin!!IzrMb% zZwoY#Vp^<-gDBx(Ty`-)j<@lmfCN^8v8wez278Lh+TOMU85})W2!oz72RGq^*d=?~ z4s76VyeK*0qKhpqXEzxn82<-IwJN^N+yRml04PY|(>Im~!B ze(^`&I8OJ~1uCX?ZjbY~e6^8ca#m+?OLuS!evK;)Gu(A*NUL_>=-o2eiplQIS$6rNi1i$&1QXj}bu;(Fg(I4l^R81etAF72zj^N*mF+Q$LM@$F$S9iI3$N}EsC3V( zJ!!xN4&V$irp3kzi#xN?0vyh5$WwIKEGba*y<)i;VTsy<~F(N%1^SoyP@UZ9S~b;&FEShN7Gj zwUmry%t_x(0(Q??cvVg~>iX)%UvFh}+DkDtL(|%0d9=+SAON&`41<(<5+6!^yx?& z^VZMT6Eyohb=BLeEuBx)Z>U&oa_5Auew)``vs=$(c^fB6fYZRsI(Vd`Wxs|z#REUS zzP|o{@k8I=0v&3F{U-~0Ctoa(IGc-EnQU@SC}B%6VaHZIfWD-3Qyy`dvPLmu5Qo@W{o zxXL4TOpxkRHofEidIM@3f$@%_ffcjSEGsmt5!@GIOwR8_)-2qV-f$s~H|A4|+n1J!(I`=)KE4=`BaA^|}h_oR6+=XLcPl z`^@Mq!)$8YpY{Y_S4xU}X8#-h!s+6(Jxg+@Pq6r5l zV^aDOx2lT3Y<>XERfP&iDLMRRCM$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