]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/cutscene.cpp
Don't define variables in header files.
[quix0rs-blobwars.git] / src / cutscene.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "cutscene.h"
23
24 List sceneList;
25
26 void createSceneList()
27 {
28         char sceneLine[1024] = "";
29         char *line = NULL;
30         int waitTime = 0;
31         Cutscene *scene = NULL;
32         
33         while (true)
34         {
35                 line = strtok(NULL, "\n");
36                 if (line == NULL)
37                         break;
38                 
39                 if (strcmp(sceneLine, "@EOF@") == 0)
40                         break;
41         
42                 if (line[0] == '[')
43                         break;  
44                 
45                 if (strcmp(line, "END") == 0)
46                         break;
47                 
48                 if (strcmp(line, "NEW") == 0)
49                 {
50                         scene = new Cutscene();
51                         sceneList.add(scene);
52                         
53                         // Assume graphics is first line after new
54                         line = strtok(NULL, "\n");
55                         if (strcmp(line, "@none@") != 0)
56                         {
57                                 strlcpy(scene->sprite, line, sizeof scene->sprite);
58                                 debug(("Loading cutscene image %s\n", scene->sprite));
59                                 graphics.quickSprite(scene->sprite, graphics.loadImage(scene->sprite));
60                         }
61                         line = strtok(NULL, "\n");
62                         sscanf(line, "%d", &waitTime);
63                         scene->waitTime = (waitTime * 100);
64                         line = strtok(NULL, "\n");
65                 }
66                 
67                 if (strcmp(line, "@none@") != 0)
68                 {
69                         scene->appendText(line);
70                 }
71         }
72 }
73
74 bool setupScene(const char *stagename)
75 {
76         sceneList.clear();
77         
78         char sceneLine[1024];
79
80         if (!engine.loadData(_("data/ending")))
81                 graphics.showErrorAndExit("Couldn't load cutscene data file (%s)", _("data/ending"));
82
83         char *line = strtok((char*)engine.dataBuffer, "\n");
84         int i = 0;
85
86         graphics.clearChatString();
87         
88         bool found = false;
89
90         while (!found)
91         {       
92                 if (line[0] == '[')
93                 {
94                         sscanf(line, "%*c %[^]]", sceneLine);
95                         if (strcmp(sceneLine, stagename) == 0)
96                         {
97                                 found = true;
98                         }
99                 }
100
101                 if (!found)
102                 {
103                         line = strtok(NULL, "\n");
104                         if (line == NULL)
105                                 break;
106                 }
107
108                 i++;
109
110                 // sanity check!
111                 if (i == 10000)
112                 {
113                         exit(1);
114                 }
115         }
116         
117         if (found)
118         {
119                 createSceneList();
120         }
121         
122         return found;
123 }
124
125 void showScene(bool allowSkip)
126 {
127         graphics.setFontSize(0);
128         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
129         
130         SDL_FillRect(graphics.screen, NULL, graphics.black);
131         graphics.delay(500);
132         
133         Cutscene *scene = (Cutscene*)sceneList.getHead();
134         
135         SDL_Surface *panel = graphics.quickSprite("panel", graphics.createSurface(640, 90));
136         SDL_Surface *image = NULL;
137         SDL_FillRect(panel, NULL, graphics.black);
138         
139         float panelAlpha = 0;
140         
141         SDL_SetAlpha(panel, 0);
142         
143         engine.clearInput();
144         engine.flushInput();
145         
146         float changeTime = 100;
147         
148         engine.resetTimeDifference();
149         
150         audio.playMusicOnce();
151         
152         while (true)
153         {
154                 graphics.updateScreen();
155                 engine.getInput();
156                 config.populate();
157                 
158                 engine.doTimeDifference();
159                 
160                 if ((engine.userAccepts()) && (allowSkip))
161                 {
162                         changeTime = 0;
163                         panelAlpha = 255;
164                         engine.clearInput();
165                 }
166                 
167                 if (panelAlpha < 256)
168                 {
169                         panelAlpha += (1 * engine.getTimeDifference());
170                         SDL_SetAlpha(panel, panelAlpha);
171                         if (image != NULL)
172                         {
173                                 SDL_SetAlpha(image, panelAlpha);
174                                 graphics.blit(image, 0, 0, graphics.screen, false);
175                         }
176                         graphics.blit(panel, 0, 390, graphics.screen, false);
177                 }
178                 
179                 changeTime -= (1 * engine.getTimeDifference());
180                 
181                 if (changeTime <= 0)
182                 {
183                         if (scene->next != NULL)
184                         {
185                                 scene = (Cutscene*)scene->next;
186                                 panelAlpha = 0;
187                                 changeTime = scene->waitTime;
188                                 graphics.clearChatString();
189                                 graphics.createChatString(scene->text);
190                                 SDL_FillRect(panel, NULL, graphics.black);
191                                 graphics.drawChatString(panel, 0);
192                                 image = NULL;
193                                 
194                                 if (strcmp(scene->sprite, "") != 0)
195                                 {
196                                         debug(("Getting cutscene %s\n", scene->sprite));
197                                         image = graphics.getSprite(scene->sprite, true)->image[0];
198                                         SDL_SetColorKey(image, 0, SDL_MapRGB(image->format, 0, 0, 0));
199                                 }
200                         }
201                         else
202                         {
203                                 break;
204                         }
205                 }
206
207                 SDL_Delay(16);
208         }
209         
210         SDL_FillRect(graphics.screen, NULL, graphics.black);
211         graphics.delay(500);
212 }
213
214 void checkStartCutscene()
215 {
216         // Easy mode doesn't have cutscenes!
217         if (game.skill == 0)
218         {
219                 return;
220         }
221         
222         char sceneName[1024];
223         snprintf(sceneName, sizeof sceneName, "%s Start", game.stageName);
224         
225         if (setupScene(sceneName))
226         {
227                 audio.loadMusic("music/cutscene");
228                 showScene(true);
229         }
230         
231         graphics.free();
232         audio.free();
233 }
234
235 void checkEndCutscene()
236 {
237         // Easy mode doesn't have cutscenes!
238         if (game.skill == 0)
239         {
240                 return;
241         }
242         
243
244         char sceneName[1024];
245         snprintf(sceneName, sizeof sceneName, "%s End", game.stageName);
246         
247         debug(("%s\n", sceneName));
248         
249         bool allowSkip = true;
250         
251         // Don't let the player skip the end of game cutscene...
252         // So we get the music timed well! :)
253         if (strcmp(game.stageName, "Final Battle") == 0)
254         {
255                 allowSkip = false;
256         }
257         
258         if (setupScene(sceneName))
259         {
260                 if (strcmp(game.stageName, "Final Battle") != 0)
261                 {
262                         audio.loadMusic("music/cutscene");
263                 }
264                 else
265                 {
266                         audio.loadMusic("music/end");
267                 }
268
269                 showScene(allowSkip);
270         }
271         
272         graphics.free();
273         audio.free();
274 }
275
276 void easyGameFinished()
277 {
278         graphics.free();
279         audio.free();
280         
281         audio.loadMusic("music/gameover");
282         setupScene("Easy Game Finished");
283         showScene(true);
284         audio.fadeMusic();
285         graphics.delay(3500);
286         
287         graphics.free();
288         audio.free();
289 }
290