]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/intro.cpp
42916b025d3b9c49c8ce2ab59c230a3f77da6043
[quix0rs-blobwars.git] / src / intro.cpp
1 /*
2 Copyright (C) 2004-2010 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 "intro.h"
22
23 void playIntro(int tx, int ty, int delay)
24 {
25         unsigned int frameLimit = SDL_GetTicks() + 16;
26         unsigned int time = 0;
27
28         graphics.setFontSize(1);
29         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
30
31         char *line[3];
32         SDL_Surface *text[3];
33
34         for (int i = 0 ; i < 3 ; i++)
35         {
36                 line[i] = strtok(NULL, "\n");
37
38                 if (!line[i])
39                 {
40                         graphics.showErrorAndExit("Malformed Intro Data", "");
41                 }
42
43                 text[i] = NULL;
44
45                 if (strcmp(line[i], "@none@"))
46                 {
47                         text[i] = graphics.getString(line[i], true);
48                 }
49         }
50
51         player.tx += tx;
52         player.ty += ty;
53
54         while (true)
55         {
56                 engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
57
58                 doGameStuff();
59                 doSpawnPoints();
60                 drawMapTopLayer();
61
62                 for (int i = 0 ; i < 3 ; i++)
63                         if (text[i] != NULL)
64                                 graphics.blit(text[i], 320, 150 + (i * 30), graphics.screen, true);
65
66                 if (engine.userAccepts())
67                         break;
68
69                 if (player.x < player.tx) player.x++;
70                 if (player.x > player.tx) player.x--;
71
72                 if ((int)player.x == player.tx)
73                 {
74                         if (time == 0)
75                         {
76                                 time = SDL_GetTicks() + (delay * 1000);
77                         }
78                 }
79
80                 if (time > 0)
81                         if (SDL_GetTicks() > time)
82                                 break;
83
84                 engine.delay(frameLimit);
85                 frameLimit = SDL_GetTicks() + 16;
86         }
87         
88         for (int i = 0 ; i < 3 ; i++)
89                 if (text[i])
90                         SDL_FreeSurface(text[i]);
91 }
92
93 void showIntroError()
94 {
95         SDL_FillRect(graphics.screen, NULL, graphics.black);
96
97         engine.flushInput();
98
99         graphics.setFontSize(1);
100
101         graphics.drawString("Couldn't play intro - Data file is missing.", 320, 150, true, graphics.screen);
102         graphics.drawString("This is not a fatal error, but could mean that the intro", 320, 180, true, graphics.screen);
103         graphics.drawString("file has not been found or was not in the expected format.", 320, 210, true, graphics.screen);
104         graphics.drawString("However it may also be a sign that the game may not work correctly.", 320, 240, true, graphics.screen);
105         graphics.drawString("Press Escape to Continue", 320, 350, true, graphics.screen);
106
107         while (true)
108         {
109                 graphics.updateScreen();
110                 engine.getInput();
111                 config.populate();
112                 if (engine.keyState[SDLK_ESCAPE])
113                         break;
114                 SDL_Delay(16);
115         }
116 }
117
118 void parseIntroCommand()
119 {
120         char *line;
121         char command[25], param[25];
122
123         line = strtok(NULL, "\n");
124         sscanf(line, "%s %s", command, param);
125
126         if (strcmp(command, "SPAWN") == 0)
127         {
128                 SpawnPoint *sp = (SpawnPoint*)map.spawnList.getHead();
129
130                 while (sp->next != NULL)
131                 {
132                         sp = (SpawnPoint*)sp->next;
133
134                         if (strcmp(param, sp->name) == 0)
135                         {
136                                 sp->active = true;
137                         }
138                 }
139         }
140 }
141
142 int doIntro()
143 {
144         int x, y, delay;
145
146         game.setMapName("data/introMap");
147
148         loadResources();
149
150         for (int i = 0 ; i < 4 ; i++)
151         {
152                 addItem(101, "CherryPlant", Math::prand() % 640, 9050, "CherryPlant", 100, 1, 0, false);
153         }
154
155         if (!engine.loadData(_("data/introText")))
156         {
157                 showIntroError();
158                 return SECTION_TITLE;
159         }
160         
161         player.health = -100;
162
163         audio.playMusic();
164
165         char *line = strtok((char*)engine.dataBuffer, "\n");
166
167         while (true)
168         {
169                 line = strtok(NULL, "\n");
170                 sscanf(line, "%d %d %d", &x, &y, &delay);
171
172                 if (delay == -1)
173                         break;
174
175                 parseIntroCommand();
176
177                 playIntro(x, y, delay);
178
179                 if (engine.userAccepts())
180                         break;
181         }
182
183         if (!engine.userAccepts())
184         {
185                 audio.fadeMusic();
186                 graphics.fadeToBlack();
187         }
188
189         map.clear();
190
191         return SECTION_TITLE;
192 }