From 6f801c22d97251799740317fb1d0caf2e744b321 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Fri, 26 Nov 2004 14:45:42 +0000 Subject: [PATCH] some cleanups memory leak fixes and moving of source files SVN-Revision: 2202 --- lib/special/sprite.cpp | 2 +- src/badguy/badguy.cpp | 2 +- src/badguy/badguy.h | 2 +- src/gameloop.cpp | 10 ++-- src/level.cpp | 23 ++------ src/level.h | 1 - src/leveleditor.cpp | 34 +++++++++--- src/leveleditor.h | 3 + src/misc.h | 6 +- src/{ => object}/background.cpp | 2 - src/{ => object}/background.h | 0 src/{ => object}/camera.cpp | 0 src/{ => object}/camera.h | 0 src/{ => object}/gameobjs.cpp | 0 src/{ => object}/gameobjs.h | 0 src/{ => object}/particlesystem.cpp | 0 src/{ => object}/particlesystem.h | 0 src/{ => object}/player.cpp | 6 +- src/{ => object}/player.h | 0 src/{ => object}/tilemap.cpp | 0 src/{ => object}/tilemap.h | 0 src/resources.cpp | 4 +- src/sector.cpp | 106 +++++++----------------------------- src/sector.h | 10 +--- src/supertux.cpp | 1 - src/title.cpp | 6 +- src/trigger/trigger_base.cpp | 2 +- 27 files changed, 79 insertions(+), 141 deletions(-) rename src/{ => object}/background.cpp (98%) rename src/{ => object}/background.h (100%) rename src/{ => object}/camera.cpp (100%) rename src/{ => object}/camera.h (100%) rename src/{ => object}/gameobjs.cpp (100%) rename src/{ => object}/gameobjs.h (100%) rename src/{ => object}/particlesystem.cpp (100%) rename src/{ => object}/particlesystem.h (100%) rename src/{ => object}/player.cpp (99%) rename src/{ => object}/player.h (100%) rename src/{ => object}/tilemap.cpp (100%) rename src/{ => object}/tilemap.h (100%) diff --git a/lib/special/sprite.cpp b/lib/special/sprite.cpp index 6ae659855..f07cfc1ef 100644 --- a/lib/special/sprite.cpp +++ b/lib/special/sprite.cpp @@ -87,7 +87,7 @@ Sprite::update() frame += frame_inc; - if(frame > get_frames()) { + if(frame >= get_frames()) { frame = fmodf(frame+get_frames(), get_frames()); animation_loops--; diff --git a/src/badguy/badguy.cpp b/src/badguy/badguy.cpp index b075dd1f5..25825a4c3 100644 --- a/src/badguy/badguy.cpp +++ b/src/badguy/badguy.cpp @@ -1,7 +1,7 @@ #include #include "badguy.h" -#include "camera.h" +#include "object/camera.h" static const float SQUISH_TIME = 2; static const float X_OFFSCREEN_DISTANCE = 1600; diff --git a/src/badguy/badguy.h b/src/badguy/badguy.h index c0904083c..68cdb57ed 100644 --- a/src/badguy/badguy.h +++ b/src/badguy/badguy.h @@ -7,7 +7,7 @@ #include "special/moving_object.h" #include "special/sprite.h" #include "math/physic.h" -#include "player.h" +#include "object/player.h" #include "serializable.h" #include "resources.h" #include "sector.h" diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 2947f848c..a5933de17 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -47,19 +47,19 @@ #include "high_scores.h" #include "gui/menu.h" #include "sector.h" -#include "player.h" #include "level.h" #include "scene.h" #include "tile.h" -#include "particlesystem.h" +#include "object/particlesystem.h" +#include "object/background.h" +#include "object/tilemap.h" +#include "object/camera.h" +#include "object/player.h" #include "resources.h" -#include "background.h" -#include "tilemap.h" #include "app/gettext.h" #include "worldmap.h" #include "intro.h" #include "misc.h" -#include "camera.h" #include "statistics.h" #include "timer.h" #include "object/fireworks.h" diff --git a/src/level.cpp b/src/level.cpp index 88f006389..6960828e6 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -30,7 +30,6 @@ #include "app/globals.h" #include "app/setup.h" -#include "camera.h" #include "video/screen.h" #include "level.h" #include "math/physic.h" @@ -39,9 +38,10 @@ #include "tile.h" #include "utils/lispreader.h" #include "resources.h" -#include "gameobjs.h" #include "utils/lispwriter.h" -#include "tilemap.h" +#include "object/gameobjs.h" +#include "object/camera.h" +#include "object/tilemap.h" using namespace std; @@ -52,16 +52,6 @@ Level::Level() } void -Level::create(const std::string& filename) -{ - Level level; - const size_t width = 25; - const size_t height = 19; - level.add_sector(Sector::create("main", width, height)); - level.save(filename); -} - -void Level::load(const std::string& filepath) { LispReader* level = LispReader::load(filepath, "supertux-level"); @@ -167,8 +157,10 @@ Level::~Level() void Level::do_vertical_flip() { +#if 0 for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) i->second->do_vertical_flip(); +#endif } void @@ -242,10 +234,7 @@ Level::get_total_coins() int total_coins = 0; for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) { TileMap* solids = i->second->solids; - if(!solids) { - std::cerr << "Sector '" << i->first << "' contains no solids!?!\n"; - continue; - } + assert(solids != 0); for(size_t x = 0; x < solids->get_width(); ++x) for(size_t y = 0; y < solids->get_height(); ++y) { const Tile* tile = solids->get_tile(x, y); diff --git a/src/level.h b/src/level.h index d627d8245..a26435355 100644 --- a/src/level.h +++ b/src/level.h @@ -54,7 +54,6 @@ public: // loads a levelfile void load(const std::string& filename); void save(const std::string& filename); - static void create(const std::string& filename); EndSequenceType get_end_sequence_type() const { return end_sequence_type; } diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index c9b232010..d6d8a2ecc 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -31,13 +31,13 @@ #include "leveleditor.h" #include "resources.h" #include "tile.h" -#include "tilemap.h" #include "tile_manager.h" #include "sector.h" -#include "background.h" #include "gameloop.h" -#include "gameobjs.h" -#include "camera.h" +#include "object/gameobjs.h" +#include "object/camera.h" +#include "object/tilemap.h" +#include "object/background.h" LevelEditor::LevelEditor() { @@ -288,7 +288,9 @@ while(SDL_PollEvent(&event)) level_subset->description = create_subset_menu->get_item_by_id(MN_ID_DESCRIPTION_SUBSET).input; //FIXME: generate better level filenames level_subset->add_level(subset_name+'/'+"new_level.stl"); - Level::create(level_subset->get_level_filename(0)); + Level* newlevel = new Level(); + newlevel->add_sector(create_sector("main", 25, 19)); + newlevel->save(level_subset->get_level_filename(0)); level_subset->save(); load_level(0); @@ -368,7 +370,9 @@ while(SDL_PollEvent(&event)) if(confirm_dialog(NULL, str)) { level_subset->add_level("new_level.stl"); - Level::create(level_subset->get_level_filename(level_nb + 1)); + Level* newlevel = new Level(); + newlevel->add_sector(create_sector("main", 25, 19)); + newlevel->save(level_subset->get_level_filename(level_nb + 1)); level_subset->save(); load_level(level_nb + 1); } @@ -780,7 +784,7 @@ if(sector_ == NULL) { if(!confirm_dialog(NULL, _("No more sectors exist. Create another?"))) return; - sector_ = Sector::create("new_sector",25,19); + sector_ = create_sector("new_sector",25,19); level->add_sector(sector_); } @@ -1033,3 +1037,19 @@ for(unsigned int i = 0; i < sizeof(text) / sizeof(text[0]); i++) show_grid = show_grid_t; mouse_cursor->set_state(MC_NORMAL); } + +Sector* +LevelEditor::create_sector(const std::string& name, size_t width, size_t height) +{ + Sector* sector = new Sector; + sector->set_name(name); + + sector->add_object(new TileMap(LAYER_BACKGROUNDTILES, false, width, height)); + sector->add_object(new TileMap(LAYER_TILES, true, width, height)); + sector->add_object(new TileMap(LAYER_FOREGROUNDTILES, false, width, height)); + sector->add_object(new Camera(sector)); + sector->update_game_objects(); + + return sector; +} + diff --git a/src/leveleditor.h b/src/leveleditor.h index 32099a202..5867d88e6 100644 --- a/src/leveleditor.h +++ b/src/leveleditor.h @@ -143,6 +143,9 @@ private: Vector selection_ini, selection_end; bool level_changed; + +private: + Sector* create_sector(const std::string& name, size_t width, size_t height); }; #endif diff --git a/src/misc.h b/src/misc.h index e74a85450..bef88781b 100644 --- a/src/misc.h +++ b/src/misc.h @@ -19,14 +19,14 @@ #define SUPERTUX_MISC_H #include "app/setup.h" -#include "resources.h" +#include "app/gettext.h" #include "gui/menu.h" #include "utils/configfile.h" -#include "player.h" #include "title.h" +#include "resources.h" #include "worldmap.h" #include "gameloop.h" -#include "app/gettext.h" +#include "object/player.h" class MyConfig : public Config { diff --git a/src/background.cpp b/src/object/background.cpp similarity index 98% rename from src/background.cpp rename to src/object/background.cpp index 60d217cf3..2d6cc4121 100644 --- a/src/background.cpp +++ b/src/object/background.cpp @@ -47,7 +47,6 @@ Background::Background(LispReader& reader) Background::~Background() { - printf("bgfree.\n"); delete image; } @@ -90,7 +89,6 @@ Background::set_image(const std::string& name, float speed) this->imagefile = name; this->speed = speed; - printf("seti %p\n", this); delete image; image = new Surface(datadir + "/images/background/" + name, false); } diff --git a/src/background.h b/src/object/background.h similarity index 100% rename from src/background.h rename to src/object/background.h diff --git a/src/camera.cpp b/src/object/camera.cpp similarity index 100% rename from src/camera.cpp rename to src/object/camera.cpp diff --git a/src/camera.h b/src/object/camera.h similarity index 100% rename from src/camera.h rename to src/object/camera.h diff --git a/src/gameobjs.cpp b/src/object/gameobjs.cpp similarity index 100% rename from src/gameobjs.cpp rename to src/object/gameobjs.cpp diff --git a/src/gameobjs.h b/src/object/gameobjs.h similarity index 100% rename from src/gameobjs.h rename to src/object/gameobjs.h diff --git a/src/particlesystem.cpp b/src/object/particlesystem.cpp similarity index 100% rename from src/particlesystem.cpp rename to src/object/particlesystem.cpp diff --git a/src/particlesystem.h b/src/object/particlesystem.h similarity index 100% rename from src/particlesystem.h rename to src/object/particlesystem.h diff --git a/src/player.cpp b/src/object/player.cpp similarity index 99% rename from src/player.cpp rename to src/object/player.cpp index 6608f8c48..11a6ae214 100644 --- a/src/player.cpp +++ b/src/object/player.cpp @@ -31,13 +31,13 @@ #include "tile.h" #include "special/sprite.h" #include "sector.h" -#include "tilemap.h" -#include "camera.h" -#include "gameobjs.h" #include "resources.h" #include "video/screen.h" #include "statistics.h" #include "gameloop.h" +#include "object/tilemap.h" +#include "object/camera.h" +#include "object/gameobjs.h" #include "trigger/trigger_base.h" static const int TILES_FOR_BUTTJUMP = 3; diff --git a/src/player.h b/src/object/player.h similarity index 100% rename from src/player.h rename to src/object/player.h diff --git a/src/tilemap.cpp b/src/object/tilemap.cpp similarity index 100% rename from src/tilemap.cpp rename to src/object/tilemap.cpp diff --git a/src/tilemap.h b/src/object/tilemap.h similarity index 100% rename from src/tilemap.h rename to src/object/tilemap.h diff --git a/src/resources.cpp b/src/resources.cpp index da929586f..5267f4b8a 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -25,9 +25,9 @@ #include "gui/menu.h" #include "gui/button.h" #include "scene.h" -#include "player.h" -#include "gameobjs.h" #include "resources.h" +#include "object/gameobjs.h" +#include "object/player.h" Surface* img_waves[3]; Surface* img_water; diff --git a/src/sector.cpp b/src/sector.cpp index fedeee512..98731009a 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -29,12 +29,12 @@ #include "app/globals.h" #include "sector.h" #include "utils/lispreader.h" -#include "gameobjs.h" -#include "camera.h" -#include "background.h" -#include "particlesystem.h" +#include "object/gameobjs.h" +#include "object/camera.h" +#include "object/background.h" +#include "object/particlesystem.h" +#include "object/tilemap.h" #include "tile.h" -#include "tilemap.h" #include "audio/sound_manager.h" #include "gameloop.h" #include "resources.h" @@ -69,13 +69,13 @@ Sector::Sector() song_title = "Mortimers_chipdisko.mod"; player = new Player(); add_object(player); - - printf("seccreated: %p.\n", this); } Sector::~Sector() { - printf("secdel: %p.\n", this); + update_game_objects(); + assert(gameobjects_new.size() == 0); + for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) { delete *i; @@ -89,23 +89,6 @@ Sector::~Sector() _current = 0; } -Sector *Sector::create(const std::string& name, size_t width, size_t height) -{ - Sector *sector = new Sector; - sector->name = name; - TileMap *background = new TileMap(LAYER_BACKGROUNDTILES, false, width, height); - TileMap *interactive = new TileMap(LAYER_TILES, true, width, height); - TileMap *foreground = new TileMap(LAYER_FOREGROUNDTILES, false, width, height); - sector->add_object(background); - sector->add_object(interactive); - sector->add_object(foreground); - sector->solids = interactive; - sector->camera = new Camera(sector); - sector->add_object(sector->camera); - sector->update_game_objects(); - return sector; -} - GameObject* Sector::parse_object(const std::string& name, LispReader& reader) { @@ -151,14 +134,6 @@ Sector::parse_object(const std::string& name, LispReader& reader) } else if(name == "nolok_01") { return new Nolok_01(reader); } -#if 0 - else if(badguykind_from_string(name) != BAD_INVALID) { - return new BadGuy(badguykind_from_string(name), reader); - } else if(name == "trampoline") { - return new Trampoline(reader); - } else if(name == "flying-platform") { - return new FlyingPlatform(reader); -#endif std::cerr << "Unknown object type '" << name << "'.\n"; return 0; @@ -197,6 +172,9 @@ Sector::parse(LispReader& lispreader) } } + update_game_objects(); + fix_old_tiles(); + update_game_objects(); if(!camera) { std::cerr << "sector '" << name << "' does not contain a camera.\n"; camera = new Camera(this); @@ -237,11 +215,11 @@ Sector::parse_old_format(LispReader& reader) bkgd_bottom.blue = b; if(backgroundimage != "") { - background = new Background; + Background* background = new Background; background->set_image(backgroundimage, bgspeed); add_object(background); } else { - background = new Background; + Background* background = new Background; background->set_gradient(bkgd_top, bkgd_bottom); add_object(background); } @@ -275,10 +253,7 @@ Sector::parse_old_format(LispReader& reader) || reader.read_int_vector("tilemap", tiles)) { TileMap* tilemap = new TileMap(); tilemap->set(width, height, tiles, LAYER_TILES, true); - solids = tilemap; add_object(tilemap); - - fix_old_tiles(); } if(reader.read_int_vector("background-tm", tiles)) { @@ -338,8 +313,14 @@ Sector::parse_old_format(LispReader& reader) } // add a camera - camera = new Camera(this); + Camera* camera = new Camera(this); add_object(camera); + + update_game_objects(); + fix_old_tiles(); + update_game_objects(); + if(solids == 0) + throw std::runtime_error("sector does not contain a solid tile layer."); } void @@ -411,41 +392,6 @@ Sector::write(LispWriter& writer) } void -Sector::do_vertical_flip() -{ - // remove or fix later -#if 0 - 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 - 32; - FlyingPlatform* flying_platform = dynamic_cast (*i); - if(flying_platform) - flying_platform->base.y = solids->get_height()*32 - flying_platform->base.y - 32; - Door* door = dynamic_cast (*i); - if(door) - door->set_area(door->get_area().x, solids->get_height()*32 - door->get_area().y - 32); - } - - for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end(); - ++i) { - SpawnPoint* spawn = *i; - spawn->pos.y = solids->get_height()*32 - spawn->pos.y - 32; - } -#endif -} - -void Sector::add_object(GameObject* object) { // make sure the object isn't already in the list @@ -536,8 +482,7 @@ Sector::action(float elapsed_time) } /* Handle all possible collisions. */ - collision_handler(); - + collision_handler(); update_game_objects(); } @@ -573,7 +518,6 @@ Sector::update_game_objects() if(tilemap && tilemap->is_solid()) { if(solids == 0) { solids = tilemap; - fix_old_tiles(); } else { std::cerr << "Another solid tilemaps added. Ignoring."; } @@ -774,14 +718,6 @@ Sector::collision_handler() } } -void -Sector::add_score(const Vector& pos, int s) -{ - global_stats.add_points(SCORE_STAT, s); - - add_object(new FloatingText(pos, s)); -} - bool Sector::add_bullet(const Vector& pos, float xm, Direction dir) { diff --git a/src/sector.h b/src/sector.h index 598842bb5..a30e5d41d 100644 --- a/src/sector.h +++ b/src/sector.h @@ -67,8 +67,6 @@ public: Sector(); ~Sector(); - /// create new sector - static Sector *create(const std::string& name, size_t width, size_t height); /// read sector from lisp file void parse(LispReader& reader); void parse_old_format(LispReader& reader); @@ -88,6 +86,8 @@ public: /// adds a gameobject void add_object(GameObject* object); + void set_name(const std::string& name) + { this->name = name; } const std::string& get_name() const { return name; } @@ -102,16 +102,10 @@ public: case (or not). */ void collision_handler(); - void add_score(const Vector& pos, int s); - bool add_bullet(const Vector& pos, float xm, Direction dir); bool add_smoke_cloud(const Vector& pos); void add_floating_text(const Vector& pos, const std::string& text); - /** 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@ but can#t always be avoided in current design... */ static Sector* current() { return _current; } diff --git a/src/supertux.cpp b/src/supertux.cpp index 23a3fcf21..77ea138fe 100644 --- a/src/supertux.cpp +++ b/src/supertux.cpp @@ -39,7 +39,6 @@ #include "video/surface.h" #include "tile_manager.h" #include "app/gettext.h" -#include "player.h" #include "misc.h" #include "utils/configfile.h" diff --git a/src/title.cpp b/src/title.cpp index 4874916c1..714f9bd0b 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -52,14 +52,14 @@ #include "worldmap.h" #include "leveleditor.h" #include "scene.h" -#include "player.h" #include "tile.h" #include "sector.h" -#include "tilemap.h" +#include "object/tilemap.h" +#include "object/camera.h" +#include "object/player.h" #include "resources.h" #include "app/gettext.h" #include "misc.h" -#include "camera.h" static Surface* bkg_title; static Surface* logo; diff --git a/src/trigger/trigger_base.cpp b/src/trigger/trigger_base.cpp index e6b143a2d..608b784a3 100644 --- a/src/trigger/trigger_base.cpp +++ b/src/trigger/trigger_base.cpp @@ -19,8 +19,8 @@ #include #include "trigger_base.h" -#include "player.h" #include "video/drawing_context.h" +#include "object/player.h" TriggerBase::TriggerBase() : sprite(0) -- 2.11.0