3 // Pingus - A free Lemmings clone
4 // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
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.
25 #include "lispreader.h"
29 namespace WorldMapNS {
31 TileManager* TileManager::instance_ = 0;
33 TileManager::TileManager()
36 FILE* in = fopen(DATA_PREFIX "images/worldmap/antarctica.scm", "r");
38 lisp_stream_init_file (&stream, in);
39 lisp_object_t* root_obj = lisp_read (&stream);
41 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
43 lisp_object_t* cur = lisp_cdr(root_obj);
45 while(!lisp_nil_p(cur))
47 lisp_object_t* element = lisp_car(cur);
49 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
52 std::string filename = "<invalid>";
54 Tile* tile = new Tile;
61 LispReader reader(lisp_cdr(element));
62 reader.read_int("id", &id);
63 reader.read_bool("north", &tile->north);
64 reader.read_bool("south", &tile->south);
65 reader.read_bool("west", &tile->west);
66 reader.read_bool("east", &tile->east);
67 reader.read_bool("stop", &tile->stop);
68 reader.read_string("image", &filename);
70 texture_load(&tile->sprite,
71 const_cast<char*>((std::string(DATA_PREFIX "/images/worldmap/") + filename).c_str()),
74 if (id >= tiles.size())
81 puts("Unhandled symbol");
94 TileManager::get(int i)
96 assert(i >=0 && i < tiles.size());
107 texture_load(&tux_sprite, DATA_PREFIX "/images/worldmap/tux.png", USE_ALPHA);
108 texture_load(&level_sprite, DATA_PREFIX "/images/worldmap/levelmarker.png", USE_ALPHA);
115 input_direction = NONE;
116 tux_direction = NONE;
120 music = "SALCON.MOD";
126 WorldMap::~WorldMap()
133 lisp_stream_t stream;
134 FILE* in = fopen(DATA_PREFIX "levels/default/worldmap.scm", "r");
136 lisp_stream_init_file (&stream, in);
137 lisp_object_t* root_obj = lisp_read (&stream);
139 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
141 lisp_object_t* cur = lisp_cdr(root_obj);
143 while(!lisp_nil_p(cur))
145 lisp_object_t* element = lisp_car(cur);
147 if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
149 LispReader reader(lisp_cdr(element));
150 reader.read_int("width", &width);
151 reader.read_int("height", &height);
152 reader.read_int_vector("data", &tilemap);
154 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
156 LispReader reader(lisp_cdr(element));
157 reader.read_string("name", &name);
158 reader.read_string("music", &music);
160 else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
162 lisp_object_t* cur = lisp_cdr(element);
164 while(!lisp_nil_p(cur))
166 lisp_object_t* element = lisp_car(cur);
168 if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
171 LispReader reader(lisp_cdr(element));
172 reader.read_string("name", &level.name);
173 reader.read_int("x-pos", &level.x);
174 reader.read_int("y-pos", &level.y);
175 levels.push_back(level);
192 WorldMap::get_input()
198 while (SDL_PollEvent(&event))
207 switch(event.key.keysym.sym)
222 Uint8 *keystate = SDL_GetKeyState(NULL);
224 input_direction = NONE;
226 if (keystate[SDLK_LEFT])
227 input_direction = WEST;
228 else if (keystate[SDLK_RIGHT])
229 input_direction = EAST;
230 else if (keystate[SDLK_UP])
231 input_direction = NORTH;
232 else if (keystate[SDLK_DOWN])
233 input_direction = SOUTH;
237 WorldMap::get_next_tile(Point pos, Direction direction)
239 // FIXME: Cleanup, seperate tux
240 switch(input_direction)
267 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
269 if (i->x == tux_tile_pos.x &&
270 i->y == tux_tile_pos.y)
272 std::cout << "Enter the current level: " << i->name << std::endl;;
274 gameloop(const_cast<char*>((DATA_PREFIX "levels/default/" + i->name).c_str()),
275 1, ST_GL_LOAD_LEVEL_FILE);
285 Point next_tile = get_next_tile(tux_tile_pos, input_direction);
286 if (next_tile.x >= 0 && next_tile.x < width
287 && next_tile.y >= 0 && next_tile.y < height)
289 // FIXME: Cleanup, seperate tux
290 switch(input_direction)
293 if (at(tux_tile_pos)->west && at(next_tile)->east)
297 tux_direction = input_direction;
301 if (at(tux_tile_pos)->east && at(next_tile)->west)
305 tux_direction = input_direction;
309 if (at(tux_tile_pos)->north && at(next_tile)->south)
313 tux_direction = input_direction;
317 if (at(tux_tile_pos)->south && at(next_tile)->north)
321 tux_direction = input_direction;
327 tux_direction = input_direction;
335 tux_direction = NONE;
346 if (at(tux_tile_pos)->stop)
348 tux_direction = NONE;
353 // FIXME: Cleanup, seperate tux
354 switch(tux_direction)
357 if (at(tux_tile_pos)->west)
361 if (at(tux_tile_pos)->east)
365 if (at(tux_tile_pos)->north)
369 if (at(tux_tile_pos)->south)
380 WorldMap::at(Point p)
386 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
392 for(int y = 0; y < height; ++y)
393 for(int x = 0; x < width; ++x)
395 Tile* tile = at(Point(x, y));
396 texture_draw(&tile->sprite, x*32, y*32, NO_UPDATE);
400 float x = tux_tile_pos.x * 32;
401 float y = tux_tile_pos.y * 32;
403 switch(tux_direction)
406 x -= tux_offset - 32;
409 x += tux_offset - 32;
412 y -= tux_offset - 32;
415 y += tux_offset - 32;
419 for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
421 texture_draw(&level_sprite, i->x*32, i->y*32, NO_UPDATE);
424 texture_draw(&tux_sprite, (int)x, (int)y, NO_UPDATE);
433 song = load_song(const_cast<char*>((DATA_PREFIX "/music/" + music).c_str()));
446 } // namespace WorldMapNS
450 WorldMapNS::WorldMap worldmap;