Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Sprite Class Reference

#include <Sprite.hxx>

+ Collaboration diagram for Sprite:

Public Member Functions

 Sprite (Point isoCoordinates)
 
virtual ~Sprite ()=default
 
void render () const
 
void refresh (const Layer &layer=Layer::NONE)
 
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 clearSprite (Layer layer)
 
void setRenderFlag (const Layer &layer, bool render)
 enable/disable rendering for this sprite on a specific layer More...
 
SDL_Rect getDestRect (Layer layer=Layer::TERRAIN)
 
SDL_Rect getClipRect (Layer layer=Layer::TERRAIN)
 
SDL_Rect getActiveClipRect ()
 
SDL_Rect getActiveDestRect ()
 
void setSpriteTranparencyFactor (const Layer &layer, unsigned char alpha)
 
bool isLayerUsed (Layer layer)
 

Public Attributes

size_t spriteCount = 1
 
bool highlightSprite = false
 
SpriteRGBColor highlightColor = SpriteHighlightColor::GRAY
 
Point isoCoordinates {0, 0, 0, 0}
 

Private Attributes

SDL_Point m_screenCoordinates {0, 0}
 
bool m_needsRefresh = false
 
double m_currentZoomLevel = 0
 
std::vector< SpriteDatam_SpriteData
 
std::vector< bool > m_renderLayer
 

Detailed Description

Definition at line 32 of file Sprite.hxx.

Constructor & Destructor Documentation

◆ Sprite()

Sprite::Sprite ( Point  isoCoordinates)
explicit

Definition at line 17 of file Sprite.cxx.

19 {
21 }
+ Here is the call graph for this function:

◆ ~Sprite()

virtual Sprite::~Sprite ( )
virtualdefault

Member Function Documentation

◆ clearSprite()

void Sprite::clearSprite ( Layer  layer)

Definition at line 174 of file Sprite.cxx.

175 {
176  m_SpriteData[layer].clipRect = {0, 0, 0, 0};
177  m_SpriteData[layer].destRect = {0, 0, 0, 0};
178  m_SpriteData[layer].texture = nullptr;
179 }

◆ getActiveClipRect()

SDL_Rect Sprite::getActiveClipRect ( )

Definition at line 142 of file Sprite.cxx.

143 {
145  m_SpriteData[Layer::BUILDINGS].clipRect.h != 0)
146  {
147  return m_SpriteData[Layer::BUILDINGS].clipRect;
148  }
150  {
151  return m_SpriteData[Layer::TERRAIN].clipRect;
152  }
153  return {0, 0, 0, 0};
154 }
+ Here is the call graph for this function:

◆ getActiveDestRect()

SDL_Rect Sprite::getActiveDestRect ( )

Definition at line 160 of file Sprite.cxx.

161 {
163  m_SpriteData[Layer::BUILDINGS].destRect.h != 0)
164  {
165  return m_SpriteData[Layer::BUILDINGS].destRect;
166  }
168  {
169  return m_SpriteData[Layer::TERRAIN].destRect;
170  }
171  return {0, 0, 0, 0};
172 }
+ Here is the call graph for this function:

◆ getClipRect()

SDL_Rect Sprite::getClipRect ( Layer  layer = Layer::TERRAIN)
inline

Definition at line 59 of file Sprite.hxx.

59 { return m_SpriteData[layer].clipRect; };

◆ getDestRect()

SDL_Rect Sprite::getDestRect ( Layer  layer = Layer::TERRAIN)
inline

Definition at line 58 of file Sprite.hxx.

58 { return m_SpriteData[layer].destRect; };

◆ isLayerUsed()

bool Sprite::isLayerUsed ( Layer  layer)

Definition at line 155 of file Sprite.cxx.

156 {
157  return (MapLayers::isLayerActive(layer) && m_SpriteData[layer].clipRect.w != 0 && m_SpriteData[layer].clipRect.h != 0);
158 }
+ Here is the call graph for this function:

◆ refresh()

void Sprite::refresh ( const Layer layer = Layer::NONE)

Definition at line 73 of file Sprite.cxx.

