X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fsprite.cpp;h=bb31251a8becf225554b29f1983bab43813eaa2e;hb=2ec1be264110139466ab70422b8f4fd9c22e5c8c;hp=f7aed2c457313d7224154573dc467898d08c6ea2;hpb=cf8bf29d39d117783c3068270a3564595b21a53a;p=supertux.git diff --git a/src/sprite.cpp b/src/sprite.cpp index f7aed2c45..bb31251a8 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -18,8 +18,10 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include +#include #include "globals.h" #include "sprite.h" +#include "setup.h" Sprite::Sprite(lisp_object_t* cur) { @@ -27,17 +29,30 @@ Sprite::Sprite(lisp_object_t* cur) LispReader reader(cur); + if(!reader.read_string("name", &name)) + st_abort("Sprite wihtout name", ""); reader.read_int("x-hotspot", &x_hotspot); reader.read_int("y-hotspot", &y_hotspot); - reader.read_float("fps", &fps); + reader.read_float("fps", &fps); + std::vector images; - reader.read_string_vector("images", &images); - surfaces.resize(images.size()); + if(!reader.read_string_vector("images", &images)) + st_abort("Sprite contains no images: ", name.c_str()); for(std::vector::size_type i = 0; i < images.size(); ++i) { - texture_load(&surfaces[i], datadir + "/images/" + images[i], USE_ALPHA); + surfaces.push_back( + new Surface(datadir + "/images/" + images[i], USE_ALPHA)); } + + frame_delay = 1000.0f/fps; +} + +Sprite::~Sprite() +{ + for(std::vector::iterator i = surfaces.begin(); i != surfaces.end(); + ++i) + delete *i; } void @@ -45,32 +60,61 @@ Sprite::init_defaults() { x_hotspot = 0; y_hotspot = 0; - fps = 15; + fps = 10; time = 0; frame_delay = 1000.0f/fps; } void -Sprite::update(float delta) +Sprite::update(float /*delta*/) { - time += 10*delta; + //time += 10*delta; //std::cout << "Delta: " << delta << std::endl; } void -Sprite::draw(int x, int y) +Sprite::draw(float x, float y) { - unsigned int frame = static_cast(fmodf(time, surfaces.size()*frame_delay)/frame_delay); - - /* - std::cout << "Frame: " - << frame << " " - << time << " " - << surfaces.size() << " " - << frame_delay << " " - << static_cast(fmodf(time, surfaces.size()*frame_delay)/frame_delay) << std::endl;*/ + time = SDL_GetTicks(); + unsigned int frame = get_current_frame(); + if (frame < surfaces.size()) - texture_draw(&surfaces[frame], x - x_hotspot, y - y_hotspot); + surfaces[frame]->draw(x - x_hotspot, y - y_hotspot); +} + +void +Sprite::draw_part(float sx, float sy, float x, float y, float w, float h) +{ + time = SDL_GetTicks(); + unsigned int frame = get_current_frame(); + + if (frame < surfaces.size()) + surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h); +} + +void +Sprite::reset() +{ + time = 0; +} + +int +Sprite::get_current_frame() const +{ + unsigned int frame = static_cast(fmodf(time, surfaces.size()*frame_delay)/frame_delay); + return frame % surfaces.size(); +} + +int +Sprite::get_width() const +{ + return surfaces[get_current_frame()]->w; +} + +int +Sprite::get_height() const +{ + return surfaces[get_current_frame()]->h; } /* EOF */