From 40a40b4e11a0af4648d680c686f9e93dcde59e8f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 15 Jul 2013 15:41:41 +1000 Subject: [PATCH] Refactored Image --- client/image.cpp | 40 ++++++++++++++++++++++++---------------- client/image.hpp | 13 +++++++++---- client/sprite_sheet.cpp | 3 ++- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/client/image.cpp b/client/image.cpp index b7cdb95..9d8669c 100644 --- a/client/image.cpp +++ b/client/image.cpp @@ -21,33 +21,41 @@ */ #include "image.hpp" -Image::Image(SDL_Surface* p) { - SetSurface(p); -} +#include -Image::Image(SDL_Surface* p, SDL_Rect r) { - SetSurface(p, r); -} - -SDL_Surface* Image::SetSurface(SDL_Surface* p) { - if (!(surface = p)) { - clip = {0, 0, 0, 0}; - } - else { - clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; +SDL_Surface* Image::LoadSurface(std::string fname) { + SDL_Surface* p = SDL_LoadBMP(fname.c_str()); + if (!p) { + throw(std::runtime_error(std::string() + "Failed to load file: " + fname)); } + surface = p; + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + local = true; return surface; } -SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) { +void Image::FreeSurface() { + if (local) { + SDL_FreeSurface(surface); + local = false; + } + surface = nullptr; + clip = {0, 0, 0, 0}; +} + +SDL_Surface* Image::SetSurface(SDL_Surface* p) { + if (!p) { + throw(std::invalid_argument("No surface pointer provided")); + } surface = p; - clip = r; + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + local = false; return surface; } void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { if (!surface) { - return; + throw(std::logic_error("No image surface to draw")); } SDL_Rect sclip = clip, dclip = {x,y}; SDL_BlitSurface(surface, &sclip, dest, &dclip); diff --git a/client/image.hpp b/client/image.hpp index 710b842..463e382 100644 --- a/client/image.hpp +++ b/client/image.hpp @@ -23,16 +23,18 @@ #define IMAGE_HPP_ #include "SDL/SDL.h" +#include class Image { public: Image() = default; - Image(SDL_Surface*); - Image(SDL_Surface*, SDL_Rect); - ~Image() = default; + Image(SDL_Surface* p) { SetSurface(p); } + ~Image() { FreeSurface(); } + + SDL_Surface* LoadSurface(std::string fname); + void FreeSurface(); SDL_Surface* SetSurface(SDL_Surface*); - SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); SDL_Surface* GetSurface() const { return surface; } void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); @@ -50,9 +52,12 @@ public: Sint16 GetClipY() const { return clip.y; } Uint16 GetClipW() const { return clip.w; } Uint16 GetClipH() const { return clip.h; } + + bool GetLocal() { return local; } protected: SDL_Surface* surface = nullptr; SDL_Rect clip = {0, 0, 0, 0}; + bool local = false; }; #endif diff --git a/client/sprite_sheet.cpp b/client/sprite_sheet.cpp index 14af497..1f2cccd 100644 --- a/client/sprite_sheet.cpp +++ b/client/sprite_sheet.cpp @@ -33,7 +33,8 @@ void SpriteSheet::Update(double delta) { } SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const s, Uint16 w, Uint16 h) { - image.SetSurface(s, {0, 0, w, h}); + image.SetSurface(s); + image.SetClip({0, 0, w, h}); currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW(); currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH(); delay = ticks = 0;