core_high_dpi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - high dpi
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.0, last time updated with raylib 5.5
  8. *
  9. * Example contributed by Jonathan Marler (@marler8997) 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 Jonathan Marler (@marler8997)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. //------------------------------------------------------------------------------------
  19. // Module Functions Declaration
  20. //------------------------------------------------------------------------------------
  21. static void DrawTextCenter(const char *text, int x, int y, int fontSize, Color color);
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. SetConfigFlags(FLAG_WINDOW_HIGHDPI | FLAG_WINDOW_RESIZABLE);
  32. InitWindow(screenWidth, screenHeight, "raylib [core] example - high dpi");
  33. SetWindowMinSize(450, 450);
  34. int logicalGridDescY = 120;
  35. int logicalGridLabelY = logicalGridDescY + 30;
  36. int logicalGridTop = logicalGridLabelY + 30;
  37. int logicalGridBottom = logicalGridTop + 80;
  38. int pixelGridTop = logicalGridBottom - 20;
  39. int pixelGridBottom = pixelGridTop + 80;
  40. int pixelGridLabelY = pixelGridBottom + 30;
  41. int pixelGridDescY = pixelGridLabelY + 30;
  42. int cellSize = 50;
  43. float cellSizePx = (float)cellSize;
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. int monitorCount = GetMonitorCount();
  52. if ((monitorCount > 1) && IsKeyPressed(KEY_N))
  53. {
  54. SetWindowMonitor((GetCurrentMonitor() + 1)%monitorCount);
  55. }
  56. int currentMonitor = GetCurrentMonitor();
  57. Vector2 dpiScale = GetWindowScaleDPI();
  58. cellSizePx = ((float)cellSize)/dpiScale.x;
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. int windowCenter = GetScreenWidth()/2;
  65. DrawTextCenter(TextFormat("Dpi Scale: %f", dpiScale.x), windowCenter, 30, 40, DARKGRAY);
  66. DrawTextCenter(TextFormat("Monitor: %d/%d ([N] next monitor)", currentMonitor+1, monitorCount), windowCenter, 70, 20, LIGHTGRAY);
  67. DrawTextCenter(TextFormat("Window is %d \"logical points\" wide", GetScreenWidth()), windowCenter, logicalGridDescY, 20, ORANGE);
  68. bool odd = true;
  69. for (int i = cellSize; i < GetScreenWidth(); i += cellSize, odd = !odd)
  70. {
  71. if (odd) DrawRectangle(i, logicalGridTop, cellSize, logicalGridBottom-logicalGridTop, ORANGE);
  72. DrawTextCenter(TextFormat("%d", i), i, logicalGridLabelY, 10, LIGHTGRAY);
  73. DrawLine(i, logicalGridLabelY + 10, i, logicalGridBottom, GRAY);
  74. }
  75. odd = true;
  76. const int minTextSpace = 30;
  77. int lastTextX = -minTextSpace;
  78. for (int i = cellSize; i < GetRenderWidth(); i += cellSize, odd = !odd)
  79. {
  80. int x = (int)(((float)i)/dpiScale.x);
  81. if (odd) DrawRectangle(x, pixelGridTop, (int)cellSizePx, pixelGridBottom - pixelGridTop, CLITERAL(Color){ 0, 121, 241, 100 });
  82. DrawLine(x, pixelGridTop, (int)(((float)i)/dpiScale.x), pixelGridLabelY - 10, GRAY);
  83. if ((x - lastTextX) >= minTextSpace)
  84. {
  85. DrawTextCenter(TextFormat("%d", i), x, pixelGridLabelY, 10, LIGHTGRAY);
  86. lastTextX = x;
  87. }
  88. }
  89. DrawTextCenter(TextFormat("Window is %d \"physical pixels\" wide", GetRenderWidth()), windowCenter, pixelGridDescY, 20, BLUE);
  90. const char *text = "Can you see this?";
  91. Vector2 size = MeasureTextEx(GetFontDefault(), text, 20, 3);
  92. Vector2 pos = (Vector2){ GetScreenWidth() - size.x - 5, GetScreenHeight() - size.y - 5 };
  93. DrawTextEx(GetFontDefault(), text, pos, 20, 3, LIGHTGRAY);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }
  103. //------------------------------------------------------------------------------------
  104. // Module Functions Definition
  105. //------------------------------------------------------------------------------------
  106. static void DrawTextCenter(const char *text, int x, int y, int fontSize, Color color)
  107. {
  108. Vector2 size = MeasureTextEx(GetFontDefault(), text, (float)fontSize, 3);
  109. Vector2 pos = (Vector2){ x - size.x/2, y - size.y/2 };
  110. DrawTextEx(GetFontDefault(), text, pos, (float)fontSize, 3, color);
  111. }