cmdfx 0.3.2
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 <memory>
19#include <string>
20#include <vector>
21
22namespace CmdFX
23{
24
33class Scene final {
34 private:
35 CmdFX_Scene* scene;
36
37 public:
38 Scene(CmdFX_Scene* scene) : scene(scene) {
39 }
40 Scene(int width, int height) {
41 scene = Scene_create(width, height);
42 }
43 Scene(int width, int height, char c, char* ansi, int z) {
44 scene = Scene_createFilled(width, height, c, ansi, z);
45 }
46 Scene(
47 std::vector<std::string> data,
48 std::vector<std::vector<std::string>> ansi
49 ) {
50 scene = Scene_createFromData(to2DArray(data), to3DArray(ansi));
51 }
52 Scene(char** data, char*** ansi) {
53 scene = Scene_createFromData(data, ansi);
54 }
55
56 ~Scene() {
57 if (scene) {
58 Scene_free(scene);
59 }
60 }
61
67 return scene;
68 }
69
70 int getX() const {
71 return scene->x;
72 }
73
74 int getY() const {
75 return scene->y;
76 }
77
78 int getWidth() const {
79 return scene->width;
80 }
81
82 int getHeight() const {
83 return scene->height;
84 }
85
86 int getZ() const {
87 return scene->z;
88 }
89
99 int getUniqueId() const {
100 return scene->uid;
101 }
102
107 std::vector<std::string> getData() {
108 std::vector<std::string> data;
109 for (int i = 0; i < scene->height; i++) {
110 data.push_back(std::string(scene->data[i]));
111 }
112 return data;
113 }
114
115 int setData(char** data) {
116 return Scene_setData(scene, data);
117 }
118
124 int setData(std::vector<std::string> data) {
125 char** text = to2DArray(data);
126 return Scene_setData(scene, text);
127 }
128
133 std::vector<std::vector<std::string>> getAnsi() {
134 std::vector<std::vector<std::string>> ansi;
135 for (int i = 0; i < scene->height; i++) {
136 std::vector<std::string> row;
137 for (int j = 0; j < scene->width; j++) {
138 row.push_back(std::string(scene->ansiData[i][j]));
139 }
140 ansi.push_back(row);
141 }
142 return ansi;
143 }
144
145 int setAnsi(char*** ansi) {
146 return Scene_setAnsiData(scene, ansi);
147 }
148
154 int setAnsi(std::vector<std::vector<std::string>> ansi) {
155 char*** ansiData = to3DArray(ansi);
156 return Scene_setAnsiData(scene, ansiData);
157 }
158
159 int appendAnsi(char*** ansi) {
160 return Scene_appendAnsiData(scene, ansi);
161 }
162
168 int appendAnsi(std::vector<std::vector<std::string>> ansi) {
169 char*** ansiData = to3DArray(ansi);
170 return Scene_appendAnsiData(scene, ansiData);
171 }
172
173 int setForeground(int x, int y, int width, int height, int rgb) {
174 return Scene_setForeground(scene, x, y, width, height, rgb);
175 }
176
177 int setForegroundAll(int rgb) {
178 return Scene_setForegroundAll(scene, rgb);
179 }
180
181 int setBackground(int x, int y, int width, int height, int rgb) {
182 return Scene_setBackground(scene, x, y, width, height, rgb);
183 }
184
185 int setBackgroundAll(int rgb) {
186 return Scene_setBackgroundAll(scene, rgb);
187 }
188
189 int clear() {
190 return Scene_clear(scene);
191 }
192
193 int draw(int x, int y) {
194 return Scene_draw(scene, x, y);
195 }
196
197 int drawPortion(int x, int y, int sx, int sy, int width, int height) {
198 return Scene_drawPortion(scene, x, y, sx, sy, width, height);
199 }
200
201 bool isOnTop() const {
202 return Scene_isOnTop(scene);
203 }
204
205 bool isOnTopAt(int x, int y) const {
206 return Scene_isOnTopAt(scene, x, y);
207 }
208
209 bool isOnBottom() const {
210 return Scene_isOnBottom(scene);
211 }
212
213 bool isOnBottomAt(int x, int y) const {
214 return Scene_isOnBottomAt(scene, x, y);
215 }
216
217 int remove() {
218 return Scene_remove(scene);
219 }
220
221 bool isEmpty() const {
222 return Scene_isEmpty(scene);
223 }
224
225 int switchTo(int x, int y) {
226 return Scene_switchTo(scene, x, y);
227 }
228
229 static std::unique_ptr<Scene> getSceneAt(int x, int y) {
230 CmdFX_Scene* foundScene = Scene_getSceneAt(x, y);
231 if (foundScene == nullptr) return nullptr;
232 return std::make_unique<Scene>(foundScene);
233 }
234
241 return Scene_register(scene);
242 }
243
250 return Scene_unregister(scene);
251 }
252
253 static int drawRegistered(int uid, int x, int y) {
254 return Scene_drawRegistered(uid, x, y);
255 }
256
257 static int switchToRegistered(int uid, int x, int y) {
258 return Scene_switchToRegistered(uid, x, y);
259 }
260
261 static int scroll(int uid, int dx, int dy) {
262 return Scene_scroll(uid, dx, dy);
263 }
264
279 int scroll(int dx, int dy) {
280 if (scene->uid < 0) return -1;
281 return Scene_scroll(scene->uid, dx, dy);
282 }
283};
284
285namespace Canvas
286{
287
292std::vector<std::unique_ptr<Scene>> getDrawnScenes() {
293 std::vector<std::unique_ptr<Scene>> scenes;
294 CmdFX_Scene** drawnScenes = Canvas_getDrawnScenes();
295 for (int i = 0; i < Canvas_getDrawnScenesCount(); i++) {
296 scenes.push_back(std::make_unique<Scene>(drawnScenes[i]));
297 }
298 return scenes;
299}
300
308
313std::vector<std::unique_ptr<Scene>> getRegisteredScenes() {
314 std::vector<std::unique_ptr<Scene>> scenes;
315 CmdFX_Scene** registeredScenes = Scene_getRegisteredScenes();
316 for (int i = 0; i < Scene_getRegisteredScenesCount(); i++) {
317 scenes.push_back(std::make_unique<Scene>(registeredScenes[i]));
318 }
319 return scenes;
320}
321
329
336std::unique_ptr<Scene> getRegisteredScene(int uid) {
338 if (scene) {
339 return std::make_unique<Scene>(scene);
340 }
341
342 return nullptr;
343}
344
345} // namespace Canvas
346
347} // namespace CmdFX
C++ Extensions for the Builder API.
CmdFX_Scene * getScene()
Get the Scene object associated with this class.
Definition scenes.hpp:66
int scroll(int dx, int dy)
Scrolls the scene by the specified amount.
Definition scenes.hpp:279
int registerScene()
Registers the scene to be drawn on the screen.
Definition scenes.hpp:240
int getUniqueId() const
Gets the unique identifier of the scene.
Definition scenes.hpp:99
int unregisterScene()
Unregisters the scene from being drawn on the screen.
Definition scenes.hpp:249
std::vector< std::string > getData()
Get the data of a scene as a 1D vector of strings.
Definition scenes.hpp:107
std::vector< std::vector< std::string > > getAnsi()
Gets the ANSI data of a scene as a 2D vector of strings.
Definition scenes.hpp:133
int appendAnsi(std::vector< std::vector< std::string > > ansi)
Appends the ANSI data to the current ANSI data of the scene.
Definition scenes.hpp:168
int setData(std::vector< std::string > data)
Sets the data of a scene.
Definition scenes.hpp:124
int setAnsi(std::vector< std::vector< std::string > > ansi)
Sets the ANSI data of a scene.
Definition scenes.hpp:154
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:313
int getRegisteredScenesCount()
Gets the number of registered scenes.
Definition scenes.hpp:326
std::vector< std::unique_ptr< Scene > > getDrawnScenes()
Gets all of the drawn scenes.
Definition scenes.hpp:292
int getDrawnScenesCount()
Gets the number of scenes that are drawn on the screen.
Definition scenes.hpp:305
std::unique_ptr< Scene > getRegisteredScene(int uid)
Gets a registered scene by its unique identifier.
Definition scenes.hpp:336
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
Represents a CmdFX Scene.
Definition scenes.h:35
int x
Gets the x-coordinate of the scene.
Definition scenes.h:100
int y
Gets the y-coordinate of the scene.
Definition scenes.h:110