You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
7.9 KiB

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