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.

281 lines
8.1 KiB

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