forked from barak/quantumminigolf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackSelector.cpp
269 lines (245 loc) · 7.57 KB
/
TrackSelector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Quantum Minigolf, a computer game illustrating quantum mechanics
Copyright (C) 2007 Friedemann Reinhard <friedemann.reinhard@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "TrackSelector.h"
// TrackSelector constructor: preload the desired track bitmaps
// and store them in the track list
TrackSelector::TrackSelector (Renderer * renderer,
ClassicSimulator * csimulator)
{
this->renderer = renderer;
this->csimulator = csimulator;
this->width = renderer->width;
this->height = renderer->height;
char line[80];
char V[sizeof (line)];
char hard[sizeof (line)];
char soft[sizeof (line)];
string fname;
SDL_Surface *bmp;
trackrecord *entry;
help = true;
/*read the config file, load the track bitmaps and store
them in the track vector */
FILE *conf = fopen (TRACKSDIR "/tracks.cfg", "r");
//load an empty dummy track, if there is no config file present
if (conf == NULL)
{
cout << "Cannot open track config file" << endl;
entry = new trackrecord;
tracks.push_back (entry);
entry->V = BlackTrack ();
entry->hard = BlackTrack ();
entry->soft = BlackTrack ();
}
//config file present: load all tracks mentioned in the config file
else
{
cout << "preloading tracks..." << endl;
while (fgets (line, sizeof (line) - 1, conf))
{
for (size_t i = 0; i < sizeof (hard) - 1; i++)
{
hard[i] = ' ';
soft[i] = ' ';
}
sscanf (line, "%s %s %s\n", V, hard, soft);
fname = string (TRACKSDIR "/");
fname.append (V);
cout << fname << "...";
bmp = (SDL_Surface *) SDL_LoadBMP (fname.c_str ());
/*if bitmap cannot be loaded or has the wrong format, skip it */
if (bmp == NULL || bmp->w != width || bmp->h != height)
{
cout << "failed: no quantum track found" << endl;
SDL_FreeSurface (bmp);
bmp = BlackTrack ();
}
entry = new trackrecord;
tracks.push_back (entry);
entry->V =
SDL_ConvertSurface (bmp, renderer->screen->format, SDL_SWSURFACE);
fname = string (TRACKSDIR "/");
fname.append (hard);
bmp = (SDL_Surface *) SDL_LoadBMP (fname.c_str ());
/*if bitmap cannot be loaded or has the wrong format, skip it */
if (bmp == NULL || bmp->w != width || bmp->h != height)
{
cout << fname << ": failed to load hardcore potential" << endl;
SDL_FreeSurface (bmp);
bmp = BlackTrack ();
}
entry->hard =
SDL_ConvertSurface (bmp, renderer->screen->format, SDL_SWSURFACE);
fname = string (TRACKSDIR "/");
fname.append (soft);
bmp = (SDL_Surface *) SDL_LoadBMP (fname.c_str ());
/*if bitmap cannot be loaded or has the wrong format, skip it */
if (bmp == NULL || bmp->w != width || bmp->h != height)
{
cout << fname << ": failed to load softcore potential" << endl;
SDL_FreeSurface (bmp);
bmp = BlackTrack ();
}
entry->soft =
SDL_ConvertSurface (bmp, renderer->screen->format, SDL_SWSURFACE);
cout << "done" << endl;
}
}
trackiterator = tracks.begin ();
renderer->V = (*trackiterator)->V;
csimulator->hard = (*trackiterator)->hard;
csimulator->soft = (*trackiterator)->soft;
renderer->RenderTrack ();
renderer->Blit ();
}
TrackSelector::~TrackSelector (void)
{
list < trackrecord * >::iterator l;
for (l = tracks.begin (); l != tracks.end (); ++l)
{
SDL_FreeSurface ((*l)->V);
SDL_FreeSurface ((*l)->hard);
SDL_FreeSurface ((*l)->soft);
delete *l;
}
}
//TrackSelector::GetTrack
// take control of the renderer and generate the track choice menu
// release control of the renderer only when the user has chosen a track
// or quit the game
int
TrackSelector::GetTrack (bool * quantum)
{
SDL_Event dummyevent;
int finished = 0;
SDL_EventState (SDL_MOUSEBUTTONUP, SDL_IGNORE);
SDL_EventState (SDL_MOUSEBUTTONDOWN, SDL_IGNORE);
renderer->V = (*trackiterator)->V;
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
while (!finished)
{
if (SDL_PollEvent (&dummyevent) == 0)
continue;
switch (dummyevent.type)
{
case SDL_KEYDOWN:
switch (dummyevent.key.keysym.sym)
{
case SDLK_ESCAPE: //Esc - quit
return 0;
break;
case SDLK_RETURN: // RET or Space - start a game on the current track
case SDLK_SPACE:
SDL_EventState (SDL_MOUSEBUTTONUP, SDL_ENABLE);
SDL_EventState (SDL_MOUSEBUTTONDOWN, SDL_ENABLE);
return 1;
break;
case SDLK_LEFT: // left or right - cycle tracks
if (trackiterator == tracks.begin ())
{
trackiterator = tracks.end ();
--trackiterator;
}
else
--trackiterator;
renderer->V = (*trackiterator)->V;
csimulator->hard = (*trackiterator)->hard;
csimulator->soft = (*trackiterator)->soft;
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
break;
case SDLK_RIGHT:
++trackiterator;
if (trackiterator == tracks.end ())
trackiterator = tracks.begin ();
renderer->V = (*trackiterator)->V;
csimulator->hard = (*trackiterator)->hard;
csimulator->soft = (*trackiterator)->soft;
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
break;
case SDLK_q: // q - toggle quantum mode
*quantum = !*quantum;
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
break;
case SDLK_h: // h - toggle help overlay
help = !help;
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
break;
case SDLK_c: // c - toggle color map
renderer->ToggleCmap ();
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
break;
case SDLK_b: // b - toggle background rendering
renderer->ToggleBackgroundRendering ();
renderer->RenderTrack ();
if (help)
renderer->RenderMenu (*quantum);
renderer->Blit ();
break;
default:
cerr << "warning: unhandled SDLK event" << endl;
break;
}
break;
default:
renderer->Blit ();
break;
}
}
// Unreachable: finished is never set
return 0; // avoid spurious warning
}
//BlackTrack -- generate an empty track surface
SDL_Surface *
TrackSelector::BlackTrack (void)
{
SDL_Surface *result = NULL;
result = SDL_CreateRGBSurface (SDL_SWSURFACE, width,
height,
renderer->screen->format->BitsPerPixel,
renderer->screen->format->Rmask,
renderer->screen->format->Gmask,
renderer->screen->format->Bmask,
renderer->screen->format->Amask);
if (result == NULL)
{
cout << "failed to create software surface" << endl;
exit (1);
}
Uint32 *pdata = ((Uint32 *) (result->pixels));
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
pdata[j * width + i] = 0;
}
return result;
}