diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common.h | 45 | ||||
| -rw-r--r-- | src/graphics.c | 23 | ||||
| -rw-r--r-- | src/graphics.h | 10 | ||||
| -rw-r--r-- | src/main.c | 78 | ||||
| -rw-r--r-- | src/math.c | 4 | ||||
| -rw-r--r-- | src/math.h | 4 | ||||
| -rw-r--r-- | src/phy_math.c | 4 | ||||
| -rw-r--r-- | src/phy_math.h | 11 | ||||
| -rw-r--r-- | src/physics.c | 61 | ||||
| -rw-r--r-- | src/physics.h | 25 |
10 files changed, 123 insertions, 142 deletions
diff --git a/src/common.h b/src/common.h index 2c43f90..b8fb306 100644 --- a/src/common.h +++ b/src/common.h @@ -3,7 +3,6 @@ #define COMMON_H #include <stdint.h> - typedef uint_least8_t u8; typedef int_least8_t i8; typedef uint_least16_t u16; @@ -11,6 +10,29 @@ typedef int_least16_t i16; typedef uint_least32_t u32; typedef int_least32_t i32; +#define SHAPE_ENUM_SIZE 3 +typedef enum { + SHAPE_CIRCLE, + SHAPE_SQUARE, + SHAPE_RECTANGLE +} shape_t; + +typedef struct { + Color color; + float elast; +// Vector2 force; + float frict; + Vector2 pos; + Vector2 pos_prev; + Vector2 vel; + bool grabbed; + bool registered; + float mass; + float size_x; + float size_y; + shape_t obj_type; +} object_t; + // settings @@ -26,21 +48,14 @@ typedef int_least32_t i32; #define ARR_LEN(arr) (sizeof(arr)/sizeof(arr[0])) -typedef enum { - SHAPE_CIRCLE, - SHAPE_SQUARE, - SHAPE_RECTANGLE -} shape_t; +#define RANDOM_SHAPE() ((shape_t)(GetRandomValue(0, SHAPE_ENUM_SIZE - 1))) -shape_t get_random_shape(void) -{ - shape_t valid_shapes[] = { - SHAPE_CIRCLE, - SHAPE_SQUARE, - SHAPE_RECTANGLE - }; +#define RANDOM_COLOR() ((Color) { \ + GetRandomValue(0, 255), \ + GetRandomValue(0, 255), \ + GetRandomValue(0, 255), \ + 255 \ + }) - return valid_shapes(GetRandomValue(0, ARR(valid_shapes))); -} #endif // COMMON_H diff --git a/src/graphics.c b/src/graphics.c index 42a06cd..ecc0a58 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1,8 +1,9 @@ // graphics.c #include <raylib.h> #include <stdint.h> +#include <stdio.h> #include "graphics.h" -#include "math.h" +#include "common.h" void init_graphics(uint32_t x, uint32_t y, const char* title) { @@ -37,16 +38,16 @@ void draw_graphics_object(const object_t* obj) Vector2 pos = obj->pos; - switch(obj->type) { - case OBJECT_CIRCLE: - DrawCircleV(pos, obj->size_x, color); + switch(obj->obj_type) { + case SHAPE_CIRCLE: + DrawCircleV(pos, obj->size_x/2.0f, color); break; - case OBJECT_SQUARE: + case SHAPE_SQUARE: DrawRectangleV( (Vector2){pos.x - obj->size_x/2, pos.y - obj->size_y/2}, (Vector2){obj->size_x, obj->size_y}, color); break; - case OBJECT_RECTANGLE: + case SHAPE_RECTANGLE: DrawRectangleV( (Vector2){pos.x - obj->size_x/2, pos.y - obj->size_y/2}, (Vector2){obj->size_x, obj->size_y}, color); @@ -56,18 +57,18 @@ void draw_graphics_object(const object_t* obj) void draw_graphics_objects(const object_t* world, uint32_t count) { - for (uint32_t iter = 0; iter < count, i++) + for (uint32_t iter = 0; iter < count; iter++) { - if(world[iter].registered) draw_graphics_object(&world[i]); + if(world[iter].registered) draw_graphics_object(&world[iter]); } } -void draw_graphics_info(uint32_t fps, uint32_t objs, uint32_t max_objs) +void draw_graphics_info(uint32_t objs) { char buffer[128]; - snprintf(buffer, sizeof(buffer), "FPS: %d", fps; + snprintf(buffer, sizeof(buffer), "FPS: %d", GetFPS()); DrawText(buffer, 10, 10, 20, BLACK); - snprintf(buffer, sizeof(buffer), "OBJ: %d/%d", objs, max_objs); + snprintf(buffer, sizeof(buffer), "OBJ: %d/%d", objs, MAX_OBJECTS); DrawText(buffer, 10, 35, 20, BLACK); DrawText("ESC: Exit", 10, 60, 20, BLACK); DrawText("R: Random velocity", 10, 85, 20, BLACK); diff --git a/src/graphics.h b/src/graphics.h index 65c2ddc..62dd874 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -1,18 +1,18 @@ // graphics.h #ifndef GRAPHICS_H #define GRAPHICS_H +#include "common.h" #include <raylib.h> #include <stdint.h> -#include "common.h" void init_graphics(uint32_t x, uint32_t y, const char* title); -void close_graphics(void) +void close_graphics(void); void begin_graphics_drawing(void); void end_graphics_drawing(void); void clear_graphics(const Color color); -void draw_graphics_object(const object_t* obj, Color color); -void draw_graphics_objects(const object_t* world, uint32_t count, Color color); -void draw_graphics_info(uint32_t fps, uint32_t objs, uint32_t max_objs); +void draw_graphics_object(const object_t* obj); +void draw_graphics_objects(const object_t* world, uint32_t count); +void draw_graphics_info(uint32_t objs); bool should_close_graphics(void); #endif // GRAPHICS_H @@ -5,100 +5,106 @@ #include <stdio.h> #include <stdlib.h> -#include "common.h" + #include "graphics.h" #include "physics.h" +#include "common.h" - -int(void) +int main(void) { // physics related - object_t world = malloc(sizeof(object_t)*max_objs); + object_t world[MAX_OBJECTS]; uint32_t objs_count = 0; float force_gravity = FORCE_GRAVITY_DEFAULT; - float force_linear_damping = FORCE_LINEAR_DAMPING_DEFAULT; + object_t* grabbed_obj = NULL; world[0] = (object_t){ - .color = BLUE, + .color = RANDOM_COLOR(), .elast = 0.9f, .frict = 0.99f, .grabbed = true, .pos_prev = {0}, - .registered = true; + .registered = true, .size_x = 40, .size_y = 40, .vel = {200, 200}, - .pos = {GetScreenWidth()/2.0f, GetScreenheight()/2.0f}, - object_type_t = OBJECT_CIRCLE + .pos = { GetScreenWidth()/2.0f, GetScreenHeight()/2.0f }, + .obj_type = RANDOM_SHAPE() }; + objs_count = 1; + init_physics(world, MAX_OBJECTS); init_graphics(WINDOW_X, WINDOW_Y, SCREEN_TITLE); while(!WindowShouldClose()) { /* handle_input(world) */ // physics_update - float dt = GetFrameTime(); // delta time + /* float dt = GetFrameTime(); delta time */ Vector2 pos_cursor = GetMousePosition(); if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - for(uint32 iter = objs_count -1; iter >= 0; i--) + if(objs_count == 0) { - object_t* obj = &world[i]; - - if(hypot(pos_cursor.x, pos_cursor.y) <= (obj->size_x && obj->size_y)) + grabbed_obj = NULL; + } else { + for(uint32_t iter = objs_count; iter > 0; iter--) { - obj->grabbed = true; - grabbed_obj = obj; - break; + object_t* obj = &world[iter - 1]; + + + float dx = pos_cursor.x - obj->pos.x; + float dy = pos_cursor.y - obj->pos.y; + + if(hypot(dx, dy) <= (obj->size_x && obj->size_y)) + { + obj->grabbed = true; + grabbed_obj = obj; + break; + } } } } if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { - if(grabbed_obj != NULL) - { + if(grabbed_obj != NULL) { grabbed_obj->grabbed = false; grabbed_obj = NULL; } } - if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) - { - if(objs_count =< max_objs) - { - world[objs_count++] = (object_t*){ - .color = { - GetRandomValue(0, 255), - GetRandomValue(0, 255), - GetRandomValue(0, 255), - 255 - }, + if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { + if(objs_count < MAX_OBJECTS) { + world[objs_count++] = (object_t){ + .color = RANDOM_COLOR(), .elast = 0.9f, .frict = 0.99f, - .pos = pos_cursor; - .pos_prev = 0.99f, + .pos = pos_cursor, + .pos_prev = pos_cursor, .vel = { (float)GetRandomValue(-300, 300), (float)GetRandomValue(-300, 300) }, .grabbed = false, - .registered = true; - .mass = GetRandomValue(0, 100), // will be done later + .registered = true, + .mass = GetRandomValue(3, 100), // will be done later .size_x = GetRandomValue(3, 20), //everything smaller than 3 would be too small .size_y = GetRandomValue(3, 20), - .obj_type = get_random_shape() + .obj_type = RANDOM_SHAPE() }; } } + float dt = GetFrameTime(); + update_physics(world, objs_count, force_gravity, dt); + begin_graphics_drawing(); clear_graphics(RAYWHITE); draw_graphics_objects(world, objs_count); - draw_graphics_info(GetFPS(), objs_count, MAX_OBJECTS); + draw_graphics_info(objs_count); end_graphics_drawing(); } diff --git a/src/math.c b/src/math.c deleted file mode 100644 index 0ee755b..0000000 --- a/src/math.c +++ /dev/null @@ -1,4 +0,0 @@ -// math.c -#include "phy_math.h" - - diff --git a/src/math.h b/src/math.h deleted file mode 100644 index 0aa9c3d..0000000 --- a/src/math.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef PHY_MATH -#define PHY_MATH - -#endif diff --git a/src/phy_math.c b/src/phy_math.c deleted file mode 100644 index 0ee755b..0000000 --- a/src/phy_math.c +++ /dev/null @@ -1,4 +0,0 @@ -// math.c -#include "phy_math.h" - - diff --git a/src/phy_math.h b/src/phy_math.h deleted file mode 100644 index 1110319..0000000 --- a/src/phy_math.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef PHY_MATH -#define PHY_MATH - -typedef struct { - float x; - float y; -} Vec2d; - -Vec2d vec2d_add(Vec2d v1, Vec2d v2); -Vec2d vec2d_sub(Vec2d v1, Vec2d ) -#endif diff --git a/src/physics.c b/src/physics.c index cbcc977..14d786a 100644 --- a/src/physics.c +++ b/src/physics.c @@ -7,51 +7,54 @@ void init_physics(object_t* world, uint32_t max_objs) { - for(uint32_t iter = 0; iter < max_objs; iter++) world[iter].registered = false; + for(uint32_t iter = 0; iter < max_objs; iter++) { + world[iter].registered = false; + world[iter].grabbed = false; + } } -void update_physics(object_t* world, uint32_t objs_count, float gravity, - float damping, float dt) +void update_physics(object_t* world, uint32_t objs_count, float gravity, float dt) { const uint32_t screen_width = GetScreenWidth(); const uint32_t screen_height = GetScreenHeight(); + if(dt > 0.033f) dt = 0.033f; + for(uint32_t iter = 0; iter < objs_count; iter++) { object_t* obj = &world[iter]; - if(!obj->grabbed) { + if(!obj->grabbed && obj->registered) { obj->pos.x += obj->vel.x * dt; obj->pos.y += obj->vel.y * dt; - if((obj->pos.x + obj->size_x >= screen_width)) { - obj->pos.x = screen_width - obj->size_x; + float half_width = obj->size_x/2.0f; + float half_height = obj->size_y/2.0f; + + if((obj->pos.x + half_width >= screen_width)) { + obj->pos.x = screen_width - half_width; obj->vel.x = -obj->vel.x * obj->elast; - } else if((obj->pos.x - obj->size_x) <= 0) { - obj->pos.x = obj->radius; + + } else if((obj->pos.x - half_width) <= 0) { + obj->pos.x = half_width; obj->vel.x = -obj->vel.x * obj->elast; - } else if((obj->pos.y + obj->size_y) >= screen_height) { - obj->pos.y = screen_height - obj->size_y; + + } else if((obj->pos.y + half_height) >= screen_height) { + obj->pos.y = screen_height - half_height; + obj->vel.y = -obj->vel.y * obj->elast; + + } else if((obj->pos.y - half_height) <= 0) { + obj->pos.y = half_height; obj->vel.y = -obj->vel.y * obj->elast; - } else if((obj->pos.y - obj->size_y) <= 0) { - obj->pos.y = obj->size_y; - obj->vel.y -obj->vel.y * obj->elast; + } obj->vel.x = obj->vel.x * obj->frict; - obj->vel.y = obj->vel.y * obj->frict * gravity; + obj->vel.y = obj->vel.y * obj->frict * gravity * dt; + } + + if(obj->grabbed) { + Vector2 cursor_pos = GetMousePosition(); + obj->pos = cursor_pos; + obj->vel = (Vector2){0, 0}; } } - -/* void apply_force(object_t* obj, Vector2 force) */ -/* { */ -/* if(obj->registered == true) { */ -/* obj->force = Vector2Add(body->force, force) */ - -/* } */ -/* } */ - -/* void add_object(object_t* world, uint32_t* count, object_type_t type, Vector2 pos) */ -/* { */ -/* world */ -/* } */ -/* void clear_all_physics(object_t* world, uint32_t* count); */ - void push_pos(object_t* world, uint32_t count, Vector2 pos, float radius, float strength); +} diff --git a/src/physics.h b/src/physics.h index 10148c1..a5168cd 100644 --- a/src/physics.h +++ b/src/physics.h @@ -6,32 +6,11 @@ #include <time.h> #include "common.h" -typedef enum { - SHAPE_CIRCLE, - SHAPE_SQUARE, - SHAPE_RECTANGLE -} shape_t; - -typedef struct { - Color color; - Vector2 elast; -// Vector2 force; - Vector2 frict; - Vector2 pos; - Vector2 pos_prev; - Vector2 vel; - bool grabbed; - bool registered; - float mass; - float size_x; - float size_y; - shape_t obj_type; -} object_t; void init_physics(object_t* world, uint32_t max_objs); -void update_physics(object_t* world, uint32_t objs_count, float gravity, float damping); +void update_physics(object_t* world, uint32_t objs_count, float gravity, float dt); void apply_force(object_t* obj, Vector2 force); -void add_object(object_t* world, uint32_t* count, object_type_t type, Vector2 pos); +void add_object(object_t* world, uint32_t* count, shape_t type, Vector2 pos); void clear_all_physics(object_t* world, uint32_t* count); void push_pos(object_t* world, uint32_t count, Vector2 pos, float radius, float strength); |
