core_window_flags.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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);// | FLAG_WINDOW_TRANSPARENT);
  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)
  85. {
  86. RestoreWindow(); // Restore window after 3 seconds
  87. framesCounter = 0;
  88. }
  89. }
  90. if (IsKeyPressed(KEY_M))
  91. {
  92. // NOTE: Requires FLAG_WINDOW_RESIZABLE enabled!
  93. if (IsWindowState(FLAG_WINDOW_MAXIMIZED)) RestoreWindow();
  94. else MaximizeWindow();
  95. }
  96. if (IsKeyPressed(KEY_U))
  97. {
  98. if (IsWindowState(FLAG_WINDOW_UNFOCUSED)) ClearWindowState(FLAG_WINDOW_UNFOCUSED);
  99. else SetWindowState(FLAG_WINDOW_UNFOCUSED);
  100. }
  101. if (IsKeyPressed(KEY_T))
  102. {
  103. if (IsWindowState(FLAG_WINDOW_TOPMOST)) ClearWindowState(FLAG_WINDOW_TOPMOST);
  104. else SetWindowState(FLAG_WINDOW_TOPMOST);
  105. }
  106. if (IsKeyPressed(KEY_A))
  107. {
  108. if (IsWindowState(FLAG_WINDOW_ALWAYS_RUN)) ClearWindowState(FLAG_WINDOW_ALWAYS_RUN);
  109. else SetWindowState(FLAG_WINDOW_ALWAYS_RUN);
  110. }
  111. if (IsKeyPressed(KEY_V))
  112. {
  113. if (IsWindowState(FLAG_VSYNC_HINT)) ClearWindowState(FLAG_VSYNC_HINT);
  114. else SetWindowState(FLAG_VSYNC_HINT);
  115. }
  116. if (IsKeyPressed(KEY_B)) ToggleBorderlessWindowed();
  117. // Bouncing ball logic
  118. ballPosition.x += ballSpeed.x;
  119. ballPosition.y += ballSpeed.y;
  120. if ((ballPosition.x >= (GetScreenWidth() - ballRadius)) || (ballPosition.x <= ballRadius)) ballSpeed.x *= -1.0f;
  121. if ((ballPosition.y >= (GetScreenHeight() - ballRadius)) || (ballPosition.y <= ballRadius)) ballSpeed.y *= -1.0f;
  122. //-----------------------------------------------------
  123. // Draw
  124. //-----------------------------------------------------
  125. BeginDrawing();
  126. if (IsWindowState(FLAG_WINDOW_TRANSPARENT)) ClearBackground(BLANK);
  127. else ClearBackground(RAYWHITE);
  128. DrawCircleV(ballPosition, ballRadius, MAROON);
  129. DrawRectangleLinesEx((Rectangle) { 0, 0, (float)GetScreenWidth(), (float)GetScreenHeight() }, 4, RAYWHITE);
  130. DrawCircleV(GetMousePosition(), 10, DARKBLUE);
  131. DrawFPS(10, 10);
  132. DrawText(TextFormat("Screen Size: [%i, %i]", GetScreenWidth(), GetScreenHeight()), 10, 40, 10, GREEN);
  133. // Draw window state info
  134. DrawText("Following flags can be set after window creation:", 10, 60, 10, GRAY);
  135. if (IsWindowState(FLAG_FULLSCREEN_MODE)) DrawText("[F] FLAG_FULLSCREEN_MODE: on", 10, 80, 10, LIME);
  136. else DrawText("[F] FLAG_FULLSCREEN_MODE: off", 10, 80, 10, MAROON);
  137. if (IsWindowState(FLAG_WINDOW_RESIZABLE)) DrawText("[R] FLAG_WINDOW_RESIZABLE: on", 10, 100, 10, LIME);
  138. else DrawText("[R] FLAG_WINDOW_RESIZABLE: off", 10, 100, 10, MAROON);
  139. if (IsWindowState(FLAG_WINDOW_UNDECORATED)) DrawText("[D] FLAG_WINDOW_UNDECORATED: on", 10, 120, 10, LIME);
  140. else DrawText("[D] FLAG_WINDOW_UNDECORATED: off", 10, 120, 10, MAROON);
  141. if (IsWindowState(FLAG_WINDOW_HIDDEN)) DrawText("[H] FLAG_WINDOW_HIDDEN: on", 10, 140, 10, LIME);
  142. else DrawText("[H] FLAG_WINDOW_HIDDEN: off (hides for 3 seconds)", 10, 140, 10, MAROON);
  143. if (IsWindowState(FLAG_WINDOW_MINIMIZED)) DrawText("[N] FLAG_WINDOW_MINIMIZED: on", 10, 160, 10, LIME);
  144. else DrawText("[N] FLAG_WINDOW_MINIMIZED: off (restores after 3 seconds)", 10, 160, 10, MAROON);
  145. if (IsWindowState(FLAG_WINDOW_MAXIMIZED)) DrawText("[M] FLAG_WINDOW_MAXIMIZED: on", 10, 180, 10, LIME);
  146. else DrawText("[M] FLAG_WINDOW_MAXIMIZED: off", 10, 180, 10, MAROON);
  147. if (IsWindowState(FLAG_WINDOW_UNFOCUSED)) DrawText("[G] FLAG_WINDOW_UNFOCUSED: on", 10, 200, 10, LIME);
  148. else DrawText("[U] FLAG_WINDOW_UNFOCUSED: off", 10, 200, 10, MAROON);
  149. if (IsWindowState(FLAG_WINDOW_TOPMOST)) DrawText("[T] FLAG_WINDOW_TOPMOST: on", 10, 220, 10, LIME);
  150. else DrawText("[T] FLAG_WINDOW_TOPMOST: off", 10, 220, 10, MAROON);
  151. if (IsWindowState(FLAG_WINDOW_ALWAYS_RUN)) DrawText("[A] FLAG_WINDOW_ALWAYS_RUN: on", 10, 240, 10, LIME);
  152. else DrawText("[A] FLAG_WINDOW_ALWAYS_RUN: off", 10, 240, 10, MAROON);
  153. if (IsWindowState(FLAG_VSYNC_HINT)) DrawText("[V] FLAG_VSYNC_HINT: on", 10, 260, 10, LIME);
  154. else DrawText("[V] FLAG_VSYNC_HINT: off", 10, 260, 10, MAROON);
  155. if (IsWindowState(FLAG_BORDERLESS_WINDOWED_MODE)) DrawText("[B] FLAG_BORDERLESS_WINDOWED_MODE: on", 10, 280, 10, LIME);
  156. else DrawText("[B] FLAG_BORDERLESS_WINDOWED_MODE: off", 10, 280, 10, MAROON);
  157. DrawText("Following flags can only be set before window creation:", 10, 320, 10, GRAY);
  158. if (IsWindowState(FLAG_WINDOW_HIGHDPI)) DrawText("FLAG_WINDOW_HIGHDPI: on", 10, 340, 10, LIME);
  159. else DrawText("FLAG_WINDOW_HIGHDPI: off", 10, 340, 10, MAROON);
  160. if (IsWindowState(FLAG_WINDOW_TRANSPARENT)) DrawText("FLAG_WINDOW_TRANSPARENT: on", 10, 360, 10, LIME);
  161. else DrawText("FLAG_WINDOW_TRANSPARENT: off", 10, 360, 10, MAROON);
  162. if (IsWindowState(FLAG_MSAA_4X_HINT)) DrawText("FLAG_MSAA_4X_HINT: on", 10, 380, 10, LIME);
  163. else DrawText("FLAG_MSAA_4X_HINT: off", 10, 380, 10, MAROON);
  164. EndDrawing();
  165. //-----------------------------------------------------
  166. }
  167. // De-Initialization
  168. //---------------------------------------------------------
  169. CloseWindow(); // Close window and OpenGL context
  170. //----------------------------------------------------------
  171. return 0;
  172. }