1 #include <SDL.h>
2 #include "SoundSystem.h"
3 
4 #include "UtilityClass.h"
5 #include "Game.h"
6 #include "Graphics.h"
7 #include "KeyPoll.h"
8 #include "titlerender.h"
9 
10 #include "Tower.h"
11 #include "WarpClass.h"
12 #include "Labclass.h"
13 #include "Finalclass.h"
14 #include "Map.h"
15 
16 #include "Screen.h"
17 
18 #include "Script.h"
19 
20 #include "Logic.h"
21 
22 #include "Input.h"
23 #include "editor.h"
24 #include "preloader.h"
25 
26 #include "FileSystemUtils.h"
27 #include "Network.h"
28 
29 #include <stdio.h>
30 #include <string.h>
31 
32 scriptclass script;
33  edentities edentity[3000];
34 
35  editorclass ed;
36 
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39     if(!FILESYSTEM_init(argv[0]))
40     {
41         return 1;
42     }
43     SDL_Init(
44         SDL_INIT_VIDEO |
45         SDL_INIT_AUDIO |
46         SDL_INIT_JOYSTICK |
47         SDL_INIT_GAMECONTROLLER
48     );
49 
50     if (argc > 2 && strcmp(argv[1], "-renderer") == 0)
51     {
52         SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, argv[2], SDL_HINT_OVERRIDE);
53     }
54 
55     NETWORK_init();
56 
57     Screen gameScreen;
58 
59 	printf("\t\t\n");
60 	printf("\t\t\n");
61 	printf("\t\t       VVVVVV\n");
62 	printf("\t\t\n");
63 	printf("\t\t\n");
64 	printf("\t\t  8888888888888888  \n");
65 	printf("\t\t88888888888888888888\n");
66 	printf("\t\t888888    8888    88\n");
67 	printf("\t\t888888    8888    88\n");
68 	printf("\t\t88888888888888888888\n");
69 	printf("\t\t88888888888888888888\n");
70 	printf("\t\t888888            88\n");
71 	printf("\t\t88888888        8888\n");
72 	printf("\t\t  8888888888888888  \n");
73 	printf("\t\t      88888888      \n");
74 	printf("\t\t  8888888888888888  \n");
75 	printf("\t\t88888888888888888888\n");
76 	printf("\t\t88888888888888888888\n");
77 	printf("\t\t88888888888888888888\n");
78 	printf("\t\t8888  88888888  8888\n");
79 	printf("\t\t8888  88888888  8888\n");
80 	printf("\t\t    888888888888    \n");
81 	printf("\t\t    8888    8888    \n");
82 	printf("\t\t  888888    888888  \n");
83 	printf("\t\t  888888    888888  \n");
84 	printf("\t\t  888888    888888  \n");
85 	printf("\t\t\n");
86 	printf("\t\t\n");
87 
88     //Set up screen
89 
90 
91 
92 
93     UtilityClass help;
94     // Load Ini
95 
96 
97     Graphics graphics;
98 
99 
100 
101     musicclass music;
102     Game game;
103     game.infocus = true;
104 
105     graphics.MakeTileArray();
106     graphics.MakeSpriteArray();
107     graphics.maketelearray();
108 
109 
110     graphics.images.push_back(graphics.grphx.im_image0);
111     graphics.images.push_back(graphics.grphx.im_image1);
112     graphics.images.push_back(graphics.grphx.im_image2);
113     graphics.images.push_back(graphics.grphx.im_image3);
114     graphics.images.push_back(graphics.grphx.im_image4);
115     graphics.images.push_back(graphics.grphx.im_image5);
116     graphics.images.push_back(graphics.grphx.im_image6);
117 
118     graphics.images.push_back(graphics.grphx.im_image7);
119     graphics.images.push_back(graphics.grphx.im_image8);
120     graphics.images.push_back(graphics.grphx.im_image9);
121     graphics.images.push_back(graphics.grphx.im_image10);
122     graphics.images.push_back(graphics.grphx.im_image11);
123     graphics.images.push_back(graphics.grphx.im_image12);
124 
125     const SDL_PixelFormat* fmt = gameScreen.GetFormat();
126     graphics.backBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE ,320 ,240 ,32,fmt->Rmask,fmt->Gmask,fmt->Bmask,fmt->Amask ) ;
127     SDL_SetSurfaceBlendMode(graphics.backBuffer, SDL_BLENDMODE_NONE);
128     graphics.Makebfont();
129 
130 
131     graphics.forgroundBuffer =  SDL_CreateRGBSurface(SDL_SWSURFACE ,320 ,240 ,fmt->BitsPerPixel,fmt->Rmask,fmt->Gmask,fmt->Bmask,fmt->Amask  );
132     SDL_SetSurfaceBlendMode(graphics.forgroundBuffer, SDL_BLENDMODE_NONE);
133 
134     graphics.screenbuffer = &gameScreen;
135 
136     graphics.menubuffer = SDL_CreateRGBSurface(SDL_SWSURFACE ,320 ,240 ,fmt->BitsPerPixel,fmt->Rmask,fmt->Gmask,fmt->Bmask,fmt->Amask );
137     SDL_SetSurfaceBlendMode(graphics.menubuffer, SDL_BLENDMODE_NONE);
138 
139     graphics.towerbuffer =  SDL_CreateRGBSurface(SDL_SWSURFACE ,320 ,240 ,fmt->BitsPerPixel,fmt->Rmask,fmt->Gmask,fmt->Bmask,fmt->Amask  );
140     SDL_SetSurfaceBlendMode(graphics.towerbuffer, SDL_BLENDMODE_NONE);
141 
142 	graphics.tempBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE ,320 ,240 ,fmt->BitsPerPixel,fmt->Rmask,fmt->Gmask,fmt->Bmask,fmt->Amask  );
143     SDL_SetSurfaceBlendMode(graphics.tempBuffer, SDL_BLENDMODE_NONE);
144 
145     //Make a temporary rectangle to hold the offsets
146     // SDL_Rect offset;
147     //Give the offsets to the rectangle
148     // offset.x = 60;
149     // offset.y = 80;
150 
151     //game.gamestate = TITLEMODE;
152     //game.gamestate=EDITORMODE;
153     game.gamestate = PRELOADER; //Remember to uncomment this later!
154 
155     game.menustart = false;
156     game.mainmenu = 0;
157 
158     KeyPoll key;
159     mapclass map;
160 
161     map.ypos = (700-29) * 8;
162     map.bypos = map.ypos / 2;
163 
164     //Moved screensetting init here from main menu V2.1
165     game.loadstats(map, graphics);
166 		if(game.usingmmmmmm==0) music.usingmmmmmm=false;
167 		if(game.usingmmmmmm==1) music.usingmmmmmm=true;
168     if (game.slowdown == 0) game.slowdown = 30;
169 
170     switch(game.slowdown){
171       case 30: game.gameframerate=34; break;
172       case 24: game.gameframerate=41; break;
173       case 18: game.gameframerate=55; break;
174       case 12: game.gameframerate=83; break;
175       default: game.gameframerate=34; break;
176     }
177 
178 		//Check to see if you've already unlocked some achievements here from before the update
179 		if (game.swnbestrank > 0){
180 		  if(game.swnbestrank >= 1) NETWORK_unlockAchievement("vvvvvvsupgrav5");
181 			if(game.swnbestrank >= 2) NETWORK_unlockAchievement("vvvvvvsupgrav10");
182 			if(game.swnbestrank >= 3) NETWORK_unlockAchievement("vvvvvvsupgrav15");
183 			if(game.swnbestrank >= 4) NETWORK_unlockAchievement("vvvvvvsupgrav20");
184 			if(game.swnbestrank >= 5) NETWORK_unlockAchievement("vvvvvvsupgrav30");
185 			if(game.swnbestrank >= 6) NETWORK_unlockAchievement("vvvvvvsupgrav60");
186 		}
187 
188 		if(game.unlock[5]) NETWORK_unlockAchievement("vvvvvvgamecomplete");
189 		if(game.unlock[19]) NETWORK_unlockAchievement("vvvvvvgamecompleteflip");
190 		if(game.unlock[20]) NETWORK_unlockAchievement("vvvvvvmaster");
191 
192 		if (game.bestgamedeaths > -1) {
193 			if (game.bestgamedeaths <= 500) {
194 				NETWORK_unlockAchievement("vvvvvvcomplete500");
195 			}
196 			if (game.bestgamedeaths <= 250) {
197 				NETWORK_unlockAchievement("vvvvvvcomplete250");
198 			}
199 			if (game.bestgamedeaths <= 100) {
200 				NETWORK_unlockAchievement("vvvvvvcomplete100");
201 			}
202 			if (game.bestgamedeaths <= 50) {
203 				NETWORK_unlockAchievement("vvvvvvcomplete50");
204 			}
205 		}
206 
207 		if(game.bestrank[0]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_station1_fixed");
208 		if(game.bestrank[1]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_lab_fixed");
209 		if(game.bestrank[2]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_tower_fixed");
210 		if(game.bestrank[3]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_station2_fixed");
211 		if(game.bestrank[4]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_warp_fixed");
212 		if(game.bestrank[5]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_final_fixed");
213 
214     entityclass obj;
215     obj.init();
216 
217     //Quick hack to start in final level ---- //Might be useful to leave this commented in for testing
218     /*
219     //game.gamestate=GAMEMODE;
220 		//game.start(obj,music);
221 		//script.startgamemode(8, key, graphics, game, map, obj, help, music);
222    // map.finalmode = true; //Enable final level mode
223 		//map.finalx = 41; map.finaly = 52; //Midpoint
224 		//map.finalstretch = true;
225 		//map.final_colormode = true;
226 		//map.final_mapcol = 0;
227 		//map.final_colorframe = 0;
228 
229 		//game.starttest(obj, music);
230 
231     game.savex = 5 * 8; game.savey = 15 * 8; game.saverx = 41; game.savery = 52;
232     game.savegc = 0; game.savedir = 1;
233     game.state = 0; game.deathseq = -1; game.lifeseq = 10;
234 		//obj.createentity(game, game.savex, game.savey, 0);
235 		map.gotoroom(game.saverx, game.savery, graphics, game, obj, music);
236 		//music.play(1);
237 		*/
238     //End hack here ----
239 
240     volatile Uint32 time, timePrev = 0;
241     game.infocus = true;
242     key.isActive = true;
243 
244     while(!key.quitProgram)
245     {
246 		//gameScreen.ClearScreen(0x00);
247 
248         time = SDL_GetTicks();
249 
250         // Update network per frame.
251         NETWORK_update();
252 
253         //framerate limit to 30
254         Uint32 timetaken = time - timePrev;
255         if(game.gamestate==EDITORMODE)
256 		{
257           if (timetaken < 24)
258           {
259               volatile Uint32 delay = 24 - timetaken;
260               SDL_Delay( delay );
261               time = SDL_GetTicks();
262           }
263           timePrev = time;
264 
265         }else{
266           if (timetaken < game.gameframerate)
267           {
268               volatile Uint32 delay = game.gameframerate - timetaken;
269               SDL_Delay( delay );
270               time = SDL_GetTicks();
271           }
272           timePrev = time;
273 
274         }
275 
276 
277 
278         key.Poll();
279 		if(key.toggleFullscreen)
280 		{
281 			if(!gameScreen.isWindowed)
282 			{
283 				//SDL_WM_GrabInput(SDL_GRAB_ON);
284 				SDL_ShowCursor(SDL_DISABLE);
285 				SDL_ShowCursor(SDL_ENABLE);
286 			}
287 			else
288 			{
289 				SDL_ShowCursor(SDL_ENABLE);
290 			}
291 
292 
293 			if(game.gamestate == EDITORMODE)
294 			{
295 				SDL_ShowCursor(SDL_ENABLE);
296 			}
297 
298 			gameScreen.toggleFullScreen();
299 			game.fullscreen = !game.fullscreen;
300 			key.toggleFullscreen = false;
301 
302 				key.keymap.clear(); //we lost the input due to a new window.
303 				game.press_left = false;
304 				game.press_right = false;
305 				game.press_action = true;
306 				game.press_map = false;
307 			printf("Error: failed: %s\n", SDL_GetError());
308 
309 
310 
311 
312 		}
313 		/*if(key.quitProgram)
314 		{
315 			music.playef(2);
316 		}*/
317 
318         game.infocus = key.isActive;
319         if(!game.infocus)
320         {
321             if(game.getGlobalSoundVol()> 0)
322             {
323                 game.setGlobalSoundVol(0);
324             }
325             FillRect(graphics.backBuffer, 0x00000000);
326             graphics.bprint(5, 110, "Game paused", 196 - help.glow, 255 - help.glow, 196 - help.glow, true);
327             graphics.bprint(5, 120, "[click to resume]", 196 - help.glow, 255 - help.glow, 196 - help.glow, true);
328             graphics.bprint(5, 230, "Press M to mute in game", 164 - help.glow, 196 - help.glow, 164 - help.glow, true);
329             graphics.render();
330             //We are minimised, so lets put a bit of a delay to save CPU
331             SDL_Delay(100);
332         }
333         else
334         {
335             switch(game.gamestate)
336             {
337             case PRELOADER:
338                 //Render
339                 preloaderrender(graphics, game, help);
340                 break;
341             case EDITORMODE:
342 				graphics.flipmode = false;
343                 //Input
344                 editorinput(key, graphics, game, map, obj, help, music);
345                 //Render
346                 editorrender(key, graphics, game, map, obj, help);
347                 ////Logic
348                 editorlogic(key, graphics, game, obj, music, map, help);
349                 break;
350             case TITLEMODE:
351                 //Input
352                 titleinput(key, graphics, map, game, obj, help, music);
353                 //Render
354                 titlerender(graphics, map, game, obj, help, music);
355                 ////Logic
356                 titlelogic(graphics, game, obj, help, music, map);
357                 break;
358             case GAMEMODE:
359                 if (map.towermode)
360                 {
361 					gameinput(key, graphics, game, map, obj, help, music);
362 
363                     //if(game.recording==1)
364                     //{
365                     // ///recordinput(key, graphics, game, map, obj, help, music);
366                     //}
367                     //else
368                     //{
369                     //}
370                     towerrender(graphics, game, map, obj, help);
371                     towerlogic(graphics, game,  obj,  music, map, help);
372 
373                 }
374                 else
375                 {
376 
377                     if (game.recording == 1)
378                     {
379                         //recordinput(key, dwgfx, game, map, obj, help, music);
380                     }
381                     else
382                     {
383                         if (script.running)
384                         {
385                             script.run(key, graphics, game, map, obj, help, music);
386                         }
387 
388                         gameinput(key, graphics, game, map, obj, help, music);
389                         //}
390                         gamerender(graphics,map, game,  obj, help);
391                         gamelogic(graphics, game,obj, music, map,  help);
392 
393 
394                     }
395                     break;
396                 case MAPMODE:
397                     maprender(graphics, game, map, obj, help);
398                     if (game.recording == 1)
399                     {
400                         //recordinput(key, dwgfx, game, map, obj, help, music); //will implement this later if it's actually needed
401                     }
402                     else
403                     {
404                         mapinput(key, graphics, game, map, obj, help, music);
405                     }
406                     maplogic(graphics, game, obj ,music , map, help );
407                     break;
408                 case TELEPORTERMODE:
409                     teleporterrender(graphics, game, map, obj, help);
410                     if (game.recording == 1)
411                     {
412                         //recordinput(key, graphics, game, map, obj, help, music);
413                     }
414                     else
415                     {
416                         if(game.useteleporter)
417                         {
418                             teleporterinput(key, graphics, game, map, obj, help, music);
419                         }
420                         else
421                         {
422                             if (script.running)
423                             {
424                                 script.run(key, graphics, game, map, obj, help, music);
425                             }
426                             gameinput(key, graphics, game, map, obj, help, music);
427                         }
428                     }
429                     maplogic(graphics, game,  obj, music, map, help);
430                     break;
431                 case GAMECOMPLETE:
432                     gamecompleterender(graphics, game, obj, help, map);
433                     //Input
434                     gamecompleteinput(key, graphics, game, map, obj, help, music);
435                     //Logic
436                     gamecompletelogic(graphics, game,  obj, music, map, help);
437                     break;
438                 case GAMECOMPLETE2:
439                     gamecompleterender2(graphics, game, obj, help);
440                     //Input
441                     gamecompleteinput2(key, graphics, game, map, obj, help, music);
442                     //Logic
443                     gamecompletelogic2(graphics, game,  obj, music, map, help);
444                     break;
445                 case CLICKTOSTART:
446 
447                     //dwgfx.bprint(5, 115, "[Click to start]", 196 - help.glow, 196 - help.glow, 255 - help.glow, true);
448                     //dwgfx.drawgui(help);
449                     //dwgfx.render();
450                     //dwgfx.backbuffer.unlock();
451 
452                     help.updateglow();
453                     // if (key.click) {
454                     //  dwgfx.textboxremove();
455                     // }
456                     // if (dwgfx.ntextbox == 0) {
457                     //  //music.play(6);
458                     //  map.ypos = (700-29) * 8;
459                     //  map.bypos = map.ypos / 2;
460                     //  map.cameramode = 0;
461 
462                     //  game.gamestate = TITLEMODE;
463                     // }
464                     break;
465                 default:
466 
467                 break;
468                 }
469 
470             }
471 
472         }
473 
474         if (game.savemystats)
475         {
476             game.savemystats = false;
477             game.savestats(map, graphics);
478         }
479 
480         //Mute button
481         if (key.isDown(KEYBOARD_m) && game.mutebutton<=0 && !ed.textentry)
482         {
483             game.mutebutton = 8;
484             if (game.muted)
485             {
486                 game.muted = false;
487             }
488             else
489             {
490                 game.muted = true;
491             }
492         }
493         if(game.mutebutton>0)
494         {
495             game.mutebutton--;
496         }
497 
498         if (game.muted)
499         {
500             //if (game.globalsound == 1)
501             //{
502                 game.globalsound = 0;
503                 Mix_VolumeMusic(0) ;
504                 Mix_Volume(-1,0);
505             //}
506         }
507 
508         if (!game.muted && game.globalsound == 0)
509         {
510             game.globalsound = 1;
511             Mix_VolumeMusic(MIX_MAX_VOLUME) ;
512             Mix_Volume(-1,MIX_MAX_VOLUME);
513         }
514 
515 		if(key.resetWindow)
516 		{
517 			key.resetWindow = false;
518 			gameScreen.ResizeScreen(-1, -1);
519 		}
520 
521         music.processmusic();
522         graphics.processfade();
523         game.gameclock();
524         gameScreen.FlipScreen();
525 
526         //SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
527     }
528 
529 
530 	  //SDL_Delay(300);
531 
532     //TODO
533     //Free the loaded image
534     //SDL_FreeSurface( gameScreen );
535 
536     //Quit SDL
537     NETWORK_shutdown();
538     SDL_Quit();
539     FILESYSTEM_deinit();
540 
541     return 0;
542 }
543