core_window_should_close.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Window should close
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 4.2, last time updated with raylib 4.2
  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) 2013-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - window should close");
  26. SetExitKey(KEY_NULL); // Disable KEY_ESCAPE to close window, X-button still works
  27. bool exitWindowRequested = false; // Flag to request window to exit
  28. bool exitWindow = false; // Flag to set window to exit
  29. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  30. //--------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!exitWindow)
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. // Detect if X-button or KEY_ESCAPE have been pressed to close window
  37. if (WindowShouldClose() || IsKeyPressed(KEY_ESCAPE)) exitWindowRequested = true;
  38. if (exitWindowRequested)
  39. {
  40. // A request for close window has been issued, we can save data before closing
  41. // or just show a message asking for confirmation
  42. if (IsKeyPressed(KEY_Y)) exitWindow = true;
  43. else if (IsKeyPressed(KEY_N)) exitWindowRequested = false;
  44. }
  45. //----------------------------------------------------------------------------------
  46. // Draw
  47. //----------------------------------------------------------------------------------
  48. BeginDrawing();
  49. ClearBackground(RAYWHITE);
  50. if (exitWindowRequested)
  51. {
  52. DrawRectangle(0, 100, screenWidth, 200, BLACK);
  53. DrawText("Are you sure you want to exit program? [Y/N]", 40, 180, 30, WHITE);
  54. }
  55. else DrawText("Try to close the window to get confirmation message!", 120, 200, 20, LIGHTGRAY);
  56. EndDrawing();
  57. //----------------------------------------------------------------------------------
  58. }
  59. // De-Initialization
  60. //--------------------------------------------------------------------------------------
  61. CloseWindow(); // Close window and OpenGL context
  62. //--------------------------------------------------------------------------------------
  63. return 0;
  64. }