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