Files
sinbad/include/game.h
T
2025-10-26 21:24:31 +01:00

149 lines
3.4 KiB
C

#ifndef GAME_H
#define GAME_H
#include <camera.h>
#include <input.h>
#include "floor.h"
#include "player.h"
#include "gameassets.h"
#include <postfx.h>
#include <soundtrack.h>
#include <asset.h>
typedef struct
{
Assets assets;
Texture2D textures[TEX_COUNT];
PostProcessing postFX[SHADER_COUNT];
Soundtrack soundtracks[SOUNDTRACK_COUNT];
RenderTexture2D target;
Floor floor;
Player player;
Camera2D camera;
Mouse mouse;
Image image;
} GameState;
void InitGame(GameState *g, Window *window)
{
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);
}
g->target = LoadRenderTexture(window->width, window->height);
g->player = CreatePlayer();
g->floor = CreateFloor();
g->camera = CreateCamera(window->width, window->height, g->player.physics.position, 10.0f);
Texture2D mouseTexture = LoadTexture("assets/SproutLandsUI/Spritesheets/Mouse sprites/TriangleMouseicon1.png");
g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &mouseTexture);
}
void UpdateGame(GameState *g, State *state, Flags *flags)
{
if(!state->gameStarted){
state->gameStarted = true;
}
if (GetFPS() <= 60)
{
SetTargetFPS(0);
}
if (IsKeyPressed(KEY_ESCAPE))
{
state->currentView = 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], flags->soundtrackOn);
}
UpdateMouse(&g->mouse, &g->camera);
UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse);
UpdateFloor(&g->floor, &g->camera);
UpdatePlayer(&g->player);
}
void RenderComponents(GameState *g)
{
RenderFloor(&g->floor);
RenderPlayer(&g->player);
}
void RenderGame(GameState *g, const State *appState, Flags *flags)
{
BeginTextureMode(g->target);
ClearBackground(BLACK);
BeginMode2D(g->camera);
RenderComponents(g);
EndMode2D();
DrawTextureRec(g->mouse.cursorTexture, (Rectangle){0, 0, g->mouse.cursorTexture.width, g->mouse.cursorTexture.height}, (Vector2){g->mouse.screenPosition.x, g->mouse.screenPosition.y}, WHITE);
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
for (int i = 0; i < SHADER_COUNT; i++)
{
RenderPostFX(&g->postFX[i], &g->target);
}
if (flags->showFPS)
{
DrawFPS(10, 10);
}
EndDrawing();
}
void CleanupGame(GameState *g)
{
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);
}
#endif // GAME_H