1 /*
2  * Shotgun Debugger
3  * Copyright 2005 Game Creation Society
4  *
5  * Programmed by Matt Sarnoff
6  * http://www.contrib.andrew.cmu.edu/~msarnoff
7  * http://www.gamecreation.org
8  *
9  * main.cpp - where it all begins
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25 
26 #include "sdb.h"
27 
28 bool GFXMODE_ISACTIVE;
29 BitmapFont font;
30 GLUtesselator *tess;
31 Timer timer;
32 int screenshotCount;
33 bool screenshotKeyPressed;
34 
main(int argc,char * argv[])35 int main (int argc, char *argv[])
36 {
37   GFXMODE_ISACTIVE = false;
38   bool playInSequence = true;
39   screenshotCount = 1;
40   screenshotKeyPressed = false;
41 
42   // Redirect standard output on Win32 since we'll never run from a console
43 	#ifdef _WIN32
44 	freopen("stdout.txt", "w", stdout);
45 	#endif
46 
47   // Load configuration, load libraries, and start graphics mode
48   gameWelcomeMessage();
49   srand(time(NULL));
50   config.loadFromFile(CONFIG_FILE);
51   gameInitSDL();
52   gameSetVideoMode(config.xres, config.yres, config.depth, (config.fullscreen) ? SDL_FULLSCREEN : 0);
53   gameInitOpenGL();
54   gameSetAntialiasing(config.antialias);
55   gameInitTessellator();
56 
57   // Load game resources
58   gameLoadSprites();     // loads the sprite textures
59   gameLoadModels();
60 
61   if (config.use_sound)
62   {
63     gameLoadSounds();      // loads the sounds
64     gameLoadMusic();       // loads the music
65   }
66 
67   font.create(TEX_FONT); // loads the system font from texture slot 0
68 
69   // initialize types
70   initObjectTypes();
71   initWeaponTypes();
72   P.loadPlayerData();
73   P.setKeys(config.keys);
74 
75   printf("Initialization complete\n");
76 
77   string levelToPlay = "";
78 
79   if (argc >= 2)
80   {
81     config.setDefaultLevel(argv[1]);
82     levelToPlay = argv[1];
83     playInSequence = false;
84   }
85   else if (config.defaultLevel != "")
86   {
87     levelToPlay = config.defaultLevel;
88     playInSequence = false;
89   }
90 
91   bool loop = true, first = true;
92   int lvl = 0;
93   while (loop)
94   {
95     stopMusic();
96     startMusic(MUS_TITLE);
97 
98     lvl = title(first, playInSequence);
99 
100     if (first) first = false;
101 
102     if (lvl == -1) // Exit
103       loop = false;
104     else if (lvl == -2) // Continue game
105       game(levelToPlay, true);
106     else
107     {
108       if (playInSequence)
109         P.level = P.startingLevel = lvl-1;
110 
111       game(levelToPlay, playInSequence);
112     }
113   }
114   currLevel.unload();
115   gameShutdown();
116 
117   // Save configuration data
118   config.writeToFile(CONFIG_FILE);
119   P.writePlayerData();
120   return 0;
121 }
122 
123 // Starts the sequence of levels at the given starting point.
game(string levelToPlay,bool playInSequence)124 void game(string levelToPlay, bool playInSequence)
125 {
126   string lvl = (playInSequence) ? getLevelFileName(P.level-((P.level == NUM_LEVELS)?1:0)) : levelToPlay;
127 
128   bool loop = true;
129   bool music = false;
130   while (loop)
131   {
132     if (lvl != currLevel.loadedFileName)
133     {
134       currLevel.unload();
135 
136       // Level loading sequence
137       loadingScreen(0);
138       currLevel.defaults();
139       currLevel.readLevelData((char *)lvl.c_str());
140 
141       if (playInSequence)
142       {
143         if (P.level == NUM_LEVELS)
144           currLevel.readLevelIntroText((char *)getLevelIntroFileName(P.level-1).c_str());
145         else
146           currLevel.readLevelIntroText((char *)getLevelIntroFileName(P.level).c_str());
147       }
148 
149       glColor4f(0.3, 1.0, 0.3, 0.8); glBlendFunc(GL_SRC_ALPHA, GL_ONE);
150       loadingScreen(1); // level built
151       currLevel.calculateLighting();
152       loadingScreen(2); // lights done
153       currLevel.calculateGrid();
154       loadingScreen(3); // grid done
155       currLevel.calculateNodeGraph();
156       loadingScreen(4); // nodes done
157       currLevel.makeDisplayList();
158     }
159 
160     if (!music)
161     { stopMusic(); startMusic(MUS_DD2); music = true; }
162 
163     if (playInSequence)
164       levelIntroScreen(false);
165 
166     if (config.mgrab) SDL_WM_GrabInput(SDL_GRAB_ON);
167     int levelOutcome = playCurrentLevel(playInSequence);
168     if (config.mgrab) SDL_WM_GrabInput(SDL_GRAB_OFF);
169     switch (levelOutcome)
170     {
171       case LEVEL_QUIT:
172         loop = false;
173         break;
174       case LEVEL_LOSE:
175         loop = true;
176         break;
177       case LEVEL_WIN:
178         if (playInSequence)
179         {
180           P.level++;
181 
182           // Beat the game!
183           if (P.level >= NUM_LEVELS)
184           {
185             if (P.startingLevel == 0 && !P.cheated && !P.usedAugment)
186               P.computeFinalStatistics();
187 
188             finalStatistics();
189 
190             // Unload level, load ending text, roll credits
191             startMusic(MUS_END);
192             currLevel.unload();
193             currLevel.defaults();
194 
195             currLevel.readLevelIntroText((char *)getLevelIntroFileName(NUM_LEVELS).c_str());
196 
197             levelIntroScreen(true); // displays ending text
198             finalCredits();
199 
200             printf("A winner is you!\n");
201             loop = false;
202             P.level = NUM_LEVELS;
203           }
204           else
205           {
206             P.unlockLevel();
207             lvl = getLevelFileName(P.level);
208           }
209         }
210         else
211           loop = false;
212         break;
213     }
214   }
215 }
216