diff options
author | BoredGuy <osome3717@gmail.com> | 2025-08-07 10:10:18 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-08-07 10:10:18 +0300 |
commit | 251be1ac2d808dfd0fca5c0eb37398357ca7bb20 (patch) | |
tree | 4bc7ccdaee4773b1ef55ce734a40c528384d4efe | |
parent | 2b95f565e8b88841d20f79205ac600cc14e28458 (diff) |
Huge Commit
- Added (static)sprites
- Removed variable fps (removes wierd camera jitter)
Also I'm becoming increasingly more anti-VSync
-rw-r--r-- | include/constants.h | 6 | ||||
-rw-r--r-- | include/game.h | 19 | ||||
-rw-r--r-- | src/assets.c | 5 | ||||
-rw-r--r-- | src/background.c | 11 | ||||
-rw-r--r-- | src/game.c | 32 | ||||
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/player.c | 37 |
7 files changed, 85 insertions, 29 deletions
diff --git a/include/constants.h b/include/constants.h index 2f9625a..870c7fb 100644 --- a/include/constants.h +++ b/include/constants.h @@ -4,4 +4,8 @@ #define WINDOW_HEIGHT 720 #define MAX_ENTITY_COUNT 2048 -#define MAX_AREA_COUNT 4
\ No newline at end of file +#define MAX_AREA_COUNT 4 + +#define MAX_SPRITE_COUNT 4 //Maximum number of sprites per entity + +#define NO_OFFSET 0 diff --git a/include/game.h b/include/game.h index fbfe2e1..e54afd3 100644 --- a/include/game.h +++ b/include/game.h @@ -21,6 +21,12 @@ typedef enum DrawLayer { Foreground_Layer = 1 } DrawLayer; +typedef struct Sprite { + Texture2D texture; + DrawLayer layer; + Rectangle destRect; //Destination rectangle relative to player position +} Sprite; + typedef struct Entity { int id; EntityType type; @@ -36,10 +42,8 @@ typedef struct Entity { int numHurtBoxes; Rectangle hurtBoxes[MAX_AREA_COUNT]; - //Graphics Information - Texture2D texture; - DrawLayer drawLayer; - Rectangle destRect; //Relative to the players position + Sprite sprites[MAX_SPRITE_COUNT]; + int numSprites; #ifdef BEATEMUP_DEBUG //Debug information @@ -73,6 +77,13 @@ static inline bool SameEntity(const Entity* a, const Entity* b) { return a->id == b->id; } +static inline void AddSpriteToEntity(Entity* e, Sprite s) { + if (e->numSprites < MAX_SPRITE_COUNT) { + e->sprites[e->numSprites++] = s; + } +} + + #ifdef BEATEMUP_DEBUG void DebugHighlights(const Entity* e); #endif diff --git a/src/assets.c b/src/assets.c index 498c801..b08201a 100644 --- a/src/assets.c +++ b/src/assets.c @@ -15,6 +15,11 @@ Asset assets[] = { .name = "rails", .filePath = "assets/art/backgrounds/rails.png" }, + { + .type = Texture_Asset, + .name = "human-shadow", + .filePath = "assets/art/characters/shadow.png" + } }; #define ASSET_COUNT (sizeof(assets) / sizeof(Asset)) diff --git a/src/background.c b/src/background.c index 76a3edf..d3588a7 100644 --- a/src/background.c +++ b/src/background.c @@ -9,7 +9,6 @@ void AddBackground(const char* backgroundTextureName) { e.type = Background_Entity; e.flags |= ENTITY_VISIBLE; - e.drawLayer = Background_Layer; e.position = (Vector2) {0.0f, 0.0f}; @@ -32,11 +31,11 @@ void AddBackground(const char* backgroundTextureName) { Vector2 backgroundBounds = Vector2Scale((Vector2) {backgroundTexture.width, backgroundTexture.height}, backgroundSizeScale); - e.destRect = (Rectangle) { - .width = backgroundBounds.x, - .height = backgroundBounds.y - }; - e.texture = backgroundTexture; + AddSpriteToEntity(&e, (Sprite){ + .texture = backgroundTexture, + .layer = Background_Layer, + .destRect = {NO_OFFSET, NO_OFFSET, backgroundBounds.x, backgroundBounds.y} + }); AddEntity(&e); } @@ -7,17 +7,17 @@ #include "assets.h" #include "player.h" #include "background.h" +#include <math.h> Game game; static inline bool ShouldDrawEntity(const Entity* e, DrawLayer layerDrawing) { return EntityAllocated(e) - && (e->flags & ENTITY_VISIBLE) - && (layerDrawing == e->drawLayer); + && (e->flags & ENTITY_VISIBLE); } -static inline Rectangle GetDrawDestinationRectGlobal(const Entity* e) { - Rectangle destRectGlobal = e->destRect; +static inline Rectangle GetSpriteDrawDestinationRectGlobal(const Entity* e, int spriteIndex) { + Rectangle destRectGlobal = e->sprites[spriteIndex].destRect; destRectGlobal.x += e->position.x; destRectGlobal.y += e->position.y; @@ -78,20 +78,30 @@ void UpdateGame(float deltaTime) { } } -void DefaultDrawEntity(const Entity* e) { - Rectangle srcRect = {0, 0, e->texture.width, e->texture.height}; +void DrawEntitySprite(const Entity* e, int spriteIndex) { + const Sprite* drawnSprite = &e->sprites[spriteIndex]; + + Rectangle srcRect = + {0, 0, drawnSprite->texture.width, drawnSprite->texture.height}; Vector2 origin = {0.0f, 0.0f}; float rotation = 0.0f; - Rectangle destRect = GetDrawDestinationRectGlobal(e); + Rectangle destRect = GetSpriteDrawDestinationRectGlobal(e, spriteIndex); + + DrawTexturePro(drawnSprite->texture, srcRect, destRect, origin, rotation, WHITE); +} - DrawTexturePro(e->texture, srcRect, destRect, origin, rotation, WHITE); +void DefaultDrawEntity(const Entity* e, DrawLayer toLayer) { + for (int i = 0; i < e->numSprites; i++) { + if (e->sprites[i].layer == toLayer) + DrawEntitySprite(e, i); + } } -void DrawEntity(const Entity* e) { +void DrawEntity(const Entity* e, DrawLayer toLayer) { switch (e->type) { default: - DefaultDrawEntity(e); + DefaultDrawEntity(e, toLayer); } } @@ -100,7 +110,7 @@ void DrawEntitiesInLayer(int layer) { Entity* currentEntity = &game.entities[i]; if (ShouldDrawEntity(currentEntity, layer)) - DrawEntity(currentEntity); + DrawEntity(currentEntity, layer); } } @@ -6,15 +6,13 @@ #include "player.h" int main() { - SetConfigFlags(FLAG_VSYNC_HINT); //Always better than fixed FPS imo InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "My BeatEmup"); - SetTargetFPS(0); + SetTargetFPS(60); LoadAssets(); InitGame(); AddBackground("bar-background"); - AddPlayer(0, 0); AddWall(100, 100, 1000, 100); diff --git a/src/player.c b/src/player.c index 6426db7..67f971f 100644 --- a/src/player.c +++ b/src/player.c @@ -1,13 +1,29 @@ +#include <stdlib.h> #include <math.h> #include "constants.h" #include "game.h" #include "player.h" #include "physics.h" +#include "assets.h" extern Game game; #define PLAYER_SPEED 300.0f +const Rectangle shadowDestRect = (Rectangle) { + .x = 0, + .y = 100, + .width = 100, + .height = 40 +}; + +const Rectangle physicsCollider = (Rectangle) { + .x = 0, + .y = 0, + .width = 100, + .height = 100 +}; + void UpdatePlayer(Entity* player, float deltaTime) { player->velocity = (Vector2) {0.0f, 0.0f}; @@ -31,7 +47,7 @@ void UpdatePlayer(Entity* player, float deltaTime) { MoveAndSlide(player, deltaTime); game.camera.target = - (Vector2) {fmax(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; + (Vector2) {fmaxf(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; } void AddPlayer(float xpos, float ypos) { @@ -39,13 +55,26 @@ void AddPlayer(float xpos, float ypos) { player.type = Player_Entity; player.position = (Vector2) {xpos, ypos}; - player.flags |= (ENTITY_PHYSICS_ACTIVE); + player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE); - player.physicsCollider = (Rectangle) {0, 0, 100, 100}; + player.physicsCollider = physicsCollider; #ifdef BEATEMUP_DEBUG player.physicsColliderColor = RED; #endif - + + Asset* shadowTexture = GetMatchingAssetWithType("human-shadow", Texture_Asset); + + if (shadowTexture == NULL) { + TraceLog(LOG_ERROR, "Failed to find texture asset human-shadow, exitting!"); + exit(EXIT_FAILURE); + } + + AddSpriteToEntity(&player, (Sprite){ + .texture = shadowTexture->texture, + .layer = Foreground_Layer, + .destRect = shadowDestRect + }); + AddEntity(&player); } |