[Super-tux-commit] supertux/lib/special collision.cpp,NONE,1.1 collision.h,NONE,1.1 collision_hit.h,
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2004-11-20 22:15:19
|
Update of /cvsroot/super-tux/supertux/lib/special In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5941/lib/special Modified Files: frame_rate.cpp game_object.cpp game_object.h moving_object.cpp moving_object.h sprite.cpp sprite.h sprite_manager.cpp sprite_manager.h timer.cpp timer.h Added Files: collision.cpp collision.h collision_hit.h sprite_data.h Log Message: The BIG COMMIT(tm) This is it, expect a broken supertux I only fixed the most annoying issues so far, lots more are still waiting in the TODO list, but I had to bring this stuff to cvs sometime. Changes: - Completely new collision detection scheme, which collides all objects with each other once per frame, instead of single objects manually testing for collisions - New collision detection routines which support slopes and provide additional info on collisions: penetration depth, hit normal vector - Lots of general cleanup/refactoring - Splitted the monolithic badguy class and reimplemented most of them as single classes with a badguy base class. - Splitted the monolithic Special class into several classes - Rewrote the timer and all timing related stuff to work on float and seconds (not like the mixup before where you had frame_ratio, msecs and secs in different parts of the app) - Support for unisolid tiles - Created a flying platform prototype (doesn't work completely yet) - rename InteractiveObjects to triggers and implemented a new sequence trigger, (only supported sequence is endsequence at the moment) - transformed the bonusblocks and bricks into objects Index: moving_object.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/moving_object.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- moving_object.cpp 22 Jul 2004 19:07:50 -0000 1.3 +++ moving_object.cpp 20 Nov 2004 22:14:36 -0000 1.4 @@ -17,14 +17,14 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include <config.h> + #include "../special/moving_object.h" using namespace SuperTux; MovingObject::MovingObject() { - base.x = base.y = base.width = base.height = 0; - old_base = base; } MovingObject::~MovingObject() Index: timer.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/timer.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- timer.h 28 Jul 2004 23:06:12 -0000 1.5 +++ timer.h 20 Nov 2004 22:14:36 -0000 1.6 @@ -30,12 +30,12 @@ { public: /// Time a game is running. (Non-pause mode, etc.) - static unsigned int get(void); + static unsigned int get(); - static void pause_init(void); - static void pause_start(void); - static void pause_stop(void); - static bool pause_started(void); + static void pause_init(); + static void pause_start(); + static void pause_stop(); + static bool pause_started(); private: static unsigned int pause_ticks; Index: frame_rate.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/frame_rate.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- frame_rate.cpp 29 Jul 2004 11:05:28 -0000 1.2 +++ frame_rate.cpp 20 Nov 2004 22:14:36 -0000 1.3 @@ -16,6 +16,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include <config.h> + #include "SDL.h" #include "../special/frame_rate.h" Index: sprite_manager.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite_manager.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- sprite_manager.cpp 17 Aug 2004 21:53:20 -0000 1.4 +++ sprite_manager.cpp 20 Nov 2004 22:14:36 -0000 1.5 @@ -16,13 +16,19 @@ // 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 <config.h> #include <iostream> +#include <sstream> +#include <stdexcept> -#include "../utils/lispreader.h" -#include "../special/sprite_manager.h" +#include "utils/lispreader.h" +#include "sprite_manager.h" +#include "sprite_data.h" +#include "sprite.h" -using namespace SuperTux; +namespace SuperTux +{ SpriteManager::SpriteManager(const std::string& filename) { @@ -31,8 +37,7 @@ SpriteManager::~SpriteManager() { - for(std::map<std::string, Sprite*>::iterator i = sprites.begin(); - i != sprites.end(); ++i) { + for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) { delete i->second; } } @@ -53,47 +58,42 @@ return; cur = lisp_cdr(cur); - while(cur) - { - lisp_object_t* el = lisp_car(cur); - - if (strcmp(lisp_symbol(lisp_car(el)), "sprite") == 0) - { - Sprite* sprite = new Sprite(lisp_cdr(el)); + while(cur) { + lisp_object_t* el = lisp_car(cur); - Sprites::iterator i = sprites.find(sprite->get_name()); - if (i == sprites.end()) - { - sprites[sprite->get_name()] = sprite; - } - else - { - delete i->second; - i->second = sprite; - std::cout << "Warning: dulpicate entry: '" << sprite->get_name() << "'" << std::endl; - } - } - else - { - std::cout << "SpriteManager: Unknown tag" << std::endl; - } + if (strcmp(lisp_symbol(lisp_car(el)), "sprite") == 0) { + SpriteData* spritedata = new SpriteData(lisp_cdr(el)); - cur = lisp_cdr(cur); + Sprites::iterator i = sprites.find(spritedata->get_name()); + if (i == sprites.end()) { + sprites[spritedata->get_name()] = spritedata; + } else { + delete i->second; + i->second = spritedata; + std::cout << "Warning: dulpicate entry: '" << spritedata->get_name() + << "' in spritefile." << std::endl; + } + } else { + std::cout << "SpriteManager: Unknown tag in spritefile.\n"; } + cur = lisp_cdr(cur); + } + lisp_free(root_obj); } Sprite* -SpriteManager::load(const std::string& name) +SpriteManager::create(const std::string& name) { Sprites::iterator i = sprites.find(name); - if (i == sprites.end()) - { - std::cerr << "Warning: Sprite '" << name << "' not found" << std::endl; - return 0; - } - return i->second; + if(i == sprites.end()) { + std::stringstream msg; + msg << "Sprite '" << name << "' not found."; + throw std::runtime_error(msg.str()); + } + return new Sprite(*i->second); +} + } -/* EOF */ --- NEW FILE: collision.h --- #ifndef __COLLISION_H__ #define __COLLISION_H__ namespace SuperTux { class Rectangle; class CollisionHit; class AATriangle; class Collision { public: /** does collision detection between 2 rectangles. Returns true in case of * collision and fills in the hit structure then. */ static bool rectangle_rectangle(CollisionHit& hit, const Rectangle& r1, const Rectangle& r2); /** does collision detection between a rectangle and an axis aligned triangle * Returns true in case of a collision and fills in the hit structure then. */ static bool rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect, const AATriangle& triangle); }; } #endif Index: game_object.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/game_object.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- game_object.cpp 22 Jul 2004 19:07:50 -0000 1.3 +++ game_object.cpp 20 Nov 2004 22:14:36 -0000 1.4 @@ -1,7 +1,7 @@ // $Id$ // // SuperTux - A Jump'n Run -// Copyright (C) 2004 Matthias Braun <ma...@br... +// Copyright (C) 2004 Matthias Braun <ma...@br...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -17,12 +17,15 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include <config.h> + #include "../special/game_object.h" -using namespace SuperTux; +namespace SuperTux +{ GameObject::GameObject() - : wants_to_die(false) + : wants_to_die(false), flags(0) { } @@ -30,3 +33,4 @@ { } +} Index: sprite.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.cpp,v retrieving revision 1.29 retrieving revision 1.30 diff -u -d -r1.29 -r1.30 --- sprite.cpp 25 Oct 2004 03:32:49 -0000 1.29 +++ sprite.cpp 20 Nov 2004 22:14:36 -0000 1.30 @@ -16,247 +16,141 @@ // 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 <config.h> #include <iostream> #include <cmath> +#include <stdexcept> #include "../app/globals.h" #include "../app/setup.h" #include "../special/sprite.h" #include "../video/drawing_context.h" -using namespace SuperTux; - -Sprite::Sprite(lisp_object_t* cur) +namespace SuperTux { - for(; !lisp_nil_p(cur); cur = lisp_cdr(cur)) - { - std::string token = lisp_symbol(lisp_car(lisp_car(cur))); - lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur))); - LispReader reader(lisp_cdr(lisp_car(cur))); - - if(token == "name") - name = lisp_string(data); - else if(token == "action") - parse_action(reader); - else - std::cerr << "Warning: Unknown sprite field: " << token << std::endl; - } - - if(name.empty()) - Termination::abort("Error: Sprite wihtout name.", ""); - if(actions.empty()) - Termination::abort("Error: Sprite wihtout actions.", ""); -} -Sprite::~Sprite() +Sprite::Sprite(SpriteData& newdata) + : data(newdata) { - for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act) - { - for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin(); - i_sur != i_act->second->surfaces.end(); ++i_sur) - { - delete *i_sur; - } - delete i_act->second; - } + action = data.actions.begin()->second; + reset(); } -void -Sprite::parse_action(LispReader& lispreader) +Sprite::Sprite(const Sprite& other) + : data(other.data), frame(other.frame), + animation_loops(other.animation_loops), last_tick(other.last_tick), + action(other.action), next_action(other.next_action) { - action = new Action; - - init_defaults(action); - - if(!lispreader.read_string("name", action->name)) - if(!actions.empty()) - Termination::abort("Error: If there are more than one action, they need names!", ""); - lispreader.read_int("x-offset", action->x_offset); - lispreader.read_int("y-offset", action->y_offset); - lispreader.read_int("z-order", action->z_order); - lispreader.read_float("fps", action->fps); - - /* TODO: add a top filter entry */ - std::vector <int> mask_color; - lispreader.read_int_vector("apply-mask", mask_color); - if(mask_color.size() == 4) - { - for(std::vector<Surface*>::iterator i = action->surfaces.begin(); - i < action->surfaces.end(); i++) - { - (*i)->apply_filter(MASK_FILTER, Color(mask_color)); - } - } - - std::string mirror_action; - lispreader.read_string("mirror-action", mirror_action); - if(!mirror_action.empty()) - { - Action* act_tmp = get_action(mirror_action); - if(act_tmp == NULL) - std::cerr << "Warning: Could not mirror action. Action not found\n" - "Mirror actions must be defined after the real one!\n"; - else - { - for(int i = 0; static_cast<unsigned int>(i) < act_tmp->surfaces.size(); i++) - { - Surface* surface = new Surface(sdl_surface_from_sdl_surface( - act_tmp->surfaces[i]->impl->get_sdl_surface(), true), true); - surface->apply_filter(HORIZONTAL_FLIP_FILTER); - action->surfaces.push_back(surface); - } - } - } - else // Load images - { - std::vector<std::string> images; - if(!lispreader.read_string_vector("images", images)) - Termination::abort("Sprite contains no images: ", action->name); - - for(std::vector<std::string>::size_type i = 0; i < images.size(); i++) - { - action->surfaces.push_back( - new Surface(datadir + "/images/" + images[i], true)); - } - } - actions[action->name] = action; } -/*void Sprite::parse_filter(LispReader& lispreader) -{ - -}*/ - -void -Sprite::init_defaults(Action* act) +Sprite::~Sprite() { - act->x_offset = 0; - act->y_offset = 0; - act->z_order = 0; - act->fps = 10; - - start_animation(-1); } void -Sprite::set_action(std::string act) +Sprite::set_action(std::string name) { -if(!next_action.empty() && animation_loops > 0) - { - next_action = act; - return; - } -Actions::iterator i = actions.find(act); -if(i == actions.end()) - { - std::cerr << "Warning: Action '" << act << "' not found on Sprite '" << name << "'\n"; - return; + if(!next_action.empty() && animation_loops > 0) { + next_action = name; + return; } -action = i->second; -} + SpriteData::Action* newaction = data.get_action(name); + if(!action) + return; -Sprite::Action* -Sprite::get_action(std::string act) -{ -Actions::iterator i = actions.find(act); -if(i == actions.end()) - { - std::cerr << "Warning: Action '" << act << "' not found on Sprite '" << name << "'\n"; - return NULL; - } -return i->second; + action = newaction; } void Sprite::start_animation(int loops) { -reset(); -animation_loops = loops; + reset(); + animation_loops = loops; } void Sprite::reset() { -frame = 0; -last_tick = SDL_GetTicks(); -animation_reversed = false; -next_action.clear(); + frame = 0; + last_tick = SDL_GetTicks(); + animation_reversed = false; + animation_loops = -1; + next_action.clear(); } bool Sprite::check_animation() { -return animation_loops; + return animation_loops; } void Sprite::reverse_animation(bool reverse) { -animation_reversed = reverse; + animation_reversed = reverse; -if(animation_reversed) - frame = get_frames()-1; -else - frame = 0; + if(animation_reversed) + frame = get_frames()-1; + else + frame = 0; } void Sprite::update() { -if(animation_loops == 0) + if(animation_loops == 0) { - if(frame >= get_frames() || frame < 0) - frame = 0; - return; + if(frame >= get_frames() || frame < 0) + frame = 0; + return; } -float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick); -last_tick = SDL_GetTicks(); + float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick); + last_tick = SDL_GetTicks(); -if(animation_reversed) - frame -= frame_inc; -else - frame += frame_inc; + if(animation_reversed) + frame -= frame_inc; + else + frame += frame_inc; -if(animation_reversed) - { - if(frame < 0 || frame >= (float)get_frames()) - { // last case can happen when not used reverse_animation() - float excedent = frame - 0; - frame = get_frames() - 1; - if(animation_loops > 0) + if(animation_reversed) { + if(frame < 0 || frame >= (float)get_frames()) { + // last case can happen when not used reverse_animation() + float excedent = frame - 0; + frame = get_frames() - 1; + if(animation_loops > 0) { - animation_loops--; - if(animation_loops == 0 && !next_action.empty()) + animation_loops--; + if(animation_loops == 0 && !next_action.empty()) { - set_action(next_action); - start_animation(-1); + set_action(next_action); + start_animation(-1); } } - if(fabsf(excedent) < get_frames()) - frame += excedent; + if(fabsf(excedent) < get_frames()) + frame += excedent; } } -else + else { - if(frame >= (float)get_frames()) + if(frame >= (float)get_frames()) { - float excedent = frame - get_frames(); - frame = 0; - if(animation_loops > 0) + float excedent = frame - get_frames(); + frame = 0; + if(animation_loops > 0) { - animation_loops--; - if(animation_loops == 0 && !next_action.empty()) + animation_loops--; + if(animation_loops == 0 && !next_action.empty()) { - set_action(next_action); - start_animation(-1); + set_action(next_action); + start_animation(-1); } } - if(excedent < get_frames()) - frame += excedent; + if(excedent < get_frames()) + frame += excedent; } } } @@ -273,12 +167,13 @@ << "/" << get_action_name() << std::endl; else context.draw_surface(action->surfaces[(int)frame], - pos - Vector(action->x_offset, action->y_offset), layer + action->z_order, drawing_effect); + pos - Vector(action->x_offset, action->y_offset), + layer + action->z_order, drawing_effect); } void -Sprite::draw_part(DrawingContext& context, const Vector& source, const Vector& size, - const Vector& pos, int layer, Uint32 drawing_effect) +Sprite::draw_part(DrawingContext& context, const Vector& source, + const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect) { update(); @@ -288,19 +183,22 @@ << "/" << get_action_name() << std::endl; else context.draw_surface_part(action->surfaces[(int)frame], source, size, - pos - Vector(action->x_offset, action->y_offset), layer + action->z_order, drawing_effect); + pos - Vector(action->x_offset, action->y_offset), + layer + action->z_order, drawing_effect); } int -Sprite::get_width() +Sprite::get_width() const { return action->surfaces[get_frame()]->w; } int -Sprite::get_height() +Sprite::get_height() const { return action->surfaces[get_frame()]->h; } +} + /* EOF */ Index: moving_object.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/moving_object.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- moving_object.h 22 Jul 2004 19:07:50 -0000 1.3 +++ moving_object.h 20 Nov 2004 22:14:36 -0000 1.4 @@ -20,10 +20,12 @@ #ifndef SUPERTUX_MOVING_OBJECT_H #define SUPERTUX_MOVING_OBJECT_H -#include "../special/base.h" -#include "../special/game_object.h" -#include "../math/vector.h" -//#include "rectangle.h" +#include "game_object.h" +#include "collision_hit.h" +#include "math/vector.h" +#include "math/rectangle.h" + +class Sector; namespace SuperTux { @@ -40,26 +42,35 @@ /** this function is called when the object collided with any other object */ - virtual void collision(const MovingObject& other_object, - int collision_type) = 0; + virtual HitResponse collision(GameObject& other, + const CollisionHit& hit) = 0; - Vector get_pos() const + const Vector& get_pos() const { - return Vector(base.x, base.y); + return bbox.p1; } - base_type base; - base_type old_base; + /** returns the bounding box of the Object */ + const Rectangle& get_bbox() const + { + return bbox; + } + + const Vector& get_movement() const + { + return movement; + } protected: -#if 0 // this will be used in my collision detection rewrite later - /// the current position of the object - Vector pos; - /// the position we want to move until next frame - Vector new_pos; - /// the bounding box relative to the current position - Rectangle bounding_box; -#endif + friend class Sector; + + /** The bounding box of the object (as used for collision detection, this + * isn't necessarily the bounding box for graphics) + */ + Rectangle bbox; + /** The movement that will happen till next frame + */ + Vector movement; }; } //namespace SuperTux --- NEW FILE: sprite_data.h --- // $Id: sprite_data.h,v 1.1 2004/11/20 22:14:36 matzebraun Exp $ // // SuperTux // Copyright (C) 2004 Ingo Ruhnke <gr...@gm...>, // (C) 2004 Matthias Braun <ma...@br...> // // 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_SPRITE_DATA_H #define SUPERTUX_SPRITE_DATA_H #include <string> #include <vector> #include <map> #include "../utils/lispreader.h" #include "../video/surface.h" namespace SuperTux { class SpriteData { public: /** cur has to be a pointer to data in the form of ((x-offset 5) (y-offset 10) ...) */ SpriteData(lisp_object_t* cur); ~SpriteData(); const std::string& get_name() const { return name; } private: friend class Sprite; struct Action { Action(); ~Action(); std::string name; /** Position correction */ int x_offset; int y_offset; /** Drawing priority in queue */ int z_order; /** Frames per second */ float fps; /** Mirror is used to avoid duplicating left and right side sprites */ // bool mirror; std::vector<Surface*> surfaces; }; typedef std::map <std::string, Action*> Actions; Actions actions; void parse_action(LispReader& lispreader); /** Get an action */ Action* get_action(std::string act); std::string name; }; } #endif --- NEW FILE: collision.cpp --- #include <config.h> #include "collision.h" #include <algorithm> #include <iostream> #include <stdio.h> #include <float.h> #include <math.h> #include "math/vector.h" #include "math/aatriangle.h" #include "math/rectangle.h" #include "collision_hit.h" namespace SuperTux { bool Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1, const Rectangle& r2) { if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x) return false; if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y) return false; hit.depth = r1.p2.x - r2.p1.x; hit.normal.x = -1; hit.normal.y = 0; float px2 = r2.p2.x - r1.p1.x; if(px2 < hit.depth) { hit.depth = px2; hit.normal.x = 1; hit.normal.y = 0; } float py1 = r1.p2.y - r2.p1.y; if(py1 < hit.depth) { hit.depth = py1; hit.normal.x = 0; hit.normal.y = -1; } float py2 = r2.p2.y - r1.p1.y; if(py2 < hit.depth) { hit.depth = py2; hit.normal.x = 0; hit.normal.y = 1; } return true; } //--------------------------------------------------------------------------- static void makePlane(const Vector& p1, const Vector& p2, Vector& n, float& c) { n = Vector(p2.y-p1.y, p1.x-p2.x); c = -(p2 * n); float nval = n.norm(); n /= nval; c /= nval; } bool Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect, const AATriangle& triangle) { if(!rectangle_rectangle(hit, rect, (const Rectangle&) triangle)) return false; Vector normal; float c; Vector p1; switch(triangle.dir) { case AATriangle::SOUTHWEST: p1 = Vector(rect.p1.x, rect.p2.y); makePlane(triangle.p1, triangle.p2, normal, c); break; case AATriangle::NORTHEAST: p1 = Vector(rect.p2.x, rect.p1.y); makePlane(triangle.p2, triangle.p1, normal, c); break; case AATriangle::SOUTHEAST: p1 = rect.p2; makePlane(Vector(triangle.p1.x, triangle.p2.y), Vector(triangle.p2.x, triangle.p1.y), normal, c); break; case AATriangle::NORTHWEST: p1 = rect.p1; makePlane(Vector(triangle.p2.x, triangle.p1.y), Vector(triangle.p1.x, triangle.p2.y), normal, c); break; } float depth = -(normal * p1) - c; if(depth < 0) return false; if(depth < hit.depth) { hit.depth = depth; hit.normal = normal; } return true; } } Index: sprite.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite.h,v retrieving revision 1.16 retrieving revision 1.17 diff -u -d -r1.16 -r1.17 --- sprite.h 19 Oct 2004 17:48:22 -0000 1.16 +++ sprite.h 20 Nov 2004 22:14:36 -0000 1.17 @@ -27,115 +27,83 @@ #include "../utils/lispreader.h" #include "../video/surface.h" #include "../math/vector.h" +#include "sprite_data.h" namespace SuperTux - { - +{ class Sprite - { - private: - - struct Action - { - std::string name; - - /** Position correction */ - int x_offset; - int y_offset; - /** Drawing priority in queue */ - int z_order; - - /** Frames per second */ - float fps; - - /** Mirror is used to avoid duplicating left and right side - sprites */ -// bool mirror; - - std::vector<Surface*> surfaces; - }; - - public: - /** cur has to be a pointer to data in the form of ((x-offset 5) - (y-offset 10) ...) */ - Sprite(lisp_object_t* cur); - ~Sprite(); - - /** Draw sprite, automatically calculates next frame */ - void draw(DrawingContext& context, const Vector& pos, int layer, - Uint32 drawing_effect = NONE_EFFECT); - - void draw_part(DrawingContext& context, const Vector& source, - const Vector& size, const Vector& pos, int layer, - Uint32 drawing_effect = NONE_EFFECT); - - /** Set action (or state) */ - void set_action(std::string act); - - /* Start an animation - -1 - for infinite - 0 - stopped - 1,2,3 - one, two, three times... */ - void start_animation(int loops); - /* Stop animation */ - void stop_animation() - { start_animation(0); } - /** Check if animation is stopped or not */ - bool check_animation(); - /** Reverse the animation */ - void reverse_animation(bool reverse); + { + public: + Sprite(SpriteData& data); + Sprite(const Sprite& other); + ~Sprite(); + + /** Draw sprite, automatically calculates next frame */ + void draw(DrawingContext& context, const Vector& pos, int layer, + Uint32 drawing_effect = NONE_EFFECT); - float get_fps() - { return action->fps; } - /** Get current action total frames */ - int get_frames() - { return action->surfaces.size(); } - /** Get sprite's name */ - std::string get_name() const - { return name; } - /** Get current action name */ - std::string get_action_name() const - { return action->name; } - int get_width(); - int get_height(); + void draw_part(DrawingContext& context, const Vector& source, + const Vector& size, const Vector& pos, int layer, + Uint32 drawing_effect = NONE_EFFECT); - /** Get current frame */ - int get_frame() - { return (int)frame; } - /** Set current frame */ - void set_frame(int frame_) - { if(frame_ > get_frames()) frame = 0; else frame = frame_; } - Surface* get_frame(unsigned int frame) - { - if(frame < action->surfaces.size()) - return action->surfaces[frame]; - else - return action->surfaces[0]; - } - private: - void init_defaults(Action* act); - void parse_action(LispReader& lispreader); + /** Set action (or state) */ + void set_action(std::string act); - /** Get an action */ - Action* get_action(std::string act); + /* Start an animation + -1 - for infinite + 0 - stopped + 1,2,3 - one, two, three times... */ + void start_animation(int loops); + /* Stop animation */ + void stop_animation() + { start_animation(0); } + /** Check if animation is stopped or not */ + bool check_animation(); + /** Reverse the animation */ + void reverse_animation(bool reverse); - void update(); - void reset(); + float get_fps() const + { return action->fps; } + /** Get current action total frames */ + int get_frames() const + { return action->surfaces.size(); } + /** Get sprite's name */ + const std::string& get_name() const + { return data.name; } + /** Get current action name */ + const std::string& get_action_name() const + { return action->name; } - std::string name; + int get_width() const; + int get_height() const; - float frame; - int animation_loops; - bool animation_reversed; - float last_tick; + /** Get current frame */ + int get_frame() const + { return (int)frame; } + /** Set current frame */ + void set_frame(int frame_) + { if(frame_ > get_frames()) frame = 0; else frame = frame_; } + Surface* get_frame(unsigned int frame) + { + if(frame < action->surfaces.size()) + return action->surfaces[frame]; + else + return action->surfaces[0]; + } + private: + void update(); + void reset(); - typedef std::map <std::string, Action*> Actions; - Actions actions; + SpriteData& data; - Action* action; - std::string next_action; - }; + float frame; + int animation_loops; + bool animation_reversed; + float last_tick; + SpriteData::Action* action; + std::string next_action; + }; } //namespace SuperTux #endif /*SUPERTUX_SPRITE_H*/ --- NEW FILE: collision_hit.h --- // $Id: collision_hit.h,v 1.1 2004/11/20 22:14:36 matzebraun Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Matthias Braun <ma...@br... // // 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_HIT_H #define SUPERTUX_COLLISION_HIT_H #include "math/vector.h" namespace SuperTux { /** * Used as return value for the collision functions, to indicate how the * collision should be handled */ enum HitResponse { ABORT_MOVE, // don't move the object FORCE_MOVE, // do the move ignoring the collision CONTINUE // move object out of collision and check for collisions again // if this happens to often then the move will just be aborted }; /** * This class collects data about a collision */ class CollisionHit { public: /// penetration depth float depth; // The normal of the side we collided with Vector normal; }; } #endif Index: timer.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/timer.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- timer.cpp 27 Jul 2004 13:25:05 -0000 1.4 +++ timer.cpp 20 Nov 2004 22:14:36 -0000 1.5 @@ -18,6 +18,8 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. +#include <config.h> + #include "SDL.h" #include "../special/timer.h" Index: game_object.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/game_object.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- game_object.h 24 Sep 2004 21:33:59 -0000 1.3 +++ game_object.h 20 Nov 2004 22:14:36 -0000 1.4 @@ -1,7 +1,7 @@ // $Id$ // // SuperTux - A Jump'n Run -// Copyright (C) 2004 Matthias Braun <ma...@br... +// Copyright (C) 2004 Matthias Braun <ma...@br...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -46,8 +46,9 @@ virtual ~GameObject(); /** This function is called once per frame and allows the object to update - * it's state. The elapsed_time is the time since the last frame and should be - * the base for all timed things. + * it's state. The elapsed_time is the time since the last frame in + * seconds and should be the base for all timed calculations (don't use + * SDL_GetTicks directly as this will fail in pause mode) */ virtual void action(float elapsed_time) = 0; @@ -67,11 +68,27 @@ wants_to_die = true; } + // flags + enum { + /// the tile so you can stand on it + FLAG_SOLID = 0x0001, + /// can be used to temporatily disable collision detection + FLAG_NO_COLLDET = 0x0002 + }; + + int get_flags() const + { + return flags; + } + private: /** this flag indicates if the object should be removed at the end of the * frame */ bool wants_to_die; + + protected: + int flags; }; } Index: sprite_manager.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/sprite_manager.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- sprite_manager.h 22 Jul 2004 19:07:50 -0000 1.3 +++ sprite_manager.h 20 Nov 2004 22:14:36 -0000 1.4 @@ -25,24 +25,23 @@ #include "../special/sprite.h" namespace SuperTux - { +{ class SpriteManager - { - private: - typedef std::map<std::string, Sprite*> Sprites; - Sprites sprites; - public: - SpriteManager(const std::string& filename); - ~SpriteManager(); - - void load_resfile(const std::string& filename); - /** loads a sprite. - * WARNING: You must not delete the returned object. - */ - Sprite* load(const std::string& name); - }; + { + private: + typedef std::map<std::string, SpriteData*> Sprites; + Sprites sprites; + public: + SpriteManager(const std::string& filename); + ~SpriteManager(); + void load_resfile(const std::string& filename); + /** loads a sprite. + * (contrary to the old api you have to delete the sprite!) + */ + Sprite* create(const std::string& name); + }; } //namespace SuperTux #endif /*SUPERTUX_SPRITE_MANAGER_H*/ |