core_window_should_close.c 3.1 KB

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