-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
281 lines (246 loc) · 7.14 KB
/
main.c
File metadata and controls
281 lines (246 loc) · 7.14 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <stdio.h>
#include <stdlib.h>
#include <allegro5/allegro.h>
#include "bodies.h"
#include "drawing.h"
#include "libbodies.h"
#include "input.h"
#include "loading.h"
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_COLOR pColor = al_map_rgb(foreCols[0], foreCols[1], foreCols[2]);
int playerAbility = ABIL_NONE;
bool key[5] = {false, false, false, false, false};
bool redraw = true;
bool doexit = false;
body players[3];
int playerCount = 0;
FILE *levl;
char ch;
int charAbility = ABIL_NONE;
bool trippy = false;
for(int i = 2; i < argc; i++)
{
if (strcmp("-res", argv[i]) == 0){SCREEN_W = atoi(argv[i+1]); SCREEN_H = atoi(argv[i+2]);}
if (strcmp("-trippy", argv[i]) == 0){trippy = true;}
if (strcmp("-ability", argv[i]) == 0)
{
if(strcmp(argv[i + 1], "morph") == 0) {
playerAbility = ABIL_MORPH;
printf("Player gets morph ability!\n");
}
if(strcmp(argv[i + 1], "boost") == 0) {
playerAbility = ABIL_BOOST;
printf("Player gets boost ability!\n");
}
printf("ability value: %d\n", playerAbility);
}
if (strcmp(argv[i], "-backgroundColor") == 0)
{
for (int n = 0; n < 3; n++)
{
backCols[n] = atoi(argv[i + 1 + n]);
}
}
if (strcmp(argv[i], "-foregroundColor") == 0)
{
for (int n = 0; n < 3; n++)
{
foreCols[n] = atoi(argv[i + 1 + n]);
}
}
}
levl = fopen(argv[1], "r");
if(levl == NULL)
{
printf("Please specify a filename for the level. For example, ./Shape example_level.txt should run the example level.\n");
exit (0);
}
ch = fgetc(levl);
int staticLimit = 50;//Don't store more statics than this variable allows for
body statics[staticLimit]; //TODO: IMPLEMENT A SYSTEM THAT COUNTS THE STATIC OBJECTS IN A FILE
//RATHER THAN GIVE THE STATICS VARIABLE AN ARBITARY LIMIT
//HOW I WAS ABLE TO STORE 14 STATIC OBJECTS IN A TABLE THAT ONLY HAD ROOM FOR 10 IS BEYOND ME
//IN FACT, THE COMPILER DIDNT EVEN THROW ANY WARNINGS AND THE GAME RAN FINE
//THE ONLY REASON I NOTICED WAS THAT THE PROGRAM THREW AN ERROR WHEN I EXITED OUT OF IT
int staticcount = 0;
while (ch != EOF)
{
//printf("Char is not EOF!\n");
while(ch == ' ' || ch == '\n' || ch == ';'){ch = fgetc(levl); } //printf("Looks like we've got some dead space, or a stray semicolon.\n");}
if (ch == 's')
{
//printf("Let's add a static object.\n");
statics[staticcount] = spoutStatic(levl, 4, TYPE_STATIC);
staticcount++;
ch = fgetc(levl);
}
if (ch == 'p')
{
//printf("Let's add a player.\n");
players[playerCount] = spoutStatic(levl, 4, TYPE_PLAYER);
cameraFocusObject = &players[playerCount];
players[playerCount].ability = playerAbility;
playerCount++;
ch = fgetc(levl);
}
if (ch == 'c')
{
//printf("Let's add a checkpoint.\n");
statics[staticcount] = spoutStatic(levl, 2, TYPE_CHECKPOINT);
staticcount++;
ch = fgetc(levl);
}
if (ch == 'h')
{
//printf("Let's add a hurt trigger.\n");
statics[staticcount] = spoutStatic(levl, 4, TYPE_HURT_VIS);
staticcount++;
ch = fgetc(levl);
}
}
fclose(levl);
if(!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
if(!al_install_keyboard())
{
fprintf(stderr, "failed to initialize the keyboard!\n");
return -1;
}
timer = al_create_timer(1.0 / FPS);
if(!timer)
{
fprintf(stderr, "failed to create timer!\n");
return -1;
}
display = al_create_display(SCREEN_W, SCREEN_H);
if(!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}
bool prim = al_init_primitives_addon();
if(!prim)
{
fprintf(stderr, "failed to init primitives!\n");
al_destroy_timer(timer);
return -1;
}
event_queue = al_create_event_queue();
if(!event_queue)
{
fprintf(stderr, "failed to create event_queue!\n");
al_shutdown_primitives_addon();
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
int colorTrip = 1;
int colorSkip = 2;
while(!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER)
{
for(int i = 0; i < playerCount; i++)
{
players[i] = updateBody(players[i], statics, staticcount, key);
}
if (cameraFocusObject != NULL) //this pertains to players but isn't in the for loop
{//because we don't want to make the screen scroll for every player, just the first one.
if(cameraFocusObject->x > SCREEN_W + xscreen - scrollBoundaries && cameraFocusObject->xvel > 0){xscreen = xscreen + cameraFocusObject->xvel;}
if(cameraFocusObject->x < xscreen + scrollBoundaries && cameraFocusObject->xvel < 0){xscreen = xscreen + cameraFocusObject->xvel;}
if(cameraFocusObject->y > SCREEN_H + yscreen - scrollBoundaries && cameraFocusObject->yvel > 0){yscreen = yscreen + cameraFocusObject->yvel;}
if(cameraFocusObject->y < yscreen + scrollBoundaries && cameraFocusObject->yvel <= 0){yscreen = yscreen + cameraFocusObject->yvel;}
}
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
doexit = true;
}
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
key[KEY_UP] = true;
break;
case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = true;
break;
case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = true;
break;
case ALLEGRO_KEY_SPACE:
key[KEY_SPACE] = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
key[KEY_UP] = false;
break;
case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = false;
break;
case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = false;
break;
case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
case ALLEGRO_KEY_SPACE:
key[KEY_SPACE] = false;
break;
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
if (trippy == true)
{ //color shifting code
for (int f = 1; f <= colorSkip; f++)
{
for (int a = 0; a < 3; a++){foreCols[a] = foreCols[a] + colorTrip;}
if (foreCols[1] + colorTrip > 255 || foreCols[1] + colorTrip < 0){break;}
}
if (foreCols[1] >= 250){colorTrip = -1;}
if (foreCols[1] <= 5){colorTrip = 1;}
}
al_clear_to_color(al_map_rgb(backCols[0], backCols[1], backCols[2]));
for (int i = 0; i < playerCount; i++){drawBody(players[i]);}
for(int i = 0; i<staticcount; i++){drawBody(statics[i]);} //draw everything
al_flip_display();
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
al_shutdown_primitives_addon();
printf("Size of a single body in memory: %d\n", sizeof(body));
printf("Size of the statics table in total: %d\n", sizeof(statics));
return 0;
}