big refactor

This commit is contained in:
2025-04-12 12:46:00 +02:00
parent 8c23e1fe44
commit 52ca20f244
14 changed files with 105 additions and 88 deletions
+7
View File
@@ -0,0 +1,7 @@
{
"files.associations": {
"asset.h": "c",
"stdlib.h": "c",
"view.h": "c"
}
}
+4 -4
View File
@@ -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 window_title = The Last Hive
display_width = 1280 ; in Pixel window_width = 1280 ; in Pixel
display_height = 720 ; in Pixel window_height = 720 ; in Pixel
showFPS = 1 ; OFF = 0, ON = 1 showFPS = 1 ; OFF = 0, ON = 1
+2 -2
View File
@@ -4,9 +4,9 @@
#include "input.h" #include "input.h"
#include <raymath.h> #include <raymath.h>
Camera2D CreateCamera(int screenWidth, int screenHeight, Vector2 playerPosition, float initZoom){ Camera2D CreateCamera(int windowWidth, int windowHeight, Vector2 playerPosition, float initZoom){
return (Camera2D){ return (Camera2D){
.offset = (Vector2){screenWidth / 2.0f, screenHeight / 2.0f}, .offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f},
.target = playerPosition, .target = playerPosition,
.rotation = 0.0f, .rotation = 0.0f,
.zoom = initZoom .zoom = initZoom
-1
View File
@@ -6,7 +6,6 @@
#include <rini.h> #include <rini.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
typedef struct { typedef struct {
char *path; char *path;
+1 -1
View File
@@ -5,6 +5,6 @@ typedef struct {
int width; int width;
int height; int height;
const char* title; const char* title;
} Display; } Window;
#endif // DISPLAY_H #endif // DISPLAY_H
+25 -22
View File
@@ -2,10 +2,11 @@
#define APP_H #define APP_H
#include <config.h> #include <config.h>
#include <display.h> #include <window.h>
#include "screen.h" #include "view.h"
#include "applicationstate.h" #include "state.h"
#include "flags.h"
#include "font.h" #include "font.h"
#include "logo.h" #include "logo.h"
#include "menu.h" #include "menu.h"
@@ -14,60 +15,62 @@
typedef struct { typedef struct {
Config config; Config config;
State state; State state;
Display display; Flags flags;
Window window;
Font font; Font font;
LogoState logo; LogoState logo;
GameState game; GameState game;
MenuState menu; MenuState menu;
} Application; } App;
void ConfigApp(Application* app){ void ConfigApp(App* app){
InitConfig(&app->config, "config/config.ini"); 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.running = true;
app->state.gameStarted = false; app->state.gameStarted = false;
app->state.flagFPS = GetConfigInt(&app->config, "showFPS"); app->flags.showFPS = GetConfigInt(&app->config, "showFPS");
app->display.title = GetConfigString(&app->config, "display_title"); app->window.title = GetConfigString(&app->config, "window_title");
app->display.width = GetConfigInt(&app->config, "display_width"); app->window.width = GetConfigInt(&app->config, "window_width");
app->display.height = GetConfigInt(&app->config, "display_height"); app->window.height = GetConfigInt(&app->config, "window_height");
} }
void InitApp(Application *app){ void InitApp(App *app){
InitFont(&app->font); InitFont(&app->font);
InitLogo(&app->logo); InitLogo(&app->logo);
InitMenu(&app->menu); 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) while (app->state.running)
{ {
switch (app->state.currentScreen) switch (app->state.currentView)
{ {
case LOGO: case LOGO:
{ {
UpdateLogo(&app->logo, &app->state); UpdateLogo(&app->logo, &app->state.currentView, &app->state.running);
RenderLogo(&app->logo, &app->state); RenderLogo(&app->logo, &app->flags.showFPS);
}break; }break;
case MENU: case MENU:
{ {
UpdateMenu(&app->menu, &app->state); UpdateMenu(&app->menu, &app->state.currentView, &app->state.running);
RenderMenu(&app->menu, &app->state, &app->font, app->display.title); RenderMenu(&app->menu, &app->font, app->window.title, &app->flags);
}break; }break;
case GAMEPLAY: case GAMEPLAY:
{ {
UpdateGame(&app->game, &app->state); UpdateGame(&app->game, &app->state.currentView, &app->state.running);
RenderGame(&app->game, &app->state); RenderGame(&app->game, &app->state, &app->flags);
}break; }break;
default: break; default: break;
} }
} }
} }
void CleanupApp(Application *app){ void CleanupApp(App *app){
CleanupMenu(&app->menu); CleanupMenu(&app->menu);
CleanupGame(&app->game); CleanupGame(&app->game);
CleanupFont(&app->font); CleanupFont(&app->font);
CleanUpConfig(&app->config);
} }
#endif //APP_H #endif //APP_H
-13
View File
@@ -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
+8
View File
@@ -0,0 +1,8 @@
#ifndef FLAGS_H
#define FLAGS_H
typedef struct {
bool showFPS;
} Flags;
#endif // FLAGS_H
+8 -9
View File
@@ -8,7 +8,6 @@
#include <soundtrack.h> #include <soundtrack.h>
#include <asset.h> #include <asset.h>
#include "applicationstate.h"
#include "gameassets.h" #include "gameassets.h"
#include "floor.h" #include "floor.h"
#include "player.h" #include "player.h"
@@ -33,7 +32,7 @@ typedef struct
Image image; Image image;
} GameState; } GameState;
void InitGame(GameState *g, Display *display) void InitGame(GameState *g, Window *window)
{ {
g->assets = (Assets){ g->assets = (Assets){
@@ -57,7 +56,7 @@ void InitGame(GameState *g, Display *display)
} }
// Target texture // Target texture
g->target = LoadRenderTexture(display->width, display->height); g->target = LoadRenderTexture(window->width, window->height);
// Static Entities // Static Entities
g->flowers = CreateFlowers(g->textures[FLOWER], g->assets.textureAssets[FLOWER].frameWidth, g->assets.textureAssets[FLOWER].rotation, 50.0f); 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); 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 // 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]); 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) if (GetFPS() <= 60)
{ {
@@ -82,11 +81,11 @@ void UpdateGame(GameState *g, State *state)
} }
if (IsKeyPressed(KEY_ESCAPE)) if (IsKeyPressed(KEY_ESCAPE))
{ {
state->currentScreen = MENU; *currentView = MENU;
} }
if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))
{ {
state->running = false; *running = false;
} }
for (int i = 0; i < SHADER_COUNT; i++) for (int i = 0; i < SHADER_COUNT; i++)
{ {
@@ -113,7 +112,7 @@ void RenderComponents(GameState *g)
RenderSprite(&g->bee1.sprite); RenderSprite(&g->bee1.sprite);
} }
void RenderGame(GameState *g, const State *appState) void RenderGame(GameState *g, const State *appState, Flags *flags)
{ {
BeginTextureMode(g->target); BeginTextureMode(g->target);
ClearBackground(BLACK); ClearBackground(BLACK);
@@ -130,7 +129,7 @@ void RenderGame(GameState *g, const State *appState)
} }
DrawText(TextFormat("%d", g->player.inventar.seedCount), 10, 10, 50, WHITE); DrawText(TextFormat("%d", g->player.inventar.seedCount), 10, 10, 50, WHITE);
if (appState->flagFPS) if (flags->showFPS)
{ {
DrawFPS(10, 10); DrawFPS(10, 10);
} }
+5 -7
View File
@@ -1,8 +1,6 @@
#ifndef LOGO_H #ifndef LOGO_H
#define LOGO_H #define LOGO_H
#include "applicationstate.h"
typedef struct { typedef struct {
int frameCounter; int frameCounter;
int positionX; int positionX;
@@ -30,10 +28,10 @@ void InitLogo(LogoState *l) {
l->alpha = 1.0f; l->alpha = 1.0f;
} }
void UpdateLogo(LogoState *l, State *state) { void UpdateLogo(LogoState *l, View *currentView, bool *running) {
if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) {
state->running = false; *running = false;
} }
l->frameCounter++; l->frameCounter++;
@@ -76,11 +74,11 @@ void UpdateLogo(LogoState *l, State *state) {
} }
} }
else if (l->state == 4) { 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(); BeginDrawing();
ClearBackground(RAYWHITE); 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)); 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); DrawFPS(10, 10);
} }
+27 -23
View File
@@ -7,7 +7,6 @@
#include <input.h> #include <input.h>
#include <soundtrack.h> #include <soundtrack.h>
#include "applicationstate.h"
#include "menuassets.h" #include "menuassets.h"
#include "colors.h" #include "colors.h"
#include "background.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->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &m->textures[MENU_CURSOR_TEX]);
m->menuScale = 4; m->menuScale = 4;
int screenWidth = GetScreenWidth(); int windowWidth = GetScreenWidth();
int screenHeight = GetScreenHeight(); int windowHeight = GetScreenHeight();
Vector2 screenCenter = {screenWidth / 2, screenHeight / 2}; 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; float boardScaleFactor = (float)boardWidth / m->assets.textureAssets[MENU_BOARD_TEX].frameWidth;
int boardHeight = (int)(m->assets.textureAssets[MENU_BOARD_TEX].frameHeight * boardScaleFactor); int boardHeight = (int)(m->assets.textureAssets[MENU_BOARD_TEX].frameHeight * boardScaleFactor);
Vector2 boardSource = {128.0f, 0.0f}; Vector2 boardSource = {128.0f, 0.0f};
Rectangle boardDestRec = { Rectangle boardDestRec = {
.x = screenCenter.x - (boardWidth / 2), .x = windowCenter.x - (boardWidth / 2),
.y = screenCenter.y - (boardHeight / 2), .y = windowCenter.y - (boardHeight / 2),
.width = boardWidth, .width = boardWidth,
.height = boardHeight}; .height = boardHeight};
m->board = CreateImageElement(&m->textures[MENU_BOARD_TEX], &m->assets.textureAssets[MENU_BOARD_TEX], &boardSource, &boardDestRec); 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; 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 buttonHeight = (int)(m->assets.textureAssets[MENU_BIG_BUTTONS_TEX].frameHeight * buttonScaleFactor);
int buttonSpacing = buttonHeight; int buttonSpacing = buttonHeight;
@@ -88,14 +87,14 @@ void InitMenu(MenuState *m)
Vector2 quitButtonPressed = {192.0f, 0.0f}; Vector2 quitButtonPressed = {192.0f, 0.0f};
Rectangle playButtonDestRec = { Rectangle playButtonDestRec = {
.x = screenCenter.x - (buttonWidth / 2), .x = windowCenter.x - (buttonWidth / 2),
.y = screenCenter.y - (buttonHeight / 2), .y = windowCenter.y - (buttonHeight / 2),
.width = buttonWidth, .width = buttonWidth,
.height = buttonHeight}; .height = buttonHeight};
Rectangle quitButtonDestRec = { Rectangle quitButtonDestRec = {
.x = screenCenter.x - (buttonWidth / 2), .x = windowCenter.x - (buttonWidth / 2),
.y = screenCenter.y - (buttonHeight / 2) + buttonSpacing, .y = windowCenter.y - (buttonHeight / 2) + buttonSpacing,
.width = buttonWidth, .width = buttonWidth,
.height = buttonHeight}; .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); 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++) for (int i = 0; i < MENU_SOUNDTRACK_COUNT; i++)
@@ -111,25 +110,30 @@ void UpdateMenu(MenuState *m, State *state)
UpdateSoundtrack(&m->soundtracks[i]); UpdateSoundtrack(&m->soundtracks[i]);
} }
UpdateBackground(&m->background); if (GetFPS() >= 60)
UpdateBackground(&m->midground); {
UpdateBackground(&m->foreground); SetTargetFPS(60);
}
UpdateMouseScreen(&m->mouse);
if (isImageButtonClicked(&m->playButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND])) if (isImageButtonClicked(&m->playButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND]))
{ {
state->gameStarted = true; *running = true;
state->currentScreen = GAMEPLAY; *currentView = GAMEPLAY;
} }
if (isImageButtonClicked(&m->quitButton, m->sounds[HOVER_SOUND], m->sounds[CLICK_SOUND]) || (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE))) 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(); BeginDrawing();
ClearBackground(WHITE); ClearBackground(WHITE);
@@ -149,7 +153,7 @@ void RenderMenu(MenuState *m, State *appState, Font *font, const char *title)
0.0f, 0.0f,
WHITE); WHITE);
if (appState->flagFPS) if (flags->showFPS)
{ {
DrawFPS(10, 10); DrawFPS(10, 10);
} }
+12
View File
@@ -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
+4 -4
View File
@@ -1,10 +1,10 @@
#ifndef SCREEN_H #ifndef VIEW_H
#define SCREEN_H #define VIEW_H
typedef enum Screen { typedef enum View {
LOGO = 0, LOGO = 0,
MENU = 1, MENU = 1,
GAMEPLAY = 2 GAMEPLAY = 2
} Screen; } View;
#endif // SCREEN_H #endif // SCREEN_H
+2 -2
View File
@@ -3,9 +3,9 @@
int main(void) int main(void)
{ {
Application app; App app;
ConfigApp(&app); ConfigApp(&app);
InitWindow(app.display.width, app.display.height, app.display.title); InitWindow(app.window.width, app.window.height, app.window.title);
InitAudioDevice(); InitAudioDevice();
InitApp(&app); InitApp(&app);
UpdateApp(&app); UpdateApp(&app);