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