Cytopia  0.3
A city building simulation game
isoMath.cxx
Go to the documentation of this file.
1 #include "isoMath.hxx"
2 
3 #include "Camera.hxx"
4 #include "Settings.hxx"
5 #include "Point.hxx"
6 #include "LOG.hxx"
7 #include <MapFunctions.hxx>
8 
9 bool reverseDirection = false;
10 
11 Point calculateIsoCoordinates(const SDL_Point &screenCoordinates)
12 {
13  const SDL_Point &cameraOffset = Camera::instance().cameraOffset();
14  const double &zoomLevel = Camera::instance().zoomLevel();
15  const SDL_Point &tileSize = Camera::instance().tileSize();
16 
17  const int isoX = static_cast<int>(
18  (screenCoordinates.x + cameraOffset.x + 2.0 * (screenCoordinates.y + cameraOffset.y)) / (tileSize.x * zoomLevel) + 1);
19  const int isoY = static_cast<int>((screenCoordinates.x + cameraOffset.x - 2.0 * (screenCoordinates.y + cameraOffset.y)) /
20  (tileSize.x * zoomLevel));
21 
22  return {isoX, isoY, 0, 0};
23 }
24 
25 SDL_Point convertIsoToScreenCoordinates(const Point &isoCoordinates, bool calcWithoutOffset)
26 {
27  const int heightOffset = 24;
28  const SDL_Point &cameraOffset = Camera::instance().cameraOffset();
29  const double &zoomLevel = Camera::instance().zoomLevel();
30  const SDL_Point &tileSize = Camera::instance().tileSize();
31 
32  int x = static_cast<int>(std::round(static_cast<double>((isoCoordinates.x + isoCoordinates.y) * tileSize.x) * zoomLevel) / 2);
33  int y = static_cast<int>(std::round(static_cast<double>((isoCoordinates.x - isoCoordinates.y) * tileSize.y) * zoomLevel) / 2);
34 
35  if (!calcWithoutOffset)
36  {
37  x -= cameraOffset.x;
38  y -= cameraOffset.y;
39  }
40 
41  if (isoCoordinates.height > 0)
42  {
43  y -= static_cast<int>(std::round(static_cast<double>((tileSize.x - heightOffset) * isoCoordinates.height) * zoomLevel));
44  }
45 
46  return {x, y};
47 }
48 
49 Point convertScreenToIsoCoordinates(const SDL_Point &screenCoordinates)
50 {
51  Point foundCoordinates = Point();
52  foundCoordinates = MapFunctions::instance().findNodeInMap(screenCoordinates);
53 
54  // if negative coordinates are returned, this means that the point is outside of the grid
55  // calculate the coordinates instead and make sure it's within grid boundaries
56  if (foundCoordinates.x == -1)
57  {
58  foundCoordinates = calculateIsoCoordinates(screenCoordinates);
59  if (foundCoordinates.x < 0)
60  {
61  foundCoordinates.x = 0;
62  }
63  else if (foundCoordinates.x >= Settings::instance().mapSize)
64  {
65  // map (vector) size is 128, but coordinates ranges from 0 - 127
66  foundCoordinates.x = Settings::instance().mapSize - 1;
67  }
68  if (foundCoordinates.y < 0)
69  {
70  foundCoordinates.y = 0;
71  }
72  else if (foundCoordinates.y >= Settings::instance().mapSize)
73  {
74  // map (vector) size is 128, but coordinates range from 0 - 127
75  foundCoordinates.y = Settings::instance().mapSize - 1;
76  }
77  }
78  return foundCoordinates;
79 }
80 
81 bool isPointWithinMapBoundaries(const std::vector<Point> &isoCoordinates)
82 {
83  for (auto p : isoCoordinates)
84  {
85  if (!(p.isWithinMapBoundaries()))
86  {
87  return false;
88  }
89  }
90  return true;
91 }
isoMath.hxx
calculateIsoCoordinates
Point calculateIsoCoordinates(const SDL_Point &screenCoordinates)
Calculates screen space coordinates to isometric space coordinates.
Definition: isoMath.cxx:11
LOG.hxx
MapFunctions.hxx
Camera.hxx
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
SettingsData::mapSize
int mapSize
the size of the map
Definition: Settings.hxx:34
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
convertScreenToIsoCoordinates
Point convertScreenToIsoCoordinates(const SDL_Point &screenCoordinates)
converts screen space coordinates to isometric space coordinates.
Definition: isoMath.cxx:49
isPointWithinMapBoundaries
bool isPointWithinMapBoundaries(const std::vector< Point > &isoCoordinates)
Check if given coordinates are within map boundaries.
Definition: isoMath.cxx:81
Camera::zoomLevel
const double & zoomLevel() const noexcept
Definition: Camera.cxx:97
Settings.hxx
Point::height
int height
The height level.
Definition: Point.hxx:26
MapFunctions::findNodeInMap
Point findNodeInMap(const SDL_Point &screenCoordinates, const Layer &layer=Layer::NONE)
Returns the node at given screen coordinates.
Definition: MapFunctions.cxx:558
reverseDirection
bool reverseDirection
Definition: isoMath.cxx:9
Point
Definition: Point.hxx:7
Singleton< Camera >::instance
static Camera & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
Camera::cameraOffset
const SDL_Point & cameraOffset() const noexcept
Definition: Camera.cxx:95
convertIsoToScreenCoordinates
SDL_Point convertIsoToScreenCoordinates(const Point &isoCoordinates, bool calcWithoutOffset)
converts coordinates from isometric to screen space
Definition: isoMath.cxx:25
Camera::tileSize
const SDL_Point & tileSize() const noexcept
Definition: Camera.cxx:99
Point.hxx