animationSurface.cpp
00001
00002
00003
00004
00005
00006
00007 #include "animationSurface.h"
00008 #include "deleteObjects.h"
00009
00010
00011 AnimationSurface::AnimationSurface(void) : ticks_msec(0), cycle(0) {
00012 w = 0;
00013 h = 0;
00014 }
00015
00016
00017 AnimationSurface::~AnimationSurface(void) {
00018 for_each(cells.begin(), cells.end(), DeleteObjects());
00019 cells.clear();
00020 }
00021
00022
00023 bool AnimationSurface::draw(const VXV::Rect& size, const VXV::Grid& pos,
00024 unsigned long ticks) {
00025 if (cycle <= 0) {
00026 return false;
00027 }
00028
00029 long time = ticks % cycle;
00030 for (std::vector<CellInformation>::iterator it = cell_info.begin();
00031 it != cell_info.end(); ++it) {
00032 time -= it->ticks * ticks_msec;
00033 if (time < 0) {
00034
00035 DrawSurface *surface = cells[it->index];
00036 surface->draw(VXV::Rect(surface->w, surface->h),
00037 pos + it->offset, ticks);
00038 break;
00039 }
00040 }
00041 return true;
00042 }
00043
00044
00045 void AnimationSurface::sizeUpdate(DrawSurface* surface) {
00046 w = (w < surface->w) ? surface->w : w;
00047 h = (h < surface->h) ? surface->h : h;
00048 }
00049
00050
00051 int AnimationSurface::addCell(const char* bmpFile, bool transparent) {
00052 DrawSurface* surface = new DrawSurface(bmpFile, transparent);
00053 cells.push_back(surface);
00054 sizeUpdate(surface);
00055
00056 return static_cast<int>(cells.size() -1);
00057 }
00058
00059
00060 int AnimationSurface::addCell(SDL_Surface* sdlSurface, bool transparent) {
00061 DrawSurface* surface = new DrawSurface(sdlSurface, transparent);
00062 cells.push_back(surface);
00063 sizeUpdate(surface);
00064
00065 return static_cast<int>(cells.size() -1);
00066 }
00067
00068
00069 void AnimationSurface::addCellInformation(const CellInformation& info) {
00070 cell_info.push_back(info);
00071 }
00072
00073
00074 void AnimationSurface::setTicksMsec(unsigned long msec) {
00075 ticks_msec = msec;
00076 cycle = static_cast<int>(cells.size() * ticks_msec);
00077
00078
00079 for (std::vector<CellInformation>::iterator it = cell_info.begin();
00080 it != cell_info.end(); ++it) {
00081 DrawSurface* surface = cells[it->index];
00082
00083 it->offset.x = 0;
00084 if (it->align & VXV::Center) {
00085 it->offset.x = w/2 - surface->w/2;
00086 } else if (it->align & VXV::Right) {
00087 it->offset.x = w - surface->w;
00088 }
00089 it->offset.y = 0;
00090 if (it->align & VXV::Middle) {
00091 it->offset.y = h/2 - surface->h/2;
00092 } else if (it->align & VXV::Bottom) {
00093 it->offset.y = h - surface->h;
00094 }
00095 }
00096 }
00097