core_basic_screen_manager.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] examples - basic screen manager
  4. *
  5. * NOTE: This example illustrates a very simple screen manager based on a states machines
  6. *
  7. * Example originally created with raylib 4.0, last time updated with raylib 4.0
  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) 2021-2023 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------------
  17. // Types and Structures Definition
  18. //------------------------------------------------------------------------------------------
  19. typedef enum GameScreen { LOGO = 0, TITLE, GAMEPLAY, ENDING } GameScreen;
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [core] example - basic screen manager");
  30. GameScreen currentScreen = LOGO;
  31. // TODO: Initialize all required variables and load all required data here!
  32. int framesCounter = 0; // Useful to count frames
  33. SetTargetFPS(60); // Set desired framerate (frames-per-second)
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. switch(currentScreen)
  41. {
  42. case LOGO:
  43. {
  44. // TODO: Update LOGO screen variables here!
  45. framesCounter++; // Count frames
  46. // Wait for 2 seconds (120 frames) before jumping to TITLE screen
  47. if (framesCounter > 120)
  48. {
  49. currentScreen = TITLE;
  50. }
  51. } break;
  52. case TITLE:
  53. {
  54. // TODO: Update TITLE screen variables here!
  55. // Press enter to change to GAMEPLAY screen
  56. if (IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP))
  57. {
  58. currentScreen = GAMEPLAY;
  59. }
  60. } break;
  61. case GAMEPLAY:
  62. {
  63. // TODO: Update GAMEPLAY screen variables here!
  64. // Press enter to change to ENDING screen
  65. if (IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP))
  66. {
  67. currentScreen = ENDING;
  68. }
  69. } break;
  70. case ENDING:
  71. {
  72. // TODO: Update ENDING screen variables here!
  73. // Press enter to return to TITLE screen
  74. if (IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP))
  75. {
  76. currentScreen = TITLE;
  77. }
  78. } break;
  79. default: break;
  80. }
  81. //----------------------------------------------------------------------------------
  82. // Draw
  83. //----------------------------------------------------------------------------------
  84. BeginDrawing();
  85. ClearBackground(RAYWHITE);
  86. switch(currentScreen)
  87. {
  88. case LOGO:
  89. {
  90. // TODO: Draw LOGO screen here!
  91. DrawText("LOGO SCREEN", 20, 20, 40, LIGHTGRAY);
  92. DrawText("WAIT for 2 SECONDS...", 290, 220, 20, GRAY);
  93. } break;
  94. case TITLE:
  95. {
  96. // TODO: Draw TITLE screen here!
  97. DrawRectangle(0, 0, screenWidth, screenHeight, GREEN);
  98. DrawText("TITLE SCREEN", 20, 20, 40, DARKGREEN);
  99. DrawText("PRESS ENTER or TAP to JUMP to GAMEPLAY SCREEN", 120, 220, 20, DARKGREEN);
  100. } break;
  101. case GAMEPLAY:
  102. {
  103. // TODO: Draw GAMEPLAY screen here!
  104. DrawRectangle(0, 0, screenWidth, screenHeight, PURPLE);
  105. DrawText("GAMEPLAY SCREEN", 20, 20, 40, MAROON);
  106. DrawText("PRESS ENTER or TAP to JUMP to ENDING SCREEN", 130, 220, 20, MAROON);
  107. } break;
  108. case ENDING:
  109. {
  110. // TODO: Draw ENDING screen here!
  111. DrawRectangle(0, 0, screenWidth, screenHeight, BLUE);
  112. DrawText("ENDING SCREEN", 20, 20, 40, DARKBLUE);
  113. DrawText("PRESS ENTER or TAP to RETURN to TITLE SCREEN", 120, 220, 20, DARKBLUE);
  114. } break;
  115. default: break;
  116. }
  117. EndDrawing();
  118. //----------------------------------------------------------------------------------
  119. }
  120. // De-Initialization
  121. //--------------------------------------------------------------------------------------
  122. // TODO: Unload all loaded data (textures, fonts, audio) here!
  123. CloseWindow(); // Close window and OpenGL context
  124. //--------------------------------------------------------------------------------------
  125. return 0;
  126. }