|
| 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 | +} |
0 commit comments