aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common.h14
-rw-r--r--src/graphics.c34
-rw-r--r--src/main.c110
-rw-r--r--src/physics.c52
-rw-r--r--src/physics.h39
5 files changed, 196 insertions, 53 deletions
diff --git a/src/common.h b/src/common.h
index c757489..2c43f90 100644
--- a/src/common.h
+++ b/src/common.h
@@ -24,9 +24,23 @@ typedef int_least32_t i32;
#define FORCE_GRAVITY_DEFAULT 1000.0f
#define FORCE_LINEAR_DAMPING_DEFAULT 0.995f;
+#define ARR_LEN(arr) (sizeof(arr)/sizeof(arr[0]))
+
typedef enum {
SHAPE_CIRCLE,
SHAPE_SQUARE,
SHAPE_RECTANGLE
} shape_t;
+
+shape_t get_random_shape(void)
+{
+ shape_t valid_shapes[] = {
+ SHAPE_CIRCLE,
+ SHAPE_SQUARE,
+ SHAPE_RECTANGLE
+ };
+
+ return valid_shapes(GetRandomValue(0, ARR(valid_shapes)));
+}
+
#endif // COMMON_H
diff --git a/src/graphics.c b/src/graphics.c
index ccc3fa7..42a06cd 100644
--- a/src/graphics.c
+++ b/src/graphics.c
@@ -30,8 +30,9 @@ void clear_graphics(const Color color)
ClearBackground(color);
}
-void draw_graphics_object(const object_t* obj, Color color)
+void draw_graphics_object(const object_t* obj)
{
+ const Color color = obj->color;
if(!obj->registered) return;
Vector2 pos = obj->pos;
@@ -51,14 +52,35 @@ void draw_graphics_object(const object_t* obj, Color color)
(Vector2){obj->size_x, obj->size_y}, color);
break;
}
-
}
-void draw_graphics_objects(const object_t* world, uint32_t count, Color color)
+void draw_graphics_objects(const object_t* world, uint32_t count)
{
-
+ for (uint32_t iter = 0; iter < count, i++)
+ {
+ if(world[iter].registered) draw_graphics_object(&world[i]);
+ }
}
+void draw_graphics_info(uint32_t fps, uint32_t objs, uint32_t max_objs)
+{
+ char buffer[128];
+ snprintf(buffer, sizeof(buffer), "FPS: %d", fps;
+ DrawText(buffer, 10, 10, 20, BLACK);
+ snprintf(buffer, sizeof(buffer), "OBJ: %d/%d", objs, max_objs);
+ DrawText(buffer, 10, 35, 20, BLACK);
+ DrawText("ESC: Exit", 10, 60, 20, BLACK);
+ DrawText("R: Random velocity", 10, 85, 20, BLACK);
+ DrawText("G: Random gravity", 10, 110, 20, BLACK);
+ DrawText("C: Clear screen", 10, 135, 20, BLACK);
+ DrawText("RCLICK: Push object", 10, 160, 20, BLACK);
+ DrawText("LCLICK: Add object", 10, 185, 20, BLACK);
+ DrawText("1: Spawn Circle", 10, 210, 20, BLACK);
+ DrawText("2: Spawn Square", 10, 235, 20, BLACK);
+ DrawText("3: Spawn Rectangle", 10, 260, 20, BLACK);
+}
-void draw_graphics_info(uint32_t fps, uint32_t objs, uint32_t max_objs);
-bool should_close_graphics(void);
+bool should_close_graphics(void)
+{
+ return WindowShouldClose();
+}
diff --git a/src/main.c b/src/main.c
index 12a3ac7..1d3beb2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,60 +1,108 @@
// main.c
+#include <math.h>
+#include <raylib.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stdbool.h>
-#include <raylib.h>
-#include <math.h>
#include "common.h"
#include "graphics.h"
#include "physics.h"
-void draw_graphics_info()
-{
- char buffer[128];
- snprintf(buffer, sizeof(buffer), "FPS: %d", GetFPS());
- DrawText(buffer, 10, 10, 20, BLACK);
- /* snprintf(buffer, sizeof(buffer), "OBJ: %d/%d", objects, MAX_OBJECTS); */
- /* DrawText(buffer, 10, 35, 20, BLACK); */
- DrawText("ESC: Exit", 10, 60, 20, BLACK);
- DrawText("R: Random velocity", 10, 85, 20, BLACK);
- DrawText("G: Random gravity", 10, 110, 20, BLACK);
- DrawText("C: Clear screen", 10, 135, 20, BLACK);
- DrawText("RCLICK: Push object", 10, 160, 20, BLACK);
- DrawText("LCLICK: Add object", 10, 185, 20, BLACK);
- DrawText("1: Spawn Circle", 10, 210, 20, BLACK);
- DrawText("2: Spawn Square", 10, 235, 20, BLACK);
- DrawText("3: Spawn Rectangle", 10, 260, 20, BLACK);
-}
-int main(void)
+int(void)
{
- bool should_run = true;
-
// physics related
- object_t world[MAX_OBJECTS];
- float force_resitution = FORCE_RESITUTION_DEFAULT;
+ object_t world = malloc(sizeof(object_t)*max_objs);
+ uint32_t objs_count = 0;
float force_gravity = FORCE_GRAVITY_DEFAULT;
float force_linear_damping = FORCE_LINEAR_DAMPING_DEFAULT;
- InitWindow(WINDOW_X, WINDOW_Y, SCREEN_TITLE);
- SetTargetFPS(60);
+ world[0] = (object_t){
+ .color = BLUE,
+ .elast = 0.9f,
+ .frict = 0.99f,
+ .grabbed = true,
+ .pos_prev = {0},
+ .registered = true;
+ .size_x = 40,
+ .size_y = 40,
+ .vel = {200, 200},
+ .pos = {GetScreenWidth()/2.0f, GetScreenheight()/2.0f},
+ object_type_t = OBJECT_CIRCLE
+ };
- while(!WindowShouldClose() && should_run)
+ init_graphics(WINDOW_X, WINDOW_Y, SCREEN_TITLE);
+ while(!WindowShouldClose())
{
/* handle_input(world) */
// physics_update
+ float dt = GetFrameTime(); // delta time
+ Vector2 pos_cursor = GetMousePosition();
+ if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
+ {
+ for(uint32 iter = objs_count -1; iter >= 0; i--)
+ {
+ object_t* obj = &world[i];
+
+ if(hypot(pos_cursor.x, pos_cursor.y) <= (obj->size_x && obj->size_y))
+ {
+ obj->grabbed = true;
+ grabbed_obj = obj;
+ break;
+ }
+ }
+ }
+
+ if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
+ {
+ 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
+ },
+ .elast = 0.9f,
+ .frict = 0.99f,
+ .pos = pos_cursor;
+ .pos_prev = 0.99f,
+ .vel = {
+ (float)GetRandomValue(-300, 300),
+ (float)GetRandomValue(-300, 300)
+ },
+ .grabbed = false,
+ .registered = true;
+ .mass = GetRandomValue(0, 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()
+ };
+ }
+ }
+
begin_graphics_drawing();
clear_graphics(RAYWHITE);
- draw_graphics_objects(world, object_count, BLACK);
- draw_graphics_info(GetFPS(), object_count, MAX_OBJECTS);
+ draw_graphics_objects(world, objs_count);
+ draw_graphics_info(GetFPS(), objs_count, MAX_OBJECTS);
end_graphics_drawing();
}
- CloseWindow();
+ close_graphics();
return EXIT_SUCCESS;
}
diff --git a/src/physics.c b/src/physics.c
index 4432dee..cbcc977 100644
--- a/src/physics.c
+++ b/src/physics.c
@@ -1,9 +1,57 @@
// physics.c
#include <raylib.h>
+#include <time.h>
#include "physics.h"
#include "common.h"
-void register_new_object(object_t* world, u32 object_x, u32 object_y)
+
+void init_physics(object_t* world, uint32_t max_objs)
{
- world
+ for(uint32_t iter = 0; iter < max_objs; iter++) world[iter].registered = false;
}
+
+void update_physics(object_t* world, uint32_t objs_count, float gravity,
+ float damping, float dt)
+{
+ const uint32_t screen_width = GetScreenWidth();
+ const uint32_t screen_height = GetScreenHeight();
+ for(uint32_t iter = 0; iter < objs_count; iter++)
+ {
+ object_t* obj = &world[iter];
+ if(!obj->grabbed) {
+ 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;
+ obj->vel.x = -obj->vel.x * obj->elast;
+ } else if((obj->pos.x - obj->size_x) <= 0) {
+ obj->pos.x = obj->radius;
+ 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;
+ 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;
+ }
+ }
+
+/* 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 b1fef97..10148c1 100644
--- a/src/physics.h
+++ b/src/physics.h
@@ -3,25 +3,36 @@
#define PHYSICS_H
#include <raylib.h>
+#include <time.h>
#include "common.h"
+typedef enum {
+ SHAPE_CIRCLE,
+ SHAPE_SQUARE,
+ SHAPE_RECTANGLE
+} shape_t;
+
typedef struct {
- shape_t type;
- Vec2d pos;
- Vec2d vel;
- Vec2d acc;
- float width;
- float height;
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;
-typedef sturct {
- object_t* object_arr;
- u16 object_count;
- size_t capacity;
-} world_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 apply_force(object_t* obj, Vector2 force);
+void add_object(object_t* world, uint32_t* count, object_type_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);
-void register_new_object(world_t* world, u32 object_x, u32 object_y);
-void delete_object(world_t* world);
-void clear_scene(world_t* world);
#endif // PHYSICS_H