Refactored Image
This commit is contained in:
+24
-16
@@ -21,33 +21,41 @@
|
||||
*/
|
||||
#include "image.hpp"
|
||||
|
||||
Image::Image(SDL_Surface* p) {
|
||||
SetSurface(p);
|
||||
}
|
||||
#include <stdexcept>
|
||||
|
||||
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);
|
||||
|
||||
+9
-4
@@ -23,16 +23,18 @@
|
||||
#define IMAGE_HPP_
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user