From 59627781c9feead4bc72f61dcb56090213e12da6 Mon Sep 17 00:00:00 2001 From: exterlulz Date: Mon, 9 Aug 2010 14:34:54 +0200 Subject: [PATCH] add basic image loading --- CMakeLists.txt | 18 +++++++++++++---- main.cpp | 13 +++++++++--- musk/animation.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ musk/animation.h | 37 ++++++++++++++++++++++++++++++++++ musk/image.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ musk/image.h | 37 ++++++++++++++++++++++++++++++++++ 6 files changed, 212 insertions(+), 7 deletions(-) create mode 100644 musk/animation.cpp create mode 100644 musk/animation.h create mode 100644 musk/image.cpp create mode 100644 musk/image.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 09bfb34..b9c4b95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,22 @@ project( sdl-cmake ) cmake_minimum_required( VERSION 2.8 ) -include( FindSDL ) -include_directories( ${SDL_INCLUDE_DIR} ) +if( APPLE ) + include( FindSDL ) + include( FindSDL_image ) + + include_directories( ${SDL_INCLUDE_DIR} ) + include_directories( ${SDLIMAGE_INCLUDE_DIR} ) +endif( APPLE ) + +set( MUSK_SOURCES + musk/image.cpp + musk/animation.cpp ) # add: if( APPLE ) elseif( UNIX ) endif(...) for SDLmain -add_executable( sdl-cmake MACOSX_BUNDLE SDLmain.m main.cpp ) -target_link_libraries( sdl-cmake ${SDL_LIBRARY} ) +# add_executable( sdl-cmake MACOSX_BUNDLE SDLmain.m main.cpp ) +add_executable( sdl-cmake MACOSX_BUNDLE main.cpp SDLmain.m ${MUSK_SOURCES} ) +target_link_libraries( sdl-cmake ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ) if( APPLE ) # include( BundleUtilities ) diff --git a/main.cpp b/main.cpp index 7d9c72b..9ab9cf4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,24 @@ #include +#include + +#include "musk/image.h" + +// TODO: Find a way to copy the SDL framework AND the resources +// TODO: move source code in src, resources in res int main(int argc, char **argv) { SDL_Init(SDL_INIT_EVERYTHING); + IMG_Init(IMG_INIT_PNG); SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); - SDL_Surface *hello = SDL_LoadBMP("hello.bmp"); + musk::Image hello("hello.bmp"); - SDL_BlitSurface(hello, 0, screen, 0); + hello.draw(screen); SDL_Flip(screen); SDL_Delay(2000); - SDL_FreeSurface(hello); + IMG_Quit(); SDL_Quit(); } diff --git a/musk/animation.cpp b/musk/animation.cpp new file mode 100644 index 0000000..85b9925 --- /dev/null +++ b/musk/animation.cpp @@ -0,0 +1,55 @@ +#include "animation.h" + +namespace musk { + + using std::string; + + Animation::Animation(const string& path, size_t frames, uint32_t durations) : + Image(path), + + frames_(frames), + durations_(), + currentFrame_(0), + currentTicks_(0), + width_(0) { + + if (frames_ > 1) { + width_ = Image::width() / frames_; + } + else { + frames_ = 1; + width_ = Image::width(); + } + + clipMask_.w = width_; + durations_.assign(frames_, durations); + } + + int Animation::width() const { + return width_; + } + + unsigned int Animation::durationForFrame(size_t n) const { + return durations_.at(n); + } + + void Animation::reset() { + clipMask_.x = 0; + } + + void Animation::step(uint32_t ticks) + { + currentTicks_ += ticks; + if (currentTicks_ < durationForFrame(currentFrame_)) { + return; + } + currentTicks_ -= durationForFrame(currentFrame_); + currentFrame_ = (currentFrame_ + 1) % frames_; + + clipMask_.x += width(); + if (clipMask_.x >= Image::width()) { + reset(); + } + } + +} // namespace musk diff --git a/musk/animation.h b/musk/animation.h new file mode 100644 index 0000000..6095beb --- /dev/null +++ b/musk/animation.h @@ -0,0 +1,37 @@ +#ifndef MUSK_ANIMATION_H_ +#define MUSK_ANIMATION_H_ + +#include "image.h" + +#include +#include + +namespace musk { + + class Animation : public Image + { + public: + // TODO: doc: duration is the duration of each frame (in ms) + Animation(const std::string& path, + size_t frames = 1, uint32_t durations = 250); + + virtual int width() const; + + unsigned int durationForFrame(size_t n) const; + + void reset(); + void step(uint32_t ticks); + + private: + size_t frames_; + std::vector durations_; + + size_t currentFrame_; + uint32_t currentTicks_; + + int width_; + }; + +} // namespace musk + +#endif // MUSK_ANIMATION_H_ diff --git a/musk/image.cpp b/musk/image.cpp new file mode 100644 index 0000000..9e25187 --- /dev/null +++ b/musk/image.cpp @@ -0,0 +1,59 @@ +#include "image.h" + +#include + +namespace musk { + + using std::string; + using std::pair; + using std::make_pair; + + Image::Image(const string& path) : + surface_(0), + clipMask_(), + + position_(0, 0) { + + surface_ = IMG_Load(path.c_str()); + if (surface_ != 0) { + SDL_Surface *optimized = SDL_DisplayFormat(surface_); + SDL_FreeSurface(surface_); + surface_ = optimized; + } + + clipMask_.x = 0; + clipMask_.y = 0; + clipMask_.w = width(); + clipMask_.h = height(); + } + + Image::~Image() + { + SDL_FreeSurface(surface_); + surface_ = 0; + } + + int Image::width() const { + return surface_->w; + } + + int Image::height() const { + return surface_->h; + } + + const std::pair Image::position() const { + return position_; + } + + void Image::setPosition(const std::pair& position) { + position_ = position; + } + + void Image::draw(SDL_Surface *dest) const + { + SDL_Rect dstrect = { position_.first, position_.second, 0, 0 }; + SDL_BlitSurface(surface_, const_cast(&clipMask_), + dest, &dstrect); + } + +} // namespace musk diff --git a/musk/image.h b/musk/image.h new file mode 100644 index 0000000..192bb7f --- /dev/null +++ b/musk/image.h @@ -0,0 +1,37 @@ +#ifndef MUSK_IMAGE_H_ +#define MUSK_IMAGE_H_ + +#include + +#include +#include + +namespace musk { + + // typedef std::pair Position; + + class Image + { + public: + Image(const std::string& path); + ~Image(); + + virtual int width() const; + virtual int height() const; + + const std::pair position() const; + void setPosition(const std::pair& position); + + // TODO: set a scene in the constructor, and draw into that scene + void draw(SDL_Surface *dest) const; + + protected: + SDL_Surface *surface_; + SDL_Rect clipMask_; + + std::pair position_; + }; + +} // namespace musk + +#endif // MUSK_IMAGE_H_ -- 2.11.4.GIT