Refactored Image
This commit is contained in:
+23
-15
@@ -21,33 +21,41 @@
|
|||||||
*/
|
*/
|
||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
Image::Image(SDL_Surface* p) {
|
#include <stdexcept>
|
||||||
SetSurface(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
Image::Image(SDL_Surface* p, SDL_Rect r) {
|
SDL_Surface* Image::LoadSurface(std::string fname) {
|
||||||
SetSurface(p, r);
|
SDL_Surface* p = SDL_LoadBMP(fname.c_str());
|
||||||
}
|
if (!p) {
|
||||||
|
throw(std::runtime_error(std::string() + "Failed to load file: " + fname));
|
||||||
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
|
||||||
if (!(surface = p)) {
|
|
||||||
clip = {0, 0, 0, 0};
|
|
||||||
}
|
}
|
||||||
else {
|
surface = p;
|
||||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||||
}
|
local = true;
|
||||||
return surface;
|
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;
|
surface = p;
|
||||||
clip = r;
|
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||||
|
local = false;
|
||||||
return surface;
|
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;
|
throw(std::logic_error("No image surface to draw"));
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
|||||||
+9
-4
@@ -23,16 +23,18 @@
|
|||||||
#define IMAGE_HPP_
|
#define IMAGE_HPP_
|
||||||
|
|
||||||
#include "SDL/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class Image {
|
class Image {
|
||||||
public:
|
public:
|
||||||
Image() = default;
|
Image() = default;
|
||||||
Image(SDL_Surface*);
|
Image(SDL_Surface* p) { SetSurface(p); }
|
||||||
Image(SDL_Surface*, SDL_Rect);
|
~Image() { FreeSurface(); }
|
||||||
~Image() = default;
|
|
||||||
|
SDL_Surface* LoadSurface(std::string fname);
|
||||||
|
void FreeSurface();
|
||||||
|
|
||||||
SDL_Surface* SetSurface(SDL_Surface*);
|
SDL_Surface* SetSurface(SDL_Surface*);
|
||||||
SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect);
|
|
||||||
SDL_Surface* GetSurface() const { return surface; }
|
SDL_Surface* GetSurface() const { return surface; }
|
||||||
|
|
||||||
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
||||||
@@ -50,9 +52,12 @@ public:
|
|||||||
Sint16 GetClipY() const { return clip.y; }
|
Sint16 GetClipY() const { return clip.y; }
|
||||||
Uint16 GetClipW() const { return clip.w; }
|
Uint16 GetClipW() const { return clip.w; }
|
||||||
Uint16 GetClipH() const { return clip.h; }
|
Uint16 GetClipH() const { return clip.h; }
|
||||||
|
|
||||||
|
bool GetLocal() { return local; }
|
||||||
protected:
|
protected:
|
||||||
SDL_Surface* surface = nullptr;
|
SDL_Surface* surface = nullptr;
|
||||||
SDL_Rect clip = {0, 0, 0, 0};
|
SDL_Rect clip = {0, 0, 0, 0};
|
||||||
|
bool local = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ void SpriteSheet::Update(double delta) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const 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);
|
||||||
|
image.SetClip({0, 0, w, h});
|
||||||
currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW();
|
currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW();
|
||||||
currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH();
|
currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH();
|
||||||
delay = ticks = 0;
|
delay = ticks = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user