You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
9.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - Advance Game template
  4. *
  5. * <Game title>
  6. * <Game description>
  7. *
  8. * This game has been created using raylib (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  16. #if defined(PLATFORM_WEB)
  17. #include <emscripten/emscripten.h>
  18. #endif
  19. GameScreen currentScreen = 0;
  20. Font font = { 0 };
  21. Music music = { 0 };
  22. Sound fxCoin = { 0 };
  23. //----------------------------------------------------------------------------------
  24. // Global Variables Definition (local to this module)
  25. //----------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. // Required variables to manage screen transitions (fade-in, fade-out)
  29. static float transAlpha = 0.0f;
  30. static bool onTransition = false;
  31. static bool transFadeOut = false;
  32. static int transFromScreen = -1;
  33. static int transToScreen = -1;
  34. // NOTE: Some global variables that require to be visible for all screens,
  35. // are defined in screens.h (i.e. currentScreen)
  36. //----------------------------------------------------------------------------------
  37. // Local Functions Declaration
  38. //----------------------------------------------------------------------------------
  39. static void ChangeToScreen(int screen); // No transition effect
  40. static void TransitionToScreen(int screen);
  41. static void UpdateTransition(void);
  42. static void DrawTransition(void);
  43. static void UpdateDrawFrame(void); // Update and Draw one frame
  44. //----------------------------------------------------------------------------------
  45. // Main entry point
  46. //----------------------------------------------------------------------------------
  47. int main(void)
  48. {
  49. // Initialization (Note windowTitle is unused on Android)
  50. //---------------------------------------------------------
  51. InitWindow(screenWidth, screenHeight, "raylib template - advance game");
  52. // Global data loading (assets that must be available in all screens, i.e. fonts)
  53. InitAudioDevice();
  54. font = LoadFont("resources/mecha.png");
  55. music = LoadMusicStream("resources/ambient.ogg");
  56. fxCoin = LoadSound("resources/coin.wav");
  57. SetMusicVolume(music, 1.0f);
  58. PlayMusicStream(music);
  59. // Setup and Init first screen
  60. currentScreen = LOGO;
  61. InitLogoScreen();
  62. #if defined(PLATFORM_WEB)
  63. emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
  64. #else
  65. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  66. //--------------------------------------------------------------------------------------
  67. // Main game loop
  68. while (!WindowShouldClose()) // Detect window close button or ESC key
  69. {
  70. UpdateDrawFrame();
  71. }
  72. #endif
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. // Unload current screen data before closing
  76. switch (currentScreen)
  77. {
  78. case LOGO: UnloadLogoScreen(); break;
  79. case TITLE: UnloadTitleScreen(); break;
  80. case GAMEPLAY: UnloadGameplayScreen(); break;
  81. case ENDING: UnloadEndingScreen(); break;
  82. default: break;
  83. }
  84. // Unload all global loaded data (i.e. fonts) here!
  85. UnloadFont(font);
  86. UnloadMusicStream(music);
  87. UnloadSound(fxCoin);
  88. CloseAudioDevice(); // Close audio context
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }
  93. //----------------------------------------------------------------------------------
  94. // Module specific Functions Definition
  95. //----------------------------------------------------------------------------------
  96. // Change to next screen, no transition
  97. static void ChangeToScreen(int screen)
  98. {
  99. // Unload current screen
  100. switch (currentScreen)
  101. {
  102. case LOGO: UnloadLogoScreen(); break;
  103. case TITLE: UnloadTitleScreen(); break;
  104. case GAMEPLAY: UnloadGameplayScreen(); break;
  105. case ENDING: UnloadEndingScreen(); break;
  106. default: break;
  107. }
  108. // Init next screen
  109. switch (screen)
  110. {
  111. case LOGO: InitLogoScreen(); break;
  112. case TITLE: InitTitleScreen(); break;
  113. case GAMEPLAY: InitGameplayScreen(); break;
  114. case ENDING: InitEndingScreen(); break;
  115. default: break;
  116. }
  117. currentScreen = screen;
  118. }
  119. // Define transition to next screen
  120. static void TransitionToScreen(int screen)
  121. {
  122. onTransition = true;
  123. transFadeOut = false;
  124. transFromScreen = currentScreen;
  125. transToScreen = screen;
  126. transAlpha = 0.0f;
  127. }
  128. // Update transition effect
  129. static void UpdateTransition(void)
  130. {
  131. if (!transFadeOut)
  132. {
  133. transAlpha += 0.05f;
  134. // NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
  135. // For that reason we compare against 1.01f, to avoid last frame loading stop
  136. if (transAlpha > 1.01f)
  137. {
  138. transAlpha = 1.0f;
  139. // Unload current screen
  140. switch (transFromScreen)
  141. {
  142. case LOGO: UnloadLogoScreen(); break;
  143. case TITLE: UnloadTitleScreen(); break;
  144. case OPTIONS: UnloadOptionsScreen(); break;
  145. case GAMEPLAY: UnloadGameplayScreen(); break;
  146. case ENDING: UnloadEndingScreen(); break;
  147. default: break;
  148. }
  149. // Load next screen
  150. switch (transToScreen)
  151. {
  152. case LOGO: InitLogoScreen(); break;
  153. case TITLE: InitTitleScreen(); break;
  154. case GAMEPLAY: InitGameplayScreen(); break;
  155. case ENDING: InitEndingScreen(); break;
  156. default: break;
  157. }
  158. currentScreen = transToScreen;
  159. // Activate fade out effect to next loaded screen
  160. transFadeOut = true;
  161. }
  162. }
  163. else // Transition fade out logic
  164. {
  165. transAlpha -= 0.02f;
  166. if (transAlpha < -0.01f)
  167. {
  168. transAlpha = 0.0f;
  169. transFadeOut = false;
  170. onTransition = false;
  171. transFromScreen = -1;
  172. transToScreen = -1;
  173. }
  174. }
  175. }
  176. // Draw transition effect (full-screen rectangle)
  177. static void DrawTransition(void)
  178. {
  179. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
  180. }
  181. // Update and draw game frame
  182. static void UpdateDrawFrame(void)
  183. {
  184. // Update
  185. //----------------------------------------------------------------------------------
  186. UpdateMusicStream(music); // NOTE: Music keeps playing between screens
  187. if (!onTransition)
  188. {
  189. switch(currentScreen)
  190. {
  191. case LOGO:
  192. {
  193. UpdateLogoScreen();
  194. if (FinishLogoScreen()) TransitionToScreen(TITLE);
  195. } break;
  196. case TITLE:
  197. {
  198. UpdateTitleScreen();
  199. if (FinishTitleScreen() == 1) TransitionToScreen(OPTIONS);
  200. else if (FinishTitleScreen() == 2) TransitionToScreen(GAMEPLAY);
  201. } break;
  202. case OPTIONS:
  203. {
  204. UpdateOptionsScreen();
  205. if (FinishOptionsScreen()) TransitionToScreen(TITLE);
  206. } break;
  207. case GAMEPLAY:
  208. {
  209. UpdateGameplayScreen();
  210. if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
  211. //else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
  212. } break;
  213. case ENDING:
  214. {
  215. UpdateEndingScreen();
  216. if (FinishEndingScreen() == 1) TransitionToScreen(TITLE);
  217. } break;
  218. default: break;
  219. }
  220. }
  221. else UpdateTransition(); // Update transition (fade-in, fade-out)
  222. //----------------------------------------------------------------------------------
  223. // Draw
  224. //----------------------------------------------------------------------------------
  225. BeginDrawing();
  226. ClearBackground(RAYWHITE);
  227. switch(currentScreen)
  228. {
  229. case LOGO: DrawLogoScreen(); break;
  230. case TITLE: DrawTitleScreen(); break;
  231. case OPTIONS: DrawOptionsScreen(); break;
  232. case GAMEPLAY: DrawGameplayScreen(); break;
  233. case ENDING: DrawEndingScreen(); break;
  234. default: break;
  235. }
  236. // Draw full screen rectangle in front of everything
  237. if (onTransition) DrawTransition();
  238. //DrawFPS(10, 10);
  239. EndDrawing();
  240. //----------------------------------------------------------------------------------
  241. }