1 #include "hpp/SWTex.hpp"
12 SWTex_Base::SWTex_Base() :
15 SWTex_Base::SWTex_Base(
const geom::Size s,
const pixelfmt::Id fmt) :
16 h_(SDL_CreateRGBSurfaceWithFormat(0, s.w, s.h, static_cast<int>(pixelfmt::bitSizeOf(fmt)), pixelfmt::
id::unsafe::toSDLEnum(fmt)))
22 SWTex_Base::~SWTex_Base() noexcept {
28 SWTex_Base::SWTex_Base(SWTex_Base&& that) noexcept :
33 SWTex_Base& SWTex_Base::operator=(SWTex_Base&& that) noexcept {
38 void swap(SWTex_Base& a, SWTex_Base& b) noexcept {
43 pixelfmt::Id SWTex_Base::fmtId() noexcept {
44 return pixelfmt::id::unsafe::fromSDLEnum(fmt().format);
46 pixelfmt::Id SWTex_Base::fmtId() const noexcept {
47 return pixelfmt::id::unsafe::fromSDLEnum(fmt().format);
50 SDL_PixelFormat& SWTex_Base::fmt() noexcept {
51 return *unsafeRaw()->format;
53 SDL_PixelFormat& SWTex_Base::fmt() const noexcept {
54 return *unsafeRaw()->format;
57 unsigned int SWTex_Base::pitch() noexcept {
58 return static_cast<unsigned int>(unsafeRaw()->pitch);
60 unsigned int SWTex_Base::pitch() const noexcept {
61 return static_cast<unsigned int>(unsafeRaw()->pitch);
64 int SWTex_Base::w() noexcept {
65 return unsafeRaw()->w;
67 int SWTex_Base::w() const noexcept {
68 return unsafeRaw()->w;
70 int SWTex_Base::h() noexcept {
71 return unsafeRaw()->h;
73 int SWTex_Base::h() const noexcept {
74 return unsafeRaw()->h;
77 bool SWTex_Base::mustLock() noexcept {
78 return SDL_MUSTLOCK(unsafeRaw()) == SDL_TRUE;
80 bool SWTex_Base::mustLock() const noexcept {
81 return SDL_MUSTLOCK(unsafeRaw()) == SDL_TRUE;
89 SDL_Surface* SWTex_Base::unsafeRaw() noexcept {
90 assert(h_ !=
nullptr);
93 const SDL_Surface* SWTex_Base::unsafeRaw() const noexcept {
94 assert(h_ !=
nullptr);
98 uint32_t& SWTex_Base::unsafePoint(
const geom::Pos p) noexcept {
99 return *reinterpret_cast<uint32_t*>(
100 static_cast<char*>(unsafeRawPixels()) +
101 static_cast<unsigned int>(p.y)*pitch() +
102 static_cast<unsigned int>(p.x)*fmt().BytesPerPixel
105 const uint32_t& SWTex_Base::unsafePoint(
const geom::Pos p)
const noexcept {
106 return *reinterpret_cast<const uint32_t*>(static_cast<const char*>(unsafeRawPixels()) + static_cast<unsigned int>(p.y)*pitch() + p.x);
109 void* SWTex_Base::unsafeRawPixels() noexcept {
110 return unsafeRaw()->pixels;
112 const void* SWTex_Base::unsafeRawPixels() const noexcept {
113 return unsafeRaw()->pixels;
116 SWTex_Base& SWTex_Base::drawPoint(
const RGBA c,
const geom::Pos p) noexcept {
118 const uint32_t mask = static_cast<uint32_t>((~0 << fmt().BitsPerPixel));
120 uint32_t& val = unsafePoint(p);
121 val = (val & mask) | SDL_MapRGBA(&fmt(), c.r, c.g, c.b, c.a);
125 RGBA SWTex_Base::getPoint(
const geom::Pos p)
const noexcept {
127 SDL_GetRGBA(unsafePoint(p), &fmt(), &c.r, &c.g, &c.b, &c.a);
136 SWTex::SWTex(
const geom::Size s,
const pixelfmt::Id fmt) :
141 SWTex::SWTex(SWTex_RLE&& rle) noexcept :
145 throwOnErr(SDL_SetSurfaceRLE(unsafeRaw(),
false));
148 SWTex& SWTex::drawPoint(
const RGBA c,
const geom::Pos p) noexcept {
149 SWTex_Base::drawPoint(c, p);
152 RGBA SWTex::getPoint(
const geom::Pos p)
const noexcept {
153 return SWTex_Base::getPoint(p);
157 SWTex_RLE::SWTex_RLE(
const geom::Size s,
const pixelfmt::Id fmt) :
164 SWTex_RLE::SWTex_RLE(SWTex&& nonRle) noexcept :
168 throwOnErr(SDL_SetSurfaceRLE(unsafeRaw(),
true));
172 SWTex_RLELock::SWTex_RLELock() noexcept :
175 SWTex_RLELock::SWTex_RLELock(SWTex_RLE& t) :
181 SWTex_RLELock::~SWTex_RLELock() noexcept {
183 SDL_UnlockSurface(h_->unsafeRaw());
187 SWTex_RLELock::SWTex_RLELock(SWTex_RLELock&& that) noexcept :
192 SWTex_RLELock& SWTex_RLELock::operator=(SWTex_RLELock&& that) noexcept {
197 void swap(SWTex_RLELock& a, SWTex_RLELock& b) noexcept {
202 SWTex_RLELock& SWTex_RLELock::drawPoint(
const RGBA c,
const geom::Pos p) noexcept {
206 RGBA SWTex_RLELock::getPoint(
const geom::Pos p)
const noexcept {
207 return h_->getPoint(p);
211 SWTex& Win_SW::swtex() {
212 if (tex.h_ !=
nullptr)
215 SDL_Surface* res = SDL_GetWindowSurface(
unsafeRaw());
217 throw sdlexception();
219 assert(!tex.mustLock());
223 Win_SW::~Win_SW() noexcept {