From: Matthias Braun Date: Fri, 26 Nov 2004 13:56:32 +0000 (+0000) Subject: fixed broken 1-time animations in sprites, fixed collision code returning no-collisio... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=6e843b1780f62f45b7021bd8c38181aa211588ee;p=supertux.git fixed broken 1-time animations in sprites, fixed collision code returning no-collision if both objects didn't move, some cleanups SVN-Revision: 2201 --- diff --git a/lib/app/globals.cpp b/lib/app/globals.cpp index 3b042f11d..67819d0aa 100644 --- a/lib/app/globals.cpp +++ b/lib/app/globals.cpp @@ -70,7 +70,8 @@ std::string st_dir, st_save_dir; SDL_Joystick * js; /* Returns 1 for every button event, 2 for a quit event and 0 for no event. */ -int wait_for_event(SDL_Event& event,unsigned int min_delay, unsigned int max_delay, bool empty_events) +int wait_for_event(SDL_Event& event, unsigned int min_delay, + unsigned int max_delay, bool empty_events) { int i; Timer maxdelay; diff --git a/lib/math/physic.cpp b/lib/math/physic.cpp index 7cc570b97..6276a0918 100644 --- a/lib/math/physic.cpp +++ b/lib/math/physic.cpp @@ -23,7 +23,6 @@ #include #include "math/physic.h" -#include "special/timer.h" using namespace SuperTux; diff --git a/lib/special/collision.cpp b/lib/special/collision.cpp index b192dc37b..889899f76 100644 --- a/lib/special/collision.cpp +++ b/lib/special/collision.cpp @@ -38,7 +38,11 @@ Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1, hit.normal.y = 0; } else { if(movement.y > -DELTA && movement.y < DELTA) { - return false; + hit.time = 0; + hit.depth = 0; + hit.normal.x = 1; + hit.normal.y = 0; + return true; } hit.time = FLT_MAX; } diff --git a/lib/special/sprite.cpp b/lib/special/sprite.cpp index 18c606777..6ae659855 100644 --- a/lib/special/sprite.cpp +++ b/lib/special/sprite.cpp @@ -57,7 +57,7 @@ Sprite::set_action(std::string name, int loops) return; SpriteData::Action* newaction = data.get_action(name); - if(!action) { + if(!newaction) { #ifdef DEBUG std::cerr << "Action '" << name << "' not found.\n"; #endif @@ -72,7 +72,7 @@ Sprite::set_action(std::string name, int loops) bool Sprite::check_animation() { - return animation_loops; + return animation_loops == 0; } void @@ -87,14 +87,12 @@ Sprite::update() frame += frame_inc; - float lastframe = frame; - frame = fmodf(frame+get_frames(), get_frames()); - if(frame != lastframe) { - if(animation_loops > 0) { - animation_loops--; - if(animation_loops == 0) - frame = 0; - } + if(frame > get_frames()) { + frame = fmodf(frame+get_frames(), get_frames()); + + animation_loops--; + if(animation_loops == 0) + frame = 0; } } @@ -146,4 +144,3 @@ Sprite::get_height() const } -/* EOF */ diff --git a/src/background.cpp b/src/background.cpp index 56506e5ba..60d217cf3 100644 --- a/src/background.cpp +++ b/src/background.cpp @@ -47,6 +47,7 @@ Background::Background(LispReader& reader) Background::~Background() { + printf("bgfree.\n"); delete image; } @@ -89,6 +90,7 @@ 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); } @@ -108,17 +110,10 @@ void Background::draw(DrawingContext& context) { if(type == GRADIENT) { - /* In case we are using OpenGL just draw the gradient, else (software mode) - use the cache. */ - if(use_gl) - context.draw_gradient(gradient_top, gradient_bottom, layer); - else - { - context.push_transform(); - context.set_translation(Vector(0, 0)); - context.draw_surface(image, Vector(0, 0), layer); - context.pop_transform(); - } + context.push_transform(); + context.set_translation(Vector(0, 0)); + context.draw_surface(image, Vector(0, 0), layer); + context.pop_transform(); } else if(type == IMAGE) { if(!image) return; diff --git a/src/camera.cpp b/src/camera.cpp index 458d7031f..b2f47ed8f 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -52,7 +52,7 @@ Camera::get_translation() const } void -Camera::read(LispReader& reader) +Camera::parse(LispReader& reader) { std::string modename; diff --git a/src/camera.h b/src/camera.h index 07656260d..2294aa964 100644 --- a/src/camera.h +++ b/src/camera.h @@ -44,7 +44,7 @@ public: virtual ~Camera(); /// parse camera mode from lisp file - void read(LispReader& reader); + void parse(LispReader& reader); /// write camera mode to a lisp file virtual void write(LispWriter& writer); diff --git a/src/collision.cpp b/src/collision.cpp deleted file mode 100644 index 237590f02..000000000 --- a/src/collision.cpp +++ /dev/null @@ -1,300 +0,0 @@ -// $Id$ -// -// SuperTux -// Copyright (C) 2004 Tobias Glaesser -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. - -#include - -#include -#include "defines.h" -#include "collision.h" -#include "scene.h" -#include "sector.h" -#include "tilemap.h" -#include "tile.h" - -#if 0 -bool rectcollision(const base_type& one, const base_type& two) -{ - return (one.x >= two.x - one.width + 1 && - one.x <= two.x + two.width - 1 && - one.y >= two.y - one.height + 1 && - one.y <= two.y + two.height - 1); -} - -bool rectcollision_offset(const base_type& one, const base_type& two, float off_x, float off_y) -{ - return (one.x >= two.x - one.width + off_x + 1 && - one.x <= two.x + two.width + off_x - 1 && - one.y >= two.y - one.height + off_y + 1 && - one.y <= two.y + two.height + off_y - 1); -} - -bool collision_object_map(const Rectangle& rect) -{ - base_type base; - base.x = rect.p1.x; - base.y = rect.p1.y; - base.width = rect.get_width(); - base.height = rect.get_height(); - return collision_object_map(base); -} - -bool collision_object_map(const base_type& base) -{ - const TileMap& tilemap = *Sector::current()->solids; - - // we make the collision rectangle 1 pixel smaller - int starttilex = int(base.x+1) / 32; - int starttiley = int(base.y+1) / 32; - int max_x = int(base.x + base.width); - int max_y = int(base.y + base.height); - - for(int x = starttilex; x*32 < max_x; ++x) { - for(int y = starttiley; y*32 < max_y; ++y) { - const Tile* tile = tilemap.get_tile(x, y); - if(tile && tile->attributes & Tile::SOLID) - return true; - } - } - - return false; -} - -void* collision_func(const base_type& base, tiletestfunction function) -{ - const TileMap& tilemap = *Sector::current()->solids; - - int starttilex = int(base.x) / 32; - int starttiley = int(base.y) / 32; - int max_x = int(base.x + base.width); - int max_y = int(base.y + base.height); - - for(int x = starttilex; x*32 < max_x; ++x) { - for(int y = starttiley; y*32 < max_y; ++y) { - const Tile* tile = tilemap.get_tile(x, y); - void* result = function(tile); - if(result != 0) - return result; - } - } - - return 0; -} - -static void* test_goal_tile_function(const Tile* tile) -{ - if(tile && (tile->attributes & Tile::GOAL)) - return const_cast ((const void*) tile); // evil cast... - return 0; -} - -const Tile* collision_goal(const Rectangle& rect) -{ - // too lazy to rewrite for now, so we transform to base_type... - base_type base; - base.x = rect.p1.x; - base.y = rect.p1.y; - base.width = rect.get_width(); - base.height = rect.get_height(); - return (const Tile*) collision_func(base, test_goal_tile_function); -} - -void collision_swept_object_map(base_type* old, base_type* current) -{ - int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */ - int h; - float lpath; /* Holds the longest path, which is either in X or Y direction. */ - float xd,yd; /* Hold the smallest steps in X and Y directions. */ - float temp, xt, yt; /* Temporary variable. */ - - lpath = 0; - xd = 0; - yd = 0; - - if(old->x == current->x && old->y == current->y) - { - return; - } - else if(old->x == current->x && old->y != current->y) - { - lpath = current->y - old->y; - if(lpath < 0) - { - yd = -1; - lpath = -lpath; - } - else - { - yd = 1; - } - - h = 1; - xd = 0; - } - else if(old->x != current->x && old->y == current->y) - { - lpath = current->x - old->x; - if(lpath < 0) - { - xd = -1; - lpath = -lpath; - } - else - { - xd = 1; - } - h = 2; - yd = 0; - } - else - { - lpath = current->x - old->x; - if(lpath < 0) - lpath = -lpath; - if(current->y - old->y > lpath || old->y - current->y > lpath) - lpath = current->y - old->y; - if(lpath < 0) - lpath = -lpath; - h = 3; - xd = (current->x - old->x) / lpath; - yd = (current->y - old->y) / lpath; - } - - steps = (int)(lpath / (float)16); - - float orig_x = old->x; - float orig_y = old->y; - old->x += xd; - old->y += yd; - - for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i) - { - if(steps > 0) - { - old->y += yd*16.; - old->x += xd*16.; - steps--; - } - - if(collision_object_map(*old)) - { - switch(h) - { - case 1: - current->y = old->y - yd; - while(collision_object_map(*current)) - current->y -= yd; - break; - case 2: - current->x = old->x - xd; - while(collision_object_map(*current)) - current->x -= xd; - break; - case 3: - xt = current->x; - yt = current->y; - current->x = old->x - xd; - current->y = old->y - yd; - while(collision_object_map(*current)) - { - current->x -= xd; - current->y -= yd; - } - - temp = current->x; - current->x = xt; - if(!collision_object_map(*current)) - break; - current->x = temp; - temp = current->y; - current->y = yt; - - if(!collision_object_map(*current)) - { - break; - } - else - { - current->y = temp; - while(!collision_object_map(*current)) - current->y += yd; - current->y -= yd; - break; - } - - break; - default: - break; - } - break; - } - } - - if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x)) - current->x = orig_x; - if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y)) - current->y = orig_y; - - *old = *current; -} - -const Tile* gettile(float x, float y) -{ - const TileMap& tilemap = *Sector::current()->solids; - return tilemap.get_tile_at(Vector(x, y)); -} - -bool issolid(float x, float y) -{ - const Tile* tile = gettile(x,y); - return tile && (tile->attributes & Tile::SOLID); -} - -bool isbrick(float x, float y) -{ - const Tile* tile = gettile(x,y); - return tile && (tile->attributes & Tile::BRICK); -} - -bool isice(float x, float y) -{ - const Tile* tile = gettile(x,y); - return tile && (tile->attributes & Tile::ICE); -} - -bool isspike(float x, float y) -{ - const Tile* tile = gettile(x,y); - return tile && (tile->attributes & Tile::SPIKE); -} - -bool isfullbox(float x, float y) -{ - const Tile* tile = gettile(x,y); - return tile && (tile->attributes & Tile::FULLBOX); -} - -bool iscoin(float x, float y) -{ - const Tile* tile = gettile(x,y); - return tile && (tile->attributes & Tile::COIN); -} - -#endif - diff --git a/src/collision.h b/src/collision.h deleted file mode 100644 index 39d658b9e..000000000 --- a/src/collision.h +++ /dev/null @@ -1,56 +0,0 @@ -// $Id$ -// -// SuperTux -// Copyright (C) 2004 Tobias Glaesser -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. - -#ifndef SUPERTUX_COLLISION_H -#define SUPERTUX_COLLISION_H - -#include "math/rectangle.h" - -using namespace SuperTux; - -#if 0 -bool rectcollision(const base_type& one, const base_type& two); -bool rectcollision_offset(const base_type& one, const base_type& two, float off_x, float off_y); - -void collision_swept_object_map(base_type* old, base_type* current); -bool collision_object_map(const base_type& object); -bool collision_object_map(const Rectangle& rect); - -/** Return a pointer to the tile at the given x/y coordinates */ -const Tile* gettile(float x, float y); - -// Some little helper function to check for tile properties -bool issolid(float x, float y); -bool isbrick(float x, float y); -bool isice(float x, float y); -bool isspike(float x, float y); -bool isfullbox(float x, float y); - -typedef void* (*tiletestfunction)(const Tile* tile); -/** invokes the function for each tile the baserectangle collides with. The - * function aborts and returns true as soon as the tiletestfunction returns - * != 0 then this value is returned. returns 0 if all tests failed. - */ -void* collision_func(const base_type& base, tiletestfunction* function); -const Tile* collision_goal(const Rectangle& rect); -#endif - -#endif /*SUPERTUX_COLLISION_H*/ - diff --git a/src/defines.h b/src/defines.h index f0b880c24..c408b688a 100644 --- a/src/defines.h +++ b/src/defines.h @@ -25,14 +25,11 @@ enum Direction { LEFT = 0, RIGHT = 1 }; -/* Direction (keyboard/joystick) states: */ - +/* keyboard/joystick states: */ #define UP 0 #define DOWN 1 /* Dying types: */ - -/* ---- NO 0 */ enum DyingType { DYING_NOT = 0, DYING_SQUISHED = 1, @@ -40,25 +37,14 @@ enum DyingType { }; /* Speed constraints: */ -#define MAX_WALK_XM 230 -#define MAX_RUN_XM 320 #define MAX_LIVES 99 -#define WALK_SPEED 100 - /* gameplay related defines */ - #define START_LIVES 4 #define MAX_FIRE_BULLETS 2 #define MAX_ICE_BULLETS 1 #define FROZEN_TIME 3.0 -#define WALK_ACCELERATION_X 300 -#define RUN_ACCELERATION_X 400 - -#define SKID_XM 200 -#define SKID_TIME .3 - #endif /*SUPERTUX_DEFINES_H*/ diff --git a/src/gameloop.cpp b/src/gameloop.cpp index fc9f471c1..2947f848c 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -50,7 +50,6 @@ #include "player.h" #include "level.h" #include "scene.h" -#include "collision.h" #include "tile.h" #include "particlesystem.h" #include "resources.h" diff --git a/src/gameobjs.h b/src/gameobjs.h index 5ebbe07df..3d928be0e 100644 --- a/src/gameobjs.h +++ b/src/gameobjs.h @@ -26,7 +26,6 @@ #include "timer.h" #include "scene.h" #include "math/physic.h" -#include "collision.h" #include "special/game_object.h" #include "special/moving_object.h" #include "serializable.h" diff --git a/src/level.cpp b/src/level.cpp index 8788cc08b..88f006389 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -126,36 +126,36 @@ Level::load_old_format(LispReader& reader) void Level::save(const std::string& filename) { - std::string filepath = "levels/" + filename; - int last_slash = filepath.find_last_of('/'); - FileSystem::fcreatedir(filepath.substr(0,last_slash).c_str()); - filepath = st_dir + "/" + filepath; - ofstream file(filepath.c_str(), ios::out); - LispWriter* writer = new LispWriter(file); + std::string filepath = "levels/" + filename; + int last_slash = filepath.find_last_of('/'); + FileSystem::fcreatedir(filepath.substr(0,last_slash).c_str()); + filepath = st_dir + "/" + filepath; + ofstream file(filepath.c_str(), ios::out); + LispWriter* writer = new LispWriter(file); - writer->write_comment("Level made using SuperTux's built-in Level Editor"); + writer->write_comment("Level made using SuperTux's built-in Level Editor"); - writer->start_list("supertux-level"); + writer->start_list("supertux-level"); - int version = 2; - writer->write_int("version", version); + int version = 2; + writer->write_int("version", version); - writer->write_string("name", name); - writer->write_string("author", author); - writer->write_int("time", timelimit); - writer->write_string("end-sequence-animation", - end_sequence_type == FIREWORKS_ENDSEQ_ANIM ? "fireworks" : "none"); + writer->write_string("name", name); + writer->write_string("author", author); + writer->write_int("time", timelimit); + writer->write_string("end-sequence-animation", + end_sequence_type == FIREWORKS_ENDSEQ_ANIM ? "fireworks" : "none"); - for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) { - writer->start_list("sector"); - i->second->write(*writer); - writer->end_list("sector"); - } + for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) { + writer->start_list("sector"); + i->second->write(*writer); + writer->end_list("sector"); + } - writer->end_list("supertux-level"); + writer->end_list("supertux-level"); - delete writer; - file.close(); + delete writer; + file.close(); } Level::~Level() diff --git a/src/player.cpp b/src/player.cpp index 2350f95b4..6608f8c48 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -40,13 +40,18 @@ #include "gameloop.h" #include "trigger/trigger_base.h" -// behavior definitions: -#define TILES_FOR_BUTTJUMP 3 -// animation times (in ms): -#define SHOOTING_TIME .150 - -// time before idle animation starts -#define IDLE_TIME 2.500 +static const int TILES_FOR_BUTTJUMP = 3; +static const float SHOOTING_TIME = .150; +/// time before idle animation starts +static const float IDLE_TIME = 2.5; + +static const float WALK_ACCELERATION_X = 300; +static const float RUN_ACCELERATION_X = 400; +static const float SKID_XM = 200; +static const float SKID_TIME = .3; +static const float MAX_WALK_XM = 230; +static const float MAX_RUN_XM = 320; +static const float WALK_SPEED = 100; // growing animation Surface* growingtux_left[GROWING_FRAMES]; diff --git a/src/player.h b/src/player.h index ada9e96aa..be6398cba 100644 --- a/src/player.h +++ b/src/player.h @@ -23,7 +23,6 @@ #include "timer.h" #include "video/surface.h" -#include "collision.h" #include "special/moving_object.h" #include "special/sprite.h" #include "math/physic.h" diff --git a/src/sector.cpp b/src/sector.cpp index e98ac976c..fedeee512 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -69,10 +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); for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) { delete *i; @@ -110,25 +113,11 @@ Sector::parse_object(const std::string& name, LispReader& reader) background = new Background(reader); return background; } else if(name == "camera") { - if(camera) { - std::cerr << "Warning: More than 1 camera defined in sector.\n"; - return 0; - } - camera = new Camera(this); - camera->read(reader); + Camera* camera = new Camera(this); + camera->parse(reader); return camera; } else if(name == "tilemap") { - TileMap* tilemap = new TileMap(reader); - - if(tilemap->is_solid()) { - if(solids) { - std::cerr << "Warning multiple solid tilemaps in sector.\n"; - return 0; - } - solids = tilemap; - fix_old_tiles(); - } - return tilemap; + return new TileMap(reader); } else if(name == "particles-snow") { SnowParticleSystem* partsys = new SnowParticleSystem(); partsys->parse(reader); @@ -565,15 +554,6 @@ Sector::update_game_objects() std::remove(bullets.begin(), bullets.end(), bullet), bullets.end()); } -#if 0 - InteractiveObject* interactive_object = - dynamic_cast (*i); - if(interactive_object) { - interactive_objects.erase( - std::remove(interactive_objects.begin(), interactive_objects.end(), - interactive_object), interactive_objects.end()); - } -#endif delete *i; i = gameobjects.erase(i); } else { @@ -585,17 +565,30 @@ Sector::update_game_objects() for(std::vector::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i) { - Bullet* bullet = dynamic_cast (*i); - if(bullet) - bullets.push_back(bullet); -#if 0 - InteractiveObject* interactive_object - = dynamic_cast (*i); - if(interactive_object) - interactive_objects.push_back(interactive_object); -#endif + Bullet* bullet = dynamic_cast (*i); + if(bullet) + bullets.push_back(bullet); + + TileMap* tilemap = dynamic_cast (*i); + if(tilemap && tilemap->is_solid()) { + if(solids == 0) { + solids = tilemap; + fix_old_tiles(); + } else { + std::cerr << "Another solid tilemaps added. Ignoring."; + } + } + + Camera* camera = dynamic_cast (*i); + if(camera) { + if(this->camera != 0) { + std::cerr << "Warning: Multiple cameras added. Ignoring."; + continue; + } + this->camera = camera; + } - gameobjects.push_back(*i); + gameobjects.push_back(*i); } gameobjects_new.clear(); } diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index 1cd860207..88ec57986 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -17,7 +17,6 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. - #include #include diff --git a/src/tile_manager.h b/src/tile_manager.h index 6c2e8cc13..2faa36224 100644 --- a/src/tile_manager.h +++ b/src/tile_manager.h @@ -89,5 +89,3 @@ class TileManager }; #endif - -/* EOF */ diff --git a/src/trigger/door.cpp b/src/trigger/door.cpp index 43a4b291d..4c338a55f 100644 --- a/src/trigger/door.cpp +++ b/src/trigger/door.cpp @@ -77,7 +77,7 @@ void Door::action(float ) { //Check if door animation is complete - if (!sprite->check_animation()) { + if(sprite->check_animation()) { GameSession::current()->respawn(target_sector, target_spawnpoint); } } diff --git a/src/worldmap.h b/src/worldmap.h index 2018d85b2..4bf2b69ce 100644 --- a/src/worldmap.h +++ b/src/worldmap.h @@ -16,7 +16,6 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - #ifndef SUPERTUX_WORLDMAP_H #define SUPERTUX_WORLDMAP_H