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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
#include <SDL3/SDL.h>
#include <vecmath.h>
#include <physics.h>
#include <stdlib.h>
#include <assert.h>
#include "../dat.h"
#define SBODY 4096
static void process_events(void);
static void render(void);
static SDL_Window *window;
static SDL_Renderer *renderer;
static bool running;
static Uint64 old_ticks, new_ticks;
void
renderrect(vec2 p, vec2 s)
{
SDL_RenderRect(renderer, &(SDL_FRect){
.x = p[0] - s[0],
.y = p[1] - s[1],
.w = s[0] * 2.0,
.h = s[1] * 2.0
});
}
int
main()
{
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError());
return 1;
}
if(!SDL_CreateWindowAndRenderer("window", 800, 600, SDL_WINDOW_OPENGL, &window, &renderer)) {
fprintf(stderr, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError());
return -1;
}
SDL_SetRenderVSync(renderer, 1);
#define FLOAT_RAND (2 * (rand() / (float)RAND_MAX) - 1)
for(int i = 0; i < SBODY; i++) {
BodyID b = phxnew(BTYPE_AABB);
assert(b != -1);
phxsetpos(b, rand() % 800, rand() % 600);
phxsetsize(b, 5.0, 5.0);
phxapplyaccel(b, (float[]){ (150 / PHX_TICK_TIME) * FLOAT_RAND, (150 / PHX_TICK_TIME) * FLOAT_RAND });
}
phxsetmap(8, 8, (int[]) {
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 1, 1, 0,
0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 1, 1, 0,
0, 1, 1, 0, 0, 1, 1, 0,
0, 0, 1, 0, 0, 0, 1, 0,
});
running = 1;
old_ticks = SDL_GetTicksNS();
while(running) {
CollisionEvent event;
double delta;
process_events();
new_ticks = SDL_GetTicksNS();
delta = (new_ticks - old_ticks) / 1000000000.0;
old_ticks = new_ticks;
static float accum = 0;
accum += delta;
while(accum >= PHX_TICK_TIME) {
phxfixtick();
while(phxnextcollevent(&event)) {
printf("Collision with %d and %d\n", event.body1, event.body2);
}
accum -= PHX_TICK_TIME;
}
render();
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
void
process_events()
{
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_EVENT_QUIT:
running = false;
break;
}
}
}
void
render(void)
{
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0xFF, 0xFF);
for(int y = 0; y < phxmapheight; y++)
for(int x = 0; x < phxmapwidth; x++) {
if(!phxmapbuffer[x + y * phxmapwidth])
continue;
renderrect((vec2){ x * 32, y * 32 }, (vec2){ 16, 16 });
}
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for(int i = 0; i < phxbodypoolsize; i++) {
vec2 p, s;
Body *b = phxbodypool + i;
if(!b->active)
continue;
renderrect(b->pos, b->size);
}
SDL_RenderPresent(renderer);
}
|