-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_collision.c
executable file
·215 lines (189 loc) · 6.02 KB
/
test_collision.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <allegro.h>
#include <allegro_primitives.h>
#include "bbox.h"
#define FPS 60
#define WIDTH 640
#define HEIGHT 480
/* 5 keys for controlling the spaceship */
int keys[8] = {0, 0, 0, 0, 0, 0, 0, 0};
enum KEYS {UP, DOWN, LEFT, RIGHT, Q, W, Z, X};
int main(int argc, char **argv) {
/* Game engine resources */
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
int redraw = 1;
int doexit = 0;
int overlap = 0;
Bbox b1, b2;
b1.center.x = WIDTH / 2;
b1.center.y = HEIGHT / 2;
b2.center.x = b1.center.x + 100;
b2.center.y = b1.center.y + 100;
b1.heading = 0.0;
b2.heading = 0.0;
b1.left = 2;
b1.right = 2;
b1.top = 3;
b1.bottom = 2;
b2.left = 20;
b2.right = 20;
b2.top = 30;
b2.bottom = 30;
/* Init Allegro */
if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
/* NOTE: it's necessary to call al_map_rgb after al_init */
b1.color = al_map_rgb(230, 230, 230);
b2.color = al_map_rgb(230, 230, 230);
/*******************************************************************
* Addons
******************************************************************/
if (!al_init_primitives_addon()) {
fprintf(stderr, "failed to initialize primitives addon!\n");
return -1;
}
/* Install keyboard */
if (!al_install_keyboard()) {
fprintf(stderr, "failed to install keyboard!\n");
return -1;
}
/* Get a timer */
timer = al_create_timer(1.0 / FPS);
if (!timer) {
fprintf(stderr, "failed to create a timer!\n");
return -1;
}
/* Get a display of WIDTH * HEIGHT */
display = al_create_display(WIDTH, HEIGHT);
if(!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer); /* rollback */
return -1;
}
/* Get an event queue */
event_queue = al_create_event_queue();
if (!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_display(display); /* rollback 1 */
al_destroy_timer(timer); /* rollback 2 */
return -1;
}
/* Registering everything in event queue */
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
/* Set background black */
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
/* kick off the timer */
al_start_timer(timer);
while (!doexit)
{
ALLEGRO_EVENT event;
/* Fetch an event from the event queue */
al_wait_for_event(event_queue, &event);
/* Check and make dicision */
if (event.type == ALLEGRO_EVENT_TIMER) {
redraw = 1;
/* move the b1 around */
if(keys[UP])
b1.center.y -= 5;
if(keys[DOWN])
b1.center.y += 5;
if(keys[LEFT])
b1.center.x -= 5;
if(keys[RIGHT])
b1.center.x += 5;
if(keys[Q])
b1.heading -= 0.05;
if(keys[W])
b1.heading += 0.05;
if(keys[Z])
b2.heading -= 0.05;
if(keys[X])
b2.heading += 0.05;
/* determine overlap */
overlap = bbox_overlap(b1, b2);
} else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { /* close */
doexit = 1;
} else if (event.type == ALLEGRO_EVENT_KEY_DOWN) { /* key press */
switch(event.keyboard.keycode) {
case ALLEGRO_KEY_UP:
keys[UP] = 1;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = 1;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = 1;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = 1;
break;
case ALLEGRO_KEY_Z:
keys[Z] = 1;
break;
case ALLEGRO_KEY_X:
keys[X] = 1;
break;
case ALLEGRO_KEY_Q:
keys[Q] = 1;
break;
case ALLEGRO_KEY_W:
keys[W] = 1;
break;
}
} else if (event.type == ALLEGRO_EVENT_KEY_UP) {
switch(event.keyboard.keycode) {
case ALLEGRO_KEY_UP:
keys[UP] = 0;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = 0;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = 0;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT] = 0;
break;
case ALLEGRO_KEY_Z:
keys[Z] = 0;
break;
case ALLEGRO_KEY_X:
keys[X] = 0;
break;
case ALLEGRO_KEY_Q:
keys[Q] = 0;
break;
case ALLEGRO_KEY_W:
keys[W] = 0;
break;
}
}
if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = 0;
/* drawing area */
bbox_draw(b1);
bbox_draw(b2);
if (overlap) {
ALLEGRO_TRANSFORM transform;
al_identity_transform(&transform);
al_translate_transform(&transform, 20, 20);
al_use_transform(&transform);
al_draw_filled_circle(0, 0, 20, al_map_rgb(255, 0, 0));
}
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
/* Destroy everything here */
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}