core_basic_screen_manager.c 5.5 KB

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