From: Ricardo Cruz Date: Wed, 7 Jul 2004 11:39:35 +0000 (+0000) Subject: Added vertical flipping to levels! X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=055fb8d0e2d75acff6185f5a4f5c75e0782503c5;p=supertux.git Added vertical flipping to levels! To enable it, add to the level file (flip #t) . SVN-Revision: 1532 --- diff --git a/src/badguy.h b/src/badguy.h index fa26832d9..03c881851 100644 --- a/src/badguy.h +++ b/src/badguy.h @@ -99,6 +99,7 @@ public: bool stay_on_platform; Direction dir; + Vector start_position; Timer frozen_timer; // gets frozen when a ice shot hits it @@ -108,7 +109,6 @@ private: int squishcount; /// number of times this enemy was squiched Vector target; // Target that badguy is aiming for (wingling uses this) Timer timer; - Vector start_position; Physic physic; float angle; diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 32dbbf4bf..15c950528 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -164,6 +164,12 @@ GameSession::levelintro(void) std::string(_("by ")) + level->get_author(), Vector(0, 400), LAYER_FOREGROUND1); + + if(level->is_level_flipped()) + context.draw_text_center(white_text, + _("Level Vertically Flipped!"), + Vector(0, 310), LAYER_FOREGROUND1); + context.do_drawing(); SDL_Event event; @@ -763,7 +769,7 @@ GameSession::drawstatus(DrawingContext& context) { sprintf(str, "%2.1f", fps_fps); context.draw_text(white_text, "FPS", - Vector(screen->w - white_text->get_text_width("FPS "), 40), + Vector(screen->w - white_text->get_text_width("FPS "), 40), LAYER_FOREGROUND1); context.draw_text(gold_text, str, Vector(screen->w-4*16, 40), LAYER_FOREGROUND1); diff --git a/src/interactive_object.h b/src/interactive_object.h index bb06a5238..62bfb4b39 100644 --- a/src/interactive_object.h +++ b/src/interactive_object.h @@ -43,6 +43,9 @@ public: const base_type& get_area() const { return area; } + void set_area(float x, float y) + { area.x = x; area.y = y; } + protected: base_type area; }; diff --git a/src/level.cpp b/src/level.cpp index f004fd2dc..a13f2f26c 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -44,6 +44,7 @@ using namespace std; Level::Level() : name("noname"), author("mr. x"), time_left(500) + { } @@ -60,6 +61,8 @@ Level::load(const std::string& filename) return; } + vertical_flip = false; + for(lisp_object_t* cur = level->get_lisp(); !lisp_nil_p(cur); cur = lisp_cdr(cur)) { std::string token = lisp_symbol(lisp_car(lisp_car(cur))); @@ -72,6 +75,8 @@ Level::load(const std::string& filename) author = lisp_string(data); } else if(token == "time") { time_left = lisp_integer(data); + } else if(token == "flip") { + vertical_flip = lisp_boolean(data); } else if(token == "sector") { Sector* sector = new Sector; sector->parse(reader); @@ -83,6 +88,12 @@ Level::load(const std::string& filename) } delete level; + + if(vertical_flip) + { + for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) + i->second->do_vertical_flip(); + } } void @@ -91,10 +102,18 @@ Level::load_old_format(LispReader& reader) reader.read_string("name", name); reader.read_string("author", author); reader.read_int("time", time_left); + vertical_flip = false; + reader.read_bool("flip", vertical_flip); Sector* sector = new Sector; sector->parse_old_format(reader); add_sector(sector); + + if(vertical_flip) + { + for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) + i->second->do_vertical_flip(); + } } void @@ -116,6 +135,9 @@ Level::save(const std::string& filename) for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) { + if(vertical_flip) + i->second->do_vertical_flip(); + writer->start_list("sector"); i->second->write(*writer); writer->end_list("sector"); diff --git a/src/level.h b/src/level.h index c955c5f2c..7e4c3354c 100644 --- a/src/level.h +++ b/src/level.h @@ -49,12 +49,19 @@ public: const std::string& get_author() const { return author; } + bool is_level_flipped() + { return vertical_flip; } + void add_sector(Sector* sector); Sector* get_sector(const std::string& name); private: void load_old_format(LispReader& reader); + + /** If true, it will flip the level vertically, during the + parsing process */ + bool vertical_flip; }; #endif /*SUPERTUX_LEVEL_H*/ diff --git a/src/sector.cpp b/src/sector.cpp index ce8e7e9d4..9c7f000e4 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -288,6 +288,38 @@ Sector::write(LispWriter& writer) } void +Sector::do_vertical_flip() +{ + for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i) + { + TileMap* tilemap = dynamic_cast (*i); + if(tilemap) + { + tilemap->do_vertical_flip(); + } + + BadGuy* badguy = dynamic_cast (*i); + if(badguy) + badguy->start_position.y = solids->get_height()*32 - badguy->start_position.y - 32; + Trampoline* trampoline = dynamic_cast (*i); + if(trampoline) + trampoline->base.y = solids->get_height()*32 - trampoline->base.y; + FlyingPlatform* flying_platform = dynamic_cast (*i); + if(flying_platform) + flying_platform->base.y = solids->get_height()*32 - flying_platform->base.y; + Door* door = dynamic_cast (*i); + if(door) + door->set_area(door->get_area().x, solids->get_height()*32 - door->get_area().y); + } + + for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end(); + ++i) { + SpawnPoint* spawn = *i; + spawn->pos.y = solids->get_height()*32 - spawn->pos.y; + } +} + +void Sector::add_object(GameObject* object) { gameobjects_new.push_back(object); diff --git a/src/sector.h b/src/sector.h index c8aa0b3b5..fe2acf1d5 100644 --- a/src/sector.h +++ b/src/sector.h @@ -112,6 +112,10 @@ public: the tile which the badguy is walking on an killing him this way */ void trybumpbadguy(const Vector& pos); + /** Flip the all the sector vertically. The purpose of this is to let + player to play the same level in a different way :) */ + void do_vertical_flip(); + /** @evil@ */ static Sector* current() { return _current; } diff --git a/src/tilemap.cpp b/src/tilemap.cpp index b5f90b411..21651784a 100644 --- a/src/tilemap.cpp +++ b/src/tilemap.cpp @@ -33,13 +33,13 @@ #include "lispwriter.h" TileMap::TileMap() - : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES) + : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES), vertical_flip(false) { tilemanager = TileManager::instance(); } TileMap::TileMap(LispReader& reader) - : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES) + : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES), vertical_flip(false) { tilemanager = TileManager::instance(); @@ -84,7 +84,7 @@ TileMap::TileMap(LispReader& reader) } TileMap::TileMap(int layer_, bool solid_, size_t width_, size_t height_) - : solid(solid_), speed(1), width(width_), height(height_), layer(layer_) + : solid(solid_), speed(1), width(width_), height(height_), layer(layer_), vertical_flip(false) { } @@ -131,9 +131,15 @@ TileMap::action(float ) void TileMap::draw(DrawingContext& context) -{ +{ if (speed == 1.0) { + if(vertical_flip) // flip vertically the tiles, in case we are playing this + { // level upside down + context.push_transform(); + context.set_drawing_effect(VERTICAL_FLIP); + } + /** if we don't round here, we'll have a 1 pixel gap on screen sometimes. * I have no idea why */ float start_x = roundf(context.get_translation().x); @@ -153,6 +159,9 @@ TileMap::draw(DrawingContext& context) tilemanager->draw_tile(context, tiles[ty*width + tx].id, pos, layer); } } + + if(vertical_flip) // disable flipping, if applied + context.pop_transform(); } else { @@ -161,6 +170,8 @@ TileMap::draw(DrawingContext& context) context.push_transform(); context.set_translation(Vector(trans_x * speed, trans_y * speed)); + if(vertical_flip) + context.set_drawing_effect(VERTICAL_FLIP); float start_x = roundf(context.get_translation().x); float start_y = roundf(context.get_translation().y); @@ -235,6 +246,19 @@ TileMap::resize(int new_width, int new_height) width = new_width; } +void +TileMap::do_vertical_flip() +{ + // remap tiles vertically flipped + for(int y = 0; y < height / 2; ++y) { + for(int x = 0; x < width; ++x) { + std::swap(tiles[y*width + x], tiles[(((height-1)*width) - (y*width)) + x]); + } + } + + vertical_flip = true; +} + Tile* TileMap::get_tile(int x, int y) const { diff --git a/src/tilemap.h b/src/tilemap.h index c46435542..3df34cb9a 100644 --- a/src/tilemap.h +++ b/src/tilemap.h @@ -64,6 +64,10 @@ public: */ void resize(int newwidth, int newheight); + /** Flip the all tile map vertically. The purpose of this is to let + player to play the same level in a different way :) */ + void do_vertical_flip(); + size_t get_width() const { return width; } @@ -96,6 +100,8 @@ private: float speed; int width, height; int layer; + + bool vertical_flip; }; #endif /*SUPERTUX_TILEMAP_H*/