big asset rework

This commit is contained in:
huaqo-games
2025-03-23 21:14:10 +01:00
parent 4eb407d286
commit 1c515eaeec
6 changed files with 140 additions and 83 deletions
+48 -58
View File
@@ -4,29 +4,21 @@
#include "../engine/camera.h"
#include "../engine/input.h"
#include "../engine/postfx.h"
#include "../engine/musicmanager.h"
#include "../engine/soundtrack.h"
#include "../engine/asset.h"
#include "../engine/applicationstate.h"
#include "gameassets.h"
#include "floor.h"
#include "player.h"
#include "bee.h"
#include "hive.h"
#include "flowers.h"
typedef enum {
PLAYER,
FLOWER,
BEE,
GRASS,
HIVE,
TEX_COUNT
} TextureID;
typedef struct {
PostProcessing postFX;
MyMusic ambient;
Assets assets;
Texture2D textures[TEX_COUNT];
PostProcessing postFX[SHADER_COUNT];
Soundtrack soundtracks[SOUNDTRACK_COUNT];
RenderTexture2D target;
Floor floor;
Object hive;
@@ -36,60 +28,59 @@ typedef struct {
Camera2D camera;
Mouse mouse;
Image image;
TextureAsset textureAssets[TEX_COUNT];
Texture2D textures[TEX_COUNT];
} GameState;
void InitGame(GameState* g, Display *display){
void InitGame(GameState* g, Display *display){
InitAudioDevice();
g->assets = (Assets){
.textureAssets = texAssets,
.shaderAssets = shaderAssets,
.soundtrackAssets = soundtrackAssets
};
for (int i = 0; i < TEX_COUNT; i++){
g->textures[i] = LoadTexture(g->assets.textureAssets[i].path);
}
for (int i = 0; i < SHADER_COUNT; i++){
g->postFX[i] = CreatePostFX(g->assets.shaderAssets[i].path);
}
for (int i = 0; i < SOUNDTRACK_COUNT; i++){
g->soundtracks[i] = LoadSoundtrack(g->assets.soundtrackAssets[i].path);
}
// Target texture
g->target = LoadRenderTexture(display->width, display->height);
// Static Entities
g->flowers = CreateFlowers(g->textures[FLOWER], g->assets.textureAssets[FLOWER].frameWidth, g->assets.textureAssets[FLOWER].rotation, 50.0f);
g->floor = CreateFloor(g->textures[GRASS], g->assets.textureAssets[GRASS].frameWidth);
g->hive = CreateHive(g->textures[HIVE], g->assets.textureAssets[HIVE].frameWidth);
g->postFX = CreatePostFX("shaders/postfx.fs", 0.3f);
// Animated Entities
g->player = CreatePlayer(IDLE_DOWN, HOE, g->textures[PLAYER], g->assets.textureAssets[PLAYER].frameWidth, 4, 2, g->hive.position, g->assets.textureAssets[PLAYER].rotation, 50.0f);
g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive);
g->ambient = CreateMusic("assets/beez.mp3");
g->textureAssets[PLAYER] = (TextureAsset){"assets/character/character.png", 64.0f, 64.0f, 180.0f};
g->textureAssets[FLOWER] = (TextureAsset){"assets/flower.png", 16.0f, 16.0f, 45.0f};
g->textureAssets[BEE] = (TextureAsset){"assets/character_bee.png", 16.0f, 16.0f, 0.0f};
g->textureAssets[GRASS] = (TextureAsset){"assets/grass.png", 16.0f, 16.0f, 0.0f};
g->textureAssets[HIVE] = (TextureAsset){"assets/hive.png", 32.0f, 32.0f, 0.0f};
g->textures[PLAYER] = LoadTexture(g->textureAssets[PLAYER].path);
g->textures[FLOWER] = LoadTexture(g->textureAssets[FLOWER].path);
g->textures[BEE] = LoadTexture(g->textureAssets[BEE].path);
g->textures[GRASS] = LoadTexture(g->textureAssets[GRASS].path);
g->textures[HIVE] = LoadTexture(g->textureAssets[HIVE].path);
g->player = CreatePlayer(IDLE_DOWN, g->textures[PLAYER], g->textureAssets[PLAYER].frameWidth, 4, 5, g->hive.position, g->textureAssets[PLAYER].rotation, 50.0f);
g->flowers = CreateFlowers(g->textures[FLOWER], g->textureAssets[FLOWER].frameWidth, g->textureAssets[FLOWER].rotation, 50.0f);
g->floor = CreateFloor(g->textures[GRASS], g->textureAssets[GRASS].frameWidth);
g->hive = CreateHive(g->textures[HIVE], g->textureAssets[HIVE].frameWidth);
g->bee1 = CreateBee(g->textures[BEE], g->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, 5.0f);
g->mouse = CreateMouse(0.10f, 5.0f, 10.0f);
g->target = LoadRenderTexture(display->width, display->height);
}
void UpdateGame(GameState* g, State *state){
SetTargetFPS(0);
UpdatePostFX(&g->postFX);
UpdateMusic(&g->ambient);
if (GetFPS() <= 60){ SetTargetFPS(0); }
if (IsKeyPressed(KEY_ESCAPE)){ state->currentScreen = MENU; }
if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { state->running = false; }
for (int i = 0; i < SHADER_COUNT; i++){ UpdatePostFX(&g->postFX[i]); }
for (int i = 0; i < SOUNDTRACK_COUNT; i++){ UpdateSoundtrack(&g->soundtracks[i]); }
UpdateMouse(&g->mouse, &g->camera);
UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse);
UpdateFloor(&g->floor, &g->camera);
UpdatePlayer(&g->player);
UpdateBee(&g->bee1, &g->flowers, &g->hive);
if (IsKeyPressed(KEY_ESCAPE)){
state->currentScreen = MENU;
}
if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) {
state->running = false;
}
}
void RenderComponents(GameState* g){
@@ -109,20 +100,19 @@ void RenderGame(GameState* g){
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
RenderPostFX(&g->postFX, &g->target);
for (int i = 0; i < SHADER_COUNT; i++){
RenderPostFX(&g->postFX[i], &g->target);
}
DrawFPS(10, 10);
EndDrawing();
}
void CleanupGame(GameState* g){
UnloadPostFX(&g->postFX);
for (int i = 0; i < SHADER_COUNT; i++){ UnloadPostFX(&g->postFX[i]); }
for (int i = 0; i < TEX_COUNT; i++){ if (g->textures[i].id > 0) { UnloadTexture(g->textures[i]); } }
for (int i = 0; i < SOUNDTRACK_COUNT; i++){ UnloadMusicStream(g->soundtracks[i].music); }
UnloadRenderTexture(g->target);
for (int i = 0; i < TEX_COUNT; i++){
if (g->textures[i].id > 0) { UnloadTexture(g->textures[i]); }
}
UnloadMusicStream(g->ambient.music);
free(g->flowers.data);
CloseAudioDevice();
}
#endif // GAME_H