diff options
author | BoredGuy <osome3717@gmail.com> | 2025-07-30 23:33:22 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-07-30 23:33:22 +0300 |
commit | c05e188ef6a4b715848f9f0b401351a43c80168d (patch) | |
tree | dc1dd17a8623b26bac9bc6b959084babe556ee32 | |
parent | 815aec62f8ae3a403e913559d5fe6138c8825007 (diff) |
Small Refactor
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/assets.h | 15 | ||||
-rw-r--r-- | include/game.h | 17 | ||||
-rw-r--r-- | src/assets.c | 42 | ||||
-rw-r--r-- | src/background.c | 8 | ||||
-rw-r--r-- | src/game.c | 91 | ||||
-rw-r--r-- | src/main.c | 10 | ||||
-rw-r--r-- | src/physics.c | 76 |
8 files changed, 133 insertions, 127 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c8989c5..2c5dd35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(game "src/player.c" "src/assets.c" "src/background.c" + "src/physics.c" ) target_include_directories(game PRIVATE "include") diff --git a/include/assets.h b/include/assets.h index e2d76b7..61b198e 100644 --- a/include/assets.h +++ b/include/assets.h @@ -2,5 +2,18 @@ #include <raylib.h> +typedef enum AssetType { + Texture_Asset +} AssetType; + +typedef struct Asset { + AssetType type; + const char* name; + const char* filePath; + + Texture2D texture; +} Asset; + void LoadAssets(); -Texture2D GetTexture(const char* name); +Asset* GetMatchingAssetWithType(const char* targetName, AssetType targetType); +void UnloadAssets(); diff --git a/include/game.h b/include/game.h index 1039ad5..eb6da29 100644 --- a/include/game.h +++ b/include/game.h @@ -49,11 +49,8 @@ typedef struct Entity { typedef struct Game { bool paused; - Entity entities[MAX_ENTITY_COUNT]; - - Texture2D background; - float backgroundPosition; + #ifdef BEATEMUP_DEBUG //Debug information bool enableDebugOverlay; @@ -66,3 +63,15 @@ void AddWall(float xpos, float ypos, float width, float height); void InitGame(); void UpdateGame(float deltaTime); void DrawGame(); + +static inline bool EntityAllocated(const Entity* e) { + return (e->flags & ENTITY_ALLOCATED); +} + +static inline bool SameEntity(const Entity* a, const Entity* b) { + return a->id == b->id; +} + +#ifdef BEATEMUP_DEBUG +void DebugHighlights(const Entity* e); +#endif diff --git a/src/assets.c b/src/assets.c index 8edfde6..073c748 100644 --- a/src/assets.c +++ b/src/assets.c @@ -4,18 +4,6 @@ #include <stdint.h> #include "assets.h" -typedef enum AssetType { - Texture_Asset -} AssetType; - -typedef struct Asset { - AssetType type; - const char* name; - const char* filePath; - - Texture2D texture; -} Asset; - Asset assets[] = { { .type = Texture_Asset, @@ -37,9 +25,12 @@ void LoadAssets() { Asset* c = &assets[i]; switch (c->type) { - case Texture_Asset: - c->texture = LoadTexture(c->filePath); - break; + case Texture_Asset: + c->texture = LoadTexture(c->filePath); + break; + + default: + break; } } } @@ -52,16 +43,21 @@ Asset* GetMatchingAssetWithType(const char* targetName, AssetType targetType) { return c; } - return NULL; + printf("Failed to load asset with name: %s, exitting!\n", targetName); + exit(EXIT_FAILURE); } -Texture2D GetTexture(const char* name) { - Asset* textureAsset = GetMatchingAssetWithType(name, Texture_Asset); +void UnloadAssets() { + for (size_t i = 0; i < ASSET_COUNT; i++) { + Asset* c = &assets[i]; + + switch (c->type) { + case Texture_Asset: + UnloadTexture(c->texture); + break; - if (textureAsset == NULL) { - fprintf(stderr, "Failed to load texture with name: %s, exitting!\n", name); - exit(EXIT_FAILURE); + default: + break; + } } - - return textureAsset->texture; } diff --git a/src/background.c b/src/background.c index 39e3c1e..060ce6a 100644 --- a/src/background.c +++ b/src/background.c @@ -10,7 +10,9 @@ void AddBackground(const char* backgroundTextureName) { e.drawLayer = Background_Layer; e.position = (Vector2) {0.0f, 0.0f}; - Texture2D backgroundTexture = GetTexture(backgroundTextureName); + + Texture2D backgroundTexture = + GetMatchingAssetWithType(backgroundTextureName, Texture_Asset)->texture; const float backgroundSizeScale = (float)WINDOW_HEIGHT / backgroundTexture.height; Vector2 backgroundBounds = @@ -24,7 +26,3 @@ void AddBackground(const char* backgroundTextureName) { AddEntity(&e); } - -void UpdateBackground(Entity* background, float deltaTime) { - background->position.x -= 600 * deltaTime; -} @@ -9,42 +9,12 @@ Game game; -static inline bool EntityAllocated(const Entity* e) { - return (e->flags & ENTITY_ALLOCATED); -} - -static inline bool PhysicsEnabled(const Entity* e) { - return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE); -} - static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { return EntityAllocated(e) && (e->flags & ENTITY_VISIBLE) && (layerDrawing == e->drawLayer); } -static inline bool SameEntity(const Entity* a, const Entity* b) { - return a->id == b->id; -} - -static inline bool IsColliding(const Rectangle* a, const Rectangle* b) { - bool collisionX = (a->x <= b->x && a->x + a->width > b->x) - || (b->x <= a->x && b->x + b->width > a->x); - - bool collisionY = (a->y <= b->y && a->y + a->height > b->y) - || (b->y <= a->y && b->y + b->height > a->y); - - return collisionX && collisionY; -} - -static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) { - Rectangle physicsColliderGlobal = e->physicsCollider; - physicsColliderGlobal.x += e->position.x; - physicsColliderGlobal.y += e->position.y; - - return physicsColliderGlobal; -} - static inline Rectangle GetDrawDestinationRectGlobal(const Entity* e) { Rectangle destRectGlobal = e->destRect; @@ -54,47 +24,6 @@ static inline Rectangle GetDrawDestinationRectGlobal(const Entity* e) { return destRectGlobal; } -void MoveAndSlide(Entity* e, float deltaTime) { - Vector2 velocity = e->velocity; - Rectangle physicsCollider = GetPhysicsColliderGlobal(e); - - physicsCollider.x += e->velocity.x * deltaTime; - for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - Entity* c = &game.entities[i]; - - if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; - Rectangle otherCollider = GetPhysicsColliderGlobal(c); - - if(IsColliding(&physicsCollider, &otherCollider)) { - if (velocity.x > 0) { - physicsCollider.x = otherCollider.x - physicsCollider.width; - } else { - physicsCollider.x = otherCollider.x + otherCollider.width; - } - } - } - - physicsCollider.y += e->velocity.y * deltaTime; - for (int i = 0; i < MAX_ENTITY_COUNT; i++) { - Entity* c = &game.entities[i]; - - if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; - Rectangle otherCollider = GetPhysicsColliderGlobal(c); - - if(IsColliding(&physicsCollider, &otherCollider)) { - if (velocity.y > 0) { - physicsCollider.y = otherCollider.y - physicsCollider.height; - } else { - physicsCollider.y = otherCollider.y + otherCollider.height; - } - } - } - - e->position.x = physicsCollider.x; - e->position.y = physicsCollider.y; -} - - void AddEntity(Entity* e) { static int nextId = 0; @@ -111,8 +40,10 @@ void AddEntity(Entity* e) { void InitGame() { game.paused = false; - LoadAssets(); - + #ifdef BEATEMUP_DEBUG + game.enableDebugOverlay = false; + #endif + memset(game.entities, 0, sizeof(game.entities)); } @@ -122,10 +53,6 @@ void UpdateEntity(Entity* e, float deltaTime) { UpdatePlayer(e, deltaTime); break; - case Background_Entity: - UpdateBackground(e, deltaTime); - break; - default: break; } @@ -146,16 +73,6 @@ void UpdateGame(float deltaTime) { } } -#ifdef BEATEMUP_DEBUG -void DebugHighlights(const Entity* e) { - if (!PhysicsEnabled(e)) - return; - - Rectangle dstRect = GetPhysicsColliderGlobal(e); - DrawRectangle(dstRect.x, dstRect.y, dstRect.width, dstRect.height, e->physicsColliderColor); -} -#endif - void DefaultDrawEntity(const Entity* e) { Rectangle srcRect = {0, 0, e->texture.width, e->texture.height}; Vector2 origin = {0.0f, 0.0f}; @@ -1,8 +1,8 @@ #include <stdio.h> #include <raylib.h> #include "constants.h" +#include "assets.h" #include "game.h" -#include "player.h" #include "background.h" int main() { @@ -10,13 +10,8 @@ int main() { InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "My BeatEmup"); SetTargetFPS(0); + LoadAssets(); InitGame(); - AddPlayer(0.0, 0.0); - - AddWall(200, 100, 10000, 100); - AddWall(200, 400, 10000, 100); - - AddBackground("bar-background"); while(!WindowShouldClose()) { UpdateGame(GetFrameTime()); @@ -24,6 +19,7 @@ int main() { DrawGame(); } + UnloadAssets(); CloseWindow(); return 0; } diff --git a/src/physics.c b/src/physics.c new file mode 100644 index 0000000..6567dc4 --- /dev/null +++ b/src/physics.c @@ -0,0 +1,76 @@ +#include "physics.h" +#include "game.h" + +extern Game game; + +static inline bool PhysicsEnabled(const Entity* e) { + return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE); +} + +static inline bool IsColliding(const Rectangle* a, const Rectangle* b) { + bool collisionX = (a->x <= b->x && a->x + a->width > b->x) + || (b->x <= a->x && b->x + b->width > a->x); + + bool collisionY = (a->y <= b->y && a->y + a->height > b->y) + || (b->y <= a->y && b->y + b->height > a->y); + + return collisionX && collisionY; +} + +static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) { + Rectangle physicsColliderGlobal = e->physicsCollider; + physicsColliderGlobal.x += e->position.x; + physicsColliderGlobal.y += e->position.y; + + return physicsColliderGlobal; +} + +void MoveAndSlide(Entity* e, float deltaTime) { + Vector2 velocity = e->velocity; + Rectangle physicsCollider = GetPhysicsColliderGlobal(e); + + physicsCollider.x += e->velocity.x * deltaTime; + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* c = &game.entities[i]; + + if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; + Rectangle otherCollider = GetPhysicsColliderGlobal(c); + + if(IsColliding(&physicsCollider, &otherCollider)) { + if (velocity.x > 0) { + physicsCollider.x = otherCollider.x - physicsCollider.width; + } else { + physicsCollider.x = otherCollider.x + otherCollider.width; + } + } + } + + physicsCollider.y += e->velocity.y * deltaTime; + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* c = &game.entities[i]; + + if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; + Rectangle otherCollider = GetPhysicsColliderGlobal(c); + + if(IsColliding(&physicsCollider, &otherCollider)) { + if (velocity.y > 0) { + physicsCollider.y = otherCollider.y - physicsCollider.height; + } else { + physicsCollider.y = otherCollider.y + otherCollider.height; + } + } + } + + e->position.x = physicsCollider.x; + e->position.y = physicsCollider.y; +} + +#ifdef BEATEMUP_DEBUG +void DebugHighlights(const Entity* e) { + if (!PhysicsEnabled(e)) + return; + + Rectangle dstRect = GetPhysicsColliderGlobal(e); + DrawRectangle(dstRect.x, dstRect.y, dstRect.width, dstRect.height, e->physicsColliderColor); +} +#endif |