Skip to content

Commit acab044

Browse files
committed
Add core_highdpi example
This example enables HIGHDPI and renders a scale showing how the logical size compares to the pixel size of the window.
1 parent 44f4400 commit acab044

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

examples/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ CORE = \
508508
core/core_custom_frame_control \
509509
core/core_custom_logging \
510510
core/core_drop_files \
511+
core/core_highdpi \
511512
core/core_input_gamepad \
512513
core/core_input_gestures \
513514
core/core_input_gestures_web \

examples/Makefile.Web

+1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ CORE = \
390390
core/core_custom_frame_control \
391391
core/core_custom_logging \
392392
core/core_drop_files \
393+
core/core_highdpi \
393394
core/core_input_gamepad \
394395
core/core_input_gestures \
395396
core/core_input_gestures_web \

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Examples using raylib core platform functionality like window creation, inputs,
5959
| 33 | [core_basic_window_web](core/core_basic_window_web.c) | <img src="core/core_basic_window_web.png" alt="core_basic_window_web" width="80"> | ⭐️☆☆☆ | 1.3 | 1.3 | [Ray](https://github.com/raysan5) |
6060
| 34 | [core_input_gestures_web](core/core_input_gestures_web.c) | <img src="core/core_input_gestures_web.png" alt="core_input_gestures_web" width="80"> | ⭐️⭐️☆☆ | 4.6-dev | 4.6-dev | [ubkp](https://github.com/ubkp) |
6161
| 35 | [core_automation_events](core/core_automation_events.c) | <img src="core/core_automation_events.png" alt="core_automation_events" width="80"> | ⭐️⭐️⭐️☆ | 5.0 | 5.0 | [Ray](https://github.com/raysan5) |
62+
| 36 | [core_highdpi](core/core_highdpi.c) | <img src="core/core_highdpi.png" alt="core_highdpi" width="80"> | ⭐️☆☆☆ | 5.0 | 5.0 | [Jonathan Marler](https://github.com/marler8997) |
6263

6364
### category: shapes
6465

examples/core/core_highdpi.c

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [core] example - HighDPI
4+
*
5+
* Example complexity rating: [★☆☆☆] e/4
6+
*
7+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
8+
* BSD-like license that allows static linking with closed source software
9+
*
10+
* Copyright (c) 2013-2025 Ramon Santamaria (@raysan5)
11+
*
12+
********************************************************************************************/
13+
14+
#include "raylib.h"
15+
16+
static void DrawTextCenter(const char *text, int x, int y, int fontSize, Color color)
17+
{
18+
Vector2 size = MeasureTextEx(GetFontDefault(), text, fontSize, 3);
19+
Vector2 pos = (Vector2){x - size.x/2, y - size.y/2 };
20+
DrawTextEx(GetFontDefault(), text, pos, fontSize, 3, color);
21+
}
22+
23+
//------------------------------------------------------------------------------------
24+
// Program main entry point
25+
//------------------------------------------------------------------------------------
26+
int main(void)
27+
{
28+
// Initialization
29+
//--------------------------------------------------------------------------------------
30+
const int screenWidth = 800;
31+
const int screenHeight = 450;
32+
33+
SetConfigFlags(FLAG_WINDOW_HIGHDPI | FLAG_WINDOW_RESIZABLE);
34+
35+
InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi");
36+
SetWindowMinSize(450, 450);
37+
38+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
39+
//--------------------------------------------------------------------------------------
40+
41+
// Main game loop
42+
while (!WindowShouldClose()) // Detect window close button or ESC key
43+
{
44+
// Update
45+
//----------------------------------------------------------------------------------
46+
int monitorCount = GetMonitorCount();
47+
if (monitorCount > 1 && IsKeyPressed(KEY_N)) {
48+
SetWindowMonitor((GetCurrentMonitor() + 1) % monitorCount);
49+
}
50+
int currentMonitor = GetCurrentMonitor();
51+
52+
// Draw
53+
//----------------------------------------------------------------------------------
54+
BeginDrawing();
55+
56+
Vector2 dpiScale = GetWindowScaleDPI();
57+
ClearBackground(RAYWHITE);
58+
59+
int windowCenter = GetScreenWidth() / 2;
60+
DrawTextCenter(TextFormat("Dpi Scale: %f", dpiScale.x), windowCenter, 30, 40, DARKGRAY);
61+
DrawTextCenter(TextFormat("Monitor: %d/%d ([N] next monitor)", currentMonitor+1, monitorCount), windowCenter, 70, 16, LIGHTGRAY);
62+
63+
const int logicalGridDescY = 120;
64+
const int logicalGridLabelY = logicalGridDescY + 30;
65+
const int logicalGridTop = logicalGridLabelY + 30;
66+
const int logicalGridBottom = logicalGridTop + 80;
67+
const int pixelGridTop = logicalGridBottom - 20;
68+
const int pixelGridBottom = pixelGridTop + 80;
69+
const int pixelGridLabelY = pixelGridBottom + 30;
70+
const int pixelGridDescY = pixelGridLabelY + 30;
71+
72+
const int cellSize = 50;
73+
const float cellSizePx = ((float)cellSize) / dpiScale.x;
74+
75+
DrawTextCenter(TextFormat("Window is %d \"logical points\" wide", GetScreenWidth()), windowCenter, logicalGridDescY, 20, ORANGE);
76+
bool odd = true;
77+
for (int i = cellSize; i < GetScreenWidth(); i += cellSize, odd = !odd) {
78+
if (odd) {
79+
DrawRectangle(i, logicalGridTop, cellSize, logicalGridBottom-logicalGridTop, ORANGE);
80+
}
81+
DrawTextCenter(TextFormat("%d", i), i, logicalGridLabelY, 12, LIGHTGRAY);
82+
DrawLine(i, logicalGridLabelY + 10, i, logicalGridBottom, GRAY);
83+
}
84+
85+
odd = true;
86+
const int minTextSpace = 30;
87+
int last_text_x = -minTextSpace;
88+
for (int i = cellSize; i < GetRenderWidth(); i += cellSize, odd = !odd) {
89+
int x = ((float)i) / dpiScale.x;
90+
if (odd) {
91+
DrawRectangle(x, pixelGridTop, cellSizePx, pixelGridBottom-pixelGridTop, CLITERAL(Color){ 0, 121, 241, 100 });
92+
}
93+
DrawLine(x, pixelGridTop, ((float)i) / dpiScale.x, pixelGridLabelY - 10, GRAY);
94+
if (x - last_text_x >= minTextSpace) {
95+
DrawTextCenter(TextFormat("%d", i), x, pixelGridLabelY, 12, LIGHTGRAY);
96+
last_text_x = x;
97+
}
98+
}
99+
100+
DrawTextCenter(TextFormat("Window is %d \"physical pixels\" wide", GetRenderWidth()), windowCenter, pixelGridDescY, 20, BLUE);
101+
102+
{
103+
const char *text = "Can you see this?";
104+
Vector2 size = MeasureTextEx(GetFontDefault(), text, 16, 3);
105+
Vector2 pos = (Vector2){GetScreenWidth() - size.x - 5, GetScreenHeight() - size.y - 5};
106+
DrawTextEx(GetFontDefault(), text, pos, 16, 3, LIGHTGRAY);
107+
}
108+
109+
EndDrawing();
110+
//----------------------------------------------------------------------------------
111+
}
112+
113+
// De-Initialization
114+
//--------------------------------------------------------------------------------------
115+
CloseWindow(); // Close window and OpenGL context
116+
//--------------------------------------------------------------------------------------
117+
118+
return 0;
119+
}

examples/core/core_highdpi.png

3.21 KB
Loading

0 commit comments

Comments
 (0)