File: main.cc

package info (click to toggle)
enemylines3 1.2-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,380 kB
  • ctags: 1,444
  • sloc: cpp: 16,323; makefile: 74; sh: 52
file content (504 lines) | stat: -rw-r--r-- 11,651 bytes parent folder | download | duplicates (6)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include "SDL.h"
#include "SDL_opengl.h"

#include <iostream>

#include "config.h"
#include "game.h"
#include "util.h"
#include "random.h"
#include "menu.h"
#include "help.h"
#include "font_ogl.h"
#include "font_data.h"
#include "font_ttf.h"
#include "font.h"
#include "config.h"
#include "audio.h"
#include "skybox.h"
#include "tips.h"

#include "models/all.h"

#include "tweak/tweak.h"

#include "release.h"

namespace PRJID {

void light() {
    GLfloat light0_pos[4]   = { 0.0, 5.0, .0, 0.0 };
    GLfloat light0_color[4] = { .6, .6, .6, 1.0 }; 
    GLfloat light1_pos[4]   = {  20.0, 5.0, 20.0, 0.0 };
    GLfloat light1_color[4] = { .4, .4, 1, 1.0 };  

    glShadeModel(GL_SMOOTH);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

    glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light0_color);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
    glLightfv(GL_LIGHT1, GL_DIFFUSE,  light1_color);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glEnable(GL_LIGHTING);

}

void load_audio(std::string dir) {
	std::string path;
	if (dir=="") {
		path="data/";
		std::cout << "  Looking in " << path << std::endl;
		if (Audio::load(path)) return;
		
		path="/usr/local/share/";
		path+=PATHNAME;
		path+="/";
		std::cout << "  Looking in " << path << std::endl;
		if (Audio::load(path)) return;

		path="/usr/share/";
		path+=PATHNAME;
		path+="/";
		std::cout << "  Looking in " << path << std::endl;
		if (Audio::load(path)) return;

	}

	if (Audio::load(dir)) return;
	std::cerr << "  Audio files not found ... They are optional however so feel free to ignore this." << std::endl;
}
void load_font(std::string dir) {
	std::string path;
	if (dir=="") {
		path="data/";
		std::cout << "  Looking in " << path << std::endl;
		if (Font::load(path)) return;
		
		path="/usr/local/share/";
		path+=PATHNAME;
		path+="/";
		std::cout << "  Looking in " << path << std::endl;
		if (Font::load(path)) return;

		path="/usr/share/";
		path+=PATHNAME;
		path+="/";
		std::cout << "  Looking in " << path << std::endl;
		if (Font::load(path)) return;

	}

	if (Font::load(dir)) return;
	std::cerr << "  Font file not found ... Using fallback." << std::endl;

	Font::load_fallback();
}
void load_resources(std::string dir) {
	std::cout << " Fonts: " << std::endl;
	load_font(dir);
	std::cout << " Audio: " << std::endl;
	load_audio(dir);
}


SDL_Surface *screen;

bool setmode(int w,int h,bool fullscreen) {
	if(!(screen=SDL_SetVideoMode(w,h,0,SDL_OPENGL|SDL_RESIZABLE | ((fullscreen) ? SDL_FULLSCREEN : 0)))) {
		std::cerr <<"SDL_SetVideoMode error " << w << "x" << h << " " << fullscreen << "  " << SDL_GetError() << std::endl;
		return false;
	}
	Config::resolution(w,h);
	SDL_WM_SetCaption(FULLNAME,"");
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(60.0f,(GLfloat)screen->w/(GLfloat)screen->h,0.15f,75.0f);
   glViewport(0, 0, screen->w, screen->h);
   glMatrixMode(GL_MODELVIEW);

   glEnable(GL_DEPTH_TEST);
	return true;
}

typedef enum e_menuids {
	M_POP=-1,
	M_NONE=0,
	M_START_EASY=1,
	M_START_MEDIUM=2,
	M_START_HARD=3,
	M_START_NIGHTMARE=4,
	M_QUIT=5
};


