cmdfx 0.3.2
Lightweight game engine for your terminal
Loading...
Searching...
No Matches
sprites.hpp
Go to the documentation of this file.
1
11#pragma once
12
13extern "C" {
14#include "cmdfx/core/sprites.h"
15}
16
18#include <memory>
19#include <string>
20#include <vector>
21
22namespace CmdFX
23{
24
33class Sprite final {
34 private:
35 CmdFX_Sprite* sprite;
36
37 public:
38 Sprite(CmdFX_Sprite* sprite) : sprite(sprite) {
39 }
40 Sprite(char** text, char*** ansi, int z) {
41 sprite = Sprite_create(text, ansi, z);
42 }
43 Sprite(
44 std::vector<std::string> text,
45 std::vector<std::vector<std::string>> ansi, int z
46 ) {
47 sprite = Sprite_create(to2DArray(text), to3DArray(ansi), z);
48 }
49
50 ~Sprite() {
51 if (sprite) {
52 Sprite_free(sprite);
53 }
54 }
55
61 return sprite;
62 }
63
64 int getX() const {
65 return sprite->x;
66 }
67
68 int getY() const {
69 return sprite->y;
70 }
71
72 int getWidth() const {
73 return sprite->width;
74 }
75
76 int getHeight() const {
77 return sprite->height;
78 }
79
80 int getZ() const {
81 return sprite->z;
82 }
83
84 int draw(int x, int y) {
85 return Sprite_draw(x, y, sprite);
86 }
87
88 void remove() {
89 Sprite_remove(sprite);
90 }
91
96 std::vector<std::string> getData() {
97 std::vector<std::string> text;
98 for (int i = 0; i < sprite->height; i++) {
99 text.push_back(std::string(sprite->data[i]));
100 }
101 return text;
102 }
103
104 void setData(std::vector<std::string> text) {
105 char** data = to2DArray(text);
106 Sprite_setData(sprite, data);
107 }
108
109 void setData(char** text) {
110 Sprite_setData(sprite, text);
111 }
112
117 std::vector<std::vector<std::string>> getAnsi() {
118 std::vector<std::vector<std::string>> ansi;
119 for (int i = 0; i < sprite->height; i++) {
120 std::vector<std::string> row;
121 for (int j = 0; j < sprite->width; j++) {
122 row.push_back(std::string(sprite->ansi[i][j]));
123 }
124 ansi.push_back(row);
125 }
126 return ansi;
127 }
128
129 void setChar(int x, int y, char c) {
130 Sprite_setChar(sprite, x, y, c);
131 }
132
133 int fillChar(int x, int y, int width, int height, char c) {
134 return Sprite_fillChar(sprite, x, y, width, height, c);
135 }
136
137 int fillCharEmpty(int x, int y, int width, int height, char c) {
138 return Sprite_fillCharEmpty(sprite, x, y, width, height, c);
139 }
140
141 int fillCharAll(char c) {
142 return Sprite_fillCharAll(sprite, c);
143 }
144
145 int fillCharAllEmpty(char c) {
146 return Sprite_fillCharAllEmpty(sprite, c);
147 }
148
149 int setAnsi(int x, int y, char* ansi) {
150 return Sprite_setAnsi(sprite, x, y, ansi);
151 }
152
153 int appendAnsi(int x, int y, char* ansi) {
154 return Sprite_appendAnsi(sprite, x, y, ansi);
155 }
156
157 int fillAnsi(int x, int y, char* ansi, int width, int height) {
158 return Sprite_fillAnsi(sprite, x, y, ansi, width, height);
159 }
160
161 int setAnsiAll(char* ansi) {
162 return Sprite_setAnsiAll(sprite, ansi);
163 }
164
165 int appendAnsiAll(char* ansi) {
166 return Sprite_appendAnsiAll(sprite, ansi);
167 }
168
169 int resize(int width, int height) {
170 return Sprite_resize(sprite, width, height);
171 }
172
173 int resizeWithPadding(int width, int height, char padding) {
174 return Sprite_resizeWithPadding(sprite, width, height, padding);
175 }
176
177 int center() {
178 return Sprite_center(sprite);
179 }
180
181 int resizeAndCenter(int width, int height) {
182 return Sprite_resizeAndCenter(sprite, width, height);
183 }
184
185 void moveTo(int x, int y) {
186 Sprite_moveTo(sprite, x, y);
187 }
188
189 void moveBy(int dx, int dy) {
190 Sprite_moveBy(sprite, dx, dy);
191 }
192
193 std::vector<std::unique_ptr<Sprite>> getCollidingSprites() {
194 std::vector<std::unique_ptr<Sprite>> collisions;
195 CmdFX_Sprite** collisionSprites = Sprite_getCollidingSprites(sprite);
196 for (int i = 0; collisionSprites[i] != nullptr; i++) {
197 collisions.push_back(std::make_unique<Sprite>(collisionSprites[i]));
198 }
199 return collisions;
200 }
201
202 bool isCollidingWith(std::unique_ptr<Sprite> other) {
203 return Sprite_isColliding(sprite, other->getSprite());
204 }
205
206 bool isOnTop(int x, int y) {
207 return Sprite_isOnTop(sprite, x, y);
208 }
209
210 bool isOnBottom(int x, int y) {
211 return Sprite_isOnBottom(sprite, x, y);
212 }
213
214 int setForeground(int x, int y, int rgb) {
215 return Sprite_setForeground(sprite, x, y, rgb);
216 }
217
218 int setForeground(int rgb) {
219 return Sprite_setForegroundAll(sprite, rgb);
220 }
221
222 int setForeground256(int color) {
223 return Sprite_setForegroundAll256(sprite, color);
224 }
225
226 int setBackground(int x, int y, int rgb) {
227 return Sprite_setBackground(sprite, x, y, rgb);
228 }
229
230 int rotate(double radians) {
231 return Sprite_rotate(sprite, radians);
232 }
233
234 double getRotationAngle() {
235 return Sprite_getRotationAngle(sprite);
236 }
237
238 int hFlip() {
239 return Sprite_hFlip(sprite);
240 }
241
242 int vFlip() {
243 return Sprite_vFlip(sprite);
244 }
245
246 int scale(double scale) {
247 return Sprite_scale(sprite, scale);
248 }
249
250 int transpose() {
251 return Sprite_transpose(sprite);
252 }
253};
254
255namespace Canvas
256{
257
262std::vector<std::unique_ptr<Sprite>> getDrawnSprites() {
263 std::vector<std::unique_ptr<Sprite>> sprites;
264 CmdFX_Sprite** drawnSprites = Canvas_getDrawnSprites();
265 for (int i = 0; i < Canvas_getDrawnSpritesCount(); i++) {
266 sprites.push_back(std::make_unique<Sprite>(drawnSprites[i]));
267 }
268
269 return sprites;
270}
271
279std::unique_ptr<Sprite> getSpriteAt(int x, int y) {
280 CmdFX_Sprite* sprite = Canvas_getSpriteAt(x, y);
281 if (sprite) {
282 return std::make_unique<Sprite>(sprite);
283 }
284
285 return nullptr;
286}
287
288} // namespace Canvas
289
290} // namespace CmdFX
C++ Extensions for the Builder API.
CmdFX_Sprite * getSprite()
Get the Sprite object associated with this class.
Definition sprites.hpp:60
std::vector< std::string > getData()
Get the data of a sprite as a 1D vector of strings.
Definition sprites.hpp:96
std::vector< std::vector< std::string > > getAnsi()
Get the ANSI data of a sprite as a 2D vector of strings.
Definition sprites.hpp:117
C++ wrapper for the CmdFX canvas.
Definition canvas.hpp:25
std::vector< std::unique_ptr< Sprite > > getDrawnSprites()
Gets all of the drawn sprites.
Definition sprites.hpp:262
std::unique_ptr< Sprite > getSpriteAt(int x, int y)
Get the Sprite at a specific position.
Definition sprites.hpp:279
Primary namespace for CmdFX.
Definition cmdfx.hpp:26
char ** to2DArray(std::vector< std::string > string)
Converts a 1D vector of strings to a 2D array of characters.
Definition builder.hpp:64
char *** to3DArray(std::vector< std::vector< std::string > > string)
Converts a 2D vector of strings to a 3D array of characters.
Definition builder.hpp:80
Sprites API for CmdFX.
CmdFX_Sprite ** Sprite_getCollidingSprites(CmdFX_Sprite *sprite)
Gets the sprites that are colliding with the given sprite.
void Sprite_remove(CmdFX_Sprite *sprite)
Removes a sprite from the terminal, making it no longer visble.
int Sprite_setAnsi(CmdFX_Sprite *sprite, int x, int y, char *ansi)
Sets the ANSI code at the given position in the sprite.
int Sprite_fillCharAllEmpty(CmdFX_Sprite *sprite, char c)
Sets the character at all positions in the sprite where there isn't a character.
int Sprite_setBackground(CmdFX_Sprite *sprite, int x, int y, int rgb)
Sets the background color of the sprite at the given position.
int Sprite_fillAnsi(CmdFX_Sprite *sprite, int x, int y, char *ansi, int width, int height)
Fills positions in the sprite with an ANSI code.
int Sprite_vFlip(CmdFX_Sprite *sprite)
Flips the sprite vertically.
int Sprite_rotate(CmdFX_Sprite *sprite, double radians)
Rotates the sprite by the given angle.
int Sprite_isOnBottom(CmdFX_Sprite *sprite, int x, int y)
Gets whether the sprite is the bottom-most sprite at the given position.
int Sprite_isColliding(CmdFX_Sprite *sprite1, CmdFX_Sprite *sprite2)
Checks if two sprites are colliding.
int Sprite_setAnsiAll(CmdFX_Sprite *sprite, char *ansi)
Sets the character at all positions in the sprite.
void Sprite_moveTo(CmdFX_Sprite *sprite, int x, int y)
Moves a sprite to the given position.
CmdFX_Sprite * Canvas_getSpriteAt(int x, int y)
Gets a sprite at the given position with the highest Z-index.
int Sprite_fillCharAll(CmdFX_Sprite *sprite, char c)
Sets the character at all positions in the sprite.
int Sprite_scale(CmdFX_Sprite *sprite, double scale)
Scales the sprite by the given factor.
CmdFX_Sprite ** Canvas_getDrawnSprites()
Gets the sprites that have been drawn to the terminal.
int Sprite_setData(CmdFX_Sprite *sprite, char **data)
Sets the text data for the sprite.
int Sprite_appendAnsiAll(CmdFX_Sprite *sprite, char *ansi)
Appends an ANSI code to all positions in the sprite.
void Sprite_free(CmdFX_Sprite *sprite)
Frees the memory allocated for a sprite.
double Sprite_getRotationAngle(CmdFX_Sprite *sprite)
Gets the rotation angle of the sprite.
void Sprite_moveBy(CmdFX_Sprite *sprite, int dx, int dy)
Moves a sprite by the given amount.
int Sprite_center(CmdFX_Sprite *sprite)
Centers the sprite on the terminal.
int Sprite_setForeground(CmdFX_Sprite *sprite, int x, int y, int rgb)
Sets the foreground color of the sprite at the given position.
int Sprite_transpose(CmdFX_Sprite *sprite)
Transposes the sprite.
int Sprite_fillCharEmpty(CmdFX_Sprite *sprite, int x, int y, int width, int height, char c)
Fills positions in the sprite where there isn't a character.
int Sprite_resizeAndCenter(CmdFX_Sprite *sprite, int width, int height)
Resizes and centers the sprite on the terminal.
int Sprite_setChar(CmdFX_Sprite *sprite, int x, int y, char c)
Sets the character at the given position in the sprite.
CmdFX_Sprite * Sprite_create(char **text, char ***ansi, int z)
Creates a new sprite.
int Sprite_draw(int x, int y, CmdFX_Sprite *sprite)
Draws a sprite to the terminal.
int Sprite_appendAnsi(CmdFX_Sprite *sprite, int x, int y, char *ansi)
Appends an ANSI code to the given position in the sprite.
int Sprite_fillChar(CmdFX_Sprite *sprite, int x, int y, int width, int height, char c)
Fills positions in the sprite.
struct CmdFX_Sprite CmdFX_Sprite
Represents a sprite that can be drawn to the terminal.
int Sprite_resize(CmdFX_Sprite *sprite, int width, int height)
Resizes the sprite to the given dimensions.
int Canvas_getDrawnSpritesCount()
Gets the number of sprites that have been drawn to the terminal.
int Sprite_setForegroundAll256(CmdFX_Sprite *sprite, int color)
Sets the foreground color of the sprite at the given position using 256-bit color codes.
int Sprite_hFlip(CmdFX_Sprite *sprite)
Flips the sprite horizontally.
int Sprite_isOnTop(CmdFX_Sprite *sprite, int x, int y)
Gets whether the sprite is the top-most sprite at the given position.
int Sprite_setForegroundAll(CmdFX_Sprite *sprite, int rgb)
Sets the foreground color of the sprite at the given position.
int Sprite_resizeWithPadding(CmdFX_Sprite *sprite, int width, int height, char padding)
Resizes the sprite to the given dimensions.
Represents a sprite that can be drawn to the terminal.
Definition sprites.h:31
int x
The X position of the sprite.
Definition sprites.h:35
int y
The Y position of the sprite.
Definition sprites.h:39