Imported graphics updates from Codebase

This commit is contained in:
Kayne Ruse
2013-06-04 05:05:40 +10:00
parent 6a16e341ec
commit 783e8928d6
9 changed files with 98 additions and 116 deletions
+2 -2
View File
@@ -80,8 +80,8 @@ void Button::SetSurfaces(SDL_Surface* imageSurface, SDL_Surface* fontSurface) {
std::string Button::SetText(std::string s) { std::string Button::SetText(std::string s) {
//one line //one line
text = s; text = s;
textX = (image.GetClipW() / 2) - (font.GetClipW() * text.size() / 2); textX = (image.GetClipW() / 2) - (font.GetCharW() * text.size() / 2);
textY = (image.GetClipH() / 2) - (font.GetClipH() / 2); textY = (image.GetClipH() / 2) - (font.GetCharH() / 2);
return text; return text;
} }
+1 -11
View File
@@ -21,10 +21,6 @@
*/ */
#include "image.hpp" #include "image.hpp"
Image::Image() {
SetSurface(nullptr);
}
Image::Image(SDL_Surface* p) { Image::Image(SDL_Surface* p) {
SetSurface(p); SetSurface(p);
} }
@@ -34,8 +30,7 @@ Image::Image(SDL_Surface* p, SDL_Rect r) {
} }
SDL_Surface* Image::SetSurface(SDL_Surface* p) { SDL_Surface* Image::SetSurface(SDL_Surface* p) {
surface = p; if (!(surface = p)) {
if (!surface) {
clip = {0, 0, 0, 0}; clip = {0, 0, 0, 0};
} }
else { else {
@@ -50,15 +45,10 @@ SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) {
return surface; return surface;
} }
SDL_Surface* Image::GetSurface() const {
return surface;
}
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
if (!surface) { if (!surface) {
return; return;
} }
SDL_Rect sclip = clip, dclip = {x,y}; SDL_Rect sclip = clip, dclip = {x,y};
SDL_BlitSurface(surface, &sclip, dest, &dclip); SDL_BlitSurface(surface, &sclip, dest, &dclip);
} }
+18 -18
View File
@@ -26,33 +26,33 @@
class Image { class Image {
public: public:
Image(); Image() = default;
Image(SDL_Surface*); Image(SDL_Surface*);
Image(SDL_Surface*, SDL_Rect); Image(SDL_Surface*, SDL_Rect);
virtual ~Image() {} ~Image() = default;
virtual SDL_Surface* SetSurface(SDL_Surface*); SDL_Surface* SetSurface(SDL_Surface*);
virtual SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect);
virtual SDL_Surface* GetSurface() const; 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 //Clip handlers
virtual SDL_Rect SetClip(SDL_Rect r) { return clip = r; } SDL_Rect SetClip(SDL_Rect r) { return clip = r; }
virtual SDL_Rect GetClip() const { return clip; } SDL_Rect GetClip() const { return clip; }
virtual Sint16 SetClipX(Sint16 x) { return clip.x = x; } Sint16 SetClipX(Sint16 x) { return clip.x = x; }
virtual Sint16 SetClipY(Sint16 y) { return clip.y = y; } Sint16 SetClipY(Sint16 y) { return clip.y = y; }
virtual Uint16 SetClipW(Uint16 w) { return clip.w = w; } Uint16 SetClipW(Uint16 w) { return clip.w = w; }
virtual Uint16 SetClipH(Uint16 h) { return clip.h = h; } Uint16 SetClipH(Uint16 h) { return clip.h = h; }
virtual Sint16 GetClipX() const { return clip.x; } Sint16 GetClipX() const { return clip.x; }
virtual Sint16 GetClipY() const { return clip.y; } Sint16 GetClipY() const { return clip.y; }
virtual Uint16 GetClipW() const { return clip.w; } Uint16 GetClipW() const { return clip.w; }
virtual Uint16 GetClipH() const { return clip.h; } Uint16 GetClipH() const { return clip.h; }
protected: protected:
SDL_Surface* surface; SDL_Surface* surface = nullptr;
SDL_Rect clip; SDL_Rect clip = {0, 0, 0, 0};
}; };
#endif #endif
+18 -22
View File
@@ -21,33 +21,29 @@
*/ */
#include "raster_font.hpp" #include "raster_font.hpp"
#include <stdexcept>
RasterFont::RasterFont(SDL_Surface* p) {
SetSurface(p);
}
void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) { void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) {
if (!surface) { if (!image.GetSurface()) {
return; throw(std::runtime_error("RasterFont not loaded"));
} }
//character size won't change here const Uint16 w = image.GetClipW();
const Sint16 w = surface->w/16; const Uint16 h = image.GetClipH();
const Sint16 h = surface->h/16; for (int i = 0; i < s.size(); i++) {
SDL_Rect sclip, dclip = {x,y,0,0}; image.SetClipX(s[i] % w * w);
for (auto c : s) { image.SetClipY(s[i] / h * h);
//TODO: inefficient image.DrawTo(dest, x + i * w, y);
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;
} }
} }
SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) { SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) {
surface = p; if (image.SetSurface(p)) {
if (!surface) { image.SetClipW(image.GetSurface()->w/16);
clip = {0, 0, 0, 0}; image.SetClipH(image.GetSurface()->h/16);
} }
else { return image.GetSurface();
clip = {0, 0, Uint16(surface->w/16), Uint16(surface->h/16)};
}
return surface;
} }
+13 -10
View File
@@ -26,18 +26,21 @@
#include <string> #include <string>
class RasterFont : protected Image { class RasterFont {
public: public:
RasterFont() {} RasterFont() = default;
RasterFont(SDL_Surface* p) {SetSurface(p);} RasterFont(SDL_Surface* p);
virtual ~RasterFont() {} ~RasterFont() = default;
virtual void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y);
virtual SDL_Surface* SetSurface(SDL_Surface*) override;
using Image::GetSurface; void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y);
using Image::GetClip;
using Image::GetClipW; //Accessors and Mutators
using Image::GetClipH; 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 #endif
+10 -14
View File
@@ -21,28 +21,24 @@
*/ */
#include "sprite_sheet.hpp" #include "sprite_sheet.hpp"
SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) {
: Image(s, {0, 0, w, h}) SetSurface(s, w, h);
{
currentFrame = 0; maxFrames = GetSurface()->w / GetClipW();
currentStrip = 0; maxStrips = GetSurface()->h / GetClipH();
interval = ticks = 0;
} }
void SpriteSheet::Update(int delta) { void SpriteSheet::Update(double delta) {
if (interval && (ticks += delta) > interval) { if (interval && (ticks += delta) >= interval) {
if (++currentFrame >= maxFrames) { if (++currentFrame >= maxFrames) {
currentFrame = 0; currentFrame = 0;
} }
ticks = 0; ticks = 0;
} }
SetClipX(currentFrame * GetClipW()); image.SetClipX(currentFrame * image.GetClipW());
SetClipY(currentStrip * GetClipH()); image.SetClipY(currentStrip * image.GetClipH());
} }
void SpriteSheet::SetSurface(SDL_Surface* s, Uint16 w, Uint16 h) { SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const s, Uint16 w, Uint16 h) {
Image::SetSurface(s, {0, 0, w, h}); image.SetSurface(s, {0, 0, w, h});
currentFrame = 0; maxFrames = GetSurface()->w / GetClipW(); currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW();
currentStrip = 0; maxStrips = GetSurface()->h / GetClipH(); currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH();
interval = ticks = 0; interval = ticks = 0;
} }
+24 -23
View File
@@ -26,40 +26,41 @@
#include "SDL/SDL.h" #include "SDL/SDL.h"
class SpriteSheet : protected Image { class SpriteSheet {
public: public:
SpriteSheet() = default;
SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h); 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; void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) {
using Image::DrawTo; image.DrawTo(dest, x, y);
}
//TODO: these aren't regulated; be careful //Accessors and Mutators
int SetWidth(int i) { return SetClipW(i); }; double SetInterval(double i) { return interval = i; }
int SetHeight(int i) { return SetClipH(i); }; double GetInterval() const { return interval; }
int SetFrames(int i) { currentFrame = 0; return maxFrames = i; };
int SetStrips(int i) { currentStrip = 0; return maxStrips = i; };
int GetWidth() const { return GetClipW(); } int SetCurrentFrame(int i) { return currentFrame = i; }
int GetHeight() const { return GetClipH(); } int SetCurrentStrip(int i) { return currentStrip = i; }
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; }
Uint16 GetFrameWidth() const { return image.GetClipW(); }
Uint16 GetFrameHeight() const { return image.GetClipH(); }
int GetCurrentFrame() const { return currentFrame; }; int GetCurrentFrame() const { return currentFrame; };
int GetCurrentStrip() const { return currentStrip; }; int GetCurrentStrip() const { return currentStrip; };
int GetInterval() const { return interval; }; int GetMaxFrames() const { return maxFrames; }
int GetMaxStrips() const { return maxStrips; }
private: private:
int currentFrame, maxFrames; Image image;
int currentStrip, maxStrips; int currentFrame = 0, maxFrames = 0;
int interval, ticks; int currentStrip = 0, maxStrips = 0;
double interval = 0, ticks = 0;
}; };
#endif #endif
+7 -11
View File
@@ -23,16 +23,12 @@
#include <stdexcept> #include <stdexcept>
SurfaceManager::SurfaceManager() { SurfaceManager::~SurfaceManager() noexcept {
//
}
SurfaceManager::~SurfaceManager() {
FreeAll(); FreeAll();
} }
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) { 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()) { if (it != surfaceMap.end()) {
throw(std::runtime_error(std::string("Surface already loaded: ") + key + std::string(", ") + fname)); 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) { 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()) { if (it != surfaceMap.end()) {
SDL_FreeSurface(it->second); SDL_FreeSurface(it->second);
surfaceMap.erase(it); surfaceMap.erase(it);
@@ -49,7 +45,7 @@ SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) {
} }
SDL_Surface* SurfaceManager::Get(std::string key) { SDL_Surface* SurfaceManager::Get(std::string key) {
mapType::iterator it = surfaceMap.find(key); MapType::iterator it = surfaceMap.find(key);
if (it == surfaceMap.end()) { if (it == surfaceMap.end()) {
throw(std::runtime_error(std::string("Could not find key: ") + key)); 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) { 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()) { if (it != surfaceMap.end()) {
throw(std::runtime_error(std::string("Key already exists: ") + key)); 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) { void SurfaceManager::Free(std::string key) {
mapType::iterator it = surfaceMap.find(key); MapType::iterator it = surfaceMap.find(key);
if (it != surfaceMap.end()) { if (it != surfaceMap.end()) {
SDL_FreeSurface(it->second); SDL_FreeSurface(it->second);
surfaceMap.erase(it); surfaceMap.erase(it);
@@ -81,7 +77,7 @@ void SurfaceManager::FreeAll() {
SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) { SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) {
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str()); SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
if (ptr == nullptr) { if (!ptr) {
throw(std::runtime_error(std::string("Failed to load file: ") + fname)); throw(std::runtime_error(std::string("Failed to load file: ") + fname));
} }
SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default
+4 -4
View File
@@ -29,8 +29,8 @@
class SurfaceManager { class SurfaceManager {
public: public:
SurfaceManager(); SurfaceManager() = default;
~SurfaceManager(); ~SurfaceManager() noexcept;
SDL_Surface* Load(std::string key, std::string fname); SDL_Surface* Load(std::string key, std::string fname);
SDL_Surface* Reload(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); }; SDL_Surface* operator[](std::string key) { return Get(key); };
private: private:
SDL_Surface* LoadSurface(std::string key, std::string fname); SDL_Surface* LoadSurface(std::string key, std::string fname);
typedef std::map<std::string, SDL_Surface*> mapType; typedef std::map<std::string, SDL_Surface*> MapType;
mapType surfaceMap; MapType surfaceMap;
}; };
#endif #endif