1 #ifdef _WIN32
2 #include "windows.h"
3 #else
4 #include <stddef.h>
5 #include <sys/types.h>
6 #include <dirent.h>
7 #endif
8 
9 #include "string.h"
10 #include "stdio.h"
11 #include "math.h"
12 #include "ctype.h"
13 
14 #include "GL/gl.h"
15 #include "GL/glu.h"
16 #include "GL/glut.h"
17 #include "SDL/SDL.h"
18 #include "SDL/SDL_mixer.h"
19 
20 #include "list.h"
21 #include "vector.h"
22 #include "cmc.h"
23 #include "3dobject.h"
24 #include "shadow3dobject.h"
25 #include "piece3dobject.h"
26 #include "myglutaux.h"
27 
28 #include "glprintf.h"
29 
30 #ifndef __WINDOWS__
strupr(char * in)31 char *strupr(char *in)
32  {
33     static char buffer[1024];
34     char *c;
35 
36     for (c = buffer;*in != 0;c++, in++)
37      {
38 	*c = toupper(*in);
39      }
40     *c = 0;
41 
42     return(buffer);
43  }
44 #endif
45 
46 extern int SCREEN_X;
47 extern int SCREEN_Y;
48 extern int COLOUR_DEPTH;
49 extern bool fullscreen;
50 extern int shadows;
51 extern int detaillevel;
52 extern bool sound;
53 extern int level;
54 extern int up_key,down_key,left_key,right_key,fire_key,pause_key;
55 extern char mapname[128];
56 extern bool show_radar;
57 List<char> mapnames;
58 
59 extern int mainmenu_status;
60 extern int mainmenu_substatus;
61 extern C3DObject *nethertittle;
62 unsigned char old_keyboard[SDLK_LAST];
63 
64 void save_configuration(void);
65 void load_configuration(void);
66 
67 
mainmenu_cycle(int width,int height)68 int mainmenu_cycle(int width,int height)
69 {
70 	int i;
71 	int retval=0;
72 	unsigned char *keyboard;
73 
74 	SDL_PumpEvents();
75 	keyboard = SDL_GetKeyState(NULL);
76 
77 	switch(mainmenu_status) {
78 	case 0:
79 		mainmenu_substatus++;
80 		if (mainmenu_substatus>=40) {
81 			mainmenu_substatus=0;
82 			mainmenu_status=1;
83 		} /* if */
84 		break;
85 	case 1:
86 		mainmenu_substatus++;
87 		if ((keyboard[fire_key] && !old_keyboard[fire_key]) ||
88 			(keyboard[SDLK_1] && !old_keyboard[SDLK_1])) {
89 			mainmenu_status=4;
90 			mainmenu_substatus=0;
91 		} /* if */
92 		if (keyboard[SDLK_2] && !old_keyboard[SDLK_2]) {
93 			mainmenu_status=6;
94 			mainmenu_substatus=0;
95 		} /* if */
96 		if (keyboard[SDLK_3] && !old_keyboard[SDLK_3]) {
97 			mainmenu_status=2;
98 			mainmenu_substatus=0;
99 		} /* if */
100 		if (keyboard[SDLK_4] && !old_keyboard[SDLK_4]) {
101 			/* Change the MAP: */
102 			if (mapnames.EmptyP()) {
103 				/* Fill the mapnames list: */
104 #ifdef _WIN32
105 				WIN32_FIND_DATA finfo;
106 				HANDLE h;
107 
108 				h=FindFirstFile("/usr/local/share/netherearth/maps/*.map",&finfo);
109 				if (h!=INVALID_HANDLE_VALUE) {
110 					if (strcmp(finfo.cFileName,".")!=0 &&
111 						strcmp(finfo.cFileName,"..")!=0) {
112 						char *name;
113 						name=new char[strlen(finfo.cFileName)+1];
114 						strcpy(name,finfo.cFileName);
115 						mapnames.Add(name);
116 					} /* if */
117 
118 					while(FindNextFile(h,&finfo)==TRUE) {
119 
120 						if (strcmp(finfo.cFileName,".")!=0 &&
121 							strcmp(finfo.cFileName,"..")!=0) {
122 							char *name;
123 							name=new char[strlen(finfo.cFileName)+1];
124 							strcpy(name,finfo.cFileName);
125 							mapnames.Add(name);
126 						} /* if */
127 					} /* while */
128 				} /* if */
129 
130 #else
131 				DIR *dp;
132 				struct dirent *ep;
133 
134 				dp = opendir ("/usr/local/share/netherearth/maps/");
135 				if (dp != NULL)
136 				 {
137 				    while (ep = readdir (dp))
138 				     {
139 					if ((strstr(ep->d_name,".map") + 4) == ep->d_name + strlen(ep->d_name))
140 					 {
141 						char *name;
142 						name=new char[strlen(ep->d_name)+1];
143 						strcpy(name,ep->d_name);
144 						mapnames.Add(name);
145 					 }
146 				     }
147 				    (void) closedir (dp);
148 				 }
149 #endif
150 				/* Look for the actualmap: */
151 				mapnames.Rewind();
152 				while(!mapnames.EndP() && strcmp(mapnames.GetObj(),mapname)!=0) mapnames.Next();
153 
154 			} /* if */
155 
156 			if (!mapnames.EmptyP()) {
157 				mapnames.Next();
158 				if (mapnames.EndP()) mapnames.Rewind();
159 				strcpy(mapname,mapnames.GetObj());
160 			} /* if */
161 
162 			save_configuration();
163 		} /* if */
164 		if (keyboard[SDLK_5] && !old_keyboard[SDLK_5]) {
165 			mainmenu_status=5;
166 			mainmenu_substatus=0;
167 		} /* if */
168 		break;
169 	case 2:
170 		mainmenu_substatus++;
171 		if (mainmenu_substatus>=40) {
172 			mainmenu_substatus=0;
173 			mainmenu_status=3;
174 		} /* if */
175 		break;
176 	case 3:
177 		if (keyboard[SDLK_1] && !old_keyboard[SDLK_1]) {
178 			switch(SCREEN_X) {
179 			case 320:
180 				SCREEN_X=400;
181 				SCREEN_Y=300;
182 				break;
183 			case 400:
184 				SCREEN_X=640;
185 				SCREEN_Y=480;
186 				break;
187 			case 640:
188 				SCREEN_X=800;
189 				SCREEN_Y=600;
190 				break;
191 			case 800:
192 				SCREEN_X=1024;
193 				SCREEN_Y=768;
194 				break;
195 			case 1024:
196 				SCREEN_X=320;
197 				SCREEN_Y=240;
198 				break;
199 			default:
200 				SCREEN_X=640;
201 				SCREEN_Y=480;
202 			} /* switch */
203 			retval=3;
204 			save_configuration();
205 		} /* if */
206 		if (keyboard[SDLK_2] && !old_keyboard[SDLK_2]) {
207 			switch(COLOUR_DEPTH) {
208 			case 8:COLOUR_DEPTH=16;
209 				break;
210 			case 16:COLOUR_DEPTH=24;
211 				break;
212 			case 24:COLOUR_DEPTH=32;
213 				break;
214 			default:
215 				COLOUR_DEPTH=8;
216 			} /* switch */
217 			retval=3;
218 			save_configuration();
219 		} /* if */
220 		if (keyboard[SDLK_3] && !old_keyboard[SDLK_3]) {
221 			if (fullscreen) fullscreen=false;
222 					   else fullscreen=true;
223 			retval=3;
224 			save_configuration();
225 		} /* if */
226 		if (keyboard[SDLK_4] && !old_keyboard[SDLK_4]) {
227 			shadows++;
228 			if (shadows>=3) shadows=0;
229 			save_configuration();
230 		} /* if */
231 		if (keyboard[SDLK_5] && !old_keyboard[SDLK_5]) {
232 			detaillevel++;
233 			if (detaillevel>=5) detaillevel=0;
234 			save_configuration();
235 		} /* if */
236 		if (keyboard[SDLK_6] && !old_keyboard[SDLK_6]) {
237 			if (sound) sound=false;
238 				  else sound=true;
239 			save_configuration();
240 		} /* if */
241 		if (keyboard[SDLK_7] && !old_keyboard[SDLK_7]) {
242 			level++;
243 			if (level>=4) level=0;
244 			save_configuration();
245 		} /* if */
246 		if (keyboard[SDLK_8] && !old_keyboard[SDLK_8]) {
247 			if (show_radar) show_radar=false;
248 					   else show_radar=true;
249 			save_configuration();
250 		} /* if */
251 		if (keyboard[SDLK_9] && !old_keyboard[SDLK_9]) {
252 			mainmenu_status=0;
253 			mainmenu_substatus=0;
254 		} /* if */
255 		break;
256 	case 4:
257 		mainmenu_substatus++;
258 		if (mainmenu_substatus>=40) {
259 			retval=1;
260 		} /* if */
261 		break;
262 	case 5:
263 		mainmenu_substatus++;
264 		if (mainmenu_substatus>=40) {
265 			retval=2;
266 		} /* if */
267 		break;
268 	case 6:
269 		mainmenu_substatus++;
270 		if (mainmenu_substatus>=40) {
271 			mainmenu_status=7;
272 			mainmenu_substatus=0;
273 		} /* if */
274 		break;
275 	case 7:
276 		{
277 			int i;
278 
279 			for(i=0;i<SDLK_LAST;i++) {
280 				if (keyboard[i] && !old_keyboard[i]) {
281 					switch(mainmenu_substatus) {
282 					case 0:up_key=i;
283 						   break;
284 					case 1:down_key=i;
285 						   break;
286 					case 2:left_key=i;
287 						   break;
288 					case 3:right_key=i;
289 						   break;
290 					case 4:fire_key=i;
291 						   break;
292 					case 5:pause_key=i;
293 						   break;
294 					} /* sritch */
295 					mainmenu_substatus++;
296 					if (mainmenu_substatus==7) {
297 						mainmenu_status=0;
298 						mainmenu_substatus=0;
299 						save_configuration();
300 					} /* if */
301 				} /* if */
302 			} /* for */
303 		}
304 		break;
305 	} /* if */
306 
307 	for(i=0;i<SDLK_LAST;i++) old_keyboard[i]=keyboard[i];
308 
309 	return retval;
310 } /* mainmenu_cycle */
311 
312 
mainmenu_draw(int width,int height)313 void mainmenu_draw(int width,int height)
314 {
315 	float lightpos2[4]={0,0,1000,0};
316 	float tmpls[4]={1.0F,1.0F,1.0F,1.0};
317 	float tmpld[4]={0.6F,0.6F,0.6F,1.0};
318 	float tmpla[4]={0.2F,0.2F,0.2F,1.0};
319     float ratio;
320 
321 	if (nethertittle==0) {
322 		nethertittle=new C3DObject("/usr/local/share/netherearth/models/tittle.asc","/usr/local/share/netherearth/textures/");
323 		nethertittle->normalize(7.0);
324 	} /* if */
325 
326 	/* Enable Lights, etc.: */
327 	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
328 	glEnable(GL_LIGHT0);
329 	glLightfv(GL_LIGHT0,GL_AMBIENT,tmpla);
330 	glLightfv(GL_LIGHT0,GL_DIFFUSE,tmpld);
331 	glLightfv(GL_LIGHT0,GL_SPECULAR,tmpls);
332 	glEnable(GL_LIGHTING);
333 	glEnable(GL_COLOR_MATERIAL);
334 	glShadeModel( GL_SMOOTH );
335 	glCullFace( GL_BACK );
336 	glFrontFace( GL_CCW );
337     glEnable( GL_CULL_FACE );
338 	glDisable( GL_SCISSOR_TEST );
339 	glEnable( GL_DEPTH_TEST );
340 
341 	glLightfv(GL_LIGHT0,GL_POSITION,lightpos2);
342     glClearColor(0,0,0,0.0);
343     glViewport(0,0,width,height);
344 	ratio=(float)width/float(height);
345     glMatrixMode( GL_PROJECTION );
346     glLoadIdentity( );
347     gluPerspective( 30.0, ratio, 1.0, 1024.0 );
348 	gluLookAt(0,0,30,0,0,0,0,1,0);
349 
350     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
351 
352     glMatrixMode(GL_MODELVIEW);
353     glLoadIdentity();
354 
355 	switch(mainmenu_status) {
356 	case 0:
357 		glTranslatef(0,3,40-mainmenu_substatus);
358 		nethertittle->draw(1.0,1.0,1.0);
359 		break;
360 	case 1:
361 		glTranslatef(0,3,0);
362 		glPushMatrix();
363 		glRotatef(sin(mainmenu_substatus*0.02)*5.0f,0,1,0);
364 		nethertittle->draw(1.0,1.0,1.0);
365 		glPopMatrix();
366 		glColor3f(0.5,0.5,1.0);
367 		glTranslatef(-6,-6,0);
368 		scaledglprintf2(0.005,0.005,"1 - START NEW GAME   ");
369 		glTranslatef(0,-1,0);
370 		scaledglprintf2(0.005,0.005,"2 - REDEFINE KEYBOARD");
371 		glTranslatef(0,-1,0);
372 		scaledglprintf2(0.005,0.005,"3 - OPTIONS          ");
373 		glTranslatef(0,-1,0);
374 		scaledglprintf2(0.005,0.005,"4 - MAP: %s",mapname);
375 		glTranslatef(0,-1,0);
376 		scaledglprintf2(0.005,0.005,"5 - EXIT GAME        ");
377 		break;
378 	case 2:
379 	case 4:
380 	case 5:
381 	case 6:
382 		glTranslatef(0,3,mainmenu_substatus);
383 		nethertittle->draw(1.0,1.0,1.0);
384 		break;
385 	case 3:
386 		glColor3f(0.5,0.5,1.0);
387 		glTranslatef(0,3.5,0);
388 		if (SCREEN_X== 320) scaledglprintf(0.005,0.005,"1 - RESOLUTION:  320x240");
389 		if (SCREEN_X== 400) scaledglprintf(0.005,0.005,"1 - RESOLUTION:  400x300");
390 		if (SCREEN_X== 640) scaledglprintf(0.005,0.005,"1 - RESOLUTION:  640x480");
391 		if (SCREEN_X== 800) scaledglprintf(0.005,0.005,"1 - RESOLUTION:  800x600");
392 		if (SCREEN_X==1024) scaledglprintf(0.005,0.005,"1 - RESOLUTION: 1024x768");
393 		glTranslatef(0,-1,0);
394 		if (COLOUR_DEPTH== 8) scaledglprintf(0.005,0.005,"2 - COLOR DEPTH:  8bit  ");
395 		if (COLOUR_DEPTH==16) scaledglprintf(0.005,0.005,"2 - COLOR DEPTH: 16bit  ");
396 		if (COLOUR_DEPTH==24) scaledglprintf(0.005,0.005,"2 - COLOR DEPTH: 24bit  ");
397 		if (COLOUR_DEPTH==32) scaledglprintf(0.005,0.005,"2 - COLOR DEPTH: 32bit  ");
398 		glTranslatef(0,-1,0);
399 		if (fullscreen) scaledglprintf(0.005,0.005,"3 - FULLSCREEN          ");
400 				   else scaledglprintf(0.005,0.005,"3 - WINDOWED            ");
401 		glTranslatef(0,-1,0);
402 		if (shadows==0) scaledglprintf(0.005,0.005,"4 - SHADOWS: OFF        ");
403 		if (shadows==1) scaledglprintf(0.005,0.005,"4 - SHADOWS: ON - DIAG  ");
404 		if (shadows==2) scaledglprintf(0.005,0.005,"4 - SHADOWS: ON - VERT  ");
405 		glTranslatef(0,-1,0);
406 		if (detaillevel==0) scaledglprintf(0.005,0.005,"5 - DETAIL: LOWEST      ");
407 		if (detaillevel==1) scaledglprintf(0.005,0.005,"5 - DETAIL: LOW         ");
408 		if (detaillevel==2) scaledglprintf(0.005,0.005,"5 - DETAIL: MEDIUM      ");
409 		if (detaillevel==3) scaledglprintf(0.005,0.005,"5 - DETAIL: HIGH        ");
410 		if (detaillevel==4) scaledglprintf(0.005,0.005,"5 - DETAIL: HIGHEST     ");
411 		glTranslatef(0,-1,0);
412 		if (sound) scaledglprintf(0.005,0.005,"6 - SOUND: ON           ");
413 			  else scaledglprintf(0.005,0.005,"6 - SOUND: OFF          ");
414 		glTranslatef(0,-1,0);
415 		if (level==0) scaledglprintf(0.005,0.005,"7 - LEVEL: EASY         ");
416 		if (level==1) scaledglprintf(0.005,0.005,"7 - LEVEL: NORMAL       ");
417 		if (level==2) scaledglprintf(0.005,0.005,"7 - LEVEL: HARD         ");
418 		if (level==3) scaledglprintf(0.005,0.005,"7 - LEVEL: IMPOSSIBLE   ");
419 		glTranslatef(0,-1,0);
420 		if (show_radar) scaledglprintf(0.005,0.005,"8 - RADAR: ON           ");
421 				   else scaledglprintf(0.005,0.005,"8 - RADAR: OFF          ");
422 		glTranslatef(0,-1,0);
423 		scaledglprintf(0.005,0.005,"9 - BACK                ");
424 		break;
425 	case 7:
426 		{
427 			char tmp[256];
428 
429 			glColor3f(0.5,0.5,1.0);
430 			glTranslatef(0,5,0);
431 			scaledglprintf(0.005,0.005,"REDEFINE KEYBOARD");
432 			glTranslatef(0,-2,0);
433 			if (mainmenu_substatus!=0) glColor3f(0.5,0.5,1.0);
434 								  else glColor3f(1.0,0.0,0.0);
435 			sprintf(tmp,"PRESS A KEY FOR UP: %s",strupr(SDL_GetKeyName((SDLKey)up_key)));
436 			scaledglprintf(0.005,0.005,tmp);
437 			glTranslatef(0,-1,0);
438 			if (mainmenu_substatus!=1) glColor3f(0.5,0.5,1.0);
439 						 	 	  else glColor3f(1.0,0.0,0.0);
440 			sprintf(tmp,"PRESS A KEY FOR DOWN: %s",strupr(SDL_GetKeyName((SDLKey)down_key)));
441 			scaledglprintf(0.005,0.005,tmp);
442 			glTranslatef(0,-1,0);
443 			if (mainmenu_substatus!=2) glColor3f(0.5,0.5,1.0);
444 								  else glColor3f(1.0,0.0,0.0);
445 			sprintf(tmp,"PRESS A KEY FOR LEFT: %s",strupr(SDL_GetKeyName((SDLKey)left_key)));
446 			scaledglprintf(0.005,0.005,tmp);
447 			glTranslatef(0,-1,0);
448 			if (mainmenu_substatus!=3) glColor3f(0.5,0.5,1.0);
449 								  else glColor3f(1.0,0.0,0.0);
450 			sprintf(tmp,"PRESS A KEY FOR RIGHT: %s",strupr(SDL_GetKeyName((SDLKey)right_key)));
451 			scaledglprintf(0.005,0.005,tmp);
452 			glTranslatef(0,-1,0);
453 			if (mainmenu_substatus!=4) glColor3f(0.5,0.5,1.0);
454 								  else glColor3f(1.0,0.0,0.0);
455 			sprintf(tmp,"PRESS A KEY FOR FIRE: %s",strupr(SDL_GetKeyName((SDLKey)fire_key)));
456 			scaledglprintf(0.005,0.005,tmp);
457 
458 			glTranslatef(0,-1,0);
459 			if (mainmenu_substatus!=5) glColor3f(0.5,0.5,1.0);
460 								  else glColor3f(1.0,0.0,0.0);
461 			sprintf(tmp,"PRESS A KEY FOR PAUSE/MENU: %s",strupr(SDL_GetKeyName((SDLKey)pause_key)));
462 			scaledglprintf(0.005,0.005,tmp);
463 
464 			glColor3f(0.5,0.5,1.0);
465 			glTranslatef(0,-2,0);
466 			scaledglprintf(0.005,0.005,"PG.UP/PG.DOWN CHANGE THE ZOOM");
467 
468 			if (mainmenu_substatus>5) {
469 				glColor3f(1,1,1);
470 				glTranslatef(0,-2,0);
471 				scaledglprintf(0.005,0.005,"PRESS ANY KEY TO RETURN TO MAIN MENU");
472 			} /* if */
473 		}
474 		break;
475 	} /* switch */
476 
477 	SDL_GL_SwapBuffers();
478 } /* NETHER::draw */
479 
480 
481 
load_configuration(void)482 void load_configuration(void)
483 {
484 	int v;
485 	FILE *fp;
486 
487 	fp=fopen("nether.cfg","r");
488 	if (fp==0) return;
489 
490 	if (2!=fscanf(fp,"%i %i",&SCREEN_X,&SCREEN_Y)) return;
491 	if (1!=fscanf(fp,"%i",&v)) return;
492 	if (v==0) fullscreen=true;
493 		 else fullscreen=false;
494 	if (1!=fscanf(fp,"%i",&shadows)) return;
495 	if (1!=fscanf(fp,"%i",&detaillevel)) return;
496 
497 	if (6!=fscanf(fp,"%i %i %i %i %i %i",&up_key,&down_key,&left_key,&right_key,&fire_key,&pause_key)) return;
498 	if (1!=fscanf(fp,"%i",&v)) return;
499 	if (v==0) sound=true;
500 		 else sound=false;
501 
502 	if (1!=fscanf(fp,"%i",&level)) return;
503 
504 	if (1!=fscanf(fp,"%s",mapname)) return;
505 
506 	fclose(fp);
507 } /* load_configuration */
508 
509 
save_configuration(void)510 void save_configuration(void)
511 {
512 	FILE *fp;
513 
514 	fp=fopen("nether.cfg","w");
515 	if (fp==0) return;
516 
517 	fprintf(fp,"%i %i\n",SCREEN_X,SCREEN_Y);
518 	fprintf(fp,"%i %i %i\n",(fullscreen ? 0 : 1),shadows,detaillevel);
519 	fprintf(fp,"%i %i %i %i %i %i\n",up_key,down_key,left_key,right_key,fire_key,pause_key);
520 	fprintf(fp,"%i\n",(sound ? 0 : 1));
521 	fprintf(fp,"%i\n",level);
522 	fprintf(fp,"%s\n",mapname);
523 
524 	fclose(fp);
525 } /* save_configuration */
526