Loading [MathJax]/extensions/MathMenu.js
Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Map.cxx
Go to the documentation of this file.
1 #include "Map.hxx"
2 
3 #include "basics/Camera.hxx"
4 #include "basics/isoMath.hxx"
5 #include "basics/mapEdit.hxx"
6 #include "basics/Settings.hxx"
7 #include "LOG.hxx"
8 #include "common/Constants.hxx"
9 #include "map/MapLayers.hxx"
11 #include "common/Constants.hxx"
12 #include "Filesystem.hxx"
13 #include "../services/Randomizer.hxx"
14 
15 #include "json.hxx"
16 
17 #include <sstream>
18 #include <string>
19 #include <set>
20 #include <queue>
21 
22 #ifdef MICROPROFILE_ENABLED
23 #include "microprofile/microprofile.h"
24 #endif
25 
27 
28 NeighborNodesPosition operator++(NeighborNodesPosition &nn, int)
29 {
30  NeighborNodesPosition res = nn;
31 
32  switch (nn)
33  {
34  case NeighborNodesPosition::BOTTOM_LEFT:
35  nn = NeighborNodesPosition::LEFT;
36  break;
37  case NeighborNodesPosition::LEFT:
38  nn = NeighborNodesPosition::TOP_LEFT;
39  break;
40  case NeighborNodesPosition::TOP_LEFT:
41  nn = NeighborNodesPosition::BOTTOM;
42  break;
43  case NeighborNodesPosition::BOTTOM:
44  nn = NeighborNodesPosition::CENTER;
45  break;
46  case NeighborNodesPosition::CENTER:
47  nn = NeighborNodesPosition::TOP;
48  break;
49  case NeighborNodesPosition::TOP:
50  nn = NeighborNodesPosition::BOTTOM_RIGHT;
51  break;
52  case NeighborNodesPosition::BOTTOM_RIGHT:
53  nn = NeighborNodesPosition::RIGHT;
54  break;
55  case NeighborNodesPosition::RIGHT:
56  nn = NeighborNodesPosition::TOP_RIGHT;
57  break;
58  case NeighborNodesPosition::TOP_RIGHT:
59  nn = NeighborNodesPosition::BOTTOM_LEFT;
60  break;
61  default:
62  assert(false);
63  break;
64  }
65 
66  return res;
67 }
68 
69 Map::Map(int columns, int rows) : pMapNodesVisible(new Sprite *[columns * rows]), m_columns(columns), m_rows(rows)
70 {
71  // TODO move Random Engine out of map
72  randomEngine.seed();
74 }
75 
76 Map::Map(int columns, int rows, const bool generateTerrain)
77  : pMapNodesVisible(new Sprite *[columns * rows]), m_columns(columns), m_rows(rows)
78 {
79  // TODO move Random Engine out of map
80  randomEngine.seed();
82 
83  if (generateTerrain)
84  {
87  }
88  else
89  {
90  for (int x = 0; x < Settings::instance().mapSize; x++)
91  {
92  for (int y = 0; y < Settings::instance().mapSize; y++)
93  {
94  mapNodes.emplace_back(MapNode{Point{x, y, 0, 1}, DEFAULT_TERRAIN});
95  }
96  }
97  }
98 }
99 
101 
102 void Map::renderMap() const
103 {
104 #ifdef MICROPROFILE_ENABLED
105  MICROPROFILE_SCOPEI("Map", "Render Map", MP_YELLOW);
106 #endif
107 
108  for (int i = 0; i < m_visibleNodesCount; ++i)
109  {
110  pMapNodesVisible[i]->render();
111  }
112 }
113 
115 {
116 #ifdef MICROPROFILE_ENABLED
117  MICROPROFILE_SCOPEI("Map", "Refresh Map", MP_YELLOW);
118 #endif
119 
121 
122  for (int i = 0; i < m_visibleNodesCount; ++i)
123  {
125  }
126 }
127 
129 {
130  const Point topLeft = calculateIsoCoordinates({0, 0});
132 
133  // Screen edges
134  const int left = topLeft.x + topLeft.y - 2;
135  const int right = bottomRight.x + bottomRight.y + 1;
136  const int top = topLeft.y - topLeft.x + 1;
137  // Lower the bottom because of high terrain nodes under the screen which will be pushed into the view
138  const int bottom = bottomRight.y - bottomRight.x - 1 - MapNode::maxHeight;
139 
141 
142  // ZOrder starts from topmost node to the right. (0,127) =1,(1,127) =2, ...
143  for (int y = m_columns - 1; y >= 0; y--)
144  {
145  for (int x = 0; x < m_rows; x++)
146  {
147  const int xVal = x + y;
148  const int yVal = y - x;
149 
150  if ((xVal >= left) && (xVal <= right) && (yVal <= top) && (yVal >= bottom))
151  {
152  pMapNodesVisible[m_visibleNodesCount++] = getMapNode({x, y}).getSprite();
153  }
154  }
155  }
156 }
Map::Map
Map(int columns, int rows, const bool generateTerrain)
Definition: Map.cxx:76
isoMath.hxx
WATER
@ WATER
6- Water tiles
Definition: enums.hxx:17
Map::mapNodes
std::vector< MapNode > mapNodes
Definition: Map.hxx:66
Map::m_terrainGen
TerrainGenerator m_terrainGen
Definition: Map.hxx:73
MapLayers::enableLayers
static void enableLayers(std::vector< unsigned int > &&layers)
Enable drawing layers.
Definition: MapLayers.cxx:26
Sprite
Definition: Sprite.hxx:32
calculateIsoCoordinates
Point calculateIsoCoordinates(const SDL_Point &screenCoordinates)
Calculates screen space coordinates to isometric space coordinates.
Definition: isoMath.cxx:11
SettingsData::screenHeight
ScreenDimension screenHeight
the screen height
Definition: Settings.hxx:46
LOG.hxx
json
nlohmann::json json
Definition: Map.cxx:26
Map::m_visibleNodesCount
int m_visibleNodesCount
Definition: Map.hxx:69
POWERLINES
@ POWERLINES
9- Powerlines
Definition: enums.hxx:20
Sprite::render
void render() const
Definition: Sprite.cxx:23
TerrainGenerator::generateTerrain
void generateTerrain(std::vector< MapNode > &mapNodes, std::vector< MapNode * > &mapNodesInDrawingOrder)
Definition: TerrainGenerator.cxx:21
MapNode::maxHeight
static const int maxHeight
Maximum height of the node.
Definition: MapNode.hxx:186
Camera.hxx
operator++
NeighborNodesPosition operator++(NeighborNodesPosition &nn, int)
Definition: Map.cxx:28
Map.hxx
DemolishMode::GROUND_DECORATION
@ GROUND_DECORATION
Remove only ground decoration.
Map::renderMap
void renderMap() const
Render the elements contained in the Map.
Definition: Map.cxx:102
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
SettingsData::mapSize
int mapSize
the size of the map
Definition: Settings.hxx:34
Map::m_rows
int m_rows
Definition: Map.hxx:71
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
SettingsData::screenWidth
ScreenDimension screenWidth
the screen width
Definition: Settings.hxx:40
MapNode
Class that holds map nodes.
Definition: MapNode.hxx:30
Map::refresh
void refresh()
Refresh all the map tile textures.
Definition: Map.cxx:114
ZONE
@ ZONE
4- Optional layer, zones(Industrial/Residential/Commercial).
Definition: enums.hxx:15
TerrainGenerator::generateRiver
void generateRiver(std::vector< MapNode > &mapNodes)
Definition: TerrainGenerator.cxx:179
BUILDINGS
@ BUILDINGS
8- Buildings, Streets and everything that goes on the terrain
Definition: enums.hxx:19
Map::~Map
~Map()
Definition: Map.cxx:100
mapEdit.hxx
Filesystem.hxx
Map::m_columns
int m_columns
Definition: Map.hxx:70
DEFAULT_TERRAIN
const std::string DEFAULT_TERRAIN
Definition: Constants.hxx:27
Settings.hxx
Map::getMapNode
MapNode & getMapNode(Point isoCoords)
Get pointer to a single mapNode at specific iso coordinates.
Definition: Map.hxx:54
FLORA
@ FLORA
10- Trees and other flora/Fauna tiles
Definition: enums.hxx:21
MapLayers.hxx
Map::mapNodesInDrawingOrder
std::vector< MapNode * > mapNodesInDrawingOrder
Definition: Map.hxx:67
Map::calculateVisibleMap
void calculateVisibleMap(void)
For implementing frustum culling, find all map nodes which are visible on the screen....
Definition: Map.cxx:128
Map::randomEngine
std::default_random_engine randomEngine
Definition: Map.hxx:72
ROAD
@ ROAD
5- Optional layer, roads.
Definition: enums.hxx:16
LayerEditMode::TERRAIN
@ TERRAIN
Default "overworld" edit mode.
JsonSerialization.hxx
Sprite::refresh
void refresh(const Layer &layer=Layer::NONE)
Definition: Sprite.cxx:73
Point
Definition: Point.hxx:7
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
json
nlohmann::json json
Definition: Settings.hxx:12
Constants.hxx
Map::pMapNodesVisible
Sprite ** pMapNodesVisible
Definition: Map.hxx:68