2014-09-18 21:00:30 +04:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib - Advance Game template
|
|
|
|
*
|
|
|
|
* <Game title>
|
|
|
|
* <Game description>
|
|
|
|
*
|
|
|
|
* This game has been created using raylib (www.raylib.com)
|
|
|
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
|
|
|
*
|
2018-01-15 14:52:58 +03:00
|
|
|
* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
|
2014-09-18 21:00:30 +04:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
#include "screens/screens.h" // NOTE: Defines global variable: currentScreen
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-18 21:00:30 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition (local to this module)
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-10-14 13:25:08 +03:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2014-09-18 21:00:30 +04:00
|
|
|
|
|
|
|
// Required variables to manage screen transitions (fade-in, fade-out)
|
2017-10-14 13:25:08 +03:00
|
|
|
static float transAlpha = 0.0f;
|
|
|
|
static bool onTransition = false;
|
|
|
|
static bool transFadeOut = false;
|
|
|
|
static int transFromScreen = -1;
|
|
|
|
static int transToScreen = -1;
|
|
|
|
|
|
|
|
// NOTE: Some global variables that require to be visible for all screens,
|
|
|
|
// are defined in screens.h (i.e. currentScreen)
|
2014-09-18 21:00:30 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Local Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-10-14 13:25:08 +03:00
|
|
|
static void ChangeToScreen(int screen); // No transition effect
|
|
|
|
|
|
|
|
static void TransitionToScreen(int screen);
|
|
|
|
static void UpdateTransition(void);
|
|
|
|
static void DrawTransition(void);
|
|
|
|
|
|
|
|
static void UpdateDrawFrame(void); // Update and Draw one frame
|
2014-09-18 21:00:30 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Main entry point
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
int main(void)
|
|
|
|
{
|
2018-03-16 23:31:10 +03:00
|
|
|
// Initialization (Note windowTitle is unused on Android)
|
2017-10-14 13:25:08 +03:00
|
|
|
//---------------------------------------------------------
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib template - advance game");
|
|
|
|
|
|
|
|
// Global data loading (assets that must be available in all screens, i.e. fonts)
|
|
|
|
InitAudioDevice();
|
|
|
|
|
|
|
|
font = LoadSpriteFont("resources/mecha.png");
|
|
|
|
music = LoadMusicStream("resources/ambient.ogg");
|
|
|
|
fxCoin = LoadSound("resources/coin.wav");
|
2014-09-18 21:00:30 +04:00
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
SetMusicVolume(music, 1.0f);
|
|
|
|
PlayMusicStream(music);
|
|
|
|
|
2014-09-18 21:00:30 +04:00
|
|
|
// Setup and Init first screen
|
|
|
|
currentScreen = LOGO;
|
|
|
|
InitLogoScreen();
|
2017-10-14 13:25:08 +03:00
|
|
|
|
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
|
|
|
#else
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
|
|
//--------------------------------------------------------------------------------------
|
2014-09-18 21:00:30 +04:00
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
2017-10-14 13:25:08 +03:00
|
|
|
UpdateDrawFrame();
|
2014-09-18 21:00:30 +04:00
|
|
|
}
|
2017-10-14 13:25:08 +03:00
|
|
|
#endif
|
2014-09-18 21:00:30 +04:00
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
// Unload current screen data before closing
|
|
|
|
switch (currentScreen)
|
|
|
|
{
|
|
|
|
case LOGO: UnloadLogoScreen(); break;
|
|
|
|
case TITLE: UnloadTitleScreen(); break;
|
|
|
|
case GAMEPLAY: UnloadGameplayScreen(); break;
|
|
|
|
case ENDING: UnloadEndingScreen(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unload all global loaded data (i.e. fonts) here!
|
|
|
|
UnloadSpriteFont(font);
|
|
|
|
UnloadMusicStream(music);
|
|
|
|
UnloadSound(fxCoin);
|
|
|
|
|
|
|
|
CloseAudioDevice(); // Close audio context
|
2014-09-18 21:00:30 +04:00
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
2014-09-18 21:00:30 +04:00
|
|
|
//--------------------------------------------------------------------------------------
|
2018-03-16 23:31:10 +03:00
|
|
|
|
2014-09-18 21:00:30 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Change to next screen, no transition
|
|
|
|
static void ChangeToScreen(int screen)
|
|
|
|
{
|
|
|
|
// Unload current screen
|
|
|
|
switch (currentScreen)
|
|
|
|
{
|
|
|
|
case LOGO: UnloadLogoScreen(); break;
|
|
|
|
case TITLE: UnloadTitleScreen(); break;
|
|
|
|
case GAMEPLAY: UnloadGameplayScreen(); break;
|
|
|
|
case ENDING: UnloadEndingScreen(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init next screen
|
|
|
|
switch (screen)
|
|
|
|
{
|
|
|
|
case LOGO: InitLogoScreen(); break;
|
|
|
|
case TITLE: InitTitleScreen(); break;
|
|
|
|
case GAMEPLAY: InitGameplayScreen(); break;
|
|
|
|
case ENDING: InitEndingScreen(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentScreen = screen;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define transition to next screen
|
|
|
|
static void TransitionToScreen(int screen)
|
2014-09-18 21:00:30 +04:00
|
|
|
{
|
|
|
|
onTransition = true;
|
2017-10-14 13:25:08 +03:00
|
|
|
transFadeOut = false;
|
2014-09-18 21:00:30 +04:00
|
|
|
transFromScreen = currentScreen;
|
|
|
|
transToScreen = screen;
|
2017-10-14 13:25:08 +03:00
|
|
|
transAlpha = 0.0f;
|
2014-09-18 21:00:30 +04:00
|
|
|
}
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
// Update transition effect
|
|
|
|
static void UpdateTransition(void)
|
2014-09-18 21:00:30 +04:00
|
|
|
{
|
|
|
|
if (!transFadeOut)
|
|
|
|
{
|
2017-10-14 13:25:08 +03:00
|
|
|
transAlpha += 0.02f;
|
|
|
|
|
|
|
|
// NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
|
|
|
|
// For that reason we compare against 1.01f, to avoid last frame loading stop
|
|
|
|
if (transAlpha > 1.01f)
|
2014-09-18 21:00:30 +04:00
|
|
|
{
|
2017-10-14 13:25:08 +03:00
|
|
|
transAlpha = 1.0f;
|
2014-09-18 21:00:30 +04:00
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
// Unload current screen
|
2014-09-18 21:00:30 +04:00
|
|
|
switch (transFromScreen)
|
|
|
|
{
|
|
|
|
case LOGO: UnloadLogoScreen(); break;
|
|
|
|
case TITLE: UnloadTitleScreen(); break;
|
|
|
|
case OPTIONS: UnloadOptionsScreen(); break;
|
|
|
|
case GAMEPLAY: UnloadGameplayScreen(); break;
|
|
|
|
case ENDING: UnloadEndingScreen(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
// Load next screen
|
2014-09-18 21:00:30 +04:00
|
|
|
switch (transToScreen)
|
|
|
|
{
|
2017-10-14 13:25:08 +03:00
|
|
|
case LOGO: InitLogoScreen(); break;
|
|
|
|
case TITLE: InitTitleScreen(); break;
|
|
|
|
case GAMEPLAY: InitGameplayScreen(); break;
|
|
|
|
case ENDING: InitEndingScreen(); break;
|
2014-09-18 21:00:30 +04:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
currentScreen = transToScreen;
|
|
|
|
|
|
|
|
// Activate fade out effect to next loaded screen
|
2014-09-18 21:00:30 +04:00
|
|
|
transFadeOut = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Transition fade out logic
|
|
|
|
{
|
2017-10-14 13:25:08 +03:00
|
|
|
transAlpha -= 0.02f;
|
2014-09-18 21:00:30 +04:00
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
if (transAlpha < -0.01f)
|
2014-09-18 21:00:30 +04:00
|
|
|
{
|
2017-10-14 13:25:08 +03:00
|
|
|
transAlpha = 0.0f;
|
2014-09-18 21:00:30 +04:00
|
|
|
transFadeOut = false;
|
|
|
|
onTransition = false;
|
|
|
|
transFromScreen = -1;
|
|
|
|
transToScreen = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 13:25:08 +03:00
|
|
|
// Draw transition effect (full-screen rectangle)
|
|
|
|
static void DrawTransition(void)
|
2014-09-18 21:00:30 +04:00
|
|
|
{
|
|
|
|
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
|
2017-10-14 13:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update and draw game frame
|
|
|
|
static void UpdateDrawFrame(void)
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
UpdateMusicStream(music); // NOTE: Music keeps playing between screens
|
|
|
|
|
|
|
|
if (!onTransition)
|
|
|
|
{
|
|
|
|
switch(currentScreen)
|
|
|
|
{
|
|
|
|
case LOGO:
|
|
|
|
{
|
|
|
|
UpdateLogoScreen();
|
|
|
|
|
|
|
|
if (FinishLogoScreen()) TransitionToScreen(TITLE);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case TITLE:
|
|
|
|
{
|
|
|
|
UpdateTitleScreen();
|
|
|
|
|
|
|
|
if (FinishTitleScreen() == 1) TransitionToScreen(OPTIONS);
|
|
|
|
else if (FinishTitleScreen() == 2) TransitionToScreen(GAMEPLAY);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case OPTIONS:
|
|
|
|
{
|
|
|
|
UpdateOptionsScreen();
|
|
|
|
|
|
|
|
if (FinishOptionsScreen()) TransitionToScreen(TITLE);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case GAMEPLAY:
|
|
|
|
{
|
|
|
|
UpdateGameplayScreen();
|
|
|
|
|
|
|
|
if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
|
|
|
|
//else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ENDING:
|
|
|
|
{
|
|
|
|
UpdateEndingScreen();
|
|
|
|
|
|
|
|
if (FinishEndingScreen() == 1) TransitionToScreen(TITLE);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else UpdateTransition(); // Update transition (fade-in, fade-out)
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
switch(currentScreen)
|
|
|
|
{
|
|
|
|
case LOGO: DrawLogoScreen(); break;
|
|
|
|
case TITLE: DrawTitleScreen(); break;
|
|
|
|
case OPTIONS: DrawOptionsScreen(); break;
|
|
|
|
case GAMEPLAY: DrawGameplayScreen(); break;
|
|
|
|
case ENDING: DrawEndingScreen(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw full screen rectangle in front of everything
|
|
|
|
if (onTransition) DrawTransition();
|
|
|
|
|
|
|
|
//DrawFPS(10, 10);
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
2018-03-16 23:31:10 +03:00
|
|
|
}
|