@@ -4,18 +4,21 @@ | |||
#include <ostream> | |||
#include <stdio.h> | |||
#include "util.h" | |||
namespace Swan { | |||
namespace OS { | |||
bool isTTY(FILE *f); | |||
class Dynlib { | |||
class Dynlib: NonCopyable { | |||
public: | |||
Dynlib(const std::string &path); | |||
Dynlib(const Dynlib &) = delete; | |||
Dynlib(Dynlib &&dl) noexcept; | |||
~Dynlib(); | |||
Dynlib &operator=(Dynlib &&dl) noexcept; | |||
template<typename T> T get(const std::string &name) { return (T)getVoid(name); } | |||
void *getVoid(const std::string &name); | |||
@@ -12,12 +12,10 @@ inline std::ostream &operator<<(std::ostream &os, const SDL_Rect &rect) { | |||
return os; | |||
} | |||
class TexLock { | |||
class TexLock: NonCopyable { | |||
public: | |||
TexLock(SDL_Texture *tex, SDL_Rect *rect = nullptr); | |||
TexLock(const TexLock &) = delete; | |||
TexLock(TexLock &&lock) noexcept; | |||
~TexLock(); | |||
~TexLock() { SDL_UnlockTexture(tex_); } | |||
int blit(SDL_Rect *destrect, SDL_Surface *srcsurf, SDL_Rect *srcrect = nullptr) { | |||
return SDL_BlitSurface(srcsurf, srcrect, surf_.get(), destrect); | |||
@@ -28,25 +26,15 @@ private: | |||
RaiiPtr<SDL_Surface> surf_ = makeRaiiPtr<SDL_Surface>(nullptr, SDL_FreeSurface); | |||
}; | |||
class TexColorMod { | |||
class TexColorMod: NonCopyable { | |||
public: | |||
TexColorMod(const TexColorMod &) = delete; | |||
TexColorMod(TexColorMod &&mod) noexcept { | |||
tex_ = mod.tex_; | |||
r_ = mod.r_; | |||
g_ = mod.g_; | |||
b_ = mod.b_; | |||
mod.tex_ = nullptr; | |||
} | |||
TexColorMod(SDL_Texture *tex, uint8_t r, uint8_t g, uint8_t b): tex_(tex) { | |||
SDL_GetTextureColorMod(tex_, &r_, &g_, &b_); | |||
SDL_SetTextureColorMod(tex_, r, g, b); | |||
} | |||
~TexColorMod() { | |||
if (tex_) | |||
SDL_SetTextureColorMod(tex_, r_, g_, b_); | |||
SDL_SetTextureColorMod(tex_, r_, g_, b_); | |||
} | |||
private: | |||
@@ -54,23 +42,15 @@ private: | |||
uint8_t r_, g_, b_; | |||
}; | |||
class TexAlphaMod { | |||
class TexAlphaMod: NonCopyable { | |||
public: | |||
TexAlphaMod(const TexAlphaMod &) = delete; | |||
TexAlphaMod(TexAlphaMod &&mod) noexcept { | |||
tex_ = mod.tex_; | |||
alpha_ = mod.alpha_; | |||
mod.tex_ = nullptr; | |||
} | |||
TexAlphaMod(SDL_Texture *tex, uint8_t alpha): tex_(tex) { | |||
SDL_GetTextureAlphaMod(tex_, &alpha_); | |||
SDL_SetTextureAlphaMod(tex_, alpha); | |||
} | |||
~TexAlphaMod() { | |||
if (tex_) | |||
SDL_SetTextureAlphaMod(tex_, alpha_); | |||
SDL_SetTextureAlphaMod(tex_, alpha_); | |||
} | |||
private: |
@@ -7,6 +7,16 @@ | |||
namespace Swan { | |||
// Inherit from this class to make a class non-copyable | |||
class NonCopyable { | |||
protected: | |||
NonCopyable() = default; | |||
NonCopyable(NonCopyable &&) = default; | |||
NonCopyable(const NonCopyable &) = delete; | |||
NonCopyable &operator=(const NonCopyable &) = delete; | |||
}; | |||
template<typename T, typename Del = void (*)(T *)> | |||
using RaiiPtr = std::unique_ptr<T, Del>; | |||
@@ -16,13 +26,18 @@ RaiiPtr<T, Del> makeRaiiPtr(T *val, Del d) { | |||
} | |||
template<typename Func> | |||
class Deferred { | |||
class Deferred: NonCopyable { | |||
public: | |||
Deferred(Func func): func_(func) {} | |||
Deferred(const Deferred &def) = delete; | |||
Deferred(Deferred &&def) noexcept: func_(def.func_) { def.active_ = false; } | |||
~Deferred() { if (active_) func_(); } | |||
Deferred &operator=(Deferred &&def) noexcept { | |||
func_ = def.func_; | |||
def.active_ = false; | |||
return *this; | |||
} | |||
private: | |||
Func func_; | |||
bool active_ = true; |
@@ -20,8 +20,7 @@ Dynlib::Dynlib(const std::string &path) { | |||
throw std::runtime_error(dlerror()); | |||
} | |||
Dynlib::Dynlib(Dynlib &&dl) noexcept { | |||
handle_ = dl.handle_; | |||
Dynlib::Dynlib(Dynlib &&dl) noexcept: handle_(dl.handle_) { | |||
dl.handle_ = nullptr; | |||
} | |||
@@ -30,6 +29,12 @@ Dynlib::~Dynlib() { | |||
dlclose(handle_); | |||
} | |||
Dynlib &Dynlib::operator=(Dynlib &&dl) noexcept { | |||
handle_ = dl.handle_; | |||
dl.handle_ = nullptr; | |||
return *this; | |||
} | |||
void *Dynlib::getVoid(const std::string &name) { | |||
return dlsym(handle_, name.c_str()); | |||
} |
@@ -42,15 +42,4 @@ TexLock::TexLock(SDL_Texture *tex, SDL_Rect *rect): tex_(tex) { | |||
32, pitch, rmask, gmask, bmask, amask)); | |||
} | |||
TexLock::TexLock(TexLock &&lock) noexcept { | |||
tex_ = lock.tex_; | |||
surf_ = std::move(lock.surf_); | |||
lock.tex_ = nullptr; | |||
} | |||
TexLock::~TexLock() { | |||
if (tex_) | |||
SDL_UnlockTexture(tex_); | |||
} | |||
} |