citro2d
sprite.h
Go to the documentation of this file.
1 /**
2  * @file sprite.h
3  * @brief Stateful sprite API
4  */
5 #pragma once
6 #include "spritesheet.h"
7 
8 typedef struct
9 {
10  C2D_Image image;
11  C2D_DrawParams params;
12 } C2D_Sprite;
13 
14 /** @defgroup Sprite Sprite functions
15  * @{
16  */
17 
18 /** @brief Initializes a sprite from an image
19  * @param[in] Pointer to sprite
20  * @param[in] image Image to use
21  */
22 static inline void C2D_SpriteFromImage(C2D_Sprite* sprite, C2D_Image image)
23 {
24  sprite->image = image;
25  sprite->params.pos.x = 0.0f;
26  sprite->params.pos.y = 0.0f;
27  sprite->params.pos.w = image.subtex->width;
28  sprite->params.pos.h = image.subtex->height;
29  sprite->params.center.x = 0.0f;
30  sprite->params.center.y = 0.0f;
31  sprite->params.angle = 0.0f;
32  sprite->params.depth = 0.0f;
33 }
34 
35 /** @brief Initializes a sprite from an image stored in a sprite sheet
36  * @param[in] Pointer to sprite
37  * @param[in] sheet Sprite sheet handle
38  * @param[in] index Index of the image inside the sprite sheet
39  */
40 static inline void C2D_SpriteFromSheet(C2D_Sprite* sprite, C2D_SpriteSheet sheet, size_t index)
41 {
42  C2D_SpriteFromImage(sprite, C2D_SpriteSheetGetImage(sheet, index));
43 }
44 
45 /** @brief Scale sprite (relative)
46  * @param[in] sprite Pointer to sprite
47  * @param[in] x X scale (negative values flip the sprite horizontally)
48  * @param[in] y Y scale (negative values flip the sprite vertically)
49  */
50 static inline void C2D_SpriteScale(C2D_Sprite* sprite, float x, float y)
51 {
52  sprite->params.pos.w *= x;
53  sprite->params.pos.h *= y;
54  sprite->params.center.x *= x;
55  sprite->params.center.y *= y;
56 }
57 
58 /** @brief Rotate sprite (relative)
59  * @param[in] sprite Pointer to sprite
60  * @param[in] radians Amount to rotate in radians
61  */
62 static inline void C2D_SpriteRotate(C2D_Sprite* sprite, float radians)
63 {
64  sprite->params.angle += radians;
65 }
66 
67 /** @brief Rotate sprite (relative)
68  * @param[in] sprite Pointer to sprite
69  * @param[in] degrees Amount to rotate in degrees
70  */
71 static inline void C2D_SpriteRotateDegrees(C2D_Sprite* sprite, float degrees)
72 {
73  C2D_SpriteRotate(sprite, C3D_AngleFromDegrees(degrees));
74 }
75 
76 /** @brief Move sprite (relative)
77  * @param[in] sprite Pointer to sprite
78  * @param[in] x X translation
79  * @param[in] y Y translation
80  */
81 static inline void C2D_SpriteMove(C2D_Sprite* sprite, float x, float y)
82 {
83  sprite->params.pos.x += x;
84  sprite->params.pos.y += y;
85 }
86 
87 /** @brief Scale sprite (absolute)
88  * @param[in] sprite Pointer to sprite
89  * @param[in] x X scale (negative values flip the sprite horizontally)
90  * @param[in] y Y scale (negative values flip the sprite vertically)
91  */
92 static inline void C2D_SpriteSetScale(C2D_Sprite* sprite, float x, float y)
93 {
94  float oldCenterX = sprite->params.center.x / sprite->params.pos.w;
95  float oldCenterY = sprite->params.center.y / sprite->params.pos.h;
96  sprite->params.pos.w = x*sprite->image.subtex->width;
97  sprite->params.pos.h = y*sprite->image.subtex->height;
98  sprite->params.center.x = fabsf(oldCenterX*sprite->params.pos.w);
99  sprite->params.center.y = fabsf(oldCenterY*sprite->params.pos.h);
100 }
101 
102 /** @brief Rotate sprite (absolute)
103  * @param[in] sprite Pointer to sprite
104  * @param[in] radians Amount to rotate in radians
105  */
106 static inline void C2D_SpriteSetRotation(C2D_Sprite* sprite, float radians)
107 {
108  sprite->params.angle = radians;
109 }
110 
111 /** @brief Rotate sprite (absolute)
112  * @param[in] sprite Pointer to sprite
113  * @param[in] degrees Amount to rotate in degrees
114  */
115 static inline void C2D_SpriteSetRotationDegrees(C2D_Sprite* sprite, float degrees)
116 {
117  C2D_SpriteSetRotation(sprite, C3D_AngleFromDegrees(degrees));
118 }
119 
120 /** @brief Set the center of a sprite in values independent of the sprite size (absolute)
121  * @param[in] sprite Pointer to sprite
122  * @param[in] x X position of the center (0.0 through 1.0)
123  * @param[in] y Y position of the center (0.0 through 1.0)
124  */
125 static inline void C2D_SpriteSetCenter(C2D_Sprite* sprite, float x, float y)
126 {
127  sprite->params.center.x = x*sprite->params.pos.w;
128  sprite->params.center.y = y*sprite->params.pos.h;
129 }
130 
131 /** @brief Set the center of a sprite in terms of pixels (absolute)
132  * @param[in] sprite Pointer to sprite
133  * @param[in] x X position of the center (in pixels)
134  * @param[in] y Y position of the center (in pixels)
135  */
136 static inline void C2D_SpriteSetCenterRaw(C2D_Sprite* sprite, float x, float y)
137 {
138  sprite->params.center.x = x;
139  sprite->params.center.y = y;
140 }
141 
142 /** @brief Move sprite (absolute)
143  * @param[in] sprite Pointer to sprite
144  * @param[in] x X position
145  * @param[in] y Y position
146  */
147 static inline void C2D_SpriteSetPos(C2D_Sprite* sprite, float x, float y)
148 {
149  sprite->params.pos.x = x;
150  sprite->params.pos.y = y;
151 }
152 
153 /** @brief Sets the depth level of a sprite (absolute)
154  * @param[in] sprite Pointer to sprite
155  * @param[in] depth Depth value
156  */
157 static inline void C2D_SpriteSetDepth(C2D_Sprite* sprite, float depth)
158 {
159  sprite->params.depth = depth;
160 }
161 
162 /** @brief Draw sprite
163  * @param[in] sprite Sprite to draw
164  */
165 static inline bool C2D_DrawSprite(const C2D_Sprite* sprite)
166 {
167  return C2D_DrawImage(sprite->image, &sprite->params, NULL);
168 }
169 
170 /** @brief Draw sprite with color tinting
171  * @param[in] sprite Sprite to draw
172  * @param[in] tint Color tinting parameters to apply to the sprite
173  */
174 static inline bool C2D_DrawSpriteTinted(const C2D_Sprite* sprite, const C2D_ImageTint* tint)
175 {
176  return C2D_DrawImage(sprite->image, &sprite->params, tint);
177 }
178 
179 /** @} */
static void C2D_SpriteSetScale(C2D_Sprite *sprite, float x, float y)
Scale sprite (absolute)
Definition: sprite.h:92
C2D_Image C2D_SpriteSheetGetImage(C2D_SpriteSheet sheet, size_t index)
Retrieves the specified image from the specified sprite sheet.
static void C2D_SpriteRotateDegrees(C2D_Sprite *sprite, float degrees)
Rotate sprite (relative)
Definition: sprite.h:71
static void C2D_SpriteSetCenterRaw(C2D_Sprite *sprite, float x, float y)
Set the center of a sprite in terms of pixels (absolute)
Definition: sprite.h:136
static bool C2D_DrawSprite(const C2D_Sprite *sprite)
Draw sprite.
Definition: sprite.h:165
static void C2D_SpriteRotate(C2D_Sprite *sprite, float radians)
Rotate sprite (relative)
Definition: sprite.h:62
bool C2D_DrawImage(C2D_Image img, const C2D_DrawParams *params, const C2D_ImageTint *tint C2D_OPTIONAL(nullptr))
Draws an image using the GPU (variant accepting C2D_DrawParams)
Definition: base.h:56
static void C2D_SpriteSetPos(C2D_Sprite *sprite, float x, float y)
Move sprite (absolute)
Definition: sprite.h:147
static void C2D_SpriteSetDepth(C2D_Sprite *sprite, float depth)
Sets the depth level of a sprite (absolute)
Definition: sprite.h:157
Definition: sprite.h:8
static void C2D_SpriteSetRotation(C2D_Sprite *sprite, float radians)
Rotate sprite (absolute)
Definition: sprite.h:106
Definition: base.h:62
static bool C2D_DrawSpriteTinted(const C2D_Sprite *sprite, const C2D_ImageTint *tint)
Draw sprite with color tinting.
Definition: sprite.h:174
Spritesheet (texture atlas) loading and management.
static void C2D_SpriteSetCenter(C2D_Sprite *sprite, float x, float y)
Set the center of a sprite in values independent of the sprite size (absolute)
Definition: sprite.h:125
Definition: base.h:19
static void C2D_SpriteFromImage(C2D_Sprite *sprite, C2D_Image image)
Initializes a sprite from an image.
Definition: sprite.h:22
static void C2D_SpriteScale(C2D_Sprite *sprite, float x, float y)
Scale sprite (relative)
Definition: sprite.h:50
static void C2D_SpriteMove(C2D_Sprite *sprite, float x, float y)
Move sprite (relative)
Definition: sprite.h:81
static void C2D_SpriteSetRotationDegrees(C2D_Sprite *sprite, float degrees)
Rotate sprite (absolute)
Definition: sprite.h:115
static void C2D_SpriteFromSheet(C2D_Sprite *sprite, C2D_SpriteSheet sheet, size_t index)
Initializes a sprite from an image stored in a sprite sheet.
Definition: sprite.h:40