diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-03-15 17:08:36 +0300 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-03-15 17:08:36 +0300 |
| commit | 9e24aa042e9fb9de34f7b445778518fcb67e24aa (patch) | |
| tree | eb71468111da570dad7e1117e04a25aeba08e29c | |
| parent | 0ed3bb4569ded185237c6d530285382e6c2b6200 (diff) | |
Some Updates
| -rw-r--r-- | #CMakeLists.txt# | 20 | ||||
| l--------- | .#CMakeLists.txt | 1 | ||||
| -rw-r--r-- | CMakeLists.txt | 7 | ||||
| -rw-r--r-- | src/constants.h | 3 | ||||
| -rw-r--r-- | src/entity.c | 39 | ||||
| -rw-r--r-- | src/entity.h | 36 | ||||
| -rw-r--r-- | src/game.c | 24 | ||||
| -rw-r--r-- | src/game.h | 19 | ||||
| -rw-r--r-- | src/main.c | 7 | ||||
| -rw-r--r-- | src/physics.c | 78 | ||||
| -rw-r--r-- | src/physics.h | 13 | ||||
| -rw-r--r-- | src/settings.c | 1 | ||||
| -rw-r--r-- | src/settings.h | 1 |
13 files changed, 222 insertions, 27 deletions
diff --git a/#CMakeLists.txt# b/#CMakeLists.txt# deleted file mode 100644 index 43a12ef..0000000 --- a/#CMakeLists.txt# +++ /dev/null @@ -1,20 +0,0 @@ -cmake_minimum_required(VERSION 3.15) -project(Simple-Platformer VERSION 1.0.0) - -find_package(raylib REQUIRED) - -add_executable(game -#===========HEADERS=============== - "src/constants.h" - "src/settings.h" - "src/entity.h" -#===========SOURCES=============== - "src/settings.c" - "src/main.c" -) - -target_link_libraries(game PRIVATE raylib) - -if (UNIX) - target_link_libraries(game PRIVATE m) -endif() diff --git a/.#CMakeLists.txt b/.#CMakeLists.txt deleted file mode 120000 index c3cc091..0000000 --- a/.#CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -ubuntu@ubuntubox.3994:1773392748
\ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a442bb1..ddf4277 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,10 +7,15 @@ add_executable(game #===========HEADERS=============== "src/constants.h" "src/settings.h" -#===========SOURCES=============== "src/entity.h" + "src/physics.h" + "src/game.h" +#===========SOURCES=============== "src/settings.c" "src/main.c" + "src/entity.c" + "src/physics.c" + "src/game.c" ) target_link_libraries(game PRIVATE raylib) diff --git a/src/constants.h b/src/constants.h index 5d76d42..a21402a 100644 --- a/src/constants.h +++ b/src/constants.h @@ -3,5 +3,8 @@ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 +#define TARGET_FPS 144 + +#define MAX_ENTITY_COUNT 500 #endif diff --git a/src/entity.c b/src/entity.c new file mode 100644 index 0000000..4dc32f2 --- /dev/null +++ b/src/entity.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <raylib.h> +#include <string.h> +#include "game.h" +#include "entity.h" + +extern struct game game; + +Rectangle get_entity_collider_world(const struct entity* entity) { + return (Rectangle) { + .x = entity->collider.x + entity->position.x, + .y = entity->collider.y + entity->position.y, + .width = entity->collider.width, + .height = entity->collider.height + }; +} + +void entity_handle_collision +( + struct entity*, + struct entity*, + enum direction + ) { +} + +void draw_entity(const struct entity* entity) { + DrawRectangleRec(get_entity_collider_world(entity), RED); +} + +void add_entity(const struct entity* e) { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + struct entity* current = &game.entities[i]; + if (entity_active(current)) + continue; + + memcpy(current, e, sizeof(struct entity)); + return; + } +} diff --git a/src/entity.h b/src/entity.h index cbde27a..2ed443f 100644 --- a/src/entity.h +++ b/src/entity.h @@ -2,24 +2,52 @@ #define ENTITY_H_ #include <raylib.h> -#define <stdint.h> +#include <stdbool.h> +#include <stdint.h> enum entity_type { Player_Entity }; +enum direction { + Direction_Up, + Direction_Down, + Direction_Left, + Direction_Right +}; + #define ENTITY_ACTIVE 1 -#define ENTITY_PHYSICS_ENABLED (1 << 1) +#define ENTITY_COLLISION_ACTIVE (1 << 1) struct entity { + int entity_id; enum entity_type type; - uint32_t entity_flags flags + uint32_t flags; Vector2 position; //The position of the entity(roughly the center) - Rectangle body; + Vector2 velocity; + Rectangle collider; }; void update_entity(struct entity* entity, float dt); void draw_entity(const struct entity* entity); +Rectangle get_entity_collider_world(const struct entity* entity); + +void entity_handle_collision +( + struct entity* self, + struct entity* other, + enum direction collision_direction); + +void add_entity(const struct entity* e); + +static inline bool entity_active(const struct entity* e) { + return e->flags & ENTITY_ACTIVE; +} + +static inline bool same_entity(const struct entity* a, const struct entity* b) { + return a->entity_id == b->entity_id; +} + #endif diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..6af6ab8 --- /dev/null +++ b/src/game.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <string.h> +#include "game.h" + +struct game game; + +void update_game(float dt) { + +} + +void draw_game() { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + const struct entity* current = &game.entities[i]; + //Skip inactive entities + if (!entity_active(current)) + continue; + + draw_entity(current); + } +} + +void init_game() { + memset(&game, 0, sizeof(struct game)); +} diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..4aa377b --- /dev/null +++ b/src/game.h @@ -0,0 +1,19 @@ +#ifndef GAME_H_ +#define GAME_H_ +/* + * Defines the data needed for the game scene + * the data for the game is stored as a global variable + * of type struct game defined in game.c + */ +#include "entity.h" +#include "constants.h" + +struct game { + struct entity entities[MAX_ENTITY_COUNT]; +}; + +void update_game(float dt); +void draw_game(); +void init_game(); + +#endif @@ -1,12 +1,17 @@ #include <stdlib.h> #include <raylib.h> #include "settings.h" +#include "physics.h" +#include "game.h" struct settings settings; +extern struct game game; int main() { - load_or_init_settings(&settings, NULL); + load_or_init_settings(&settings, "settings.ini"); + init_game(); InitWindow(settings.window_width, settings.window_height, "Platformer"); + SetTargetFPS(settings.target_fps); while (!WindowShouldClose()) { BeginDrawing(); diff --git a/src/physics.c b/src/physics.c new file mode 100644 index 0000000..33065a9 --- /dev/null +++ b/src/physics.c @@ -0,0 +1,78 @@ +#include <stdbool.h> +#include "game.h" +#include "entity.h" +#include "physics.h" + +extern struct game game; + +static inline bool can_collide(const struct entity* entity) { + return (entity->flags & ENTITY_ACTIVE) && (entity->flags & ENTITY_COLLISION_ACTIVE); +} + +static inline bool aabb(const struct entity* a, const struct entity* b) { + Rectangle acol = get_entity_collider_world(a); + Rectangle bcol = get_entity_collider_world(b); + + bool collision_x = (acol.x <= bcol.x && acol.x + acol.width > bcol.x) || + (bcol.x <= acol.x && bcol.x + bcol.width > acol.x); + bool collision_y = (acol.y <= bcol.y && acol.y + acol.height > bcol.y) || + (bcol.y <= acol.y && bcol.y + bcol.height > acol.y); + + return collision_x && collision_y; +} + +void move_and_collide +( + struct entity* entity, + float delta_time + ) { + entity->position.x += entity->velocity.x * delta_time; + + if (can_collide(entity)) { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + struct entity* other = &game.entities[i]; + if (same_entity(entity, other) || !can_collide(other) || !aabb(entity, other)) + continue; + + Rectangle acol = get_entity_collider_world(entity); + Rectangle bcol = get_entity_collider_world(other); + + if (entity->velocity.x > 0) { + entity->position.x = bcol.x - acol.width - entity->collider.x; + + entity_handle_collision(entity, other, Direction_Right); + entity_handle_collision(other, entity, Direction_Left); + } else { + entity->position.x = bcol.x + bcol.width - entity->collider.x; + + entity_handle_collision(entity, other, Direction_Left); + entity_handle_collision(other, entity, Direction_Right); + } + } + } + + entity->position.y += entity->velocity.y * delta_time; + + if (can_collide(entity)) { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + struct entity* other = &game.entities[i]; + if (same_entity(other, entity) || !can_collide(other) || !aabb(entity, other)) + continue; + + Rectangle acol = get_entity_collider_world(entity); + Rectangle bcol = get_entity_collider_world(other); + + if (entity->velocity.y > 0) { + entity->position.y = bcol.y - acol.height - entity->collider.y; + + entity_handle_collision(entity, other, Direction_Down); + entity_handle_collision(other, entity, Direction_Up); + } else { + entity->position.y = bcol.y + bcol.height - entity->collider.y; + + entity_handle_collision(entity, other, Direction_Up); + entity_handle_collision(other, entity, Direction_Down); + } + } + } +} diff --git a/src/physics.h b/src/physics.h new file mode 100644 index 0000000..e32a785 --- /dev/null +++ b/src/physics.h @@ -0,0 +1,13 @@ +#ifndef PHYSICS_H_ +#define PHYSICS_H_ + +#include <raylib.h> +#include "entity.h" + +void move_and_collide +( + struct entity* entity, + float delta_time + ); + +#endif diff --git a/src/settings.c b/src/settings.c index f7b2338..037d803 100644 --- a/src/settings.c +++ b/src/settings.c @@ -4,4 +4,5 @@ void load_or_init_settings(struct settings* settings, const char* filename) { settings->window_width = WINDOW_WIDTH; settings->window_height = WINDOW_HEIGHT; + settings->target_fps = TARGET_FPS; } diff --git a/src/settings.h b/src/settings.h index 15574be..0852299 100644 --- a/src/settings.h +++ b/src/settings.h @@ -6,6 +6,7 @@ struct settings { int window_width; int window_height; + int target_fps; }; void load_or_init_settings(struct settings* settings, const char* filename); |
