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.
24 #include "lispreader.h"
27 namespace WorldMapNS {
29 TileManager* TileManager::instance_ = 0;
31 TileManager::TileManager()
34 FILE* in = fopen(DATA_PREFIX "images/worldmap/antarctica.scm", "r");
36 lisp_stream_init_file (&stream, in);
37 lisp_object_t* root_obj = lisp_read (&stream);
39 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
41 lisp_object_t* cur = lisp_cdr(root_obj);
43 while(!lisp_nil_p(cur))
45 lisp_object_t* element = lisp_car(cur);
47 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
50 std::string filename = "<invalid>";
52 Tile* tile = new Tile;
59 LispReader reader(lisp_cdr(element));
60 reader.read_int("id", &id);
61 reader.read_bool("north", &tile->north);
62 reader.read_bool("south", &tile->south);
63 reader.read_bool("west", &tile->west);
64 reader.read_bool("east", &tile->east);
65 reader.read_bool("stop", &tile->stop);
66 reader.read_string("image", &filename);
68 texture_load(&tile->sprite,
69 const_cast<char*>((std::string(DATA_PREFIX "/images/worldmap/") + filename).c_str()),
72 if (id >= tiles.size())
79 puts("Unhandled symbol");
92 TileManager::get(int i)
94 assert(i >=0 && i < tiles.size());
105 texture_load(&tux_sprite, DATA_PREFIX "/images/worldmap/tux.png", USE_ALPHA);
112 input_direction = NONE;
113 tux_direction = NONE;
119 WorldMap::~WorldMap()
126 tilemap.resize(width * height);
140 WorldMap::get_input()
146 while (SDL_PollEvent(&event))
155 switch(event.key.keysym.sym)
170 Uint8 *keystate = SDL_GetKeyState(NULL);
172 input_direction = NONE;
174 if (keystate[SDLK_LEFT])
175 input_direction = WEST;
176 else if (keystate[SDLK_RIGHT])
177 input_direction = EAST;
178 else if (keystate[SDLK_UP])
179 input_direction = NORTH;
180 else if (keystate[SDLK_DOWN])
181 input_direction = SOUTH;
191 puts("Enter the current level");
197 // FIXME: Cleanup, seperate tux
198 switch(input_direction)
201 if (at(tux_tile_pos)->west)
205 tux_direction = input_direction;
209 if (at(tux_tile_pos)->east)
213 tux_direction = input_direction;
217 if (at(tux_tile_pos)->north)
221 tux_direction = input_direction;
225 if (at(tux_tile_pos)->south)
229 tux_direction = input_direction;
235 tux_direction = input_direction;
247 if (at(tux_tile_pos)->stop)
249 tux_direction = NONE;
254 // FIXME: Cleanup, seperate tux
255 switch(tux_direction)
258 if (at(tux_tile_pos)->west)
262 if (at(tux_tile_pos)->east)
266 if (at(tux_tile_pos)->north)
270 if (at(tux_tile_pos)->south)
281 WorldMap::at(Point p)
287 return TileManager::instance()->get(tilemap[width * p.y + p.x]);
293 for(int y = 0; y < height; ++y)
294 for(int x = 0; x < width; ++x)
296 Tile* tile = at(Point(x, y));
297 texture_draw(&tile->sprite, x*32, y*32, NO_UPDATE);
301 float x = tux_tile_pos.x * 32;
302 float y = tux_tile_pos.y * 32;
304 switch(tux_direction)
307 x -= tux_offset - 32;
310 x += tux_offset - 32;
313 y -= tux_offset - 32;
316 y += tux_offset - 32;
320 texture_draw(&tux_sprite, (int)x, (int)y, NO_UPDATE);
337 } // namespace WorldMapNS
341 WorldMapNS::WorldMap worldmap;