diff --git a/libgag/include/SDLGraphicContext.h b/libgag/include/SDLGraphicContext.h index a4bac5ec..861bd4d6 100644 --- a/libgag/include/SDLGraphicContext.h +++ b/libgag/include/SDLGraphicContext.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -151,6 +152,19 @@ namespace GAGCore virtual void drawString(DrawableSurface *Surface, int x, int y, int w, const std::string text, Uint8 alpha) = 0; virtual void drawString(DrawableSurface *Surface, float x, float y, float w, const std::string text, Uint8 alpha) = 0; }; + + // Texture dimensions and sprite sheet number + struct TextureInfo + { + //! which animation or texture atlas this surface is part of. + Sprite* sprite = nullptr; + //! sprite sheet coordinates + int texX = 0; + int texY = 0; + //! width and height of this tile if using atlas + int w = 0; + int h = 0; + }; //! A surface on which we can draw class DrawableSurface @@ -161,20 +175,14 @@ namespace GAGCore friend class Sprite; //! the underlying software SDL surface SDL_Surface *sdlsurface; - //! which animation or texture atlas this surface is part of. - Sprite* sprite = nullptr; + // Texture dimensions and sprite sheet number + boost::optional textureInfo; //! The clipping rect, we do not draw outside it SDL_Rect clipRect; //! this surface has been modified since latest blit bool dirty; //! texture index if GPU (GL) is used unsigned int texture; - //! sprite sheet coordinates - int texX = 0; - int texY = 0; - //! width and height of this tile if using atlas - int w = 0; - int h = 0; //! texture divisor float texMultX, texMultY; @@ -218,8 +226,11 @@ namespace GAGCore virtual void shiftHSV(float hue, float sat, float lum); // accessors - virtual int getW(void) { if (sprite) return w; return sdlsurface->w; } - virtual int getH(void) { if (sprite) return h; return sdlsurface->h; } + virtual int getW(void) { if (textureInfo) return textureInfo->w; return sdlsurface->w; } + virtual int getH(void) { if (textureInfo) return textureInfo->h; return sdlsurface->h; } + + virtual int getTexX(void) { if (textureInfo) { return textureInfo->texX; } return 0; } + virtual int getTexY(void) { if (textureInfo) { return textureInfo->texY; } return 0; } // capability querying virtual bool canDrawStretchedSprite(void) { return false; } diff --git a/libgag/src/GraphicContext.cpp b/libgag/src/GraphicContext.cpp index bd0c84f5..225e9ed1 100644 --- a/libgag/src/GraphicContext.cpp +++ b/libgag/src/GraphicContext.cpp @@ -310,7 +310,7 @@ namespace GAGCore void DrawableSurface::allocateTexture(void) { #ifdef HAVE_OPENGL - if (sprite) + if (textureInfo) return; if (_gc->optionFlags & GraphicContext::USEGPU) { @@ -354,7 +354,7 @@ namespace GAGCore void DrawableSurface::uploadToTexture(void) { #ifdef HAVE_OPENGL - if (sprite) + if (textureInfo) { return; } @@ -1062,22 +1062,22 @@ namespace GAGCore void DrawableSurface::drawSurface(int x, int y, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void DrawableSurface::drawSurface(float x, float y, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void DrawableSurface::drawSurface(int x, int y, int w, int h, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, w, h, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, w, h, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void DrawableSurface::drawSurface(float x, float y, float w, float h, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, w, h, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, w, h, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void DrawableSurface::drawSurface(int x, int y, DrawableSurface *surface, int sx, int sy, int sw, int sh, Uint8 alpha) @@ -1630,22 +1630,22 @@ namespace GAGCore void GraphicContext::drawSurface(int x, int y, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void GraphicContext::drawSurface(float x, float y, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void GraphicContext::drawSurface(int x, int y, int w, int h, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, w, h, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, w, h, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void GraphicContext::drawSurface(float x, float y, float w, float h, DrawableSurface *surface, Uint8 alpha) { - drawSurface(x, y, w, h, surface, surface->texX, surface->texY, surface->getW(), surface->getH(), alpha); + drawSurface(x, y, w, h, surface, surface->getTexX(), surface->getTexY(), surface->getW(), surface->getH(), alpha); } void GraphicContext::drawSurface(int x, int y, DrawableSurface *surface, int sx, int sy, int sw, int sh, Uint8 alpha) @@ -1695,10 +1695,11 @@ namespace GAGCore // draw glState.setTexture(surface->texture); - if (surface->sprite && alpha == Color::ALPHA_OPAQUE) + if (surface->textureInfo && surface->textureInfo->sprite && alpha == Color::ALPHA_OPAQUE) { - surface->sprite->vertices.insert(surface->sprite->vertices.end(), { x, y, x + w, y, x + w, y + h, x, y + h }); - surface->sprite->texCoords.insert(surface->sprite->texCoords.end(), { + Sprite* sprite = surface->textureInfo->sprite; + sprite->vertices.insert(sprite->vertices.end(), { x, y, x + w, y, x + w, y + h, x, y + h }); + sprite->texCoords.insert(sprite->texCoords.end(), { static_cast(sx) * surface->texMultX, static_cast(sy) * surface->texMultY, static_cast(sx + sw) * surface->texMultX, static_cast(sy) * surface->texMultY, static_cast(sx + sw) * surface->texMultX, static_cast(sy + sh) * surface->texMultY, diff --git a/libgag/src/Sprite.cpp b/libgag/src/Sprite.cpp index 19d81712..409962e6 100644 --- a/libgag/src/Sprite.cpp +++ b/libgag/src/Sprite.cpp @@ -157,12 +157,10 @@ namespace GAGCore for (auto image: images) { atlas->drawSurface(x, y, image); - image->texX = x; - image->texY = y; + TextureInfo info = { this, x, y, tileWidth, tileHeight }; + image->textureInfo = info; image->texMultX = 1.f; image->texMultY = 1.f; - image->w = tileWidth; - image->h = tileHeight; x += tileWidth; if (tileWidth + x > sheetWidth) { x = 0; @@ -170,12 +168,6 @@ namespace GAGCore } } atlas->uploadToTexture(); - for (auto image : images) - { - image->texture = atlas->texture; - image->sprite = this; - image->setRes(sheetWidth, sheetHeight); - } this->atlas = std::move(atlas); glGenBuffers(1, &vbo); glGenBuffers(1, &texCoordBuffer);