int main(int w,int h,bool fullscreen,bool audio,std::string dir) {
   std::cout<<std::endl<<std::endl<<FULLNAME << " "<< VERSION << std::endl;
   std::cout<<"\t Copyright (C) 2005-2006" << std::endl;
   std::cout<<"\t " << EL_URL << std::endl;
   std::cout<<"\t Using libSDL,SDL_mixer under the terms of the GNU LGPL" << std::endl;
	std::cout << std::endl;
   /*std::cout<<"This program is free software; you can redistribute it and/or"<<std::endl;
   std::cout<<"modify it under the terms of the GNU General Public License," <<std::endl;
   std::cout<<"version 2, as published by the Free Software Foundation; You"<<std::endl;
   std::cout<<"should have received a copy of the GNU General Public License"<<std::endl;
   std::cout<<"along with this program; if not, write to the Free Software" <<std::endl;
   std::cout<<"Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."<<std::endl;
   std::cout << std::endl << std::endl;*/

#ifdef NOAUDIO
	std::cout << "compiled without audio support." << std::endl;
#endif
#ifdef NOTTF
	std::cout << "compiled without ttf support." << std::endl;
#endif
#ifdef NOCURL
	std::cout << "compiled without online hiscore support." << std::endl;
#endif


	//Tweak::tick();

	std::cout << "Starting video subsys. " << std::endl;
	if(SDL_Init(SDL_INIT_VIDEO)==-1) {
		std::cerr <<"SDL_Init error SDL_INIT_VIDEO" << std::endl;
		return EXIT_FAILURE;
	}
	atexit(SDL_Quit);

	std::cout << "Setting video mode. " << std::endl;
	if (!setmode(w,h,fullscreen)) {
		std::cerr <<"SDL_SetVideoMode error" << std::endl;
		return EXIT_FAILURE;
	}

	std::cout << "Starting audio subsys. " << std::endl;
	if (audio) {
		if (!Audio::init()) {
			std::cerr << "  Error in Mix_OpenAudio" << std::endl;
		}
	} else { Audio::off(); }


	std::cout << "Loading resources: " << std::endl;

	load_resources(dir);

	std::cout << "Testing speed: " << std::endl;
	if (!testspeed()) { return EXIT_SUCCESS; }


	std::cout << "Uploading Models: " << std::endl;
	models::robot::gen_dl();	
	//models::robot2::gen_dl();	
	models::key::gen_dl();	
	models::skip::gen_dl();	
	models::weapon::gen_dl();	
	models::pill::gen_dl();	
	Skybox::gen_dl();
	error();

	Audio::play(AS_TITLE,AC_TITLE);
	light();

	Random::sseed();

	Game *game=NULL;
	Menu menu(C3(245,140));


	menu.add(Menuitem("Start Easy",M_START_EASY));
	menu.add(Menuitem("Start Medium",M_START_MEDIUM));
	menu.add(Menuitem("Start Hard",M_START_HARD));
	menu.add(Menuitem("Start Nightmare",M_START_NIGHTMARE));
	menu.add(Menuitem("Quit",M_QUIT));

   SDL_Event event;
	int timestamp,oldtimestamp=0;

	bool handled;

	unsigned int unhandled=0;
	unsigned int showunhandled=0;

	unsigned int delay;
	unsigned int record=0;


	Tips::roll();
   while (true) {

     	while  (SDL_PollEvent(&event)) {
			handled=false;
			if (game) { 
				handled=game->handle_event(event); 
			} else {
				e_menuids id=(e_menuids)menu.handle_event(event); 
				unhandled=6;
				switch (id) {
					case M_START_EASY: 
						game=new Game(NULL,D_EASY); 
						menu.reset();
						break;
					case M_START_MEDIUM: 
						game=new Game(NULL,D_MEDIUM); 
						menu.reset();
						break;
					case M_START_HARD: 
						game=new Game(NULL,D_HARD); 
						menu.reset();
						break;
					case M_START_NIGHTMARE: 
						game=new Game(NULL,D_NIGHTMARE); 
						menu.reset();
						break;
					case M_QUIT:
						SDL_Quit();
						return EXIT_SUCCESS;
						break;
					default: break;
				}
			}
			if (!handled) switch (event.type) {
					case SDL_VIDEORESIZE:
						setmode(event.resize.w,event.resize.h,false);
						break;
      			case SDL_QUIT: return EXIT_SUCCESS; break;

					case SDL_KEYDOWN: switch (event.key.keysym.sym) {
						case SDLK_w:
						case SDLK_a:
						case SDLK_s:
						case SDLK_d:
						case SDLK_LEFT:
						case SDLK_RIGHT:
						case SDLK_UP:
						case SDLK_DOWN:
						case SDLK_RETURN:
						case SDLK_ESCAPE:
						case SDLK_p:
						case SDLK_SPACE: break;

						case SDLK_F1:
						case SDLK_h:
							if (showunhandled>0) {showunhandled=0; break; }
							unhandled=6; break;

						case SDLK_r: Config::toggle_mouse_reverse(); break;
						case SDLK_g: Config::toggle_show_gun(); break;
						case SDLK_m: togglemouselock(); break;
						case SDLK_KP_ENTER:
						case SDLK_f: SDL_WM_ToggleFullScreen(screen); break;
						case SDLK_F10: screenshot(screen->w,screen->h); break;
						case SDLK_F11: 
							Config::toggle_record();
							record=0;
							break;
						case SDLK_v: Audio::toggle(); break;
						case SDLK_t: Tips::cycle(); break;
						default: 
							//unhandled key
							unhandled++;
							break;
					}
			}
		}
		//if (Random::sget(50)==10) Tweak::tick();


		glLoadIdentity();
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		if (game) {
			game->handle_state();


			int diff;
			timestamp=SDL_GetTicks();
			diff=timestamp-oldtimestamp;
			oldtimestamp=timestamp;



			game->tick(diff);


			game->draw();
			E_GameState state=game->get_state();
			if (state!=GS_PLAYING&&state!=GS_FINISHED) {
				if (state==GS_NEXTLEVEL) {
					Game *newgame=new Game(game);
					delete game;
					game = newgame;
				} else {
					delete game;
					game=NULL;
					Tips::roll();
				}
			}
			if (unhandled>5) {
				showunhandled=1000;
				unhandled=0;
			}

			if (showunhandled>0) {
				showunhandled--;
				ortho2d();
				glDisable(GL_LIGHTING);
				Help::controls();
				Help::gameplay();
				glEnable(GL_LIGHTING);
				ortho2d_off();
			}
			delay=0;
		} else {
			if (menu.isactive()==false) { SDL_Quit(); return EXIT_SUCCESS; }
			ortho2d();
			glDisable(GL_LIGHTING);
			menu_bg();
			glEnable(GL_LIGHTING);
			ortho2d_off();

			glPushMatrix();
			glTranslatef(-2.0f,2.0f,-5.0f);
			models::robot::dldraw();
			glPopMatrix();
			glPushMatrix();
			glTranslatef(2.0f,2.0f,-5.0f);
			models::robot::dldraw();
			glPopMatrix();

			ortho2d();
			glDisable(GL_LIGHTING);

			Tips::draw();


			Help::title();
			menu.draw();


			Help::gameplay();
			Help::controls();
			glEnable(GL_LIGHTING);
			ortho2d_off();
			delay=20;
		}


		SDL_GL_SwapBuffers();
		if (Config::record()) {
			record++;
			screenshot(screen->w,screen->h,record);
		}

		SDL_Delay(delay);
		Audio::tick();
		error();

	}
}

} // namespace

