From f95068d73410aa6d01e8ba91b430ca05d404a57b Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Tue, 26 Aug 2025 19:54:09 +0300 Subject: Added barrel, next up sprite Z-sorting(ugh) --- CMakeLists.txt | 6 +++++- include/barrel.h | 6 ++++++ include/barrel_data.h | 15 +++++++++++++++ include/game.h | 4 +++- include/player_data.h | 6 +++--- include/utils.h | 2 ++ src/assets.c | 5 +++++ src/background.c | 1 + src/barrel.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/game.c | 8 +------- src/main.c | 2 ++ src/player.c | 7 +++++++ 12 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 include/barrel.h create mode 100644 include/barrel_data.h create mode 100644 src/barrel.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 343273c..e1f84a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,13 +15,17 @@ add_executable(game "include/assets.h" "include/background.h" "include/utils.h" - + "include/barrel.h" + "include/player_data.h" + "include/barrel_data.h" + "src/game.c" "src/main.c" "src/player.c" "src/assets.c" "src/background.c" "src/physics.c" + "src/barrel.c" ) target_include_directories(game PRIVATE "include") diff --git a/include/barrel.h b/include/barrel.h new file mode 100644 index 0000000..7cc3ea0 --- /dev/null +++ b/include/barrel.h @@ -0,0 +1,6 @@ +#pragma once + +#include "game.h" + +void AddBarrel(float xpos, float ypos); +void UpdateBarrel(float dt); diff --git a/include/barrel_data.h b/include/barrel_data.h new file mode 100644 index 0000000..0db5fdd --- /dev/null +++ b/include/barrel_data.h @@ -0,0 +1,15 @@ +#include "barrel.h" + +static const Rectangle physicsCollider = (Rectangle) { + .x = NO_OFFSET, + .y = NO_OFFSET, + .width = 100, + .height = 100 +}; + +static const Rectangle destRect = (Rectangle) { + .x = -65, + .y = -140, + .width = 230, + .height = 300 +}; diff --git a/include/game.h b/include/game.h index 99529bf..2c4ea24 100644 --- a/include/game.h +++ b/include/game.h @@ -13,7 +13,8 @@ typedef enum EntityType { Player_Entity, Wall_Entity, - Background_Entity + Background_Entity, + Barrel_Entity } EntityType; typedef enum DrawLayer { @@ -36,6 +37,7 @@ typedef struct Sprite { Texture2D texture; DrawLayer layer; Rectangle destRect; //Destination rectangle relative to player position + Rectangle srcRect; //For un-animated sprites only bool flipX; bool flipY; diff --git a/include/player_data.h b/include/player_data.h index d9d45a1..bcd3bae 100644 --- a/include/player_data.h +++ b/include/player_data.h @@ -5,21 +5,21 @@ #define PLAYER_SPEED 300.0f -const Rectangle shadowDestRect = (Rectangle) { +static const Rectangle shadowDestRect = (Rectangle) { .x = -30, .y = 100, .width = 140, .height = 40 }; -const Rectangle physicsCollider = (Rectangle) { +static const Rectangle physicsCollider = (Rectangle) { .x = NO_OFFSET, .y = NO_OFFSET, .width = 100, .height = 100 }; -const Rectangle bodyDestRect = (Rectangle) { +static const Rectangle bodyDestRect = (Rectangle) { .x = -160, .y = -280, .width = 400, diff --git a/include/utils.h b/include/utils.h index 3381828..5a4a4ac 100644 --- a/include/utils.h +++ b/include/utils.h @@ -4,6 +4,8 @@ #include #include "game.h" +#define UNUSED(x) (void)x + static inline bool EntityAllocated(const Entity* e) { return (e->flags & ENTITY_ALLOCATED); } diff --git a/src/assets.c b/src/assets.c index f6d4792..7fb6477 100644 --- a/src/assets.c +++ b/src/assets.c @@ -24,6 +24,11 @@ Asset assets[] = { .type = Texture_Asset, .name = "player-body", .filePath = "assets/art/characters/player.png" + }, + { + .type = Texture_Asset, + .name = "barrel", + .filePath = "assets/art/props/barrel.png" } }; diff --git a/src/background.c b/src/background.c index d3647cf..7929fa1 100644 --- a/src/background.c +++ b/src/background.c @@ -37,6 +37,7 @@ void AddBackground(const char* backgroundTextureName) { .layer = Background_Layer, .numAnimations = 0, + .srcRect = {0, 0, backgroundTexture.width, backgroundTexture.height}, .destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y} }; diff --git a/src/barrel.c b/src/barrel.c new file mode 100644 index 0000000..b0ce9a7 --- /dev/null +++ b/src/barrel.c @@ -0,0 +1,41 @@ +#include "barrel.h" +#include "barrel_data.h" +#include "utils.h" +#include "assets.h" + +void AddBarrel(float xpos, float ypos) { + Entity barrel = {0}; + barrel.type = Barrel_Entity; + + barrel.position = (Vector2) {xpos, ypos}; + barrel.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE); + + Asset* barrelTexture = GetMatchingAssetExitOnFail("barrel", Texture_Asset); + + Sprite barrelSprite = (Sprite) { + .texture = barrelTexture->texture, + .layer = Foreground_Layer, + + .destRect = destRect, + .srcRect = { + .x = 0, + .y = 0, + .width = barrelTexture->texture.width / 2, + .height = barrelTexture->texture.height + } + }; + + AddSpriteToEntity(&barrel, barrelSprite); + + barrel.physicsCollider = physicsCollider; + +#ifdef BEATEMUP_DEBUG + barrel.physicsColliderColor = RED; +#endif + + AddEntity(&barrel); +} + +void UpdateBarrel(float dt) { + UNUSED(dt); +} diff --git a/src/game.c b/src/game.c index d6e8ce3..59747c6 100644 --- a/src/game.c +++ b/src/game.c @@ -56,7 +56,6 @@ void UpdateGame(float deltaTime) { game.enableDebugOverlay = !game.enableDebugOverlay; } #endif - for (int i = 0; i < MAX_ENTITY_COUNT; i++) { Entity* e = &game.entities[i]; @@ -70,12 +69,7 @@ void DrawEntitySprite(const Entity* e, int spriteIndex) { Rectangle srcRect; if (!IsAnimated(drawnSprite)) { - srcRect = (Rectangle) { - .x = 0, - .y = 0, - .width = drawnSprite->texture.width, - .height = drawnSprite->texture.height - }; + srcRect = drawnSprite->srcRect; } else { const Animation* currentAnimation = &drawnSprite->animations[drawnSprite->currentAnimation]; diff --git a/src/main.c b/src/main.c index f5be59f..7032ca9 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "game.h" #include "background.h" #include "player.h" +#include "barrel.h" int main() { InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "My BeatEmup"); @@ -15,6 +16,7 @@ int main() { AddBackground("bar-background"); AddPlayer(0, 400); AddWall(0, 160, 1000000, 100); + AddBarrel(200, 400); while(!WindowShouldClose()) { UpdateGame(GetFrameTime()); diff --git a/src/player.c b/src/player.c index ca5d3c0..896f33e 100644 --- a/src/player.c +++ b/src/player.c @@ -100,6 +100,13 @@ static void AddPlayerSprites(Entity* player) { .layer = Foreground_Layer, .destRect = shadowDestRect, + .srcRect = { + 0, + 0, + shadowTexture->texture.width, + shadowTexture->texture.height + }, + .numAnimations = 0, //Not an animated sprite }; AddSpriteToEntity(player, shadowSprite); -- cgit v1.2.3