From 51e82cca2512968a8699e6a2e76063811c64aba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Vu=C4=8Dica?= Date: Fri, 30 May 2025 16:18:16 +0100 Subject: [PATCH] Move from SDL_Surface* to abstract RGBA storage class Untested, incomplete. This was the idea: > Across the codebase, move from SDLSurface* as storage for RGBA data to an abstract RGBA storage class which can be subclassed to either contain an SDLSurface or an OpenGL texture, while also containing the RGBA data. Move Sprite* and other classes to use that. --- Update the codebase to use an abstract RGBA storage class instead of SDL_Surface* for RGBA data storage. * Add `rgba_storage.h` to define an abstract class `RGBAStorage` with subclasses `SDLSurfaceStorage` and `OpenGLTextureStorage`. * Modify `sprite.h` and `sprite.cpp` to replace `SDL_Surface*` with `std::shared_ptr` and update methods accordingly. * Modify `spritesdl.cpp` to use `RGBAStorage` methods for accessing RGBA data. * Modify `spritegl.h` and `spritegl.cpp` to use `std::shared_ptr` for RGBA data storage. * Modify `automap.cpp` to use `std::shared_ptr` for RGBA data storage. * Modify `engine.cpp` to use `std::shared_ptr` for RGBA data storage. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/opentibia/yatc?shareId=XXXX-XXXX-XXXX-XXXX). --- automap.cpp | 8 +-- engine.cpp | 8 +-- rgba_storage.h | 81 ++++++++++++++++++++++++++++ sprite.cpp | 141 +++++++++++++++++++++++++------------------------ sprite.h | 28 +++++----- spritegl.cpp | 9 ++-- spritegl.h | 1 + spritesdl.cpp | 12 ++--- 8 files changed, 186 insertions(+), 102 deletions(-) create mode 100644 rgba_storage.h diff --git a/automap.cpp b/automap.cpp index 7ca92403..c9abe39b 100644 --- a/automap.cpp +++ b/automap.cpp @@ -121,12 +121,12 @@ bool MiniMapArea::load() //Create the sprite // FIXME (nfries88): OpenGL does not work well when SDL is being used on the screen. // this operation must be changed for GL engine to work. - SDL_Surface* s = m_bitmap->lockSurface(); + auto s = m_bitmap->lockSurface(); for(int i = 0; i < 256; i++){ for(int j = 0; j < 256; j++){ uint8_t r, g, b; getRGB(m_color[i][j], r, g, b); - m_bitmap->putPixel(i, j, SDL_MapRGB(s->format, r, g, b) ,s); + m_bitmap->putPixel(i, j, SDL_MapRGB(static_cast(s.get())->getSurface()->format, r, g, b) ,static_cast(s.get())->getSurface()); } } m_bitmap->unlockSurface(); @@ -142,10 +142,10 @@ void MiniMapArea::setTileColor(uint16_t x, uint16_t y, uint8_t color, uint8_t sp m_speed[x][y] = speedindex; //update the srpite // FIXME (nfries88): Must be changed to engine-independent for OpenGL engine. - SDL_Surface* s = m_bitmap->lockSurface(); + auto s = m_bitmap->lockSurface(); uint8_t r, g, b; getRGB(color, r, g, b); - m_bitmap->putPixel(x, y, SDL_MapRGB(s->format, r, g, b) ,s); + m_bitmap->putPixel(x, y, SDL_MapRGB(static_cast(s.get())->getSurface()->format, r, g, b) ,static_cast(s.get())->getSurface()); m_bitmap->unlockSurface(); } diff --git a/engine.cpp b/engine.cpp index f48298e8..cda41c97 100644 --- a/engine.cpp +++ b/engine.cpp @@ -130,7 +130,7 @@ Engine::Engine() m_video_bpp = options.bpp; - m_screen = NULL; + m_screen = nullptr; initFont(&m_sysfont, "system"); initFont(&m_minifont, "minifont"); @@ -138,9 +138,9 @@ Engine::Engine() initFont(&m_gamefont, "gamefont"); m_fps = 0.; - m_ui = NULL; - m_light = NULL; - m_cursorBasic = m_cursorUse = NULL; + m_ui = nullptr; + m_light = nullptr; + m_cursorBasic = m_cursorUse = nullptr; // remember default cursor resetDefaultCursor(); diff --git a/rgba_storage.h b/rgba_storage.h new file mode 100644 index 00000000..ca1f1920 --- /dev/null +++ b/rgba_storage.h @@ -0,0 +1,81 @@ +#ifndef RGBA_STORAGE_H +#define RGBA_STORAGE_H + +#include +#include +#include + +class RGBAStorage { +public: + virtual ~RGBAStorage() = default; + virtual uint8_t* getData() = 0; + virtual int getWidth() const = 0; + virtual int getHeight() const = 0; + virtual int getPitch() const = 0; +}; + +class SDLSurfaceStorage : public RGBAStorage { +public: + SDLSurfaceStorage(SDL_Surface* surface) : surface_(surface) {} + ~SDLSurfaceStorage() { + if (surface_) { + SDL_FreeSurface(surface_); + } + } + + uint8_t* getData() override { + return static_cast(surface_->pixels); + } + + int getWidth() const override { + return surface_->w; + } + + int getHeight() const override { + return surface_->h; + } + + int getPitch() const override { + return surface_->pitch; + } + +private: + SDL_Surface* surface_; +}; + +class OpenGLTextureStorage : public RGBAStorage { +public: + OpenGLTextureStorage(GLuint texture, int width, int height) + : texture_(texture), width_(width), height_(height) {} + + ~OpenGLTextureStorage() { + if (texture_) { + glDeleteTextures(1, &texture_); + } + } + + uint8_t* getData() override { + // OpenGL textures do not provide direct access to pixel data + return nullptr; + } + + int getWidth() const override { + return width_; + } + + int getHeight() const override { + return height_; + } + + int getPitch() const override { + // OpenGL textures do not have a pitch value + return 0; + } + +private: + GLuint texture_; + int width_; + int height_; +}; + +#endif // RGBA_STORAGE_H diff --git a/sprite.cpp b/sprite.cpp index d459cc23..b421a5a2 100644 --- a/sprite.cpp +++ b/sprite.cpp @@ -74,9 +74,9 @@ Sprite::Sprite(int w, int h, const oRGBA& c) #ifdef USE_OPENGL m_pixelformat = GL_NONE; #endif - m_image = NULL; - m_stretchimage = NULL; - m_coloredimage = NULL; + m_image = nullptr; + m_stretchimage = nullptr; + m_coloredimage = nullptr; m_loaded = false; m_smoothstretch = 0; m_r = 1.f; @@ -99,9 +99,9 @@ Sprite::Sprite(const std::string& filename, int index) #ifdef USE_OPENGL m_pixelformat = GL_NONE; #endif - m_image = NULL; - m_stretchimage = NULL; - m_coloredimage = NULL; + m_image = nullptr; + m_stretchimage = nullptr; + m_coloredimage = nullptr; m_loaded = false; m_smoothstretch = 0; m_filename = filename; @@ -134,9 +134,9 @@ Sprite::Sprite(const std::string& filename, int index, int x, int y, int w, int m_pixelformat = GL_NONE; #endif - m_image = NULL; - m_stretchimage = NULL; - m_coloredimage = NULL; + m_image = nullptr; + m_stretchimage = nullptr; + m_coloredimage = nullptr; m_loaded = false; m_smoothstretch = 1; m_r = 1.f; @@ -153,12 +153,12 @@ Sprite::Sprite(const std::string& filename, int index, int x, int y, int w, int SDL_Surface* ns = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32, rmask, gmask, bmask, amask); SDL_Rect src = {(Sint16)x,(Sint16)y,(Uint16)w,(Uint16)h}; SDL_Rect dst = {0,0,(Uint16)w,(Uint16)h}; - SDL_BlitSurface(m_image, &src, ns, &dst); + SDL_BlitSurface(static_cast(m_image.get())->getSurface(), &src, ns, &dst); - SDL_FreeSurface(m_image); - SDL_FreeSurface(m_coloredimage); - m_image = ns; - m_coloredimage = SDL_CreateRGBSurface(SDL_HWSURFACE, m_image->w, m_image->h, 32, rmask, gmask, bmask, amask); + SDL_FreeSurface(static_cast(m_image.get())->getSurface()); + SDL_FreeSurface(static_cast(m_coloredimage.get())->getSurface()); + m_image = std::make_shared(ns); + m_coloredimage = std::make_shared(SDL_CreateRGBSurface(SDL_HWSURFACE, m_image->getWidth(), m_image->getHeight(), 32, rmask, gmask, bmask, amask)); } @@ -166,13 +166,13 @@ Sprite::Sprite(const std::string& filename, int index, int x, int y, int w, int Sprite::~Sprite() { if(m_image){ - SDL_FreeSurface(m_image); + SDL_FreeSurface(static_cast(m_image.get())->getSurface()); } if(m_stretchimage){ - SDL_FreeSurface(m_stretchimage); + SDL_FreeSurface(static_cast(m_stretchimage.get())->getSurface()); } if(m_coloredimage){ - SDL_FreeSurface(m_coloredimage); + SDL_FreeSurface(static_cast(m_coloredimage.get())->getSurface()); } } @@ -213,7 +213,7 @@ void Sprite::loadSurfaceFromFile(const std::string& filename, int index) { #endif if(extension == "bmp"){ - m_image = SDL_LoadBMP(filename.c_str()); + m_image = std::make_shared(SDL_LoadBMP(filename.c_str())); if(!m_image){ DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_ERROR, "[Sprite::loadSurfaceFromFile] SDL_LoadBMP failed on file: %s\n", filename.c_str()); return; @@ -253,36 +253,37 @@ void Sprite::loadSurfaceFromFile(const std::string& filename, int index) { yatc_fread(&where, sizeof(where), 1, f); // create surface where we'll store data, and fill it with transparency - m_image = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 32, 32, 32, rmask, gmask, bmask, amask); - if(!m_image){ + SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 32, 32, 32, rmask, gmask, bmask, amask); + if(!surface){ DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY,DEBUGPRINT_ERROR, "[Sprite::loadSurfaceFromFile] Cant create SDL Surface.\n"); goto loadFail; } - SDL_LockSurface(m_image); + SDL_LockSurface(surface); // dont make static since if we change the rendering engine at runtime, // this may change too - Uint32 magenta = SDL_MapRGBA(m_image->format, 255, 0, 255, 255); - SDL_FillRect(m_image, NULL, magenta); + Uint32 magenta = SDL_MapRGBA(surface->format, 255, 0, 255, 255); + SDL_FillRect(surface, NULL, magenta); // read the data if (where) { fseek(f, where, SEEK_SET); fgetc(f); fgetc(f); fgetc(f); // FIXME (ivucica#4#) zerocoolz says this should be colorkey, according to http://otfans.net/showpost.php?p=840634&postcount=134 - if (readSprData(f, m_image, 0, 0)) { + if (readSprData(f, surface, 0, 0)) { // error happened DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_ERROR, "[Sprite::loadSurfaceFromFile] Problem in readSprData()\n"); - SDL_FreeSurface(m_image); - m_image = NULL; + SDL_FreeSurface(surface); + m_image = nullptr; fclose(f); goto loadFail; } } fclose(f); - SDL_UnlockSurface(m_image); - SDL_UpdateRect(m_image, 0, 0, 32, 32); + SDL_UnlockSurface(surface); + SDL_UpdateRect(surface, 0, 0, 32, 32); + m_image = std::make_shared(surface); #ifdef USE_OPENGL m_pixelformat = GL_RGBA; #endif @@ -345,7 +346,7 @@ void Sprite::loadSurfaceFromFile(const std::string& filename, int index) { } fclose(f); - m_image = s; + m_image = std::make_shared(s); #ifdef USE_OPENGL m_pixelformat = GL_RGBA; #endif @@ -360,11 +361,11 @@ void Sprite::loadSurfaceFromFile(const std::string& filename, int index) { Uint32 color = SDL_MapRGB(s->format, (int)r, (int)g, (int)b); SDL_FillRect(s, NULL, color); + m_image = std::make_shared(s); #ifdef USE_OPENGL m_pixelformat = GL_RGBA; #endif - m_image = s; - m_loaded = true; + m_loaded = true; } else{ @@ -375,15 +376,15 @@ void Sprite::loadSurfaceFromFile(const std::string& filename, int index) { m_filename = filename; m_index = index; - m_coloredimage = SDL_CreateRGBSurface(SDL_SWSURFACE, m_image->w, m_image->h, 32, rmask, gmask, bmask, amask); + m_coloredimage = std::make_shared(SDL_CreateRGBSurface(SDL_SWSURFACE, m_image->getWidth(), m_image->getHeight(), 32, rmask, gmask, bmask, amask)); - SDL_SetColorKey(m_image, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_image->format, 0xFF, 0, 0xFF)); // magenta is transparent + SDL_SetColorKey(static_cast(m_image.get())->getSurface(), SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(static_cast(m_image.get())->getSurface()->format, 0xFF, 0, 0xFF)); // magenta is transparent { - SDL_Surface *ns=SDL_DisplayFormatAlpha(m_image); + SDL_Surface *ns=SDL_DisplayFormatAlpha(static_cast(m_image.get())->getSurface()); if (ns) { - SDL_FreeSurface(m_image); - m_image=ns; + SDL_FreeSurface(static_cast(m_image.get())->getSurface()); + m_image = std::make_shared(ns); } } @@ -426,16 +427,16 @@ void Sprite::templatedColorize(Sprite* templatespr, uint8_t head, uint8_t body, // return; #endif templatespr->lockSurface(); - if(SDL_MUSTLOCK(m_image)) SDL_LockSurface(m_image); - for(int i=0; i < m_image->h; i++){ - for(int j=0; j < m_image->w; j++){ - uint32_t pixel = getPixel(j,i,m_image); - uint32_t templatepixel = getPixel(j,i,templatespr->getBasicImage()); + if(SDL_MUSTLOCK(static_cast(m_image.get())->getSurface())) SDL_LockSurface(static_cast(m_image.get())->getSurface()); + for(int i=0; i < m_image->getHeight(); i++){ + for(int j=0; j < m_image->getWidth(); j++){ + uint32_t pixel = getPixel(j,i,static_cast(m_image.get())->getSurface()); + uint32_t templatepixel = getPixel(j,i,static_cast(templatespr->getBasicImage().get())->getSurface()); uint8_t rt, gt, bt; // rgb template uint8_t ro, go, bo; // rgb original - SDL_GetRGB(templatepixel, templatespr->getBasicImage()->format, &rt, >, &bt); - SDL_GetRGB(pixel, m_image->format, &ro, &go, &bo); + SDL_GetRGB(templatepixel, static_cast(templatespr->getBasicImage().get())->getSurface()->format, &rt, >, &bt); + SDL_GetRGB(pixel, static_cast(m_image.get())->getSurface()->format, &ro, &go, &bo); if(rt && gt && !bt){ // yellow == head templatedColorizePixel(head, ro, go, bo); @@ -453,10 +454,10 @@ void Sprite::templatedColorize(Sprite* templatespr, uint8_t head, uint8_t body, continue; // if nothing changed, skip the change of pixel } - putPixel(j, i, SDL_MapRGB(m_image->format, ro, go, bo), m_image); + putPixel(j, i, SDL_MapRGB(static_cast(m_image.get())->getSurface()->format, ro, go, bo), static_cast(m_image.get())->getSurface()); } } - if(SDL_MUSTLOCK(m_image)) SDL_UnlockSurface(m_image); + if(SDL_MUSTLOCK(static_cast(m_image.get())->getSurface())) SDL_UnlockSurface(static_cast(m_image.get())->getSurface()); templatespr->unlockSurface(); rebuildSelf(); @@ -465,7 +466,7 @@ void Sprite::templatedColorize(Sprite* templatespr, uint8_t head, uint8_t body, void Sprite::putPixel(int x, int y, uint32_t pixel, SDL_Surface *img) { if (!img) - img = m_image; + img = static_cast(m_image.get())->getSurface(); if (!img->pixels) { //DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_WARNING, "Trying to write a pixel into a NIL array - %d, %d on a %dx%d image\n", x, y, img->w, img->h); @@ -511,7 +512,7 @@ void Sprite::putPixel(int x, int y, uint32_t pixel, SDL_Surface *img) uint32_t Sprite::getPixel(int x, int y, SDL_Surface *img) { if (!img) - img = m_image; + img = static_cast(m_image.get())->getSurface(); int bpp = img->format->BytesPerPixel; if (!img->pixels) { @@ -551,16 +552,16 @@ void Sprite::Stretch (float w, float h, int smooth, bool force) { SDL_Surface* img; if(m_stretchimage && !force){ - if(fabs(m_stretchimage->w - w) < 2.f && fabs(m_stretchimage->h - h) < 2.f){ + if(fabs(m_stretchimage->getWidth() - w) < 2.f && fabs(m_stretchimage->getHeight() - h) < 2.f){ return; } } if(m_r == 1.f && m_g == 1.f && m_b == 1.f){ - img = m_image; + img = static_cast(m_image.get())->getSurface(); } else{ - img = m_coloredimage; + img = static_cast(m_coloredimage.get())->getSurface(); } if(smooth == -1){ @@ -573,14 +574,14 @@ void Sprite::Stretch (float w, float h, int smooth, bool force) unStretch(); - if (w == m_image->w && h == m_image->h) + if (w == m_image->getWidth() && h == m_image->getHeight()) return; - if (m_stretchimage && w == m_stretchimage->w && h == m_stretchimage->h) + if (m_stretchimage && w == m_stretchimage->getWidth() && h == m_stretchimage->getHeight()) return; // DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "Stretching to %g %g\n", w, h); - m_stretchimage = zoomSurface(img, w/img->w, h/img->h, smooth); + m_stretchimage = std::make_shared(zoomSurface(img, w/img->w, h/img->h, smooth)); // DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "New size: %d %d\n", m_stretchimage->w, m_stretchimage->h); @@ -597,29 +598,29 @@ void Sprite::addColor(float r, float g, float b) if(r == m_r && g == m_g && b == m_b){ return; } - SDL_LockSurface(m_image); - SDL_LockSurface(m_coloredimage); + SDL_LockSurface(static_cast(m_image.get())->getSurface()); + SDL_LockSurface(static_cast(m_coloredimage.get())->getSurface()); - for(register int i = 0; i < m_image->w; i++){ - for(register int j =0; j < m_image->h; j++){ + for(register int i = 0; i < m_image->getWidth(); i++){ + for(register int j =0; j < m_image->getHeight(); j++){ if (!getBasicImage()) { printf("I don't have an image!\n"); } - if (!getBasicImage()->pixels) { + if (!static_cast(getBasicImage().get())->getSurface()->pixels) { printf("I don't have image's pixels!\n"); } - SDL_GetRGBA(getPixel(i,j, getBasicImage()), getBasicImage()->format, &ro, &go, &bo, &ao); + SDL_GetRGBA(getPixel(i,j, static_cast(getBasicImage().get())->getSurface()), static_cast(getBasicImage().get())->getSurface()->format, &ro, &go, &bo, &ao); if(ao){ - putPixel(i, j, SDL_MapRGBA(m_coloredimage->format, (uint8_t)(ro*r), (uint8_t)(go*g), (uint8_t)(bo*b), ao), m_coloredimage); + putPixel(i, j, SDL_MapRGBA(static_cast(m_coloredimage.get())->getSurface()->format, (uint8_t)(ro*r), (uint8_t)(go*g), (uint8_t)(bo*b), ao), static_cast(m_coloredimage.get())->getSurface()); } } } - SDL_UnlockSurface(m_image); - SDL_UnlockSurface(m_coloredimage); + SDL_UnlockSurface(static_cast(m_image.get())->getSurface()); + SDL_UnlockSurface(static_cast(m_coloredimage.get())->getSurface()); m_r = r; m_g = g; m_b = b; - Stretch(getImage()->w, getImage()->h, -1, true); + Stretch(getImage()->getWidth(), getImage()->getHeight(),m_smoothstretch,1); } void Sprite::setAsIcon() @@ -633,7 +634,7 @@ void Sprite::setAsIcon() NativeGUIError(s.str().c_str(), "Error"); } - SDL_WM_SetIcon(m_image, NULL); + SDL_WM_SetIcon(static_cast(m_image.get())->getSurface(), NULL); } @@ -659,7 +660,7 @@ SDL_Cursor* Sprite::createCursor(int topx, int topy, int w, int h, int hot_x, in uint32_t pv; uint8_t r,g,b,a; - SDL_GetRGBA(pv=getPixel(topx+col, topy+row, m_image), m_image->format, &r, &g, &b, &a); + SDL_GetRGBA(pv=getPixel(topx+col, topy+row, static_cast(m_image.get())->getSurface()), static_cast(m_image.get())->getSurface()->format, &r, &g, &b, &a); if(a == 0){ // transparent @@ -683,18 +684,18 @@ SDL_Cursor* Sprite::createCursor(int topx, int topy, int w, int h, int hot_x, in } -SDL_Surface* Sprite::lockSurface() +std::shared_ptr Sprite::lockSurface() { //printf("LOCK %p\n", m_image); - if(SDL_MUSTLOCK(m_image)) - if(SDL_LockSurface(m_image)==-1) - return NULL; + if(SDL_MUSTLOCK(static_cast(m_image.get())->getSurface())) + if(SDL_LockSurface(static_cast(m_image.get())->getSurface())==-1) + return nullptr; return m_image; } void Sprite::unlockSurface() { //printf("UNLOCK %p\n", m_image); - if(SDL_MUSTLOCK(m_image)) SDL_UnlockSurface(m_image); + if(SDL_MUSTLOCK(static_cast(m_image.get())->getSurface())) SDL_UnlockSurface(static_cast(m_image.get())->getSurface()); Stretch(getWidth(), getHeight(),m_smoothstretch,1); // when unlocking rebuildSelf(); } diff --git a/sprite.h b/sprite.h index eae64282..fef01eec 100644 --- a/sprite.h +++ b/sprite.h @@ -34,6 +34,8 @@ #endif #include +#include +#include "rgba_storage.h" #ifndef __USE_GLSDL__ #include #else @@ -51,10 +53,10 @@ class Sprite Sprite(const std::string& filename, int index, int x, int y, int w, int h); virtual ~Sprite(); - virtual float getWidth() const { return m_stretchimage ? m_stretchimage->w : m_image->w; } - virtual float getHeight() const { return m_stretchimage ? m_stretchimage->h : m_image->h; } - float getBasicWidth() const { return m_image->w; } - float getBasicHeight() const { return m_image->h; } + virtual float getWidth() const { return m_stretchimage ? m_stretchimage->getWidth() : m_image->getWidth(); } + virtual float getHeight() const { return m_stretchimage ? m_stretchimage->getHeight() : m_image->getHeight(); } + float getBasicWidth() const { return m_image->getWidth(); } + float getBasicHeight() const { return m_image->getHeight(); } bool isLoaded() { return m_loaded;} @@ -73,7 +75,7 @@ class Sprite void templatedColorizePixel(uint8_t color, uint8_t &r, uint8_t &g, uint8_t &b); void Stretch(float neww, float newh, int smooth = -1, bool force = false); - void unStretch() { if (m_stretchimage) SDL_FreeSurface(m_stretchimage); m_stretchimage = NULL; } + void unStretch() { if (m_stretchimage) m_stretchimage.reset(); m_stretchimage = nullptr; } void setAsIcon();// used only once, in main @@ -82,21 +84,21 @@ class Sprite // use functions for direct modification SPARINGLY! - virtual SDL_Surface* getSurface() { return m_image; } - virtual SDL_Surface* lockSurface(); + virtual std::shared_ptr getSurface() { return m_image; } + virtual std::shared_ptr lockSurface(); virtual void unlockSurface(); - void putPixel(int x, int y, uint32_t pixel, SDL_Surface *img = NULL); - uint32_t getPixel(int x, int y, SDL_Surface *img = NULL); + void putPixel(int x, int y, uint32_t pixel, std::shared_ptr img = nullptr); + uint32_t getPixel(int x, int y, std::shared_ptr img = nullptr); protected: Sprite(const Sprite& original); void loadSurfaceFromFile(const std::string& filename, int index); - SDL_Surface* getColoredImage() { return m_stretchimage ? m_stretchimage : ((m_r != 1. || m_g != 1. || m_b != 1.) ? m_coloredimage : m_image); } - SDL_Surface* getImage() { return m_stretchimage ? m_stretchimage : m_image; } - SDL_Surface* getBasicImage() { return m_image; } + std::shared_ptr getColoredImage() { return m_stretchimage ? m_stretchimage : ((m_r != 1. || m_g != 1. || m_b != 1.) ? m_coloredimage : m_image); } + std::shared_ptr getImage() { return m_stretchimage ? m_stretchimage : m_image; } + std::shared_ptr getBasicImage() { return m_image; } #ifdef USE_OPENGL GLuint getPixelFormat() { return m_pixelformat; } #endif @@ -107,7 +109,7 @@ class Sprite private: bool m_loaded; - SDL_Surface *m_image, *m_stretchimage, *m_coloredimage; + std::shared_ptr m_image, m_stretchimage, m_coloredimage; bool m_smoothstretch; #ifdef USE_OPENGL diff --git a/spritegl.cpp b/spritegl.cpp index 00e66a22..157f72c7 100644 --- a/spritegl.cpp +++ b/spritegl.cpp @@ -82,12 +82,12 @@ void SpriteGL::buildGLTexture() { #endif - SDL_Surface *sfc = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, nextpow(getBasicImage()->w), nextpow(getBasicImage()->h), 32, rmask, gmask, bmask, amask); + SDL_Surface *sfc = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, nextpow(getBasicImage()->getWidth()), nextpow(getBasicImage()->getHeight()), 32, rmask, gmask, bmask, amask); - SDL_Rect s = {0,0,uint16_t(getBasicImage()->w),uint16_t(getBasicImage()->h)}; - SDL_Rect d = {0,0,uint16_t(getBasicImage()->w),uint16_t(getBasicImage()->h)}; + SDL_Rect s = {0,0,uint16_t(getBasicImage()->getWidth()),uint16_t(getBasicImage()->getHeight())}; + SDL_Rect d = {0,0,uint16_t(getBasicImage()->getWidth()),uint16_t(getBasicImage()->getHeight())}; - SDL_BlitSurface(getBasicImage(), &s, sfc, &d); + SDL_BlitSurface(static_cast(getBasicImage().get())->getSurface(), &s, sfc, &d); SDL_SetAlpha(sfc, SDL_SRCALPHA, 255); SDL_LockSurface(sfc); @@ -186,4 +186,3 @@ void SpriteGL::destroyGLTexture() } } #endif - diff --git a/spritegl.h b/spritegl.h index d89d3426..dbd414d3 100644 --- a/spritegl.h +++ b/spritegl.h @@ -42,6 +42,7 @@ class SpriteGL : public Sprite GLuint m_texture; double m_multiplierx, m_multipliery; uint32_t m_engineCreationTimestamp; + std::shared_ptr m_image; }; #endif diff --git a/spritesdl.cpp b/spritesdl.cpp index e1f8e82d..fb23dc0c 100644 --- a/spritesdl.cpp +++ b/spritesdl.cpp @@ -59,13 +59,13 @@ void SpriteSDL::_BlitInternal(float dx, float dy, float sx, float sy, float w, f // code is like this because of dx5.0 ... see docs/html/sdlrect.html in SDL documentation for more info SDL_Rect src = {(Sint16)sx,(Sint16)sy,(Uint16)(w),(Uint16)(h)}; SDL_Rect dst = {(Sint16)dx,(Sint16)dy,(Uint16)(w),(Uint16)(h)}; - while(SDL_BlitSurface(getColoredImage(), &src, g_engine->m_screen, &dst) == -2){ - while(SDL_LockSurface(getColoredImage()) < 0 ){ + while(SDL_BlitSurface(static_cast(getColoredImage().get())->getSurface(), &src, static_cast(g_engine->m_screen.get())->getSurface(), &dst) == -2){ + while(SDL_LockSurface(static_cast(getColoredImage().get())->getSurface()) < 0 ){ SDL_Delay(10); } loadSurfaceFromFile(m_filename, m_index); - SDL_UnlockSurface(getColoredImage()); + SDL_UnlockSurface(static_cast(getColoredImage().get())->getSurface()); } } @@ -78,9 +78,9 @@ void SpriteSDL::Blit(float dx, float dy, float sx, float sy, float w, float h, f double lambdaw = destw / w; double lambdah = desth / h; - double neww = getBasicImage()->w * lambdaw; - double newh = getBasicImage()->h * lambdah; - if(getImage()->w != neww || getImage()->h != newh) { + double neww = getBasicImage()->getWidth() * lambdaw; + double newh = getBasicImage()->getHeight() * lambdaw; + if(getImage()->getWidth() != neww || getImage()->getHeight() != newh) { Stretch(neww, newh, options.smoothstretch); oldw = neww; oldh = newh;