Add monitor drawing and more information

This commit is contained in:
maiconpintoabreu 2025-09-30 11:16:16 +01:00
parent 7c407ab6a1
commit 50d0da2604
2 changed files with 94 additions and 19 deletions

View File

@ -17,6 +17,19 @@
#include "raylib.h" #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 // Program main entry point
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -27,9 +40,12 @@ int main(void)
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
Monitor monitors[MAX_MONITORS] = { 0 };
InitWindow(screenWidth, screenHeight, "raylib [core] example - monitor change"); 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 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -40,18 +56,43 @@ int main(void)
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_ENTER)) { // Rebuild monitors array with the new monitor count
currentMonitor += 1; if (monitorCount != GetMonitorCount())
if(currentMonitor == GetMonitorCount()) { {
currentMonitor = 0; 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)}; if (IsKeyPressed(KEY_ENTER) && monitorCount > 1)
const int refreshRate = GetMonitorRefreshRate(currentMonitor); {
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 // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -59,16 +100,50 @@ int main(void)
ClearBackground(RAYWHITE); 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( DrawText(
TextFormat("%3.0f x %3.0f [%ihz]", TextFormat("Resolution: [%ipx x %ipx]\nRefreshRate: [%ihz]\nPhysical Size: [%imm x %imm]\nPosition: %3.2f x %3.2f",
resolution.x, currentMonitor.width,
resolution.y, currentMonitor.height,
refreshRate currentMonitor.refreshRate,
), 20, 70, 20, GRAY); currentMonitor.physicalWidth,
DrawText(TextFormat("%i", currentMonitor), screenWidth * 0.5, screenHeight * 0.5, 70, GRAY); currentMonitor.physicalHeight,
DrawRectangleLines(20, 100, screenWidth - 40, screenHeight - 120, DARKGRAY); 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(); EndDrawing();

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 16 KiB