aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: f6a1de33c112d7ddaf2e0cd26cb4c0bbeeb5e0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
  * @file main.c
  * @brief The main loop and controls
  * @date 2026-04-27
  */
#include <math.h>
#include <raylib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>


#include "graphics.h"
#include "physics.h"
#include "common.h"


int main(void)
{
	// physics related
	object_t world[MAX_OBJECTS];
	uint32_t objs_count = 0;
	float force_gravity = FORCE_GRAVITY_DEFAULT;
	object_t* grabbed_obj = NULL;
	bool anti_gravity_toggle = false;

	objs_count = 1;
	init_physics(world, MAX_OBJECTS);
	init_graphics(WINDOW_X, WINDOW_Y, SCREEN_TITLE);
	while(!WindowShouldClose()) {
		Vector2 pos_cursor = GetMousePosition();
		
		/* if(IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { */
		/* 	if(objs_count == 0) { */
		/* 		grabbed_obj = NULL; */
		/* 	} else { */
		/* 		for(uint32_t iter = objs_count; iter > 0; iter--) { */
		/* 			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_RIGHT)) {
			if(grabbed_obj != NULL) {
				grabbed_obj->grabbed = false;
				grabbed_obj = NULL;
			}
		}

		if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
			if(objs_count < MAX_OBJECTS) {
				world[objs_count++] = (object_t){
					.color = RANDOM_COLOR(),
					.elast = 0.9f,
					.frict = 0.99f,
					.pos = pos_cursor,
					.pos_prev = pos_cursor,
					.vel = { 
						(float)GetRandomValue(-300, 300), 
						(float)GetRandomValue(-300, 300)
					},
					.grabbed = false,
					.registered = true,
					.mass = GetRandomValue(3, 100), // will be done later
				};
			}
		}

		if(IsKeyPressed(KEY_G)) {
			if(anti_gravity_toggle) {
				force_gravity = -75.0f;
				anti_gravity_toggle = false;
			} else if(!anti_gravity_toggle) {
				force_gravity = FORCE_GRAVITY_DEFAULT;
				anti_gravity_toggle = true;
			}
		}

		// to-do
		/* if(IsKeyPressed(KEY_C)) { */
		/* 	clear_world(world); */
		/* } */
		
		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(objs_count);

		end_graphics_drawing();
	}

    close_graphics();
	return EXIT_SUCCESS;
}