Entity.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Beerware
7 #pragma once
8 
9 #include <SDL.h>
10 #include "AABB.h"
11 #include "Constants.h"
12 
19 typedef struct Bullet_t
20 {
22  double dPosX;
23  double dPosY;
24  Uint8 u8Size;
25  SDL_bool bOrientation;
26  double dVelocity;
27 
28 } Bullet;
29 
36 typedef struct Camera_t
37 {
38  Uint16 u16Flags;
39  double dPosX;
40  double dPosY;
41  Sint32 s32MaxPosX;
42  Sint32 s32MaxPosY;
43 
44 } Camera;
45 
52 typedef struct Entity_t
53 {
55  Uint16 u16Flags;
56  double dPosX;
57  double dPosY;
58  double dSpawnPosX;
59  double dSpawnPosY;
60  SDL_bool bIsJumping;
62  double dAcceleration;
63  double dVelocityX;
64  double dMaxVelocityX;
65  double dVelocityY;
66  Uint16 u16Width;
67  Uint16 u16Height;
70  Uint8 u8AnimFrame;
71  Uint8 u8AnimStart;
72  Uint8 u8AnimEnd;
73  double dAnimDelay;
74  double dAnimSpeed;
75 
76 } Entity;
77 
84 typedef struct Sprite_t
85 {
86  SDL_Texture* pstTexture;
87  Uint16 u16Width;
88  Uint16 u16Height;
89  Uint16 u16ImageOffsetX;
90  Uint16 u16ImageOffsetY;
91 
92 } Sprite;
93 
94 void Entity_Animate(SDL_bool bAnimate, Entity* pstEntity);
95 void Entity_ConnectHorizontalMapEnds(const Uint16 u16MapWidth, Entity* pstEntity);
96 
98  const Uint16 u16MapWidth,
99  const Uint16 u16MapHeight,
100  Entity* pstEntity);
101 
102 void Entity_ConnectVerticalMapEnds(const Uint16 u16MapHeight, Entity* pstEntity);
103 
104 int Entity_Draw(
105  const Entity* pstEntity,
106  const Camera* pstCamera,
107  const Sprite* pstSprite,
108  SDL_Renderer* pstRenderer);
109 
110 void Entity_Drop(Entity* pstEntity);
111 void Entity_Free(Entity* pstEntity);
112 void Entity_FreeCamera(Camera* pstCamera);
113 void Entity_FreeSprite(Sprite* pstSprite);
114 
115 int Entity_Init(
116  const double dPosX,
117  const double dPosY,
118  const Uint16 u16Width,
119  const Uint16 u16Height,
120  Entity** pstEntity);
121 
122 int Entity_InitCamera(Camera** pstCamera);
123 
125  const char* pacFileName,
126  const Uint16 u16Width,
127  const Uint16 u16Height,
128  const Uint16 u16ImageOffsetX,
129  const Uint16 u16ImageOffsetY,
130  Sprite** pstSprite,
131  SDL_Renderer* pstRenderer);
132 
133 SDL_bool Entity_IsCameraLocked(const Camera* pstCamera);
134 SDL_bool Entity_IsMoving(const Entity* pstEntity);
135 SDL_bool Entity_IsRising(const Entity* pstEntity);
136 void Entity_Jump(const double dForce, Entity* pstEntity);
137 void Entity_LockCamera(Camera* pstCamera);
138 void Entity_Move(Entity* pstEntity);
139 
140 void Entity_MoveFull(
141  const Direction bOrientation,
142  const double dAcceleration,
143  const double dMaxVelocityX,
144  const Uint8 u8AnimStart,
145  const Uint8 u8AnimEnd,
146  const double dAnimSpeed,
147  const Uint8 u8FrameOffsetY,
148  Entity* pstEntity);
149 
150 void Entity_Reset(Entity* pstEntity);
151 void Entity_ResetToSpawnPosition(Entity* pstEntity);
152 
154  const Sint32 s32LogicalWindowWidth,
155  const Sint32 s32LogicalWindowHeight,
156  const Uint16 u16MapWidth,
157  const Uint16 u16MapHeight,
158  Camera* pstCamera);
159 
161  const Sint32 s32LogicalWindowWidth,
162  const Sint32 s32LogicalWindowHeight,
163  const Entity* pstEntity,
164  Camera* pstCamera);
165 
167  const Uint8 u8AnimStart,
168  const Uint8 u8AnimEnd,
169  const double dAnimSpeed,
170  Entity* pstEntity);
171 
172 void Entity_SetDirection(const Direction eDirection, Entity* pstEntity);
173 void Entity_SetFrameOffset(const Uint8 u8OffsetX, const Uint8 u8OffsetY, Entity* pstEntity);
174 void Entity_SetPosition(const double dPosX, const double dPosY, Entity* pstEntity);
175 void Entity_SetSpawnPosition(const double dPosX, const double dPosY, Entity* pstEntity);
176 void Entity_SetSpeed(const double dAcceleration, const double dMaxVelocityX, Entity* pstEntity);
177 void Entity_Stop(Entity* pstEntity);
178 void Entity_UnlockCamera(Camera* pstCamera);
179 
180 void Entity_Update(
181  const double dDeltaTime,
182  const double dGravitation,
183  const Uint8 u8MeterInPixel,
184  Entity* pstEntity);
SDL_bool Entity_IsMoving(const Entity *pstEntity)
Check if entity is moving.
Definition: Entity.c:391
double Utils_Round(double dValue)
Round to integral value.
Definition: Utils.c:82
Axis-aligned bounding box handle data.
Definition: AABB.h:17
int Entity_Init(const double dPosX, const double dPosY, const Uint16 u16Width, const Uint16 u16Height, Entity **pstEntity)
Initialise entity.
Definition: Entity.c:258
double dPosY
Position along the y-axis.
Definition: Entity.h:57
void Entity_SetSpeed(const double dAcceleration, const double dMaxVelocityX, Entity *pstEntity)
Set entity speed.
Definition: Entity.c:726
void Entity_Reset(Entity *pstEntity)
Reset entity flags.
Definition: Entity.c:512
double dVelocityY
Velocity along the y-axis.
Definition: Entity.h:65
void Entity_SetPosition(const double dPosX, const double dPosY, Entity *pstEntity)
Set entity position.
Definition: Entity.c:693
struct Entity_t Entity
Entity handle type.
double dAnimDelay
Animation delay.
Definition: Entity.h:73
int Entity_InitCamera(Camera **pstCamera)
Initialise camera.
Definition: Entity.c:293
double dLeft
Left edge position.
Definition: AABB.h:20
SDL_bool Entity_IsRising(const Entity *pstEntity)
Check if a entity is rising.
Definition: Entity.c:412
SDL_bool bOrientation
The bullet's orientation.
Definition: Entity.h:25
void Entity_ConnectVerticalMapEnds(const Uint16 u16MapHeight, Entity *pstEntity)
Connect vertical map ends for entity.
Definition: Entity.c:104
Camera handle data.
Definition: Entity.h:36
void Entity_Free(Entity *pstEntity)
Free entity.
Definition: Entity.c:209
SDL_bool Entity_IsCameraLocked(const Camera *pstCamera)
Check if camera is locked.
Definition: Entity.c:370
Uint16 u16Width
Sprite width.
Definition: Entity.h:87
double dSpawnPosY
Spawn position along the y-axis.
Definition: Entity.h:59
int Entity_InitSprite(const char *pacFileName, const Uint16 u16Width, const Uint16 u16Height, const Uint16 u16ImageOffsetX, const Uint16 u16ImageOffsetY, Sprite **pstSprite, SDL_Renderer *pstRenderer)
Initialise sprite.
Definition: Entity.c:328
void Entity_SetDirection(const Direction eDirection, Entity *pstEntity)
Set entity direction.
Definition: Entity.c:654
Uint16 u16ImageOffsetX
Image x-offset in pixel.
Definition: Entity.h:89
void Entity_MoveFull(const Direction bOrientation, const double dAcceleration, const double dMaxVelocityX, const Uint8 u8AnimStart, const Uint8 u8AnimEnd, const double dAnimSpeed, const Uint8 u8FrameOffsetY, Entity *pstEntity)
Move entity with parameters.
Definition: Entity.c:489
SDL_Texture * pstTexture
SDL2 texture.
Definition: Entity.h:86
Entity is in mid-air.
Definition: Entity.c:29
SDL_bool Entity_IsMoving(const Entity *pstEntity)
Check if entity is moving.
Definition: Entity.c:391
struct Bullet_t Bullet
Bullet handle type.
Entity handler include header.
void Entity_SetSpawnPosition(const double dPosX, const double dPosY, Entity *pstEntity)
Set entity spawn position.
Definition: Entity.c:709
int Entity_SetCameraBoundariesToMapSize(const Sint32 s32LogicalWindowWidth, const Sint32 s32LogicalWindowHeight, const Uint16 u16MapWidth, const Uint16 u16MapHeight, Camera *pstCamera)
Set camera boundaries to map size.
Definition: Entity.c:610
void Entity_LockCamera(Camera *pstCamera)
Lock camera.
Definition: Entity.c:453
void Entity_FreeCamera(Camera *pstCamera)
Free camera.
Definition: Entity.c:220
int Entity_CreateBullet(const double dPosX, const double dPosY, Bullet *pstBullet)
Create bullet/projectile.
Definition: Entity.c:131
Uint16 u16ImageOffsetY
Image y-offset in pixel.
Definition: Entity.h:90
double dPosX
Position along the x-axis.
Definition: Entity.h:56
Bullet handle data.
Definition: Entity.h:19
Constants include header.
void Entity_SetPosition(const double dPosX, const double dPosY, Entity *pstEntity)
Set entity position.
Definition: Entity.c:693
Uint8 u8Size
Bullet size in pixel.
Definition: Entity.h:24
double dBottom
Bottom edge position.
Definition: AABB.h:19
int Entity_InitCamera(Camera **pstCamera)
Initialise camera.
Definition: Entity.c:293
void Entity_MoveFull(const Direction eDirection, const double dAcceleration, const double dMaxVelocityX, const Uint8 u8AnimStart, const Uint8 u8AnimEnd, const double dAnimSpeed, const Uint8 u8FrameOffsetY, Entity *pstEntity)
Move entity with parameters.
Definition: Entity.c:489
Uint16 u16Height
Entity height in pixel.
Definition: Entity.h:67
SDL_bool Entity_IsCameraLocked(const Camera *pstCamera)
Check if camera is locked.
Definition: Entity.c:370
Uint8 u8FrameOffsetY
Frame y-offset in frames.
Definition: Entity.h:69
void Utils_SetFlag(const Uint8 u8Bit, Uint16 *pu16Flags)
Set flag.
Definition: Utils.c:57
Uint8 u8AnimEnd
Animation end.
Definition: Entity.h:72
int Entity_Draw(const Entity *pstEntity, const Camera *pstCamera, const Sprite *pstSprite, SDL_Renderer *pstRenderer)
Draw entity.
Definition: Entity.c:155
Sprite handle data.
Definition: Entity.h:84
double dAcceleration
Acceleration.
Definition: Entity.h:62
double dPosX
Position along the x-axis.
Definition: Entity.h:22
void Entity_Stop(Entity *pstEntity)
Stop entity.
Definition: Entity.c:738
Entity is animated.
Definition: Entity.c:27
void Entity_ConnectMapEnds(const Uint16 u16MapWidth, const Uint16 u16MapHeight, Entity *pstEntity)
Connect all map ends for entity.
Definition: Entity.c:89
void Entity_SetFrameOffset(const Uint8 u8OffsetX, const Uint8 u8OffsetY, Entity *pstEntity)
Set frame offset.
Definition: Entity.c:677
Entity handle data.
Definition: Entity.h:52
void Entity_Update(const double dDeltaTime, const double dGravitation, const Uint8 u8MeterInPixel, Entity *pstEntity)
Update entity.
Definition: Entity.c:767
Direction eDirection
Direction.
Definition: Entity.h:61
void Entity_ResetToSpawnPosition(Entity *pstEntity)
Reset to spawn position.
Definition: Entity.c:523
void Entity_FreeSprite(Sprite *pstSprite)
Free Sprite.
Definition: Entity.c:231
void Entity_Animate(SDL_bool bAnimate, Entity *pstEntity)
Animate entity.
Definition: Entity.c:43
double dTop
Top edge position.
Definition: AABB.h:22
void Entity_SetCameraTarget(const Sint32 s32LogicalWindowWidth, const Sint32 s32LogicalWindowHeight, const Entity *pstEntity, Camera *pstCamera)
Set camera target.
Definition: Entity.c:541
void Utils_ClearFlag(const Uint8 u8Bit, Uint16 *pu16Flags)
Clear flag.
Definition: Utils.c:21
void Entity_LockCamera(Camera *pstCamera)
Lock camera.
Definition: Entity.c:453
Uint16 u16Height
Sprite height.
Definition: Entity.h:88
void Entity_ConnectHorizontalMapEnds(const Uint16 u16MapWidth, Entity *pstEntity)
Connect horizontal map ends for entity.
Definition: Entity.c:64
SDL_bool Entity_IsRising(const Entity *pstEntity)
Check if a entity is rising.
Definition: Entity.c:412
void Entity_Jump(const double dForce, Entity *pstEntity)
Jump with entity.
Definition: Entity.c:432
double dVelocity
Velocity.
Definition: Entity.h:26
void Entity_Drop(Entity *pstEntity)
Drop entity.
Definition: Entity.c:198
#define DELTA_TIME
Delta time calculation formula.
Definition: Constants.h:57
void Entity_Free(Entity *pstEntity)
Free entity.
Definition: Entity.c:209
int Entity_InitSprite(const char *pacFileName, const Uint16 u16Width, const Uint16 u16Height, const Uint16 u16ImageOffsetX, const Uint16 u16ImageOffsetY, Sprite **pstSprite, SDL_Renderer *pstRenderer)
Initialise sprite.
Definition: Entity.c:328
void Entity_SetDirection(const Direction eDirection, Entity *pstEntity)
Set entity direction.
Definition: Entity.c:654
int Entity_Init(const double dPosX, const double dPosY, const Uint16 u16Width, const Uint16 u16Height, Entity **pstEntity)
Initialise entity.
Definition: Entity.c:258
void Entity_SetAnimation(const Uint8 u8AnimStart, const Uint8 u8AnimEnd, const double dAnimSpeed, Entity *pstEntity)
Set animation for entity.
Definition: Entity.c:573
int Entity_Draw(const Entity *pstEntity, const Camera *pstCamera, const Sprite *pstSprite, SDL_Renderer *pstRenderer)
Draw entity.
Definition: Entity.c:155
Entity is dead.
Definition: Entity.c:28
Sint32 s32MaxPosX
Maximum position along the x-axis.
Definition: Entity.h:41
void Entity_ConnectMapEnds(const Uint16 u16MapWidth, const Uint16 u16MapHeight, Entity *pstEntity)
Connect all map ends for entity.
Definition: Entity.c:89
void Entity_SetFrameOffset(const Uint8 u8OffsetX, const Uint8 u8OffsetY, Entity *pstEntity)
Set frame offset.
Definition: Entity.c:677
void Entity_Update(const double dDeltaTime, const double dGravitation, const Uint8 u8MeterInPixel, Entity *pstEntity)
Update entity.
Definition: Entity.c:767
double dPosX
Position along the x-axis.
Definition: Entity.h:39
double dAnimSpeed
Animation speed.
Definition: Entity.h:74
void Entity_FreeSprite(Sprite *pstSprite)
Free Sprite.
Definition: Entity.c:231
void Entity_Animate(SDL_bool bAnimate, Entity *pstEntity)
Animate entity.
Definition: Entity.c:43
void Entity_Move(Entity *pstEntity)
Set entity in motion.
Definition: Entity.c:464
AABB stBB
Axis-aligned bounding box.
Definition: Entity.h:21
void Entity_UnlockCamera(Camera *pstCamera)
Unlock camera.
Definition: Entity.c:749
Sint32 s32MaxPosY
Maximum position along the y-axis.
Definition: Entity.h:42
void Entity_Jump(const double dForce, Entity *pstEntity)
Jump with entity.
Definition: Entity.c:432
Uint16 u16Flags
Flag mask.
Definition: Entity.h:55
double dPosY
Position along the y-axis.
Definition: Entity.h:40
struct Camera_t Camera
Camera handle type.
void Entity_SetSpeed(const double dAcceleration, const double dMaxVelocityX, Entity *pstEntity)
Set entity speed.
Definition: Entity.c:726
Uint16 u16Flags
Camera flags.
Definition: Entity.h:38
Uint8 u8FrameOffsetX
Frame x-offset in frames.
Definition: Entity.h:68
Flags_t
Entity flags data.
Definition: Entity.c:25
void Entity_Reset(Entity *pstEntity)
Reset entity flags.
Definition: Entity.c:512
Entity is moving.
Definition: Entity.c:31
AABB handler include header.
SDL_bool bIsJumping
Current jumping-state.
Definition: Entity.h:60
void Entity_SetSpawnPosition(const double dPosX, const double dPosY, Entity *pstEntity)
Set entity spawn position.
Definition: Entity.c:709
int Entity_SetCameraBoundariesToMapSize(const Sint32 s32LogicalWindowWidth, const Sint32 s32LogicalWindowHeight, const Uint16 u16MapWidth, const Uint16 u16MapHeight, Camera *pstCamera)
Set camera boundaries to map size.
Definition: Entity.c:610
double dRight
Right edge position.
Definition: AABB.h:21
void Entity_SetAnimation(const Uint8 u8AnimStart, const Uint8 u8AnimEnd, const double dAnimSpeed, Entity *pstEntity)
Set animation for entity.
Definition: Entity.c:573
SDL_bool Utils_IsFlagSet(const Uint8 u8Bit, Uint16 u16Flags)
Check if flag is set.
Definition: Utils.c:37
void Entity_FreeCamera(Camera *pstCamera)
Free camera.
Definition: Entity.c:220
void Entity_Stop(Entity *pstEntity)
Stop entity.
Definition: Entity.c:738
double dVelocityX
Velocity along the x-axis.
Definition: Entity.h:63
double dPosY
Position along the y-axis.
Definition: Entity.h:23
void Entity_ResetToSpawnPosition(Entity *pstEntity)
Reset to spawn position.
Definition: Entity.c:523
Uint8 u8AnimFrame
Current animation frame.
Definition: Entity.h:70
enum Direction_t Direction
Directional constants type.
void Entity_ConnectVerticalMapEnds(const Uint16 u16MapHeight, Entity *pstEntity)
Connect vertical map ends for entity.
Definition: Entity.c:104
struct Sprite_t Sprite
Sprite handle type.
double dMaxVelocityX
Max velocity along the x-axis.
Definition: Entity.h:64
void Entity_SetCameraTarget(const Sint32 s32LogicalWindowWidth, const Sint32 s32LogicalWindowHeight, const Entity *pstEntity, Camera *pstCamera)
Set camera target.
Definition: Entity.c:541
Uint16 u16Width
Entity width in pixel.
Definition: Entity.h:66
void Entity_ConnectHorizontalMapEnds(const Uint16 u16MapWidth, Entity *pstEntity)
Connect horizontal map ends for entity.
Definition: Entity.c:64
void Entity_Move(Entity *pstEntity)
Set entity in motion.
Definition: Entity.c:464
enum Flags_t Flags
Entity flags type.
void Entity_Drop(Entity *pstEntity)
Drop entity.
Definition: Entity.c:198
AABB stBB
Axis-aligned bounding box.
Definition: Entity.h:54
double dSpawnPosX
Spawn position along the x-axis.
Definition: Entity.h:58
Uint8 u8AnimStart
Animation start.
Definition: Entity.h:71
void Entity_UnlockCamera(Camera *pstCamera)
Unlock camera.
Definition: Entity.c:749
Camera is locked.
Definition: Entity.c:30