core_window_flags.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - window flags
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 3.5, last time updated with raylib 3.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2020-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //---------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. // Possible window flags
  26. /*
  27. FLAG_VSYNC_HINT
  28. FLAG_FULLSCREEN_MODE -> not working properly -> wrong scaling!
  29. FLAG_WINDOW_RESIZABLE
  30. FLAG_WINDOW_UNDECORATED
  31. FLAG_WINDOW_TRANSPARENT
  32. FLAG_WINDOW_HIDDEN
  33. FLAG_WINDOW_MINIMIZED -> Not supported on window creation
  34. FLAG_WINDOW_MAXIMIZED -> Not supported on window creation
  35. FLAG_WINDOW_UNFOCUSED
  36. FLAG_WINDOW_TOPMOST
  37. FLAG_WINDOW_HIGHDPI -> errors after minimize-resize, fb size is recalculated
  38. FLAG_WINDOW_ALWAYS_RUN
  39. FLAG_MSAA_4X_HINT
  40. */
  41. // Set configuration flags for window creation
  42. //SetConfigFlags(FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT | FLAG_WINDOW_HIGHDPI);
  43. InitWindow(screenWidth, screenHeight, "raylib [core] example - window flags");
  44. Vector2 ballPosition = { GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f };
  45. Vector2 ballSpeed = { 5.0f, 4.0f };
  46. float ballRadius = 20;
  47. int framesCounter = 0;
  48. //SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //----------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //-----------------------------------------------------
  55. if (IsKeyPressed(KEY_F)) ToggleFullscreen(); // modifies window size when scaling!
  56. if (IsKeyPressed(KEY_R))
  57. {
  58. if (IsWindowState(FLAG_WINDOW_RESIZABLE)) ClearWindowState(FLAG_WINDOW_RESIZABLE);
  59. else SetWindowState(FLAG_WINDOW_RESIZABLE);
  60. }
  61. if (IsKeyPressed(KEY_D))
  62. {
  63. if (IsWindowState(FLAG_WINDOW_UNDECORATED)) ClearWindowState(FLAG_WINDOW_UNDECORATED);
  64. else SetWindowState(FLAG_WINDOW_UNDECORATED);
  65. }
  66. if (IsKeyPressed(KEY_H))
  67. {
  68. if (!IsWindowState(FLAG_WINDOW_HIDDEN)) SetWindowState(FLAG_WINDOW_HIDDEN);
  69. framesCounter = 0;
  70. }
  71. if (IsWindowState(FLAG_WINDOW_HIDDEN))
  72. {
  73. framesCounter++;
  74. if (framesCounter >= 240) ClearWindowState(FLAG_WINDOW_HIDDEN); // Show window after 3 seconds
  75. }
  76. if (IsKeyPressed(KEY_N))
  77. {
  78. if (!IsWindowState(FLAG_WINDOW_MINIMIZED)) MinimizeWindow();
  79. framesCounter = 0;
  80. }
  81. if (IsWindowState(FLAG_WINDOW_MINIMIZED))
  82. {
  83. framesCounter++;
  84. if (framesCounter >= 240) RestoreWindow(); // Restore window after 3 seconds
  85. }
  86. if (IsKeyPressed(KEY_M))
  87. {
  88. // NOTE: Requires FLAG_WINDOW_RESIZABLE enabled!
  89. if (IsWindowState(FLAG_WINDOW_MAXIMIZED)) RestoreWindow();
  90. else MaximizeWindow();
  91. }
  92. if (IsKeyPressed(KEY_U))
  93. {
  94. if (IsWindowState(FLAG_WINDOW_UNFOCUSED)) ClearWindowState(FLAG_WINDOW_UNFOCUSED);
  95. else SetWindowState(FLAG_WINDOW_UNFOCUSED);
  96. }
  97. if (IsKeyPressed(KEY_T))
  98. {
  99. if (IsWindowState(FLAG_WINDOW_TOPMOST)) ClearWindowState(FLAG_WINDOW_TOPMOST);
  100. else SetWindowState(FLAG_WINDOW_TOPMOST);
  101. }
  102. if (IsKeyPressed(KEY_A))
  103. {
  104. if (IsWindowState(FLAG_WINDOW_ALWAYS_RUN)) ClearWindowState(FLAG_WINDOW_ALWAYS_RUN);
  105. else SetWindowState(FLAG_WINDOW_ALWAYS_RUN);
  106. }
  107. if (IsKeyPressed(KEY_V))
  108. {
  109. if (IsWindowState(FLAG_VSYNC_HINT)) ClearWindowState(FLAG_VSYNC_HINT);
  110. else SetWindowState(FLAG_VSYNC_HINT);
  111. }
  112. // Bouncing ball logic
  113. ballPosition.x += ballSpeed.x;
  114. ballPosition.y += ballSpeed.y;
  115. if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
  116. if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
  117. //-----------------------------------------------------
  118. // Draw
  119. //-----------------------------------------------------
  120. BeginDrawing();
  121. if (IsWindowState(FLAG_WINDOW_TRANSPARENT)) ClearBackground(BLANK);
  122. else ClearBackground(RAYWHITE);
  123. DrawCircleV(ballPosition, ballRadius, MAROON);
  124. DrawRectangleLinesEx((Rectangle) { 0, 0, (float)GetScreenWidth(), (float)GetScreenHeight() }, 4, RAYWHITE);
  125. DrawCircleV(GetMousePosition(), 10, DARKBLUE);
  126. DrawFPS(10, 10);
  127. DrawText(TextFormat("Screen Size: [%i, %i]", GetScreenWidth(), GetScreenHeight()), 10, 40, 10, GREEN);
  128. // Draw window state info
  129. DrawText("Following flags can be set after window creation:", 10, 60, 10, GRAY);
  130. if (IsWindowState(FLAG_FULLSCREEN_MODE)) DrawText("[F] FLAG_FULLSCREEN_MODE: on", 10, 80, 10, LIME);
  131. else DrawText("[F] FLAG_FULLSCREEN_MODE: off", 10, 80, 10, MAROON);
  132. if (IsWindowState(FLAG_WINDOW_RESIZABLE)) DrawText("[R] FLAG_WINDOW_RESIZABLE: on", 10, 100, 10, LIME);
  133. else DrawText("[R] FLAG_WINDOW_RESIZABLE: off", 10, 100, 10, MAROON);
  134. if (IsWindowState(FLAG_WINDOW_UNDECORATED)) DrawText("[D] FLAG_WINDOW_UNDECORATED: on", 10, 120, 10, LIME);
  135. else DrawText("[D] FLAG_WINDOW_UNDECORATED: off", 10, 120, 10, MAROON);
  136. if (IsWindowState(FLAG_WINDOW_HIDDEN)) DrawText("[H] FLAG_WINDOW_HIDDEN: on", 10, 140, 10, LIME);
  137. else DrawText("[H] FLAG_WINDOW_HIDDEN: off", 10, 140, 10, MAROON);
  138. if (IsWindowState(FLAG_WINDOW_MINIMIZED)) DrawText("[N] FLAG_WINDOW_MINIMIZED: on", 10, 160, 10, LIME);
  139. else DrawText("[N] FLAG_WINDOW_MINIMIZED: off", 10, 160, 10, MAROON);
  140. if (IsWindowState(FLAG_WINDOW_MAXIMIZED)) DrawText("[M] FLAG_WINDOW_MAXIMIZED: on", 10, 180, 10, LIME);
  141. else DrawText("[M] FLAG_WINDOW_MAXIMIZED: off", 10, 180, 10, MAROON);
  142. if (IsWindowState(FLAG_WINDOW_UNFOCUSED)) DrawText("[G] FLAG_WINDOW_UNFOCUSED: on", 10, 200, 10, LIME);
  143. else DrawText("[U] FLAG_WINDOW_UNFOCUSED: off", 10, 200, 10, MAROON);
  144. if (IsWindowState(FLAG_WINDOW_TOPMOST)) DrawText("[T] FLAG_WINDOW_TOPMOST: on", 10, 220, 10, LIME);
  145. else DrawText("[T] FLAG_WINDOW_TOPMOST: off", 10, 220, 10, MAROON);
  146. if (IsWindowState(FLAG_WINDOW_ALWAYS_RUN)) DrawText("[A] FLAG_WINDOW_ALWAYS_RUN: on", 10, 240, 10, LIME);
  147. else DrawText("[A] FLAG_WINDOW_ALWAYS_RUN: off", 10, 240, 10, MAROON);
  148. if (IsWindowState(FLAG_VSYNC_HINT)) DrawText("[V] FLAG_VSYNC_HINT: on", 10, 260, 10, LIME);
  149. else DrawText("[V] FLAG_VSYNC_HINT: off", 10, 260, 10, MAROON);
  150. DrawText("Following flags can only be set before window creation:", 10, 300, 10, GRAY);
  151. if (IsWindowState(FLAG_WINDOW_HIGHDPI)) DrawText("FLAG_WINDOW_HIGHDPI: on", 10, 320, 10, LIME);
  152. else DrawText("FLAG_WINDOW_HIGHDPI: off", 10, 320, 10, MAROON);
  153. if (IsWindowState(FLAG_WINDOW_TRANSPARENT)) DrawText("FLAG_WINDOW_TRANSPARENT: on", 10, 340, 10, LIME);
  154. else DrawText("FLAG_WINDOW_TRANSPARENT: off", 10, 340, 10, MAROON);
  155. if (IsWindowState(FLAG_MSAA_4X_HINT)) DrawText("FLAG_MSAA_4X_HINT: on", 10, 360, 10, LIME);
  156. else DrawText("FLAG_MSAA_4X_HINT: off", 10, 360, 10, MAROON);
  157. EndDrawing();
  158. //-----------------------------------------------------
  159. }
  160. // De-Initialization
  161. //---------------------------------------------------------
  162. CloseWindow(); // Close window and OpenGL context
  163. //----------------------------------------------------------
  164. return 0;
  165. }