diff --git a/common/button.cpp b/common/button.cpp index aca0f67..f9f066c 100644 --- a/common/button.cpp +++ b/common/button.cpp @@ -80,8 +80,8 @@ void Button::SetSurfaces(SDL_Surface* imageSurface, SDL_Surface* fontSurface) { std::string Button::SetText(std::string s) { //one line text = s; - textX = (image.GetClipW() / 2) - (font.GetClipW() * text.size() / 2); - textY = (image.GetClipH() / 2) - (font.GetClipH() / 2); + textX = (image.GetClipW() / 2) - (font.GetCharW() * text.size() / 2); + textY = (image.GetClipH() / 2) - (font.GetCharH() / 2); return text; } diff --git a/common/image.cpp b/common/image.cpp index 466d4d2..b7cdb95 100644 --- a/common/image.cpp +++ b/common/image.cpp @@ -21,10 +21,6 @@ */ #include "image.hpp" - Image::Image() { - SetSurface(nullptr); - } - Image::Image(SDL_Surface* p) { SetSurface(p); } @@ -34,8 +30,7 @@ Image::Image(SDL_Surface* p, SDL_Rect r) { } SDL_Surface* Image::SetSurface(SDL_Surface* p) { - surface = p; - if (!surface) { + if (!(surface = p)) { clip = {0, 0, 0, 0}; } else { @@ -50,15 +45,10 @@ SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) { return surface; } -SDL_Surface* Image::GetSurface() const { - return surface; -} - void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { if (!surface) { return; } SDL_Rect sclip = clip, dclip = {x,y}; - SDL_BlitSurface(surface, &sclip, dest, &dclip); } diff --git a/common/image.hpp b/common/image.hpp index de45d2c..710b842 100644 --- a/common/image.hpp +++ b/common/image.hpp @@ -26,33 +26,33 @@ class Image { public: - Image(); + Image() = default; Image(SDL_Surface*); Image(SDL_Surface*, SDL_Rect); - virtual ~Image() {} + ~Image() = default; - virtual SDL_Surface* SetSurface(SDL_Surface*); - virtual SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); - virtual SDL_Surface* GetSurface() const; + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); + SDL_Surface* GetSurface() const { return surface; } - virtual void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); + void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); //Clip handlers - virtual SDL_Rect SetClip(SDL_Rect r) { return clip = r; } - virtual SDL_Rect GetClip() const { return clip; } + SDL_Rect SetClip(SDL_Rect r) { return clip = r; } + SDL_Rect GetClip() const { return clip; } - virtual Sint16 SetClipX(Sint16 x) { return clip.x = x; } - virtual Sint16 SetClipY(Sint16 y) { return clip.y = y; } - virtual Uint16 SetClipW(Uint16 w) { return clip.w = w; } - virtual Uint16 SetClipH(Uint16 h) { return clip.h = h; } + Sint16 SetClipX(Sint16 x) { return clip.x = x; } + Sint16 SetClipY(Sint16 y) { return clip.y = y; } + Uint16 SetClipW(Uint16 w) { return clip.w = w; } + Uint16 SetClipH(Uint16 h) { return clip.h = h; } - virtual Sint16 GetClipX() const { return clip.x; } - virtual Sint16 GetClipY() const { return clip.y; } - virtual Uint16 GetClipW() const { return clip.w; } - virtual Uint16 GetClipH() const { return clip.h; } + Sint16 GetClipX() const { return clip.x; } + Sint16 GetClipY() const { return clip.y; } + Uint16 GetClipW() const { return clip.w; } + Uint16 GetClipH() const { return clip.h; } protected: - SDL_Surface* surface; - SDL_Rect clip; + SDL_Surface* surface = nullptr; + SDL_Rect clip = {0, 0, 0, 0}; }; #endif diff --git a/common/raster_font.cpp b/common/raster_font.cpp index 893bc93..71f1f04 100644 --- a/common/raster_font.cpp +++ b/common/raster_font.cpp @@ -21,33 +21,29 @@ */ #include "raster_font.hpp" +#include + +RasterFont::RasterFont(SDL_Surface* p) { + SetSurface(p); +} + void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) { - if (!surface) { - return; + if (!image.GetSurface()) { + throw(std::runtime_error("RasterFont not loaded")); } - //character size won't change here - const Sint16 w = surface->w/16; - const Sint16 h = surface->h/16; - SDL_Rect sclip, dclip = {x,y,0,0}; - for (auto c : s) { - //TODO: inefficient - sclip = {Sint16(c%w*w), Sint16(c/h*h), clip.w, clip.h}; -// sclip.x = c % w; -// sclip.h = c / h; -// sclip.w = clip.w; -// sclip.h = clip.h; - SDL_BlitSurface(surface, &sclip, dest, &dclip); - dclip.x += w; + const Uint16 w = image.GetClipW(); + const Uint16 h = image.GetClipH(); + for (int i = 0; i < s.size(); i++) { + image.SetClipX(s[i] % w * w); + image.SetClipY(s[i] / h * h); + image.DrawTo(dest, x + i * w, y); } } SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) { - surface = p; - if (!surface) { - clip = {0, 0, 0, 0}; + if (image.SetSurface(p)) { + image.SetClipW(image.GetSurface()->w/16); + image.SetClipH(image.GetSurface()->h/16); } - else { - clip = {0, 0, Uint16(surface->w/16), Uint16(surface->h/16)}; - } - return surface; + return image.GetSurface(); } diff --git a/common/raster_font.hpp b/common/raster_font.hpp index b46c9c3..643dde6 100644 --- a/common/raster_font.hpp +++ b/common/raster_font.hpp @@ -26,18 +26,21 @@ #include -class RasterFont : protected Image { +class RasterFont { public: - RasterFont() {} - RasterFont(SDL_Surface* p) {SetSurface(p);} - virtual ~RasterFont() {} - virtual void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y); - virtual SDL_Surface* SetSurface(SDL_Surface*) override; + RasterFont() = default; + RasterFont(SDL_Surface* p); + ~RasterFont() = default; - using Image::GetSurface; - using Image::GetClip; - using Image::GetClipW; - using Image::GetClipH; + void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y); + + //Accessors and Mutators + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* GetSurface() const { return image.GetSurface(); } + Uint16 GetCharW() { return image.GetClipW(); } + Uint16 GetCharH() { return image.GetClipH(); } +private: + Image image; }; #endif diff --git a/common/sprite_sheet.cpp b/common/sprite_sheet.cpp index 8fb6d3c..c387f79 100644 --- a/common/sprite_sheet.cpp +++ b/common/sprite_sheet.cpp @@ -21,28 +21,24 @@ */ #include "sprite_sheet.hpp" -SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) - : Image(s, {0, 0, w, h}) -{ - currentFrame = 0; maxFrames = GetSurface()->w / GetClipW(); - currentStrip = 0; maxStrips = GetSurface()->h / GetClipH(); - interval = ticks = 0; +SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) { + SetSurface(s, w, h); } -void SpriteSheet::Update(int delta) { - if (interval && (ticks += delta) > interval) { +void SpriteSheet::Update(double delta) { + if (interval && (ticks += delta) >= interval) { if (++currentFrame >= maxFrames) { currentFrame = 0; } ticks = 0; } - SetClipX(currentFrame * GetClipW()); - SetClipY(currentStrip * GetClipH()); + image.SetClipX(currentFrame * image.GetClipW()); + image.SetClipY(currentStrip * image.GetClipH()); } -void SpriteSheet::SetSurface(SDL_Surface* s, Uint16 w, Uint16 h) { - Image::SetSurface(s, {0, 0, w, h}); - currentFrame = 0; maxFrames = GetSurface()->w / GetClipW(); - currentStrip = 0; maxStrips = GetSurface()->h / GetClipH(); +SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const s, Uint16 w, Uint16 h) { + image.SetSurface(s, {0, 0, w, h}); + currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW(); + currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH(); interval = ticks = 0; -} \ No newline at end of file +} diff --git a/common/sprite_sheet.hpp b/common/sprite_sheet.hpp index 044845f..c86dea9 100644 --- a/common/sprite_sheet.hpp +++ b/common/sprite_sheet.hpp @@ -26,40 +26,41 @@ #include "SDL/SDL.h" -class SpriteSheet : protected Image { +class SpriteSheet { public: + SpriteSheet() = default; SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h); - virtual ~SpriteSheet() {} + ~SpriteSheet() = default; - void Update(int delta); + void Update(double delta); - void SetSurface(SDL_Surface*, Uint16 w, Uint16 h); + SDL_Surface* SetSurface(SDL_Surface* const, Uint16 w, Uint16 h); + SDL_Surface* GetSurface() const { + return image.GetSurface(); + } - using Image::GetSurface; - using Image::DrawTo; + void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) { + image.DrawTo(dest, x, y); + } - //TODO: these aren't regulated; be careful - int SetWidth(int i) { return SetClipW(i); }; - int SetHeight(int i) { return SetClipH(i); }; - int SetFrames(int i) { currentFrame = 0; return maxFrames = i; }; - int SetStrips(int i) { currentStrip = 0; return maxStrips = i; }; + //Accessors and Mutators + double SetInterval(double i) { return interval = i; } + double GetInterval() const { return interval; } - int GetWidth() const { return GetClipW(); } - int GetHeight() const { return GetClipH(); } - int GetFrames() const { return maxFrames; } - int GetStrips() const { return maxStrips; } - - int SetCurrentFrame(int i) { return currentFrame = i; }; - int SetCurrentStrip(int i) { return currentStrip = i; }; - int SetInterval(int i) { return interval = i; } + int SetCurrentFrame(int i) { return currentFrame = i; } + int SetCurrentStrip(int i) { return currentStrip = i; } + Uint16 GetFrameWidth() const { return image.GetClipW(); } + Uint16 GetFrameHeight() const { return image.GetClipH(); } int GetCurrentFrame() const { return currentFrame; }; int GetCurrentStrip() const { return currentStrip; }; - int GetInterval() const { return interval; }; + int GetMaxFrames() const { return maxFrames; } + int GetMaxStrips() const { return maxStrips; } private: - int currentFrame, maxFrames; - int currentStrip, maxStrips; - int interval, ticks; + Image image; + int currentFrame = 0, maxFrames = 0; + int currentStrip = 0, maxStrips = 0; + double interval = 0, ticks = 0; }; #endif diff --git a/common/surface_manager.cpp b/common/surface_manager.cpp index 6fe927a..a56afd3 100644 --- a/common/surface_manager.cpp +++ b/common/surface_manager.cpp @@ -23,16 +23,12 @@ #include -SurfaceManager::SurfaceManager() { - // -} - -SurfaceManager::~SurfaceManager() { +SurfaceManager::~SurfaceManager() noexcept { FreeAll(); } SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) { - mapType::iterator it = surfaceMap.find(key); + MapType::iterator it = surfaceMap.find(key); if (it != surfaceMap.end()) { throw(std::runtime_error(std::string("Surface already loaded: ") + key + std::string(", ") + fname)); } @@ -40,7 +36,7 @@ SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) { } SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) { - mapType::iterator it = surfaceMap.find(key); + MapType::iterator it = surfaceMap.find(key); if (it != surfaceMap.end()) { SDL_FreeSurface(it->second); surfaceMap.erase(it); @@ -49,7 +45,7 @@ SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) { } SDL_Surface* SurfaceManager::Get(std::string key) { - mapType::iterator it = surfaceMap.find(key); + MapType::iterator it = surfaceMap.find(key); if (it == surfaceMap.end()) { throw(std::runtime_error(std::string("Could not find key: ") + key)); } @@ -57,7 +53,7 @@ SDL_Surface* SurfaceManager::Get(std::string key) { } SDL_Surface* SurfaceManager::Set(std::string key, SDL_Surface* ptr) { - mapType::iterator it = surfaceMap.find(key); + MapType::iterator it = surfaceMap.find(key); if (it != surfaceMap.end()) { throw(std::runtime_error(std::string("Key already exists: ") + key)); } @@ -65,7 +61,7 @@ SDL_Surface* SurfaceManager::Set(std::string key, SDL_Surface* ptr) { } void SurfaceManager::Free(std::string key) { - mapType::iterator it = surfaceMap.find(key); + MapType::iterator it = surfaceMap.find(key); if (it != surfaceMap.end()) { SDL_FreeSurface(it->second); surfaceMap.erase(it); @@ -81,7 +77,7 @@ void SurfaceManager::FreeAll() { SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) { SDL_Surface* ptr = SDL_LoadBMP(fname.c_str()); - if (ptr == nullptr) { + if (!ptr) { throw(std::runtime_error(std::string("Failed to load file: ") + fname)); } SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default diff --git a/common/surface_manager.hpp b/common/surface_manager.hpp index fd0c025..8499a83 100644 --- a/common/surface_manager.hpp +++ b/common/surface_manager.hpp @@ -29,8 +29,8 @@ class SurfaceManager { public: - SurfaceManager(); - ~SurfaceManager(); + SurfaceManager() = default; + ~SurfaceManager() noexcept; SDL_Surface* Load(std::string key, std::string fname); SDL_Surface* Reload(std::string key, std::string fname); @@ -42,8 +42,8 @@ public: SDL_Surface* operator[](std::string key) { return Get(key); }; private: SDL_Surface* LoadSurface(std::string key, std::string fname); - typedef std::map mapType; - mapType surfaceMap; + typedef std::map MapType; + MapType surfaceMap; }; #endif