add basic image loading
[exterlulz-musk.git] / musk / image.cpp
blob9e251877898c32b04cda3d37fb8415ec4259642d
1 #include "image.h"
3 #include <SDL_image/SDL_image.h>
5 namespace musk {
7 using std::string;
8 using std::pair;
9 using std::make_pair;
11 Image::Image(const string& path) :
12 surface_(0),
13 clipMask_(),
15 position_(0, 0) {
17 surface_ = IMG_Load(path.c_str());
18 if (surface_ != 0) {
19 SDL_Surface *optimized = SDL_DisplayFormat(surface_);
20 SDL_FreeSurface(surface_);
21 surface_ = optimized;
24 clipMask_.x = 0;
25 clipMask_.y = 0;
26 clipMask_.w = width();
27 clipMask_.h = height();
30 Image::~Image()
32 SDL_FreeSurface(surface_);
33 surface_ = 0;
36 int Image::width() const {
37 return surface_->w;
40 int Image::height() const {
41 return surface_->h;
44 const std::pair<int32_t, int32_t> Image::position() const {
45 return position_;
48 void Image::setPosition(const std::pair<int32_t, int32_t>& position) {
49 position_ = position;
52 void Image::draw(SDL_Surface *dest) const
54 SDL_Rect dstrect = { position_.first, position_.second, 0, 0 };
55 SDL_BlitSurface(surface_, const_cast<SDL_Rect *>(&clipMask_),
56 dest, &dstrect);
59 } // namespace musk