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
8.3 KiB

  1. /*******************************************************************************************
  2. *
  3. * GLOBAL GAME JAM 2016 - LIGHT MY RITUAL!
  4. *
  5. * Preparing a ritual session is not that easy.
  6. * You must light all the candles before the astral alignment finishes...
  7. * but dark creatures move in the shadows to put out all your lights!
  8. * Be fast! Be smart! Light my ritual!
  9. *
  10. * This game has been created using raylib (www.raylib.com)
  11. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  12. *
  13. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  18. #include <stdlib.h>
  19. #if defined(PLATFORM_ANDROID)
  20. #include "android_native_app_glue.h"
  21. #endif
  22. #if defined(PLATFORM_WEB)
  23. #include <emscripten/emscripten.h>
  24. #endif
  25. //----------------------------------------------------------------------------------
  26. // Global Variables Definition (local to this module)
  27. //----------------------------------------------------------------------------------
  28. const int screenWidth = 1280;
  29. const int screenHeight = 720;
  30. // Required variables to manage screen transitions (fade-in, fade-out)
  31. float transAlpha = 0;
  32. bool onTransition = false;
  33. bool transFadeOut = false;
  34. int transFromScreen = -1;
  35. int transToScreen = -1;
  36. static Music music;
  37. //----------------------------------------------------------------------------------
  38. // Local Functions Declaration
  39. //----------------------------------------------------------------------------------
  40. void TransitionToScreen(int screen);
  41. void ChangeToScreen(int screen); // No transition effect
  42. void UpdateTransition(void);
  43. void DrawTransition(void);
  44. void UpdateDrawFrame(void); // Update and Draw one frame
  45. //----------------------------------------------------------------------------------
  46. // Main entry point
  47. //----------------------------------------------------------------------------------
  48. #if defined(PLATFORM_ANDROID)
  49. void android_main(struct android_app *app)
  50. #else
  51. int main(void)
  52. #endif
  53. {
  54. // Initialization
  55. //---------------------------------------------------------
  56. #if defined(PLATFORM_ANDROID)
  57. InitWindow(screenWidth, screenHeight, app);
  58. #else
  59. InitWindow(screenWidth, screenHeight, "GGJ16 - LIGHT MY RITUAL!");
  60. #endif
  61. // Global data loading (assets that must be available in all screens, i.e. fonts)
  62. InitAudioDevice();
  63. Image image = LoadImage("resources/lights_map.png"); // Load image in CPU memory (RAM)
  64. lightsMap = GetImageData(image); // Get image pixels data as an array of Color
  65. lightsMapWidth = image.width;
  66. lightsMapHeight = image.height;
  67. UnloadImage(image); // Unload image from CPU memory (RAM)
  68. font = LoadSpriteFont("resources/font_arcadian.png");
  69. //doors = LoadTexture("resources/textures/doors.png");
  70. //sndDoor = LoadSound("resources/audio/door.ogg");
  71. music = LoadMusicStream("resources/audio/ambient.ogg");
  72. PlayMusicStream(music);
  73. SetMusicVolume(music, 1.0f);
  74. // Setup and Init first screen
  75. currentScreen = LOGO_RL;
  76. //InitTitleScreen();
  77. //InitGameplayScreen();
  78. rlInitLogoScreen();
  79. #if defined(PLATFORM_WEB)
  80. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  81. #else
  82. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  83. //--------------------------------------------------------------------------------------
  84. // Main game loop
  85. while (!WindowShouldClose()) // Detect window close button or ESC key
  86. {
  87. UpdateDrawFrame();
  88. }
  89. #endif
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. switch (currentScreen)
  93. {
  94. case LOGO_RL: rlUnloadLogoScreen(); break;
  95. case TITLE: UnloadTitleScreen(); break;
  96. case GAMEPLAY: UnloadGameplayScreen(); break;
  97. default: break;
  98. }
  99. // Unload all global loaded data (i.e. fonts) here!
  100. UnloadSpriteFont(font);
  101. //UnloadSound(sndDoor);
  102. UnloadMusicStream(music);
  103. free(lightsMap);
  104. CloseAudioDevice();
  105. CloseWindow(); // Close window and OpenGL context
  106. //--------------------------------------------------------------------------------------
  107. #if !defined(PLATFORM_ANDROID)
  108. return 0;
  109. #endif
  110. }
  111. void TransitionToScreen(int screen)
  112. {
  113. onTransition = true;
  114. transFromScreen = currentScreen;
  115. transToScreen = screen;
  116. }
  117. void ChangeToScreen(int screen)
  118. {
  119. switch (currentScreen)
  120. {
  121. case LOGO_RL: rlUnloadLogoScreen(); break;
  122. case TITLE: UnloadTitleScreen(); break;
  123. case GAMEPLAY: UnloadGameplayScreen(); break;
  124. default: break;
  125. }
  126. switch (screen)
  127. {
  128. case LOGO_RL: rlInitLogoScreen(); break;
  129. case TITLE: InitTitleScreen(); break;
  130. case GAMEPLAY: InitGameplayScreen(); break;
  131. default: break;
  132. }
  133. currentScreen = screen;
  134. }
  135. void UpdateTransition(void)
  136. {
  137. if (!transFadeOut)
  138. {
  139. transAlpha += 0.05f;
  140. if (transAlpha >= 1.0)
  141. {
  142. transAlpha = 1.0;
  143. switch (transFromScreen)
  144. {
  145. case LOGO_RL: rlUnloadLogoScreen(); break;
  146. case TITLE: UnloadTitleScreen(); break;
  147. case GAMEPLAY: UnloadGameplayScreen(); break;
  148. default: break;
  149. }
  150. switch (transToScreen)
  151. {
  152. case LOGO_RL:
  153. {
  154. rlInitLogoScreen();
  155. currentScreen = LOGO_RL;
  156. } break;
  157. case TITLE:
  158. {
  159. InitTitleScreen();
  160. currentScreen = TITLE;
  161. } break;
  162. case GAMEPLAY:
  163. {
  164. InitGameplayScreen();
  165. currentScreen = GAMEPLAY;
  166. } break;
  167. default: break;
  168. }
  169. transFadeOut = true;
  170. }
  171. }
  172. else // Transition fade out logic
  173. {
  174. transAlpha -= 0.05f;
  175. if (transAlpha <= 0)
  176. {
  177. transAlpha = 0;
  178. transFadeOut = false;
  179. onTransition = false;
  180. transFromScreen = -1;
  181. transToScreen = -1;
  182. }
  183. }
  184. }
  185. void DrawTransition(void)
  186. {
  187. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
  188. }
  189. // Update and draw game frame
  190. void UpdateDrawFrame(void)
  191. {
  192. // Update
  193. //----------------------------------------------------------------------------------
  194. if (!onTransition)
  195. {
  196. switch(currentScreen)
  197. {
  198. case LOGO_RL:
  199. {
  200. rlUpdateLogoScreen();
  201. if (rlFinishLogoScreen()) TransitionToScreen(TITLE);
  202. } break;
  203. case TITLE:
  204. {
  205. UpdateTitleScreen();
  206. if (FinishTitleScreen() == 1)
  207. {
  208. StopMusicStream(music);
  209. TransitionToScreen(GAMEPLAY);
  210. }
  211. } break;
  212. case GAMEPLAY:
  213. {
  214. UpdateGameplayScreen();
  215. if (FinishGameplayScreen() == 1) ChangeToScreen(LOGO_RL);
  216. else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
  217. } break;
  218. default: break;
  219. }
  220. }
  221. else
  222. {
  223. // Update transition (fade-in, fade-out)
  224. UpdateTransition();
  225. }
  226. if (currentScreen != GAMEPLAY) UpdateMusicStream(music);
  227. //----------------------------------------------------------------------------------
  228. // Draw
  229. //----------------------------------------------------------------------------------
  230. BeginDrawing();
  231. ClearBackground(RAYWHITE);
  232. switch(currentScreen)
  233. {
  234. case LOGO_RL: rlDrawLogoScreen(); break;
  235. case TITLE: DrawTitleScreen(); break;
  236. case GAMEPLAY: DrawGameplayScreen(); break;
  237. default: break;
  238. }
  239. if (onTransition) DrawTransition();
  240. //DrawFPS(10, 10);
  241. EndDrawing();
  242. //----------------------------------------------------------------------------------
  243. }