Merge pull request #22 from huaqo-games/huaqo

big refactor
This commit is contained in:
2025-04-12 12:47:18 +02:00
committed by GitHub
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
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
+2 -2
View File
@@ -4,9 +4,9 @@
#include "input.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){
.offset = (Vector2){screenWidth / 2.0f, screenHeight / 2.0f},
.offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f},
.target = playerPosition,
.rotation = 0.0f,
.zoom = initZoom
-1
View File
@@ -6,7 +6,6 @@
#include <rini.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *path;
+1 -1
View File
@@ -5,6 +5,6 @@ typedef struct {
int width;
int height;
const char* title;
} Display;
} Window;
#endif // DISPLAY_H
+25 -22
View File
@@ -2,10 +2,11 @@
#define APP_H
#include <config.h>
#include <display.h>
#include <window.h>
#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
-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 <asset.h>
#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);
}
+5 -7
View File
@@ -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);
}
+27 -23
View File
@@ -7,7 +7,6 @@
#include <input.h>
#include <soundtrack.h>
#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);
}
+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
#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
+2 -2
View File
@@ -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);