Skip to content

Commit

Permalink
Use boost::optional for the texture information.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quipyowert2 committed Dec 27, 2024
1 parent 0cb2535 commit 0daeebc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
31 changes: 21 additions & 10 deletions libgag/include/SDLGraphicContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <string>
#include <iostream>
#include <valarray>
#include <boost/optional.hpp>

#include <set>
#include <boost/tuple/tuple.hpp>
Expand Down Expand Up @@ -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
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
Expand All @@ -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
boost::optional<TextureInfo> 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;

Expand Down Expand Up @@ -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; }
Expand Down
27 changes: 14 additions & 13 deletions libgag/src/GraphicContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ namespace GAGCore
void DrawableSurface::allocateTexture(void)
{
#ifdef HAVE_OPENGL
if (sprite)
if (textureInfo)
return;
if (_gc->optionFlags & GraphicContext::USEGPU)
{
Expand Down Expand Up @@ -354,7 +354,7 @@ namespace GAGCore
void DrawableSurface::uploadToTexture(void)
{
#ifdef HAVE_OPENGL
if (sprite)
if (textureInfo)
{
return;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<float>(sx) * surface->texMultX, static_cast<float>(sy) * surface->texMultY,
static_cast<float>(sx + sw) * surface->texMultX, static_cast<float>(sy) * surface->texMultY,
static_cast<float>(sx + sw) * surface->texMultX, static_cast<float>(sy + sh) * surface->texMultY,
Expand Down
12 changes: 2 additions & 10 deletions libgag/src/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,17 @@ 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;
y += tileHeight;
}
}
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);
Expand Down

0 comments on commit 0daeebc

Please sign in to comment.