File: main.cc

package info (click to toggle)
enemylines7 0.6-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,640 kB
  • sloc: cpp: 21,756; makefile: 24
file content (447 lines) | stat: -rw-r--r-- 10,174 bytes parent folder | download | duplicates (3)
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
#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 "config.h"
#include "audio.h"
#include "skybox.h"
#include "block/cube.h"
#include "radio.h"
#include "block/map2.h"
#include "floor.h"

#include "models/displaylists.h"
#include "tweak/tweak.h"

#include "release.h"

namespace PRJID {



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

void load_res(std::string dir) {
	dir=load_audio(dir);
	block::Map2::set_path(dir);
	Radio::instance()->load(dir);
}



bool testspeed() {
	Uint32 start,end,took;

	start=SDL_GetTicks();
	ortho2d();
	glColor3f(1,1,1);

	Font_ogl::write("loading...");
	SDL_GL_SwapBuffers();
	ortho2d_off();
	end=SDL_GetTicks();

	took=(end-start);

	if (took<200) return true;

   SDL_Event event;
   while (true) {
		glLoadIdentity();
     	while  (SDL_PollEvent(&event)) {
			if (event.type==SDL_QUIT) return false;
			if (event.type==SDL_MOUSEBUTTONDOWN) return true;
			if (event.type!=SDL_KEYDOWN) continue;
			if (event.key.keysym.sym==SDLK_ESCAPE) { return false; }
			return true;
		}
		ortho2d();
		glTranslatef(0,20,0);
		Font_ogl::write("  ... system slow");
		glTranslatef(0,20,0);
		Font_ogl::write("  probable cause: no properly configured 3d acceleration.");
		glTranslatef(0,20,0);
		Font_ogl::write("  ESC to quit. any other key: continue anyway.");
		ortho2d_off();
		SDL_GL_SwapBuffers();
		SDL_Delay(150);
	}
	return true;
}

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.10f,405.0f);
   glViewport(0, 0, screen->w, screen->h);
   glMatrixMode(GL_MODELVIEW);

   glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	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) 2006" << std::endl;
   std::cout<<"\t " << EL_URL << std::endl;
   std::cout<<"\t Using libSDL,SDL_mixer,SDL_image under the terms of the GNU LGPL" << std::endl;
	std::cout<<"\t Please consult the README file/website in case of problems/questions." << 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;*/

	//Tweak::tick();


	if(SDL_Init(SDL_INIT_VIDEO)==-1) {
		std::cerr <<"SDL_Init error SDL_INIT_VIDEO" << std::endl;
		return EXIT_FAILURE;
	}
	atexit(SDL_Quit);
	if (!setmode(w,h,fullscreen)) {
		std::cerr <<"SDL_SetVideoMode error" << std::endl;
		return EXIT_FAILURE;
	}


	std::cout << "Uploading Font. " << std::endl;
	Font_ogl::genlist();
	error();

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

	std::cout << "Loading Resources: " << std::endl;
	load_res(dir);


	std::cout << "Uploading Models: " << std::endl;
	block::Cube::gen_dl();
	Skybox::gen_dl();
	Floor::gen_dl();
	models::gen_dl();
	error();

	Audio::play(AS_TITLE,AC_TITLE);

	Random::sseed();

	Game *game=NULL;
	Menu menu(2,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;
	Uint32 timestamp,oldtimestamp=0;
	Uint32 fpsstart=SDL_GetTicks(); 
	unsigned int frames=0;

	bool handled;

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

	unsigned int delay;
	unsigned int record=0;

	


   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_ESCAPE: return EXIT_SUCCESS; break;
						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_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_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;
						default: 
							//unhandled key
							unhandled  ;
							break;
					}
			}
		}
		//if (Random::sget(50)==10) Tweak::tick();
		/*frames  ;
		if (Random::sget(200)==10){
			Uint32 timesince;
			timesince=SDL_GetTicks()-fpsstart;
			
			float spf;
			spf=timesince;
			spf/=frames;
			std::cout << " frames: " << timesince << "  " << frames << "  " << spf << std::endl;

			fpsstart=SDL_GetTicks();
			frames=0;
		} */


		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;
				}
			}
			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();

			ortho2d();
			glDisable(GL_LIGHTING);


			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=800; unsigned int h=600;
	bool fullscreen=false;
	bool audio=true;
	std::string dir="";

	std::istringstream istr;
	if (argc>1) {
		for (int i=1;i<argc;i  ) {
			std::string arg(argv[i]);

			if (arg=="-fullscreen") {
				fullscreen=true;
				continue;
			} 
			if (arg=="-noaudio") {
				audio=false;
				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 ] [ -noaudio ] [ -dir datadir ]" << std::endl;
			if (arg!="-help") {
				std::cerr << "Unrecognized argument " << arg << std::endl;
				return EXIT_FAILURE;
			}
			return EXIT_SUCCESS;
		}
	}

	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;
}