core_basic_screen_manager.c 5.7 KB

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