From 0220af27bce8736c04364a6938d77414db5d4484 Mon Sep 17 00:00:00 2001 From: Jose Date: Sat, 7 Mar 2026 22:11:16 +0100 Subject: [PATCH] change convention: from allman to K&R. add: gravity component --- src/components/gravity.h | 12 ++++++++++++ src/core/game.c | 12 ++++-------- src/core/window.c | 9 +++------ src/main.c | 6 ++---- src/math/vector.c | 3 +-- src/systems/animation_system.c | 12 ++++-------- src/systems/input_system.c | 3 +-- src/systems/intent_system.c | 6 ++---- src/systems/movement_system.c | 6 ++---- src/systems/physics_system.c | 6 +++--- src/systems/render_system.c | 13 ++++--------- src/util/animation_util.c | 6 ++---- 12 files changed, 40 insertions(+), 54 deletions(-) create mode 100644 src/components/gravity.h diff --git a/src/components/gravity.h b/src/components/gravity.h new file mode 100644 index 0000000..4fc4839 --- /dev/null +++ b/src/components/gravity.h @@ -0,0 +1,12 @@ +#ifndef GRAVITY_H +#define GRAVITY_H + +#include "raylib.h" +#include "entity.h" + +typedef struct { + EntityId id; + int a; +} GravityComponent; + +#endif \ No newline at end of file diff --git a/src/core/game.c b/src/core/game.c index d928db2..a5f3c95 100644 --- a/src/core/game.c +++ b/src/core/game.c @@ -2,8 +2,7 @@ #define MAX_ENTITIES 100 -typedef struct -{ +typedef struct { Camera2D camera; bool running; int score; @@ -21,8 +20,7 @@ VelocityComponent *velocities = NULL; MovementIntent *intents = NULL; SpriteComponent *sprites = NULL; -Result InitGame(void) -{ +Result InitGame(void) { game.camera = (Camera2D){0}; game.running = true; game.score = 0; @@ -65,8 +63,7 @@ int AddEntity(void) { return id; } -void UpdateGame(float dt) -{ +void UpdateGame(float dt) { HandleWindowEvents(); InputSystemUpdate(entities, inputs, entityCount); @@ -75,8 +72,7 @@ void UpdateGame(float dt) AnimationSystemUpdate(entities, sprites, intents, entityCount, dt); } -void DrawGame(void) -{ +void DrawGame(void) { BeginDrawing(); ClearBackground(RAYWHITE); RenderSystemDraw(entities, positions, sprites, entityCount); diff --git a/src/core/window.c b/src/core/window.c index 53fb4a1..b12b690 100644 --- a/src/core/window.c +++ b/src/core/window.c @@ -4,8 +4,7 @@ const char *TITLE = "Punk n' Spray"; const int WIDTH = 900; const int HEIGHT = 600; -Result InitGameWindow(void) -{ +Result InitGameWindow(void) { SetConfigFlags(FLAG_WINDOW_HIGHDPI); InitWindow(WIDTH, HEIGHT, TITLE); @@ -16,8 +15,7 @@ Result InitGameWindow(void) return RESULT_OK; } -void HandleWindowEvents() -{ +void HandleWindowEvents() { if (IsKeyPressed(KEY_F11)) { int display = GetCurrentMonitor(); @@ -35,8 +33,7 @@ void HandleWindowEvents() } } -void ShowSplashScreen(void) -{ +void ShowSplashScreen(void) { Image logoImg = LoadImage("resources/images/PunkNSpray.png"); Image miarmaImg = LoadImage("resources/images/Miarma.png"); ImageResize(&miarmaImg, 64, 64); diff --git a/src/main.c b/src/main.c index ec88e5f..e015ad1 100644 --- a/src/main.c +++ b/src/main.c @@ -2,13 +2,11 @@ #include "raylib.h" #include "core/game.h" -int main() -{ +int main() { InitGame(); ShowSplashScreen(); - while (!WindowShouldClose()) - { + while (!WindowShouldClose()) { float dt = GetFrameTime(); UpdateGame(dt); DrawGame(); diff --git a/src/math/vector.c b/src/math/vector.c index 6cb3db2..c6135c8 100644 --- a/src/math/vector.c +++ b/src/math/vector.c @@ -1,7 +1,6 @@ #include "vector.h" -void NormalizeVector(Vector2 *vector) -{ +void NormalizeVector(Vector2 *vector) { float m = sqrtf(vector->x * vector->x + vector->y * vector->y); if (m > 0.0001f) *vector = (Vector2){ vector->x / m, vector->y / m }; diff --git a/src/systems/animation_system.c b/src/systems/animation_system.c index d9c590b..0492ec4 100644 --- a/src/systems/animation_system.c +++ b/src/systems/animation_system.c @@ -4,10 +4,8 @@ void AnimationSystemUpdate(Entity *entities, SpriteComponent *sprites, MovementIntent *intents, size_t count, - float dt) -{ - for (size_t i = 0; i < count; i++) - { + float dt) { + for (size_t i = 0; i < count; i++) { if (!(entities[i].components & COMP_RENDERABLE)) continue; @@ -19,8 +17,7 @@ void AnimationSystemUpdate(Entity *entities, if (intents[i].direction.x != 0 || intents[i].direction.y != 0) newAnim = intents[i].running ? 2 : 1; - if (s->currentAnimation != newAnim) - { + if (s->currentAnimation != newAnim) { s->currentAnimation = newAnim; s->animations[newAnim].currentFrame = 0; s->animations[newAnim].elapsedTime = 0; @@ -30,8 +27,7 @@ void AnimationSystemUpdate(Entity *entities, // Actualizar frame animToDraw->elapsedTime += dt; - if (animToDraw->elapsedTime >= animToDraw->frameTime) - { + if (animToDraw->elapsedTime >= animToDraw->frameTime) { animToDraw->currentFrame = (animToDraw->currentFrame + 1) % animToDraw->frameCount; animToDraw->elapsedTime = 0; } diff --git a/src/systems/input_system.c b/src/systems/input_system.c index eba3a40..2729567 100644 --- a/src/systems/input_system.c +++ b/src/systems/input_system.c @@ -2,8 +2,7 @@ void InputSystemUpdate(Entity *entities, InputComponent *inputs, - size_t count) -{ + size_t count) { for (size_t i = 0; i < count; i++) { if (!(entities[i].components & COMP_INPUT)) diff --git a/src/systems/intent_system.c b/src/systems/intent_system.c index 96f1fe3..3c3184f 100644 --- a/src/systems/intent_system.c +++ b/src/systems/intent_system.c @@ -3,10 +3,8 @@ void IntentSystemUpdate(Entity *entities, InputComponent *inputs, MovementIntent *intents, - size_t count) -{ - for (size_t i = 0; i < count; i++) - { + size_t count) { + for (size_t i = 0; i < count; i++) { if (!(entities[i].components & COMP_INPUT)) continue; diff --git a/src/systems/movement_system.c b/src/systems/movement_system.c index cf53f7b..270d993 100644 --- a/src/systems/movement_system.c +++ b/src/systems/movement_system.c @@ -5,13 +5,11 @@ void MovementSystemUpdate(Entity *entities, VelocityComponent *velocities, MovementIntent *intents, size_t count, - float dt) -{ + float dt) { const float baseSpeed = 200.0f; const float runMultiplier = 1.5f; - for (size_t i = 0; i < count; i++) - { + for (size_t i = 0; i < count; i++) { if (!(entities[i].components & COMP_POSITION) || !(entities[i].components & COMP_VELOCITY)) continue; diff --git a/src/systems/physics_system.c b/src/systems/physics_system.c index a035c24..c1a8017 100644 --- a/src/systems/physics_system.c +++ b/src/systems/physics_system.c @@ -2,9 +2,9 @@ void PhysicsSystemUpdate(Entity *entities, size_t count, - float dt) -{ - for(size_t i = 0; i < count; i++) { + float dt) { + for(size_t i = 0; i < count; i++) { + } } \ No newline at end of file diff --git a/src/systems/render_system.c b/src/systems/render_system.c index c35cf64..eb76fcb 100644 --- a/src/systems/render_system.c +++ b/src/systems/render_system.c @@ -1,16 +1,13 @@ #include "render_system.h" -void RenderSystemDraw(Entity *entities, PositionComponent *positions, SpriteComponent *sprites, size_t count) -{ - for (size_t i = 0; i < count; i++) - { +void RenderSystemDraw(Entity *entities, PositionComponent *positions, SpriteComponent *sprites, size_t count) { + for (size_t i = 0; i < count; i++) { if (!(entities[i].components & COMP_POSITION)) continue; Vector2 pos = positions[i].position; - if (entities[i].components & COMP_RENDERABLE) - { + if (entities[i].components & COMP_RENDERABLE) { SpriteComponent *s = &sprites[i]; DrawTextureRec( @@ -18,9 +15,7 @@ void RenderSystemDraw(Entity *entities, PositionComponent *positions, SpriteComp s->currentAnimationPtr->source, pos, WHITE); - } - else - { + } else { DrawRectangle(pos.x, pos.y, 32, 32, RED); } } diff --git a/src/util/animation_util.c b/src/util/animation_util.c index 668254b..9c5d6ec 100644 --- a/src/util/animation_util.c +++ b/src/util/animation_util.c @@ -1,14 +1,12 @@ #include "animation_util.h" -Animation *MirrorAnimations(Animation *originalAnimations, int count) -{ +Animation *MirrorAnimations(Animation *originalAnimations, int count) { if (!originalAnimations || count <= 0) return NULL; Animation *mirrored = malloc(sizeof(Animation) * count); if (!mirrored) return NULL; - for (int i = 0; i < count; i++) - { + for (int i = 0; i < count; i++) { mirrored[i] = originalAnimations[i]; mirrored[i].currentFrame = 0; mirrored[i].elapsedTime = 0;