cmdfx 0.2.1
Lightweight game engine for your terminal
Loading...
Searching...
No Matches
scenes.hpp
Go to the documentation of this file.
1
11#pragma once
12
13extern "C" {
14 #include "cmdfx/core/scenes.h"
15}
16
18#include <string>
19#include <vector>
20#include <memory>
21
22namespace CmdFX {
23
32 class Scene final {
33 private:
34 CmdFX_Scene* scene;
35
36 public:
37 Scene(CmdFX_Scene* scene) : scene(scene) {}
38 Scene(int width, int height) {
39 scene = Scene_create(width, height);
40 }
41 Scene(int width, int height, char c, char* ansi, int z) {
42 scene = Scene_createFilled(width, height, c, ansi, z);
43 }
44 Scene(std::vector<std::string> data, std::vector<std::vector<std::string>> ansi) {
45 scene = Scene_createFromData(to2DArray(data), to3DArray(ansi));
46 }
47 Scene(char** data, char*** ansi) {
48 scene = Scene_createFromData(data, ansi);
49 }
50
51 ~Scene() {
52 if (scene) { Scene_free(scene); }
53 }
54
59 CmdFX_Scene* getScene() { return scene; }
60
61 int getX() const {
62 return scene->x;
63 }
64
65 int getY() const {
66 return scene->y;
67 }
68
69 int getWidth() const {
70 return scene->width;
71 }
72
73 int getHeight() const {
74 return scene->height;
75 }
76
77 int getZ() const {
78 return scene->z;
79 }
80
90 int getUniqueId() const {
91 return scene->uid;
92 }
93
98 std::vector<std::string> getData() {
99 std::vector<std::string> data;
100 for (int i = 0; i < scene->height; i++) {
101 data.push_back(std::string(scene->data[i]));
102 }
103 return data;
104 }
105
106 int setData(char** data) {
107 return Scene_setData(scene, data);
108 }
109
115 int setData(std::vector<std::string> data) {
116 char** text = to2DArray(data);
117 return Scene_setData(scene, text);
118 }
119
124 std::vector<std::vector<std::string>> getAnsi() {
125 std::vector<std::vector<std::string>> ansi;
126 for (int i = 0; i < scene->height; i++) {
127 std::vector<std::string> row;
128 for (int j = 0; j < scene->width; j++) {
129 row.push_back(std::string(scene->ansiData[i][j]));
130 }
131 ansi.push_back(row);
132 }
133 return ansi;
134 }
135
136 int setAnsi(char*** ansi) {
137 return Scene_setAnsiData(scene, ansi);
138 }
139
145 int setAnsi(std::vector<std::vector<std::string>> ansi) {
146 char*** ansiData = to3DArray(ansi);
147 return Scene_setAnsiData(scene, ansiData);
148 }
149
150 int appendAnsi(char*** ansi) {
151 return Scene_appendAnsiData(scene, ansi);
152 }
153
159 int appendAnsi(std::vector<std::vector<std::string>> ansi) {
160 char*** ansiData = to3DArray(ansi);
161 return Scene_appendAnsiData(scene, ansiData);
162 }
163
164 int setForeground(int x, int y, int width, int height, int rgb) {
165 return Scene_setForeground(scene, x, y, width, height, rgb);
166 }
167
168 int setForegroundAll(int rgb) {
169 return Scene_setForegroundAll(scene, rgb);
170 }
171
172 int setBackground(int x, int y, int width, int height, int rgb) {
173 return Scene_setBackground(scene, x, y, width, height, rgb);
174 }
175
176 int setBackgroundAll(int rgb) {
177 return Scene_setBackgroundAll(scene, rgb);
178 }
179
180 int clear() {
181 return Scene_clear(scene);
182 }
183
184 int draw(int x, int y) {
185 return Scene_draw(scene, x, y);
186 }
187
188 int drawPortion(int x, int y, int sx, int sy, int width, int height) {
189 return Scene_drawPortion(scene, x, y, sx, sy, width, height);
190 }
191
192 bool isOnTop() const {
193 return Scene_isOnTop(scene);
194 }
195
196 bool isOnTopAt(int x, int y) const {
197 return Scene_isOnTopAt(scene, x, y);
198 }
199
200 bool isOnBottom() const {
201 return Scene_isOnBottom(scene);
202 }
203
204 bool isOnBottomAt(int x, int y) const {
205 return Scene_isOnBottomAt(scene, x, y);
206 }
207
208 int remove() {
209 return Scene_remove(scene);
210 }
211
212 bool isEmpty() const {
213 return Scene_isEmpty(scene);
214 }
215
216 int switchTo(int x, int y) {
217 return Scene_switchTo(scene, x, y);
218 }
219
220 static std::unique_ptr<Scene> getSceneAt(int x, int y) {
221 CmdFX_Scene* foundScene = Scene_getSceneAt(x, y);
222 if (foundScene == nullptr) return nullptr;
223 return std::make_unique<Scene>(foundScene);
224 }
225
231 return Scene_register(scene);
232 }
233
239 return Scene_unregister(scene);
240 }
241
242 static int drawRegistered(int uid, int x, int y) {
243 return Scene_drawRegistered(uid, x, y);
244 }
245
246 static int switchToRegistered(int uid, int x, int y) {
247 return Scene_switchToRegistered(uid, x, y);
248 }
249
250 static int scroll(int uid, int dx, int dy) {
251 return Scene_scroll(uid, dx, dy);
252 }
253
265 int scroll(int dx, int dy) {
266 if (scene->uid < 0) return -1;
267 return Scene_scroll(scene->uid, dx, dy);
268 }
269 };
270
271 namespace Canvas {
272
277 std::vector<std::unique_ptr<Scene>> getDrawnScenes() {
278 std::vector<std::unique_ptr<Scene>> scenes;
279 CmdFX_Scene** drawnScenes = Canvas_getDrawnScenes();
280 for (int i = 0; i < Canvas_getDrawnScenesCount(); i++) {
281 scenes.push_back(std::make_unique<Scene>(drawnScenes[i]));
282 }
283 return scenes;
284 }
285
293
298 std::vector<std::unique_ptr<Scene>> getRegisteredScenes() {
299 std::vector<std::unique_ptr<Scene>> scenes;
300 CmdFX_Scene** registeredScenes = Scene_getRegisteredScenes();
301 for (int i = 0; i < Scene_getRegisteredScenesCount(); i++) {
302 scenes.push_back(std::make_unique<Scene>(registeredScenes[i]));
303 }
304 return scenes;
305 }
306
314
320 std::unique_ptr<Scene> getRegisteredScene(int uid) {
322 if (scene) {
323 return std::make_unique<Scene>(scene);
324 }
325
326 return nullptr;
327 }
328
329 }
330
331}
C++ Extensions for the Builder API.
CmdFX_Scene * getScene()
Get the Scene object associated with this class.
Definition scenes.hpp:59
int scroll(int dx, int dy)
Scrolls the scene by the specified amount.
Definition scenes.hpp:265
int registerScene()
Registers the scene to be drawn on the screen.
Definition scenes.hpp:230
int getUniqueId() const
Gets the unique identifier of the scene.
Definition scenes.hpp:90
int unregisterScene()
Unregisters the scene from being drawn on the screen.
Definition scenes.hpp:238
std::vector< std::string > getData()
Get the data of a scene as a 1D vector of strings.
Definition scenes.hpp:98
std::vector< std::vector< std::string > > getAnsi()
Gets the ANSI data of a scene as a 2D vector of strings.
Definition scenes.hpp:124
int appendAnsi(std::vector< std::vector< std::string > > ansi)
Appends the ANSI data to the current ANSI data of the scene.
Definition scenes.hpp:159
int setData(std::vector< std::string > data)
Sets the data of a scene.
Definition scenes.hpp:115
int setAnsi(std::vector< std::vector< std::string > > ansi)
Sets the ANSI data of a scene.
Definition scenes.hpp:145
Scenes API for CmdFX.
int Canvas_getDrawnScenesCount()
Gets the number of scenes that are drawn on the screen.
int Scene_register(CmdFX_Scene *scene)
Registers a scene with the Scene Engine.
int Scene_isOnTopAt(CmdFX_Scene *scene, int x, int y)
Checks if the specified scene is on top at the specified position on the screen.
int Scene_isOnTop(CmdFX_Scene *scene)
Checks if the specified scene is on top of the screen.
int Scene_setBackground(CmdFX_Scene *scene, int x, int y, int width, int height, int rgb)
Fills the background of the scene with the specified RGB color.
int Scene_unregister(CmdFX_Scene *scene)
Unregisters a scene with the Scene Engine.
int Scene_drawRegistered(int uid, int x, int y)
Draws a registered scene at the specified position on the screen.
int Scene_isOnBottomAt(CmdFX_Scene *scene, int x, int y)
Checks if the specified scene is on the bottom at the specified position on the screen.
int Scene_appendAnsiData(CmdFX_Scene *scene, char ***ansiData)
Appends the ANSi data to the current ansi data, setting it if it is not currently set.
CmdFX_Scene * Scene_createFilled(int width, int height, char c, char *ansi, int z)
Creates a filled scene with a specified width, height, and character.
CmdFX_Scene * Scene_createFromData(char **data, char ***ansiData)
Creates a scene from a 2D array of characters and ANSI strings.
int Scene_setBackgroundAll(CmdFX_Scene *scene, int rgb)
Fills the entirety of the background of the scene with the specified RGB color.
int Scene_draw(CmdFX_Scene *scene, int x, int y)
Draws a scene at the specified position on the screen.
int Scene_setData(CmdFX_Scene *scene, char **data)
Sets the data of the scene.
CmdFX_Scene ** Scene_getRegisteredScenes()
Gets a list of all registered scenes.
CmdFX_Scene * Scene_create(int width, int height)
Creates an empty scene with a specified width and height.
int Scene_drawPortion(CmdFX_Scene *scene, int x, int y, int sx, int sy, int width, int height)
Draws a portion of a scene at the specified position on the screen.
struct CmdFX_Scene CmdFX_Scene
Represents a CmdFX Scene.
int Scene_setForeground(CmdFX_Scene *scene, int x, int y, int width, int height, int rgb)
Fills the foreground of the scene with the specified RGB color.
CmdFX_Scene * Scene_getSceneAt(int x, int y)
Gets the scene at the specified position on the screen.
CmdFX_Scene ** Canvas_getDrawnScenes()
Gets a list of all scenes that are drawn on the screen.
int Scene_scroll(int uid, int dx, int dy)
Scrolls the scene by the specified amount.
int Scene_remove(CmdFX_Scene *scene)
Removes a scene from the screen.
int Scene_isOnBottom(CmdFX_Scene *scene)
Checks if the specified scene is on the bottom of the screen.
int Scene_getRegisteredScenesCount()
Gets the number of registered scenes.
int Scene_free(CmdFX_Scene *scene)
Destroys the scene, freeing the memory allocated.
int Scene_setForegroundAll(CmdFX_Scene *scene, int rgb)
Fills the entirety of the foreground of the scene with the specified RGB color.
int Scene_isEmpty(CmdFX_Scene *scene)
Checks if the specified scene is empty.
int Scene_switchTo(CmdFX_Scene *scene, int x, int y)
Removes all of the currently drawn scenes, only leaving this scene.
int Scene_setAnsiData(CmdFX_Scene *scene, char ***ansiData)
Sets the ANSI data of the scene.
CmdFX_Scene * Scene_getRegisteredScene(int uid)
Gets a registered scene by its unique identifier.
int Scene_switchToRegistered(int uid, int x, int y)
Removes all of the currently drawn scenes, only leaving the registered scene with the specified uniqu...
int Scene_clear(CmdFX_Scene *scene)
Clears a scene of its data.
std::vector< std::unique_ptr< Scene > > getRegisteredScenes()
Gets the registered scenes.
Definition scenes.hpp:298
int getRegisteredScenesCount()
Gets the number of registered scenes.
Definition scenes.hpp:311
std::vector< std::unique_ptr< Scene > > getDrawnScenes()
Gets all of the drawn scenes.
Definition scenes.hpp:277
int getDrawnScenesCount()
Gets the number of scenes that are drawn on the screen.
Definition scenes.hpp:290
std::unique_ptr< Scene > getRegisteredScene(int uid)
Gets a registered scene by its unique identifier.
Definition scenes.hpp:320
Primary namespace for CmdFX.
Definition cmdfx.hpp:22
char ** to2DArray(std::vector< std::string > string)
Converts a 1D vector of strings to a 2D array of characters.
Definition builder.hpp:39
char *** to3DArray(std::vector< std::vector< std::string > > string)
Converts a 2D vector of strings to a 3D array of characters.
Definition builder.hpp:55
Represents a CmdFX Scene.
Definition scenes.h:34
int x
Gets the x-coordinate of the scene.
Definition scenes.h:94
int y
Gets the y-coordinate of the scene.
Definition scenes.h:103