diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..721a7ec --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "asset.h": "c", + "stdlib.h": "c", + "view.h": "c" + } +} \ No newline at end of file diff --git a/config/config.ini b/config/config.ini index 90d130e..3b1e504 100644 --- a/config/config.ini +++ b/config/config.ini @@ -1,8 +1,8 @@ -startScreen = 0 ; LOGO = 0, MENU = 1, GAMEPLAY = 2 +startView = 0 ; LOGO = 0, MENU = 1, GAMEPLAY = 2 -display_title = The Last Hive -display_width = 1280 ; in Pixel -display_height = 720 ; in Pixel +window_title = The Last Hive +window_width = 1280 ; in Pixel +window_height = 720 ; in Pixel showFPS = 1 ; OFF = 0, ON = 1 diff --git a/external/engine/camera.h b/external/engine/camera.h index 58f1fe2..a7c3187 100644 --- a/external/engine/camera.h +++ b/external/engine/camera.h @@ -4,9 +4,9 @@ #include "input.h" #include -Camera2D CreateCamera(int screenWidth, int screenHeight, Vector2 playerPosition, float initZoom){ +Camera2D CreateCamera(int windowWidth, int windowHeight, Vector2 playerPosition, float initZoom){ return (Camera2D){ - .offset = (Vector2){screenWidth / 2.0f, screenHeight / 2.0f}, + .offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f}, .target = playerPosition, .rotation = 0.0f, .zoom = initZoom diff --git a/external/engine/config.h b/external/engine/config.h index 0a554ca..0d6eeca 100644 --- a/external/engine/config.h +++ b/external/engine/config.h @@ -6,7 +6,6 @@ #include #include -#include typedef struct { char *path; diff --git a/external/engine/display.h b/external/engine/window.h similarity index 92% rename from external/engine/display.h rename to external/engine/window.h index 30c954b..b56ab8e 100644 --- a/external/engine/display.h +++ b/external/engine/window.h @@ -5,6 +5,6 @@ typedef struct { int width; int height; const char* title; -} Display; +} Window; #endif // DISPLAY_H diff --git a/include/app.h b/include/app.h index 22261b4..0eca03a 100644 --- a/include/app.h +++ b/include/app.h @@ -2,10 +2,11 @@ #define APP_H #include -#include +#include -#include "screen.h" -#include "applicationstate.h" +#include "view.h" +#include "state.h" +#include "flags.h" #include "font.h" #include "logo.h" #include "menu.h" @@ -14,60 +15,62 @@ typedef struct { Config config; State state; - Display display; + Flags flags; + Window window; Font font; LogoState logo; GameState game; MenuState menu; -} Application; +} App; -void ConfigApp(Application* app){ +void ConfigApp(App* app){ InitConfig(&app->config, "config/config.ini"); - app->state.currentScreen = GetConfigInt(&app->config, "startScreen"); + app->state.currentView = GetConfigInt(&app->config, "startView"); app->state.running = true; app->state.gameStarted = false; - app->state.flagFPS = GetConfigInt(&app->config, "showFPS"); - app->display.title = GetConfigString(&app->config, "display_title"); - app->display.width = GetConfigInt(&app->config, "display_width"); - app->display.height = GetConfigInt(&app->config, "display_height"); + app->flags.showFPS = GetConfigInt(&app->config, "showFPS"); + app->window.title = GetConfigString(&app->config, "window_title"); + app->window.width = GetConfigInt(&app->config, "window_width"); + app->window.height = GetConfigInt(&app->config, "window_height"); } -void InitApp(Application *app){ +void InitApp(App *app){ InitFont(&app->font); InitLogo(&app->logo); InitMenu(&app->menu); - InitGame(&app->game,&app->display); + InitGame(&app->game,&app->window); } -void UpdateApp(Application *app){ +void UpdateApp(App *app){ while (app->state.running) { - switch (app->state.currentScreen) + switch (app->state.currentView) { case LOGO: { - UpdateLogo(&app->logo, &app->state); - RenderLogo(&app->logo, &app->state); + UpdateLogo(&app->logo, &app->state.currentView, &app->state.running); + RenderLogo(&app->logo, &app->flags.showFPS); }break; case MENU: { - UpdateMenu(&app->menu, &app->state); - RenderMenu(&app->menu, &app->state, &app->font, app->display.title); + UpdateMenu(&app->menu, &app->state.currentView, &app->state.running); + RenderMenu(&app->menu, &app->font, app->window.title, &app->flags); }break; case GAMEPLAY: { - UpdateGame(&app->game, &app->state); - RenderGame(&app->game, &app->state); + UpdateGame(&app->game, &app->state.currentView, &app->state.running); + RenderGame(&app->game, &app->state, &app->flags); }break; default: break; } } } -void CleanupApp(Application *app){ +void CleanupApp(App *app){ CleanupMenu(&app->menu); CleanupGame(&app->game); CleanupFont(&app->font); + CleanUpConfig(&app->config); } #endif //APP_H diff --git a/include/applicationstate.h b/include/applicationstate.h deleted file mode 100644 index 532b1ad..0000000 --- a/include/applicationstate.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef APPLICATIONSTATE_H -#define APPLICATIONSTATE_H - -#include "screen.h" - -typedef struct { - Screen currentScreen; - bool running; - bool gameStarted; - bool flagFPS; -} State; - -#endif // APPLICATIONSTATE_H diff --git a/include/flags.h b/include/flags.h new file mode 100644 index 0000000..de09d9e --- /dev/null +++ b/include/flags.h @@ -0,0 +1,8 @@ +#ifndef FLAGS_H +#define FLAGS_H + +typedef struct { + bool showFPS; +} Flags; + +#endif // FLAGS_H diff --git a/include/game.h b/include/game.h index d85a7df..897f4cc 100644 --- a/include/game.h +++ b/include/game.h @@ -8,7 +8,6 @@ #include #include -#include "applicationstate.h" #include "gameassets.h" #include "floor.h" #include "player.h" @@ -33,7 +32,7 @@ typedef struct Image image; } GameState; -void InitGame(GameState *g, Display *display) +void InitGame(GameState *g, Window *window) { g->assets = (Assets){ @@ -57,7 +56,7 @@ void InitGame(GameState *g, Display *display) } // Target texture - g->target = LoadRenderTexture(display->width, display->height); + g->target = LoadRenderTexture(window->width, window->height); // Static Entities g->flowers = CreateFlowers(g->textures[FLOWER], g->assets.textureAssets[FLOWER].frameWidth, g->assets.textureAssets[FLOWER].rotation, 50.0f); @@ -69,12 +68,12 @@ void InitGame(GameState *g, Display *display) g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive); // I/O - g->camera = CreateCamera(display->width, display->height, g->player.physics.position, 10.0f); + g->camera = CreateCamera(window->width, window->height, g->player.physics.position, 10.0f); g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &g->textures[CURSOR]); } -void UpdateGame(GameState *g, State *state) +void UpdateGame(GameState *g, View *currentView, bool *running) { if (GetFPS() <= 60) { @@ -82,11 +81,11 @@ void UpdateGame(GameState *g, State *state) } if (IsKeyPressed(KEY_ESCAPE)) { - state->currentScreen = MENU; + *currentView = MENU; } if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { - state->running = false; + *running = false; } for (int i = 0; i < SHADER_COUNT; i++) { @@ -113,7 +112,7 @@ void RenderComponents(GameState *g) RenderSprite(&g->bee1.sprite); } -void RenderGame(GameState *g, const State *appState) +void RenderGame(GameState *g, const State *appState, Flags *flags) { BeginTextureMode(g->target); ClearBackground(BLACK); @@ -130,7 +129,7 @@ void RenderGame(GameState *g, const State *appState) } DrawText(TextFormat("%d", g->player.inventar.seedCount), 10, 10, 50, WHITE); - if (appState->flagFPS) + if (flags->showFPS) { DrawFPS(10, 10); } diff --git a/include/logo.h b/include/logo.h index 99fbb19..ec5534d 100644 --- a/include/logo.h +++ b/include/logo.h @@ -1,8 +1,6 @@ #ifndef LOGO_H #define LOGO_H -#include "applicationstate.h" - typedef struct { int frameCounter; int positionX; @@ -30,10 +28,10 @@ void InitLogo(LogoState *l) { l->alpha = 1.0f; } -void UpdateLogo(LogoState *l, State *state) { +void UpdateLogo(LogoState *l, View *currentView, bool *running) { if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { - state->running = false; + *running = false; } l->frameCounter++; @@ -76,11 +74,11 @@ void UpdateLogo(LogoState *l, State *state) { } } else if (l->state == 4) { - state->currentScreen = MENU; + *currentView = MENU; } } -void RenderLogo(const LogoState *l, const State *appState) { +void RenderLogo(const LogoState *l, bool *showFPS) { BeginDrawing(); ClearBackground(RAYWHITE); @@ -109,7 +107,7 @@ void RenderLogo(const LogoState *l, const State *appState) { DrawText(TextSubtext("raylib", 0, l->lettersCount), GetScreenWidth() / 2 - 44, GetScreenHeight() / 2 + 48, 50, Fade(BLACK, l->alpha)); } - if (appState->flagFPS){ + if (*showFPS){ DrawFPS(10, 10); } diff --git a/include/menu.h b/include/menu.h index 0ad7e54..2c91722 100644 --- a/include/menu.h +++ b/include/menu.h @@ -7,7 +7,6 @@ #include #include -#include "applicationstate.h" #include "menuassets.h" #include "colors.h" #include "background.h" @@ -58,24 +57,24 @@ void InitMenu(MenuState *m) m->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &m->textures[MENU_CURSOR_TEX]); m->menuScale = 4; - int screenWidth = GetScreenWidth(); - int screenHeight = GetScreenHeight(); - Vector2 screenCenter = {screenWidth / 2, screenHeight / 2}; + int windowWidth = GetScreenWidth(); + int windowHeight = GetScreenHeight(); + Vector2 windowCenter = {windowWidth / 2, windowHeight / 2}; - int boardWidth = screenWidth / m->menuScale * 2.5f; + int boardWidth = windowWidth / m->menuScale * 2.5f; float boardScaleFactor = (float)boardWidth / m->assets.textureAssets[MENU_BOARD_TEX].frameWidth; int boardHeight = (int)(m->assets.textureAssets[MENU_BOARD_TEX].frameHeight * boardScaleFactor); Vector2 boardSource = {128.0f, 0.0f}; Rectangle boardDestRec = { - .x = screenCenter.x - (boardWidth / 2), - .y = screenCenter.y - (boardHeight / 2), + .x = windowCenter.x - (boardWidth / 2), + .y = windowCenter.y - (boardHeight / 2), .width = boardWidth, .height = boardHeight}; m->board = CreateImageElement(&m->textures[MENU_BOARD_TEX], &m->assets.textureAssets[MENU_BOARD_TEX], &boardSource, &boardDestRec); - int buttonWidth = screenWidth / m->menuScale; + int buttonWidth = windowWidth / m->menuScale; float buttonScaleFactor = (float)buttonWidth / m->assets.textureAssets[MENU_BIG_BUTTONS_TEX].frameWidth; int buttonHeight = (int)(m->assets.textureAssets[MENU_BIG_BUTTONS_TEX].frameHeight * buttonScaleFactor); int buttonSpacing = buttonHeight; @@ -88,14 +87,14 @@ void InitMenu(MenuState *m) Vector2 quitButtonPressed = {192.0f, 0.0f}; Rectangle playButtonDestRec = { - .x = screenCenter.x - (buttonWidth / 2), - .y = screenCenter.y - (buttonHeight / 2), + .x = windowCenter.x - (buttonWidth / 2), + .y = windowCenter.y - (buttonHeight / 2), .width = buttonWidth, .height = buttonHeight}; Rectangle quitButtonDestRec = { - .x = screenCenter.x - (buttonWidth / 2), - .y = screenCenter.y - (buttonHeight / 2) + buttonSpacing, + .x = windowCenter.x - (buttonWidth / 2), + .y = windowCenter.y - (buttonHeight / 2) + buttonSpacing, .width = buttonWidth, .height = buttonHeight}; @@ -103,7 +102,7 @@ void InitMenu(MenuState *m) m->quitButton = CreateImageButton(NOT_CLICKED, &m->textures[MENU_BIG_BUTTONS_TEX], &m->assets.textureAssets[MENU_BIG_BUTTONS_TEX], &quitButtonNotPressed, &quitButtonPressed, &quitButtonHover, &quitButtonDestRec); } -void UpdateMenu(MenuState *m, State *state) +void UpdateMenu(MenuState *m, View *currentView, bool *running) { for (int i = 0; i < MENU_SOUNDTRACK_COUNT; i++) @@ -111,25 +110,30 @@ void UpdateMenu(MenuState *m, State *state) UpdateSoundtrack(&m->soundtracks[i]); } - UpdateBackground(&m->background); - UpdateBackground(&m->midground); - UpdateBackground(&m->foreground); - - UpdateMouseScreen(&m->mouse); + if (GetFPS() >= 60) + { + SetTargetFPS(60); + } if (isImageButtonClicked(&m->playButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND])) { - state->gameStarted = true; - state->currentScreen = GAMEPLAY; + *running = true; + *currentView = GAMEPLAY; } if (isImageButtonClicked(&m->quitButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND]) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) { - state->running = false; + *running = false; } + + UpdateBackground(&m->background); + UpdateBackground(&m->midground); + UpdateBackground(&m->foreground); + UpdateMouseScreen(&m->mouse); + } -void RenderMenu(MenuState *m, State *appState, Font *font, const char *title) +void RenderMenu(MenuState *m, Font *font, const char *title, Flags *flags) { BeginDrawing(); ClearBackground(WHITE); @@ -149,7 +153,7 @@ void RenderMenu(MenuState *m, State *appState, Font *font, const char *title) 0.0f, WHITE); - if (appState->flagFPS) + if (flags->showFPS) { DrawFPS(10, 10); } diff --git a/include/state.h b/include/state.h new file mode 100644 index 0000000..43e1e2a --- /dev/null +++ b/include/state.h @@ -0,0 +1,12 @@ +#ifndef STATE_H +#define STATE_H + +#include "view.h" + +typedef struct { + View currentView; + bool running; + bool gameStarted; +} State; + +#endif // STATE_H diff --git a/include/screen.h b/include/view.h similarity index 50% rename from include/screen.h rename to include/view.h index 9110aa5..96374f7 100644 --- a/include/screen.h +++ b/include/view.h @@ -1,10 +1,10 @@ -#ifndef SCREEN_H -#define SCREEN_H +#ifndef VIEW_H +#define VIEW_H -typedef enum Screen { +typedef enum View { LOGO = 0, MENU = 1, GAMEPLAY = 2 -} Screen; +} View; #endif // SCREEN_H diff --git a/src/main.c b/src/main.c index ff39f43..7e003ce 100644 --- a/src/main.c +++ b/src/main.c @@ -3,9 +3,9 @@ int main(void) { - Application app; + App app; ConfigApp(&app); - InitWindow(app.display.width, app.display.height, app.display.title); + InitWindow(app.window.width, app.window.height, app.window.title); InitAudioDevice(); InitApp(&app); UpdateApp(&app);