From: Ingo Ruhnke Date: Thu, 25 Mar 2004 10:26:59 +0000 (+0000) Subject: - made TileManager::get() always return a valid tile X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=a63e6492099721b810fbcf9017d4d9f835fa1755;p=supertux.git - made TileManager::get() always return a valid tile - made conversion code more robust against unknown tiles - added next_tile/next_tile2 to replace the old one on some events SVN-Revision: 356 --- diff --git a/data/images/tilesets/supertux.stgt b/data/images/tilesets/supertux.stgt index b5e4be40a..76d5afbf5 100644 --- a/data/images/tilesets/supertux.stgt +++ b/data/images/tilesets/supertux.stgt @@ -1,3 +1,5 @@ +;; -*- mode: scheme -*- + (supertux-tiles (properties (id 0)) @@ -86,6 +88,7 @@ (images "bonus1.png") (solid #t) (fullbox #t) + (next-tile 84) (data 1)) (tile (id 27) (images "block1.png") @@ -248,11 +251,15 @@ (tile (id 77) (images "brick0.png") - (brick #t) + (brick #t) + (data 1) + (next-tile 0) (solid #t)) (tile (id 78) (images "brick1.png") (brick #t) + (data 1) + (next-tile 0) (solid #t)) (tile (id 79) @@ -274,6 +281,7 @@ (images "box-full.png") (fullbox #t) (solid #t) + (next-tile 84) (data 1)) (tile (id 84) @@ -304,10 +312,12 @@ (images "box-full.png") (solid #t) (fullbox #t) - (data 2)) + (data 2) + (next-tile 84)) (tile (id 103) (images "box-full.png") (solid #t) (fullbox #t) - (data 3)) + (data 3) + (next-tile 84)) ) \ No newline at end of file diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 66e392e52..36c147a75 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -1327,10 +1327,14 @@ void drawshape(float x, float y, unsigned int c) { texture_draw(&ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))],x,y); } - else + else if (ptile->images.size() == 1) { texture_draw(&ptile->images[0],x,y); } + else + { + printf("Tile not dravable %u\n", c); + } } } @@ -1508,12 +1512,12 @@ bool isdistro(float x, float y) void trybreakbrick(float x, float y) { - if (isbrick(x, y)) + Tile* tile = gettile(x, y); + if (tile->brick) { - if (shape(x, y) == 'x' || shape(x, y) == 'y') + if (tile->data > 0) { /* Get a distro from it: */ - add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32); @@ -1524,7 +1528,7 @@ void trybreakbrick(float x, float y) } if (distro_counter <= 0) - level_change(¤t_level,x, y, TM_IA, 'a'); + level_change(¤t_level,x, y, TM_IA, tile->next_tile2); play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); score = score + SCORE_DISTRO; @@ -1533,8 +1537,7 @@ void trybreakbrick(float x, float y) else { /* Get rid of it: */ - - level_change(¤t_level,x, y, TM_IA, '.'); + level_change(¤t_level,x, y, TM_IA, tile->next_tile); } @@ -1568,7 +1571,8 @@ void bumpbrick(float x, float y) void tryemptybox(float x, float y, int col_side) { - if (!isfullbox(x, y)) + Tile* tile = gettile(x,y); + if (!tile->fullbox) return; // according to the collision side, set the upgrade direction @@ -1579,7 +1583,7 @@ void tryemptybox(float x, float y, int col_side) col_side = LEFT; // FIXME: Content of boxes must be handled otherwise - switch(gettile(x,y)->data) + switch(tile->data) { case 1: //'A': /* Box with a distro! */ add_bouncy_distro(((int)(x + 1) / 32) * 32, (int)(y / 32) * 32 - 32); @@ -1602,7 +1606,7 @@ void tryemptybox(float x, float y, int col_side) } /* Empty the box: */ - level_change(¤t_level,x, y, TM_IA, 'a'); + level_change(¤t_level,x, y, TM_IA, tile->next_tile); } @@ -1613,7 +1617,7 @@ void trygrabdistro(float x, float y, int bounciness) Tile* tile = gettile(x, y); if (tile && tile->distro) { - level_change(¤t_level,x, y, TM_IA, '.'); + level_change(¤t_level,x, y, TM_IA, tile->next_tile); play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); if (bounciness == BOUNCE) diff --git a/src/level.cpp b/src/level.cpp index eebecdf1f..a32275c1d 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -10,6 +10,7 @@ // // +#include #include #include #include @@ -300,54 +301,57 @@ int level_load(st_level* plevel, const char* filename) // Convert old levels to the new tile numbers if (version == 0) { - int transtable[256]; - transtable[(int)'.'] = 0; - transtable[(int)'0'] = 0; - transtable[(int)'1'] = 1; - transtable[(int)'2'] = 2; - transtable[(int)'x'] = 77; - transtable[(int)'X'] = 77; - transtable[(int)'y'] = 78; - transtable[(int)'Y'] = 78; - transtable[(int)'A'] = 83; - transtable[(int)'B'] = 102; - transtable[(int)'!'] = 103; - transtable[(int)'a'] = 84; - transtable[(int)'C'] = 85; - transtable[(int)'D'] = 86; - transtable[(int)'E'] = 87; - transtable[(int)'F'] = 88; - transtable[(int)'c'] = 89; - transtable[(int)'d'] = 90; - transtable[(int)'e'] = 91; - transtable[(int)'f'] = 92; - - transtable[(int)'G'] = 93; - transtable[(int)'H'] = 94; - transtable[(int)'I'] = 95; - transtable[(int)'J'] = 96; - - transtable[(int)'g'] = 97; - transtable[(int)'h'] = 98; - transtable[(int)'i'] = 99; - transtable[(int)'j'] = 100 -; - transtable[(int)'#'] = 11; - transtable[(int)'['] = 13; - transtable[(int)'='] = 14; - transtable[(int)']'] = 15; - transtable[(int)'$'] = 82; - transtable[(int)'^'] = 76; - transtable[(int)'*'] = 80; - transtable[(int)'|'] = 79; - transtable[(int)'\\'] = 81; - transtable[(int)'&'] = 75; + std::map transtable; + transtable['.'] = 0; + transtable['0'] = 0; + transtable['1'] = 1; + transtable['2'] = 2; + transtable['x'] = 77; + transtable['X'] = 77; + transtable['y'] = 78; + transtable['Y'] = 78; + transtable['A'] = 83; + transtable['B'] = 102; + transtable['!'] = 103; + transtable['a'] = 84; + transtable['C'] = 85; + transtable['D'] = 86; + transtable['E'] = 87; + transtable['F'] = 88; + transtable['c'] = 89; + transtable['d'] = 90; + transtable['e'] = 91; + transtable['f'] = 92; + + transtable['G'] = 93; + transtable['H'] = 94; + transtable['I'] = 95; + transtable['J'] = 96; + + transtable['g'] = 97; + transtable['h'] = 98; + transtable['i'] = 99; + transtable['j'] = 100 + ; + transtable['#'] = 11; + transtable['['] = 13; + transtable['='] = 14; + transtable[']'] = 15; + transtable['$'] = 82; + transtable['^'] = 76; + transtable['*'] = 80; + transtable['|'] = 79; + transtable['\\'] = 81; + transtable['&'] = 75; for(std::vector::iterator i = ia_tm.begin(); i != ia_tm.end(); ++i) - if (*i < 256) - *i = transtable[*i]; - else - puts("Error: Value to high, conversion will fail"); + { + std::map::iterator j = transtable.find(*i); + if (j != transtable.end()) + *i = j->second; + else + printf("Error: conversion will fail, unsupported char: '%c' (%d)\n", *i, *i); + } } } diff --git a/src/tile.cpp b/src/tile.cpp index 80f6364a0..1140da7e5 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -38,10 +38,8 @@ void TileManager::load_tileset(std::string filename) if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0) { - int id = 0; - std::vector filenames; - - Tile* tile = new Tile; + Tile* tile = new Tile; + tile->id = -1; tile->solid = false; tile->brick = false; tile->ice = false; @@ -49,10 +47,12 @@ void TileManager::load_tileset(std::string filename) tile->distro = false; tile->data = 0; tile->alpha = 0; + tile->next_tile = 0; + tile->next_tile2 = 0; tile->anim_speed = 25; LispReader reader(lisp_cdr(element)); - reader.read_int("id", &id); + assert(reader.read_int("id", &tile->id)); reader.read_bool("solid", &tile->solid); reader.read_bool("brick", &tile->brick); reader.read_bool("ice", &tile->ice); @@ -61,9 +61,13 @@ void TileManager::load_tileset(std::string filename) reader.read_int("data", (int*)&tile->data); reader.read_int("alpha", (int*)&tile->alpha); reader.read_int("anim-speed", &tile->anim_speed); - reader.read_string_vector("images", &filenames); + reader.read_int("next-tile", &tile->next_tile); + reader.read_int("next-tile2", &tile->next_tile2); + reader.read_string_vector("images", &tile->filenames); - for(std::vector::iterator it = filenames.begin(); it != filenames.end(); ++it) + for(std::vector::iterator it = tile->filenames.begin(); + it != tile->filenames.end(); + ++it) { texture_type cur_image; tile->images.push_back(cur_image); @@ -72,10 +76,10 @@ void TileManager::load_tileset(std::string filename) USE_ALPHA); } - if (id+tileset_id >= int(tiles.size())) - tiles.resize(id+tileset_id+1); + if (tile->id + tileset_id >= int(tiles.size())) + tiles.resize(tile->id + tileset_id+1); - tiles[id+tileset_id] = tile; + tiles[tile->id + tileset_id] = tile; } else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0) { diff --git a/src/tile.h b/src/tile.h index ab78d59a0..1d301e8dd 100644 --- a/src/tile.h +++ b/src/tile.h @@ -23,7 +23,10 @@ Tile Class */ struct Tile { + int id; + std::vector images; + std::vector filenames; /** solid tile that is indestructable by Tux */ bool solid; @@ -43,6 +46,11 @@ struct Tile /** General purpose data attached to a tile (content of a box, type of coin) */ int data; + /** Id of the tile that is going to replace this tile once it has + been collected or jumped at */ + int next_tile; + int next_tile2; + int anim_speed; unsigned char alpha; }; @@ -57,7 +65,19 @@ class TileManager public: static TileManager* instance() { return instance_ ? instance_ : instance_ = new TileManager(); } - Tile* get(unsigned int id) { if( id < tiles.size()) { return tiles[id]; } else { return NULL; } } + Tile* get(unsigned int id) { + if(id < tiles.size()) + { + return tiles[id]; + } + else + { + // Never return 0, but return the 0th tile instead so that + // user code doesn't have to check for NULL pointers all over + // the place + return tiles[0]; + } + } }; #endif