int main(int argc,char *argv[]) {
	//unsigned int w=640; unsigned int h=480;
	unsigned int w=800; unsigned int h=600;
	bool fullscreen=false;
	bool offline=false;
	bool audio=true;
	std::string dir="";
	std::string name="";
	int league=0;

	std::istringstream istr;
	if (argc>1) {
		for (int i=1;i<argc;i++) {
			std::string arg(argv[i]);
			if (arg=="-offline") {
				offline=true;
				continue;
			} 
			if (arg=="-fullscreen") {
				fullscreen=true;
				continue;
			} 
			if (arg=="-noaudio") {
				audio=false;
				continue;
			} 
			if (arg=="-name") {
				if (i==argc-1) {
					std::cerr << "Argument missing for -name " << std::endl;
					return EXIT_FAILURE;
				}
				istr.clear();
				istr.str(argv[i+1]);
				istr >> name;
				i++;
				continue;
			} 
			if (arg=="-league") {
				if (i==argc-1) {
					std::cerr << "Argument missing for -league " << std::endl;
					return EXIT_FAILURE;
				}
				istr.clear();
				istr.str(argv[i+1]);
				istr >> league;
				i++;
				continue;
			} 
			if (arg=="-width") {
				if (i==argc-1) {
					std::cerr << "Argument missing for -width " << std::endl;
					return EXIT_FAILURE;
				}
				istr.clear();
				istr.str(argv[i+1]);
				istr >> w;
				i++;
				continue;
			} 
			if (arg=="-height") {
				if (i==argc-1) {
					std::cerr << "Argument missing for -height " << std::endl;
					return EXIT_FAILURE;
				}
				istr.clear();
				istr.str(argv[i+1]);
				istr >> h;
				i++;
				continue;
			} 
			if (arg=="-dir") {
				if (i==argc-1) {
					std::cerr << "Argument missing for -dir " << std::endl;
					return EXIT_FAILURE;
				}
				dir=argv[i+1];
				i++;
				continue;
			}
			std::cerr << "Usage: " << argv[0] << " [ -width xx ] [ -height xx ] [ -fullscreen ] [ -offline ] [ -noaudio ] [ -name name ] [ -league id ] [ -dir datadir ]" << std::endl;
			if (arg!="-help") {
				std::cerr << "Unrecognized argument " << arg << std::endl;
				return EXIT_FAILURE;
			}
			return EXIT_SUCCESS;
		}
	}

	PRJID::Config::set_league(league);
	PRJID::Config::set_name(name);
	PRJID::Config::set_offline(offline);

	int r;
	r=PRJID::main(w,h,fullscreen,audio,dir);

	if (r==EXIT_SUCCESS) {
		std::cout << std::endl << "   Thank you for playing " << FULLNAME << "." << std::endl;
	}
	return r;
}