]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/cutscene.cpp
Update copyright statements.
[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 void createSceneList()
25 {
26         char sceneLine[1024] = "";
27         char *line = NULL;
28         int waitTime = 0;
29         Cutscene *scene = NULL;
30         
31         while (true)
32         {
33                 line = strtok(NULL, "\n");
34                 if (line == NULL)
35                         break;
36                 
37                 if (strcmp(sceneLine, "@EOF@") == 0)
38                         break;
39         
40                 if (line[0] == '[')
41                         break;  
42                 
43                 if (strcmp(line, "END") == 0)
44                         break;
45                 
46                 if (strcmp(line, "NEW") == 0)
47                 {
48                         scene = new Cutscene();
49                         sceneList.add(scene);
50                         
51                         // Assume graphics is first line after new
52                         line = strtok(NULL, "\n");
53                         if (strcmp(line, "@none@") != 0)
54                         {
55                                 strlcpy(scene->sprite, line, sizeof scene->sprite);
56                                 debug(("Loading cutscene image %s\n", scene->sprite));
57                                 graphics.quickSprite(scene->sprite, graphics.loadImage(scene->sprite));
58                         }
59                         line = strtok(NULL, "\n");
60                         sscanf(line, "%d", &waitTime);
61                         scene->waitTime = (waitTime * 100);
62                         line = strtok(NULL, "\n");
63                 }
64                 
65                 if (strcmp(line, "@none@") != 0)
66                 {
67                         scene->appendText(line);
68                 }
69         }
70 }
71
72 bool setupScene(const char *stagename)
73 {
74         sceneList.clear();
75         
76         char sceneLine[1024];
77
78         if (!engine.loadData(_("data/ending")))
79                 graphics.showErrorAndExit("Couldn't load cutscene data file (%s)", _("data/ending"));
80
81         char *line = strtok((char*)engine.dataBuffer, "\n");
82         int i = 0;
83
84         graphics.clearChatString();
85         
86         bool found = false;
87
88         while (!found)
89         {       
90                 if (line[0] == '[')
91                 {
92                         sscanf(line, "%*c %[^]]", sceneLine);
93                         if (strcmp(sceneLine, stagename) == 0)
94                         {
95                                 found = true;
96                         }
97                 }
98
99                 if (!found)
100                 {
101                         line = strtok(NULL, "\n");
102                         if (line == NULL)
103                                 break;
104                 }
105
106                 i++;
107
108                 // sanity check!
109                 if (i == 10000)
110                 {
111                         exit(1);
112                 }
113         }
114         
115         if (found)
116         {
117                 createSceneList();
118         }
119         
120         return found;
121 }
122
123 void showScene(bool allowSkip)
124 {
125         graphics.setFontSize(0);
126         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
127         
128         SDL_FillRect(graphics.screen, NULL, graphics.black);
129         graphics.delay(500);
130         
131         Cutscene *scene = (Cutscene*)sceneList.getHead();
132         
133         SDL_Surface *panel = graphics.quickSprite("panel", graphics.createSurface(640, 90));
134         SDL_Surface *image = NULL;
135         SDL_FillRect(panel, NULL, graphics.black);
136         
137         float panelAlpha = 0;
138         
139         SDL_SetAlpha(panel, 0);
140         
141         engine.clearInput();
142         engine.flushInput();
143         
144         float changeTime = 100;
145         
146         engine.resetTimeDifference();
147         
148         audio.playMusicOnce();
149         
150         while (true)
151         {
152                 graphics.updateScreen();
153                 engine.getInput();
154                 config.populate();
155                 
156                 engine.doTimeDifference();
157                 
158                 if ((engine.userAccepts()) && (allowSkip))
159                 {
160                         changeTime = 0;
161                         panelAlpha = 255;
162                         engine.clearInput();
163                 }
164                 
165                 if (panelAlpha < 256)
166                 {
167                         panelAlpha += (1 * engine.getTimeDifference());
168                         SDL_SetAlpha(panel, panelAlpha);
169                         if (image != NULL)
170                         {
171                                 SDL_SetAlpha(image, panelAlpha);
172                                 graphics.blit(image, 0, 0, graphics.screen, false);
173                         }
174                         graphics.blit(panel, 0, 390, graphics.screen, false);
175                 }
176                 
177                 changeTime -= (1 * engine.getTimeDifference());
178                 
179                 if (changeTime <= 0)
180                 {
181                         if (scene->next != NULL)
182                         {
183                                 scene = (Cutscene*)scene->next;
184                                 panelAlpha = 0;
185                                 changeTime = scene->waitTime;
186                                 graphics.clearChatString();
187                                 graphics.createChatString(scene->text);
188                                 SDL_FillRect(panel, NULL, graphics.black);
189                                 graphics.drawChatString(panel, 0);
190                                 image = NULL;
191                                 
192                                 if (strcmp(scene->sprite, "") != 0)
193                                 {
194                                         debug(("Getting cutscene %s\n", scene->sprite));
195                                         image = graphics.getSprite(scene->sprite, true)->image[0];
196                                         SDL_SetColorKey(image, 0, SDL_MapRGB(image->format, 0, 0, 0));
197                                 }
198                         }
199                         else
200                         {
201                                 break;
202                         }
203                 }
204
205                 SDL_Delay(16);
206         }
207         
208         SDL_FillRect(graphics.screen, NULL, graphics.black);
209         graphics.delay(500);
210 }
211
212 void checkStartCutscene()
213 {
214         // Easy mode doesn't have cutscenes!
215         if (game.skill == 0)
216         {
217                 return;
218         }
219         
220         char sceneName[1024];
221         snprintf(sceneName, sizeof sceneName, "%s Start", game.stageName);
222         
223         if (setupScene(sceneName))
224         {
225                 audio.loadMusic("music/cutscene");
226                 showScene(true);
227         }
228         
229         graphics.free();
230         audio.free();
231 }
232
233 void checkEndCutscene()
234 {
235         // Easy mode doesn't have cutscenes!
236         if (game.skill == 0)
237         {
238                 return;
239         }
240         
241
242         char sceneName[1024];
243         snprintf(sceneName, sizeof sceneName, "%s End", game.stageName);
244         
245         debug(("%s\n", sceneName));
246         
247         bool allowSkip = true;
248         
249         // Don't let the player skip the end of game cutscene...
250         // So we get the music timed well! :)
251         if (strcmp(game.stageName, "Final Battle") == 0)
252         {
253                 allowSkip = false;
254         }
255         
256         if (setupScene(sceneName))
257         {
258                 if (strcmp(game.stageName, "Final Battle") != 0)
259                 {
260                         audio.loadMusic("music/cutscene");
261                 }
262                 else
263                 {
264                         audio.loadMusic("music/end");
265                 }
266
267                 showScene(allowSkip);
268         }
269         
270         graphics.free();
271         audio.free();
272 }
273
274 void easyGameFinished()
275 {
276         graphics.free();
277         audio.free();
278         
279         audio.loadMusic("music/gameover");
280         setupScene("Easy Game Finished");
281         showScene(true);
282         audio.fadeMusic();
283         graphics.delay(3500);
284         
285         graphics.free();
286         audio.free();
287 }
288