74 {
75  if (m_currentZoomLevel != Camera::instance().zoomLevel() || m_needsRefresh)
76  {
77  for (auto currentLayer : allLayersOrdered)
78  {
79  if (m_SpriteData[currentLayer].texture)
80  {
81  if (layer != NONE && currentLayer != layer)
82  {
83  continue;
84  }
86  int spriteSheetHeight = 0;
87  SDL_QueryTexture(m_SpriteData[currentLayer].texture, nullptr, nullptr, nullptr, &spriteSheetHeight);
88  // we need to offset the cliprect.y coodinate, because we've moved the "originpoint" for drawing the sprite to the screen on the bottom.
89  // the sprites need to start at the bottom, so the cliprect must too.
90  m_SpriteData[currentLayer].clipRect.y = spriteSheetHeight - m_SpriteData[currentLayer].clipRect.h;
91 
92  if (m_SpriteData[currentLayer].clipRect.w != 0)
93  {
94  m_SpriteData[currentLayer].destRect.w =
95  static_cast<int>(std::round(static_cast<double>(m_SpriteData[currentLayer].clipRect.w) * m_currentZoomLevel));
96  m_SpriteData[currentLayer].destRect.h =
97  static_cast<int>(std::round(static_cast<double>(m_SpriteData[currentLayer].clipRect.h) * m_currentZoomLevel));
98  }
99  else
100  {
101  SDL_QueryTexture(m_SpriteData[currentLayer].texture, nullptr, nullptr, &m_SpriteData[currentLayer].destRect.w,
102  &m_SpriteData[currentLayer].destRect.h);
103 
104  m_SpriteData[currentLayer].destRect.w =
105  static_cast<int>(std::round(static_cast<double>(m_SpriteData[currentLayer].clipRect.w) * m_currentZoomLevel));
106  m_SpriteData[currentLayer].destRect.h =
107  static_cast<int>(std::round(static_cast<double>(m_SpriteData[currentLayer].clipRect.h) * m_currentZoomLevel));
108  }
109  }
110  }
111  }
112 
113  // convert this tiles isometric coordinates to screen coordinates (with camera offset / zoomlevel taken into account.
115 
116  for (auto &it : m_SpriteData)
117  {
118  if (it.texture != nullptr)
119  {
120  // render the sprite in the middle of its bounding box so bigger than 1x1 sprites will render correctly
121  it.destRect.x = m_screenCoordinates.x - (it.destRect.w / 2);
122  // change y coordinates with sprites height taken into account to render the sprite at its base and not at its top.
123  it.destRect.y = m_screenCoordinates.y - it.destRect.h;
124  }
125  }
126 
127  m_needsRefresh = false;
128 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ render()

void Sprite::render ( ) const

Definition at line 23 of file Sprite.cxx.

24 {
25 #ifdef MICROPROFILE_ENABLED
26  MICROPROFILE_SCOPEI("Map", "Sprite render", MP_RED);
27 #endif
28  for (auto currentLayer : allLayersOrdered)
29  {
30  if (MapLayers::isLayerActive(currentLayer) && m_SpriteData[currentLayer].texture && m_renderLayer[currentLayer])
31  {
32  // Don't draw zones when there is a building on this sprite
33  if (currentLayer == Layer::ZONE && m_SpriteData[Layer::BUILDINGS].texture)
34  {
35  continue;
36  }
37  if (highlightSprite)
38  {
39  SDL_SetTextureColorMod(m_SpriteData[currentLayer].texture, highlightColor.r, highlightColor.g, highlightColor.b);
40  }
41 
42  if (GameStates::instance().layerEditMode == LayerEditMode::BLUEPRINT && currentLayer != Layer::BLUEPRINT &&
43  currentLayer != Layer::UNDERGROUND)
44  {
45  SDL_SetTextureAlphaMod(m_SpriteData[currentLayer].texture, 80);
46  }
47  else
48  {
49  SDL_SetTextureAlphaMod(m_SpriteData[currentLayer].texture, m_SpriteData[currentLayer].alpha);
50  }
51 
52  if (m_SpriteData[currentLayer].clipRect.w != 0)
53  {
54  SDL_RenderCopy(WindowManager::instance().getRenderer(), m_SpriteData[currentLayer].texture,
55  &m_SpriteData[currentLayer].clipRect, &m_SpriteData[currentLayer].destRect);
56  }
57  else
58  {
59  SDL_RenderCopy(WindowManager::instance().getRenderer(), m_SpriteData[currentLayer].texture, nullptr,
60  &m_SpriteData[currentLayer].destRect);
61  }
62 
63  if (highlightSprite)
64  {
65  SDL_SetTextureColorMod(m_SpriteData[currentLayer].texture, 255, 255, 255);
66  }
67 
68  SDL_SetTextureAlphaMod(m_SpriteData[currentLayer].texture, 255);
69  }
70  }
71 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setClipRect()

void Sprite::setClipRect ( SDL_Rect  clipRect,
Layer  layer = Layer::TERRAIN 
)

Definition at line 139 of file Sprite.cxx.

139 { m_SpriteData[layer].clipRect = clipRect; }

◆ setDestRect()

void Sprite::setDestRect ( SDL_Rect  clipRect,
Layer  layer = Layer::TERRAIN 
)

Definition at line 140 of file Sprite.cxx.

140 { m_SpriteData[layer].destRect = destRect; }

◆ setRenderFlag()

void Sprite::setRenderFlag ( const Layer layer,
bool  render 
)
inline

enable/disable rendering for this sprite on a specific layer

Parameters
layerwhich layer to set the flag for
renderif this sprite should render this layer

Definition at line 52 of file Sprite.hxx.

52 { m_renderLayer[layer] = render; };
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSpriteTranparencyFactor()

void Sprite::setSpriteTranparencyFactor ( const Layer layer,
unsigned char  alpha 
)
inline

Definition at line 62 of file Sprite.hxx.

62 { m_SpriteData[layer].alpha = alpha; }

◆ setTexture()

void Sprite::setTexture ( SDL_Texture *  m_texture,
Layer  layer = Layer::TERRAIN 
)

Definition at line 130 of file Sprite.cxx.

131 {
132  if (!texture)
133  throw UIError(TRACE_INFO "Called Sprite::setTexture() with a non valid texture");
134  m_SpriteData[layer].texture = texture;
135  m_needsRefresh = true;
136  refresh(layer);
137 }
+ Here is the call graph for this function:

Member Data Documentation

◆ highlightColor

SpriteRGBColor Sprite::highlightColor = SpriteHighlightColor::GRAY

Definition at line 56 of file Sprite.hxx.

◆ highlightSprite

bool Sprite::highlightSprite = false

Definition at line 55 of file Sprite.hxx.

◆ isoCoordinates

Point Sprite::isoCoordinates {0, 0, 0, 0}

Definition at line 65 of file Sprite.hxx.

◆ m_currentZoomLevel

double Sprite::m_currentZoomLevel = 0
private

Definition at line 71 of file Sprite.hxx.

◆ m_needsRefresh

bool Sprite::m_needsRefresh = false
private

Definition at line 70 of file Sprite.hxx.

◆ m_renderLayer

std::vector<bool> Sprite::m_renderLayer
private

Definition at line 74 of file Sprite.hxx.

◆ m_screenCoordinates

SDL_Point Sprite::m_screenCoordinates {0, 0}
private

Definition at line 68 of file Sprite.hxx.

◆ m_SpriteData

std::vector<SpriteData> Sprite::m_SpriteData
private

Definition at line 73 of file Sprite.hxx.

◆ spriteCount

size_t Sprite::spriteCount = 1

Definition at line 54 of file Sprite.hxx.


The documentation for this class was generated from the following files:
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
Sprite::m_screenCoordinates
SDL_Point m_screenCoordinates
Definition: Sprite.hxx:68
UIError
A UI-related error occured.
Definition: Exception.hxx:76
Sprite::highlightSprite
bool highlightSprite
Definition: Sprite.hxx:55
TerrainEdit::NONE
@ NONE
Sprite::m_SpriteData
std::vector< SpriteData > m_SpriteData
Definition: Sprite.hxx:73
SpriteRGBColor::r
uint8_t r
Definition: Sprite.hxx:20
Sprite::render
void render() const
Definition: Sprite.cxx:23
SpriteRGBColor::g
uint8_t g
Definition: Sprite.hxx:21
Sprite::m_currentZoomLevel
double m_currentZoomLevel
Definition: Sprite.hxx:71
ZONE
@ ZONE
4- Optional layer, zones(Industrial/Residential/Commercial).
Definition: enums.hxx:15
BLUEPRINT
@ BLUEPRINT
1- Optional layer - Map Blueprint
Definition: enums.hxx:12
Sprite::m_needsRefresh
bool m_needsRefresh
Definition: Sprite.hxx:70
BUILDINGS
@ BUILDINGS
8- Buildings, Streets and everything that goes on the terrain
Definition: enums.hxx:19
Camera::zoomLevel
const double & zoomLevel() const noexcept
Definition: Camera.cxx:97
Sprite::highlightColor
SpriteRGBColor highlightColor
Definition: Sprite.hxx:56
MapLayers::isLayerActive
static bool isLayerActive(unsigned int layer)
Check if given Layer is being drawn.
Definition: MapLayers.hxx:34
TERRAIN
@ TERRAIN
3- Terrain tiles, decorations, ... - must always be a "full" tile
Definition: enums.hxx:14
Sprite::isoCoordinates
Point isoCoordinates
Definition: Sprite.hxx:65
Sprite::m_renderLayer
std::vector< bool > m_renderLayer
Definition: Sprite.hxx:74
Sprite::refresh
void refresh(const Layer &layer=Layer::NONE)
Definition: Sprite.cxx:73
SpriteRGBColor::b
uint8_t b
Definition: Sprite.hxx:22
allLayersOrdered
static Layer allLayersOrdered[]
This is a ordered list of all relevant layers we need to interact with.
Definition: enums.hxx:29
Singleton< Camera >::instance
static Camera & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
LAYERS_COUNT
@ LAYERS_COUNT
this must be LAST !!!
Definition: enums.hxx:25
LayerEditMode::BLUEPRINT
@ BLUEPRINT
Placing water pipes and underground transportation on the Blueprint layer.
UNDERGROUND
@ UNDERGROUND
2- Optional layer - Pipes, Subway-pipes and so onn
Definition: enums.hxx:13
convertIsoToScreenCoordinates
SDL_Point convertIsoToScreenCoordinates(const Point &isoCoordinates, bool calcWithoutOffset)
converts coordinates from isometric to screen space
Definition: isoMath.cxx:25