From b40d44202ed0c79cbb769edba8de57412d07501a Mon Sep 17 00:00:00 2001 From: kotorifan Date: Tue, 21 Apr 2026 23:57:34 +0200 Subject: Enough for today --- src/main.c | 78 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 36 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 1d3beb2..75042da 100644 --- a/src/main.c +++ b/src/main.c @@ -5,100 +5,106 @@ #include #include -#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(); } -- cgit v1.3