Avoid/src/Main.c

223 lines
5.3 KiB
C
Raw Normal View History

/*
2022-05-29 16:02:30 +00:00
- Avoid ~ a game by Canneddonuts
- Filename ~ Main.c
- Author ~ Return0ne
- 2022
- *no license*
*/
2022-05-29 16:02:30 +00:00
#include "../include/raylib.h"
#include "Screens.h"
2022-07-19 15:04:05 +00:00
#include "Options.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
2022-03-27 20:40:15 +00:00
// screen variables
static const int screenWidth = 800;
static const int screenHeight = 450;
2022-07-19 15:04:05 +00:00
static float transAlpha = 0.0f;
static bool onTransition = false;
static bool transFadeOut = false;
static int transFromScreen = -1;
static int transToScreen = -1;
GameScreen currentScreen = 0;
2022-06-18 16:10:36 +00:00
Texture2D background;
Texture2D player_sprite;
2022-06-29 16:11:37 +00:00
Texture2D feather_sprite;
2022-06-18 16:10:36 +00:00
Texture2D enemy_sprite;
Texture2D firework_sprite;
2022-07-08 20:00:38 +00:00
Texture2D attack_sprite;
2022-03-27 20:40:15 +00:00
// Game functions
static void gameSetup(void);
2022-07-19 15:04:05 +00:00
static void update_draw_frame(void);
static void unloadGame(void);
2022-07-19 15:04:05 +00:00
static void transition_to_screen(int screen);
static void update_transition(void);
static void draw_transition(void);
2022-03-27 20:40:15 +00:00
int main(void)
{
InitWindow(screenWidth, screenHeight, "Avoid");
InitAudioDevice();
gameSetup();
2022-03-27 20:40:15 +00:00
#if defined(PLATFORM_WEB)
2022-07-19 15:04:05 +00:00
emscripten_set_main_loop(update_draw_frame, 60, 1);
#else
SetTargetFPS(60);
2022-07-19 15:04:05 +00:00
while (!WindowShouldClose()) update_draw_frame();
#endif
2022-03-27 20:40:15 +00:00
unloadGame();
2022-03-27 20:40:15 +00:00
2022-06-11 20:10:58 +00:00
CloseAudioDevice();
2022-03-27 20:40:15 +00:00
CloseWindow();
return 0;
2022-03-27 20:40:15 +00:00
}
void gameSetup(void)
2022-03-27 20:40:15 +00:00
{
// asset loading & setting of variable values
2022-03-27 20:40:15 +00:00
currentScreen = TITLE;
2022-07-19 15:04:05 +00:00
background = LoadTexture("assets/gfx/background.png");
2022-07-28 16:02:47 +00:00
SetMasterVolume(0.5);
2022-03-27 20:40:15 +00:00
}
2022-07-19 15:04:05 +00:00
static void transition_to_screen(int screen)
2022-03-27 20:40:15 +00:00
{
2022-07-19 15:04:05 +00:00
onTransition = true;
transFadeOut = false;
transFromScreen = currentScreen;
transToScreen = screen;
transAlpha = 0.0f;
2022-03-27 20:40:15 +00:00
}
2022-07-19 15:04:05 +00:00
static void update_transition(void)
2022-03-27 20:40:15 +00:00
{
2022-07-19 15:04:05 +00:00
if (!transFadeOut) {
transAlpha += 0.05f;
if (transAlpha > 1.01f) {
transAlpha = 1.0f;
switch (transFromScreen) {
case TITLE: UnloadTitleScreen(); break;
case GAMEPLAY: UnloadGameplayScreen(); break;
case GAMEOVER: UnloadGameoverScreen(); break;
case CREDITS: UnloadCreditsScreen(); break;
case OPTIONS: UnloadOptionsScreen(); break;
2022-07-28 16:02:47 +00:00
case ENDING: UnloadEndingScreen(); break;
2022-07-19 15:04:05 +00:00
default: break;
}
2022-03-27 20:40:15 +00:00
2022-07-19 15:04:05 +00:00
switch (transToScreen) {
case TITLE: InitTitleScreen(); break;
case GAMEPLAY: LoadGamplayScreen(); InitGameplayScreen(); break;
case GAMEOVER: InitGameoverScreen(); break;
case CREDITS: InitCreditsScreen(); break;
case OPTIONS: InitOptionsScreen(); break;
2022-07-28 16:02:47 +00:00
case ENDING: InitEndingScreen(); break;
2022-03-27 20:40:15 +00:00
default: break;
}
2022-07-19 15:04:05 +00:00
currentScreen = transToScreen;
transFadeOut = true;
}
} else {
transAlpha -= 0.02f;
if (transAlpha < -0.01f) {
transAlpha = 0.0f;
transFadeOut = false;
onTransition = false;
transFromScreen = -1;
transToScreen = -1;
}
}
2022-03-27 20:40:15 +00:00
}
2022-07-19 15:04:05 +00:00
static void draw_transition(void)
2022-03-27 20:40:15 +00:00
{
2022-07-19 15:04:05 +00:00
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
}
static void update_draw_frame(void)
{
if (IsKeyPressed(KEY_M)) mute = !mute;
if ((IsKeyDown(KEY_LEFT_ALT)) && (IsKeyPressed(KEY_F))) { ToggleFullscreen(); fullscreen = !fullscreen; }
if (!onTransition) {
switch (currentScreen) {
case TITLE: {
UpdateTitleScreen();
2022-07-25 19:21:52 +00:00
switch (FinishTitleScreen()) {
case 1: transition_to_screen(CREDITS); break;
case 2: transition_to_screen(GAMEPLAY); break;
case 3: transition_to_screen(OPTIONS); break;
}
2022-07-19 15:04:05 +00:00
} break;
case CREDITS: {
UpdateCreditsScreen();
if (FinishCreditsScreen() == 1) transition_to_screen(TITLE);
} break;
case GAMEPLAY: {
UpdateGameplayScreen();
2022-07-28 16:02:47 +00:00
switch (FinishGameplayScreen()) {
case 1: transition_to_screen(GAMEOVER); break;
case 2: transition_to_screen(TITLE); break;
case 3: transition_to_screen(ENDING); break;
}
2022-07-19 15:04:05 +00:00
} break;
case GAMEOVER: {
UpdateGameoverScreen();
if (FinishGameoverScreen() == 1) transition_to_screen(TITLE);
else if (FinishGameoverScreen() == 2) transition_to_screen(GAMEPLAY);
} break;
case OPTIONS: {
UpdateOptionsScreen();
if (FinishOptionsScreen() == 1) transition_to_screen(TITLE);
} break;
2022-07-28 16:02:47 +00:00
case ENDING: {
UpdateEndingScreen();
2022-08-02 13:38:36 +00:00
if (FinishEndingScreen() == 1) transition_to_screen(TITLE);
2022-07-28 16:02:47 +00:00
} break;
2022-07-19 15:04:05 +00:00
default: break;
}
} else update_transition();
BeginDrawing();
ClearBackground(RAYWHITE);
switch (currentScreen) {
case TITLE: DrawTitleScreen(); break;
case CREDITS: DrawCreditsScreen(); break;
case GAMEPLAY: DrawGameplayScreen(); break;
case GAMEOVER: DrawGameoverScreen(); break;
case OPTIONS: DrawOptionsScreen(); break;
2022-07-28 16:02:47 +00:00
case ENDING: DrawEndingScreen(); break;
2022-07-19 15:04:05 +00:00
default: break;
}
if (onTransition) draw_transition();
EndDrawing();
2022-03-27 20:40:15 +00:00
}
2022-07-19 15:04:05 +00:00
static void unloadGame(void)
2022-03-27 20:40:15 +00:00
{
2022-07-19 15:04:05 +00:00
switch (currentScreen) {
case TITLE: UnloadTitleScreen(); break;
case GAMEPLAY: UnloadGameplayScreen(); break;
case GAMEOVER: UnloadGameoverScreen(); break;
case CREDITS: UnloadCreditsScreen(); break;
case OPTIONS: UnloadOptionsScreen(); break;
2022-07-28 16:02:47 +00:00
case ENDING: UnloadEndingScreen(); break;
2022-07-19 15:04:05 +00:00
default: break;
}
2022-07-25 19:21:52 +00:00
UnloadTexture(background);
2022-03-27 20:40:15 +00:00
}