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) {
//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;
}
+1 -11
View File
@@ -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);
}
+18 -18
View File
@@ -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
+18 -22
View File
@@ -21,33 +21,29 @@
*/
#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) {
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();
}
+13 -10
View File
@@ -26,18 +26,21 @@
#include <string>
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
+11 -15
View File
@@ -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;
}
}
+24 -23
View File
@@ -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
+7 -11
View File
@@ -23,16 +23,12 @@
#include <stdexcept>
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
+4 -4
View File
@@ -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<std::string, SDL_Surface*> mapType;
mapType surfaceMap;
typedef std::map<std::string, SDL_Surface*> MapType;
MapType surfaceMap;
};
#endif