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.

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