-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
354 lines (288 loc) · 8.09 KB
/
main.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* The Clown3D game demo
* (libnds port)
*
* My first attempt at a 3D
* platformer. Very, very
* simple.
*/
extern char door_model[];
extern char key_model[];
extern char maze_model[]; /* maze world */
extern char world_model[]; /* default world */
extern char world_lowres[]; /* default world, stripped down to fit on the NDS */
extern char turtle_model[];
extern char turtle_model_lowpoly[];
extern char bullet_model[];
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "headers.h"
#ifdef PC_TARGET
/* vertex count checking */
int vc;
/* SDL event structures */
SDL_Event event;
Uint8 *keystate;
#endif
/* the game's models */
model* turtleModel;
model* worldModel;
model* keyModel;
model* doorModel;
model* bulletModel;
model* targetModel;
/* game objects */
game_obj *objs = NULL, *node = NULL, *player = NULL;
/* Here, everything runs at a constant 30 FPS,
however the clown3d engine is designed
for variable FPS */
float dtime = (1000.0f / 30.0f) * TIME_UNIT / 1000.0;
/* Camera angle, offset */
int cam_angle = 1;
float yoffs = /* 83.0 */20.0;
void runGameFrame(void);
/* This addresses a quirk in libnds.
The official GL version of glPopMatrix
requires no parameters, but the libnds
version has one parameter */
void PopMatrix(void)
{
#ifdef PC_TARGET
glPopMatrix();
#else
glPopMatrix(1); /* copied from Dovoto */
#endif
}
int main(int argc, char* argv[])
{
int chosen_world = 1;
#ifndef PC_TARGET
/* Apparently, this will halt execution
if something illegal is done */
defaultExceptionHandler();
/* Irq init code based on eKid's example.
Added this in with hopes of increasing
No$GBA's code quality rating. */
irqInit();
irqEnable(IRQ_VBLANK | IRQ_KEYS);
#endif
#ifdef PC_TARGET
/* SDL video stuff */
SDL_Surface* display;
SDL_VideoInfo* video_info;
/* select world via first argument */
if(argc > 1) sscanf(argv[1], "%d", &chosen_world);
#else
/* allow text on lower screen */
consoleDemoInit();
videoSetMode(MODE_FB0);
vramSetBankA(VRAM_A_LCD);
printf("Clown3d platformer engine\n by Bl0ckeduser\n");
printf("DS port based on 3d example code written by Dovoto (thanks !)\n\n");
printf("Made with devKitPro and libnds\n");
printf("built %s %s\n\n", __DATE__, __TIME__);
/* let the player choose one of two worlds */
printf("Press A for 'default' world\nPress B for 'maze' world\n\n");
while(1){
scanKeys();
if(keysHeld() & KEY_A){
chosen_world = 1;
break;
}
if(keysHeld() & KEY_B){
chosen_world = 2;
break;
}
swiWaitForVBlank();
}
#endif
printf("Loading turtle model...");
turtleModel = loadModel(turtle_model_lowpoly); /* player model */
printf("done.\n");
printf("Loading bullet model...");
bulletModel = loadModel(bullet_model);
printf("done.\n");
printf("Loading door model...");
doorModel = loadModel(door_model);
printf("done.\n");
printf("Loading key model...");
keyModel = loadModel(key_model);
printf("done.\n");
printf("Creating world objects... ");
objs = newList();
player = newPlayer(objs, 0, 20, 0);
if(chosen_world == 2){
/* in the maze world, there are keys and doors */
(void)newDoor(objs, -7.456112, -2.289094, -124.128632);
(void)newDoor(objs, -85.255035,-2.289177,12.114304);
(void)newKey(objs, -44.039379, 2.111141, -40.276863);
(void)newKey(objs, -73.642761, -2.289178, -166.881195);
}
printf("done.\n");
printf("Loading world model... ");
if(chosen_world == 1){
worldModel = loadModel(world_lowres);
} else {
worldModel = loadModel(maze_model);
}
printf("done.\n");
printf("Making world bounding boxes...");
genBoxes(worldModel, objs);
printf("done.\n");
printf("* STARTING GAME *\n");
#ifdef PC_TARGET
if(SDL_Init(SDL_INIT_EVERYTHING) == 0) {
video_info = (SDL_VideoInfo *)SDL_GetVideoInfo();
display = SDL_SetVideoMode(
256, 192,
video_info -> vfmt -> BitsPerPixel,
SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL);
require(display != NULL);
#else
if(1){
#endif
#ifndef PC_TARGET
/*
NDS 3D mode init code copied from
nehe3.cpp by Dovoto
*/
// Setup the Main screen for 3D
videoSetMode(MODE_0_3D);
// initialize the geometry engine
glInit();
// enable antialiasing
glEnable(GL_ANTIALIAS);
// setup the rear plane
glClearColor(0,0,0,31); // BG must be opaque for AA to work
glClearPolyID(63); // BG must have a unique polygon ID for AA to work
glClearDepth(GL_MAX_DEPTH);
// Set our viewport to be the same size as the screen
glViewport(0,0,255,191);
#else
/*
* With standard OpenGL we
* must manually enable
* Z-buffering
*/
glEnable(GL_DEPTH_TEST);
#endif
while(1) {
#ifdef PC_TARGET
SDL_PollEvent(&event);
keystate = SDL_GetKeyState(NULL);
if(keystate[SDLK_ESCAPE] || keystate[SDLK_q]) break;
if(event.type == SDL_QUIT) break;
#endif
runGameFrame();
#ifdef PC_TARGET
LimitFPS(30);
SDL_GL_SwapBuffers();
#else
/* framerate sync code based on Dovoto's examples */
glFlush(0);
swiWaitForVBlank();
swiWaitForVBlank(); /* 30 fps */
#endif
}
freeObjs(objs);
#ifdef PC_TARGET
/* Free SDL display */
SDL_FreeSurface(display);
#endif
/* Free game models */
freeModel(turtleModel);
freeModel(worldModel);
freeModel(bulletModel);
freeModel(doorModel);
freeModel(keyModel);
/* freeModel(targetModel); */
gc_stop();
#ifdef PC_TARGET
SDL_Quit();
#endif
}
/* RAM usage estimation on PC */
/* memcheck(); */
return 0;
}
void runGameFrame(void)
{
/*
* Tick game objects and resolve collisions
*/
for(node = objs; node; node = node->next)
tickFunction(node);
resolveCollisions(objs, collisionFunction);
/* Edge-evasion */
for(node = objs; node; node = node->next)
eeFunction(node);
/*
* Drawing code follows
*/
#ifdef PC_TARGET
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#endif
/* Setup the camera */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, 255.0f/192.0f, 0.1f, 100.0f);
#ifndef PC_TARGET
/*
* glPolyFmt is a libnds-specific call.
* The following line was copied from an
* example by Dovoto.
*/
glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
#endif
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
switch(cam_angle) {
case 1: /* camera placed at constant distance of player.
coords are calculated from the player by adding
a scaled version of the opposite of the X, Z direction
vector */
gluLookAt((float)(player->data[PLAYER_X] - player->data[PLAYER_DIRX]*(60))/10.0,
(float)(player->data[PLAYER_Y]+yoffs)/10.0,
(float)(player->data[PLAYER_Z]-player->data[PLAYER_DIRZ]*(60))/10.0,
(float)(player->data[PLAYER_X]/10.0),
(float)(player->data[PLAYER_Y]/10.0),
(float)(player->data[PLAYER_Z]/10.0),
0.0f, 1.0f,0.0f);
break;
case 2: /* first person view */
gluLookAt((float)(player->data[PLAYER_X] - player->data[PLAYER_DIRX]*20)/10.0,
(float)(player->data[PLAYER_Y])/10.0,
(float)(player->data[PLAYER_Z]-player->data[PLAYER_DIRZ]*20)/10.0,
(float)(player->data[PLAYER_X]/10.0),
(float)(player->data[PLAYER_Y]/10.0),
(float)(player->data[PLAYER_Z]/10.0),
0.0f, 1.0f,0.0f);
break;
}
#ifdef PC_TARGET
vc = 0; /* vertex count checking on PC */
#endif
/* draw the world */
glPushMatrix();
glScalef(0.1f, 0.1f, 0.1f);
drawModelWithGL_big(worldModel); /* see model-draw.c for _big explanation */
PopMatrix();
/* draw objects */
for(node = objs; node; node = node->next)
{
glPushMatrix();
drawFunction(node);
PopMatrix();
}
#ifdef PC_TARGET
/* vertex count checking on PC */
char buf[256];
sprintf(buf, "%d vertices", vc);
SDL_WM_SetCaption(buf, NULL);
if(vc > 6144){
printf("Vertex count too high: %d\n", vc);
}
#endif
gc_collect();
}