Map.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Beerware
6 #pragma once
7 
8 #include <SDL.h>
9 #include <tmx.h>
10 #include "AABB.h"
11 
18 typedef enum MapConstants_t
19 {
20  ANIM_TILE_MAX = 500,
25 
26 } MapConstants;
27 
34 typedef struct AnimTile_t
35 {
36  Uint16 u16Gid;
37  Uint16 u16TileId;
38  Sint16 s16DstX;
39  Sint16 s16DstY;
40  Uint8 u8FrameCount;
41  Uint8 u8AnimLen;
42 
43 } AnimTile;
44 
51 typedef struct Object_t
52 {
53  Uint16 u16Id;
54  Uint32 u32PosX;
55  Uint32 u32PosY;
56  Uint16 u16Width;
57  Uint16 u16Height;
61 
62 } Object;
63 
70 typedef struct Map_t
71 {
72  tmx_map* pstTmxMap;
73  SDL_Texture* pstAnimTexture;
74  SDL_Texture* pstTexture[MAP_TEXTURES];
75  SDL_Texture* pstTileset;
76  Uint16 u16Height;
77  Uint16 u16Width;
78  double dPosX;
79  double dPosY;
80  double dGravitation;
83  double dAnimDelay;
84  double dAnimSpeed;
85  Uint16 u16AnimTileSize;
87  Uint16 u16ObjectCount;
89 
90 } Map;
91 
92 Sint8 Map_Draw(
93  const Uint16 u16Index,
94  const SDL_bool bRenderAnimTiles,
95  const SDL_bool bRenderBgColour,
96  const char* pacLayerName,
97  const double dCameraPosX,
98  const double dCameraPosY,
99  Map* pstMap,
100  SDL_Renderer* pstRenderer);
101 
102 void Map_Free(Map* pstMap);
103 void Map_GetObjects(const Map* pstMap, Object astObject[]);
104 Uint16 Map_GetObjectCount(Map* pstMap);
105 char* Map_GetObjectName(Object* pstObject);
106 char* Map_GetObjectType(Object* pstObject);
107 
108 Sint8 Map_Init(
109  const char* pacFileName,
110  const char* pacTilesetImage,
111  const Uint8 u8MeterInPixel,
112  Map** pstMap);
113 
114 SDL_bool Map_IsCoordOfType(const char* pacType, const Map* pstMap, double dPosX, double dPosY);
115 
116 SDL_bool Map_IsObjectOfType(const char* pacType, Object* pstObject);
117 
118 SDL_bool Map_IsOnTileOfType(
119  const char* pacType,
120  const double dPosX,
121  const double dPosY,
122  const Uint8 u8EntityHeight,
123  const Map* pstMap);
124 
125 void Map_SetGravitation(const double dGravitation, const SDL_bool bUseTmxConstant, Map* pstMap);
126 void Map_SetTileAnimationSpeed(const double dAnimSpeed, Map* pstMap);
127 void Map_ShowObjects(const Map* pstMap);
Uint16 u16Gid
GID.
Definition: Map.h:36
double dGravitation
Gravitational constant.
Definition: Map.h:80
MapConstants_t
Map constants enumeration.
Definition: Map.h:18
Uint16 u16Height
Map height in pixel.
Definition: Map.h:76
Axis-aligned bounding box handle data.
Definition: AABB.h:17
Uint8 u8AnimLen
Animation length.
Definition: Map.h:41
Sint8 Map_Init(const char *pacFileName, const char *pacTilesetImage, const Uint8 u8MeterInPixel, Map **pstMap)
Initialise map.
Definition: Map.c:490
char * Map_GetObjectName(Object *pstObject)
Get object name.
Definition: Map.c:458
void Map_GetObjects(const Map *pstMap, Object astObject[])
Get objects.
Definition: Map.c:402
void Map_SetGravitation(const double dGravitation, const SDL_bool bUseTmxConstant, Map *pstMap)
Set map gravitation.
Definition: Map.c:674
SDL_Texture * pstAnimTexture
Texture for animated tiles.
Definition: Map.h:73
double dLeft
Left edge position.
Definition: AABB.h:20
double dPosX
Position along the x-axis.
Definition: Map.h:78
Map handle data.
Definition: Map.h:70
char acType[OBJECT_TYPE_LEN]
Object type.
Definition: Map.h:59
struct Object_t Object
Object handle type.
struct Map_t Map
Map handle type.
struct AnimTile_t AnimTile
Animated tile handle type.
char acTilesetImage[TS_IMG_PATH_LEN]
Tileset image.
Definition: Map.h:82
SDL_bool Map_IsCoordOfType(const char *pacType, const Map *pstMap, double dPosX, double dPosY)
Check if map coordinate is of specific type.
Definition: Map.c:567
SDL_bool Map_IsObjectOfType(const char *pacType, Object *pstObject)
Determine if object is of specific type.
Definition: Map.c:626
void Map_SetTileAnimationSpeed(const double dAnimSpeed, Map *pstMap)
Set speed of animated tiles.
Definition: Map.c:705
Max. number of animated tiles.
Definition: Map.h:20
char * Map_GetObjectType(Object *pstObject)
Get object type.
Definition: Map.c:470
Max. textures per map (not to be confused with map layers)
Definition: Map.h:21
SDL_bool Map_IsOnTileOfType(const char *pacType, const double dPosX, const double dPosY, const Uint8 u8EntityHeight, const Map *pstMap)
Determine if entity/object is on-top of tile of specific type.
Definition: Map.c:657
Uint16 u16Height
Object height in pixel.
Definition: Map.h:57
Uint16 Map_GetObjectCount(Map *pstMap)
Get object count.
Definition: Map.c:425
Uint16 u16Width
Object width in pixel.
Definition: Map.h:56
double dAnimDelay
Animation delay.
Definition: Map.h:83
Max. object type length.
Definition: Map.h:24
void Map_Free(Map *pstMap)
Free map.
Definition: Map.c:362
Animated tile data.
Definition: Map.h:34
char * Map_GetObjectName(Object *pstObject)
Get object name.
Definition: Map.c:458
double dPosY
Position along the y-axis.
Definition: Map.h:79
Uint8 u8FrameCount
Frame count.
Definition: Map.h:40
Constants include header.
Uint32 u32PosY
Position along the y-axis.
Definition: Map.h:55
double dBottom
Bottom edge position.
Definition: AABB.h:19
Sint8 Map_Draw(const Uint16 u16Index, const SDL_bool bRenderAnimTiles, const SDL_bool bRenderBgColour, const char *pacLayerName, const double dCameraPosX, const double dCameraPosY, Map *pstMap, SDL_Renderer *pstRenderer)
Draw Map.
Definition: Map.c:109
double dAnimSpeed
Animation speed.
Definition: Map.h:84
enum MapConstants_t MapConstants
Map constants handle type.
Uint16 u16Id
Object ID.
Definition: Map.h:53
void Map_ShowObjects(const Map *pstMap)
Show map objects.
Definition: Map.c:717
Object handle data.
Definition: Map.h:51
Uint16 u16TileId
Tile ID.
Definition: Map.h:37
Sint8 Map_Init(const char *pacFileName, const char *pacTilesetImage, const Uint8 u8MeterInPixel, Map **pstMap)
Initialise map.
Definition: Map.c:490
char acName[OBJECT_NAME_LEN]
Object name.
Definition: Map.h:58
void Map_GetObjects(const Map *pstMap, Object astObject[])
Get objects.
Definition: Map.c:402
Uint16 Map_GetObjectCount(Map *pstMap)
Get object count.
Definition: Map.c:425
double dTop
Top edge position.
Definition: AABB.h:22
AABB stBB
Axis-aligned bounding box.
Definition: Map.h:60
Max. tileset image path length.
Definition: Map.h:22
SDL_bool Map_IsCoordOfType(const char *pacType, const Map *pstMap, double dPosX, double dPosY)
Check if map coordinate is of specific type.
Definition: Map.c:567
void Map_SetTileAnimationSpeed(const double dAnimSpeed, Map *pstMap)
Set speed of animated tiles.
Definition: Map.c:705
char * Map_GetObjectType(Object *pstObject)
Get object type.
Definition: Map.c:470
void Map_ShowObjects(const Map *pstMap)
Show map objects.
Definition: Map.c:717
Uint32 u32PosX
Position along the x-axis.
Definition: Map.h:54
Sint16 s16DstX
Destination coordinate along the x-axis.
Definition: Map.h:38
Uint16 u16Width
Map width in pixel.
Definition: Map.h:77
Uint16 u16AnimTileSize
Animated tile size.
Definition: Map.h:85
Uint16 u16ObjectCount
Object count.
Definition: Map.h:87
void Map_SetGravitation(const double dGravitation, const SDL_bool bUseTmxConstant, Map *pstMap)
Set map gravitation.
Definition: Map.c:674
Sint8 Map_Draw(const Uint16 u16Index, const SDL_bool bRenderAnimTiles, const SDL_bool bRenderBgColour, const char *pacLayerName, const double dCameraPosX, const double dCameraPosY, Map *pstMap, SDL_Renderer *pstRenderer)
Draw Map.
Definition: Map.c:109
SDL_Texture * pstTexture[MAP_TEXTURES]
Map textures.
Definition: Map.h:74
tmx_map * pstTmxMap
TMX map handle.
Definition: Map.h:72
SDL_bool Map_IsObjectOfType(const char *pacType, Object *pstObject)
Determine if object is of specific type.
Definition: Map.c:626
SDL_bool Map_IsOnTileOfType(const char *pacType, const double dPosX, const double dPosY, const Uint8 u8EntityHeight, const Map *pstMap)
Determine if entity/object is on-top of tile of specific type.
Definition: Map.c:657
AABB handler include header.
double dRight
Right edge position.
Definition: AABB.h:21
void Map_Free(Map *pstMap)
Free map.
Definition: Map.c:362
Object astObject[]
Objects.
Definition: Map.h:88
Max. object name length.
Definition: Map.h:23
Sint16 s16DstY
Destination coordinate along the y-axis.
Definition: Map.h:39
AnimTile acAnimTile[ANIM_TILE_MAX]
Animated tiles.
Definition: Map.h:86
SDL_Texture * pstTileset
Tileset texture.
Definition: Map.h:75
Uint8 u8MeterInPixel
Definition of meter in pixel.
Definition: Map.h:81