Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/engine/GameObjects/MapNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "../map/MapLayers.hxx"
#include "GameStates.hxx"
#include "Settings.hxx"
#include "TileManager.hxx"
#include "enums.hxx"

MapNode::MapNode(Point isoCoordinates, const std::string &terrainID, const std::string &tileID)
: m_isoCoordinates(std::move(isoCoordinates)), m_sprite{std::make_unique<Sprite>(m_isoCoordinates)},
Expand Down Expand Up @@ -369,6 +371,8 @@ void MapNode::updateTexture(const Layer &layer)
static_cast<Layer>(currentLayer));
m_sprite->setTexture(TileManager::instance().getTexture(m_mapNodeData[currentLayer].tileID),
static_cast<Layer>(currentLayer));
m_sprite->setRequiredTiles(TileManager::instance().getTileData(m_mapNodeData[currentLayer].tileID)->RequiredTiles,
static_cast<Layer>(currentLayer));
}

spriteCount = m_mapNodeData[currentLayer].tileData->tiles.count;
Expand All @@ -391,6 +395,8 @@ void MapNode::updateTexture(const Layer &layer)
static_cast<Layer>(currentLayer));
m_sprite->setTexture(TileManager::instance().getTexture(m_mapNodeData[currentLayer].tileID + "_shore"),
static_cast<Layer>(currentLayer));
m_sprite->setRequiredTiles(TileManager::instance().getTileData(m_mapNodeData[currentLayer].tileID)->RequiredTiles,
static_cast<Layer>(currentLayer));
}

spriteCount = m_mapNodeData[currentLayer].tileData->shoreTiles.count;
Expand All @@ -411,6 +417,8 @@ void MapNode::updateTexture(const Layer &layer)
static_cast<Layer>(currentLayer));
m_sprite->setTexture(TileManager::instance().getTexture(m_mapNodeData[currentLayer].tileID),
static_cast<Layer>(currentLayer));
m_sprite->setRequiredTiles(TileManager::instance().getTileData(m_mapNodeData[currentLayer].tileID)->RequiredTiles,
static_cast<Layer>(currentLayer));
}
break;
default:
Expand Down
15 changes: 14 additions & 1 deletion src/engine/Sprite.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "LOG.hxx"
#include "Exception.hxx"
#include "GameStates.hxx"
#include "tileData.hxx"

#ifdef MICROPROFILE_ENABLED
#include "microprofile/microprofile.h"
Expand All @@ -29,6 +30,7 @@ void Sprite::render() const
{
if (MapLayers::isLayerActive(currentLayer) && m_SpriteData[currentLayer].texture && m_renderLayer[currentLayer])
{

// Don't draw zones when there is a building on this sprite
if (currentLayer == Layer::ZONE && m_SpriteData[Layer::BUILDINGS].texture)
{
Expand Down Expand Up @@ -118,7 +120,16 @@ void Sprite::refresh(const Layer &layer)
if (it.texture != nullptr)
{
// render the sprite in the middle of its bounding box so bigger than 1x1 sprites will render correctly
it.destRect.x = m_screenCoordinates.x - (it.destRect.w / 2);
if (it.RequiredTiles.width == it.RequiredTiles.height)
{ // handle the special case of square sprites first
it.destRect.x = m_screenCoordinates.x - (it.destRect.w / 2);
}
else
{ // and if need be, do the more expensive calculation for the otheor sprites.*/
it.destRect.x =
m_screenCoordinates.x - (it.destRect.w * it.RequiredTiles.width) / (it.RequiredTiles.width + it.RequiredTiles.height);
}

// change y coordinates with sprites height taken into account to render the sprite at its base and not at its top.
it.destRect.y = m_screenCoordinates.y - it.destRect.h;
}
Expand All @@ -138,6 +149,7 @@ void Sprite::setTexture(SDL_Texture *texture, Layer layer)

void Sprite::setClipRect(SDL_Rect clipRect, const Layer layer) { m_SpriteData[layer].clipRect = clipRect; }
void Sprite::setDestRect(SDL_Rect destRect, Layer layer) { m_SpriteData[layer].destRect = destRect; }
void Sprite::setRequiredTiles(TileSize requiredTiles, Layer layer) { m_SpriteData[layer].RequiredTiles = requiredTiles; }

SDL_Rect Sprite::getActiveClipRect()
{
Expand Down Expand Up @@ -176,4 +188,5 @@ void Sprite::clearSprite(Layer layer)
m_SpriteData[layer].clipRect = {0, 0, 0, 0};
m_SpriteData[layer].destRect = {0, 0, 0, 0};
m_SpriteData[layer].texture = nullptr;
m_SpriteData[layer].RequiredTiles = {0, 0};
}
3 changes: 3 additions & 0 deletions src/engine/Sprite.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

#include "basics/Point.hxx"
#include "common/enums.hxx"
#include "tileData.hxx"

struct SpriteData
{
SDL_Texture *texture = nullptr;
SDL_Rect clipRect{0, 0, 0, 0};
SDL_Rect destRect{0, 0, 0, 0};
unsigned char alpha = 255;
TileSize RequiredTiles;
};

struct SpriteRGBColor
Expand Down Expand Up @@ -41,6 +43,7 @@ public:
void setTexture(SDL_Texture *m_texture, Layer layer = Layer::TERRAIN);
void setClipRect(SDL_Rect clipRect, Layer layer = Layer::TERRAIN);
void setDestRect(SDL_Rect clipRect, Layer layer = Layer::TERRAIN);
void setRequiredTiles(TileSize requiredTiles, Layer layer = Layer::TERRAIN);

void clearSprite(Layer layer);

Expand Down