yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
zsprite.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SPRITE_ZSPRITE_H
2#define YAZE_APP_EDITOR_SPRITE_ZSPRITE_H
3
4#include <algorithm>
5#include <cstdint>
6#include <fstream>
7#include <string>
8#include <vector>
9
10#include "absl/status/status.h"
11#include "app/gfx/snes_tile.h"
12#include "imgui/imgui.h"
13
14namespace yaze {
15namespace app {
16namespace editor {
20namespace zsprite {
21
22struct OamTile {
23 OamTile(uint8_t x, uint8_t y, bool mx, bool my, uint16_t id, uint8_t pal,
24 bool s, uint8_t p)
25 : x(x),
26 y(y),
27 mirror_x(mx),
28 mirror_y(my),
29 id(id),
30 palette(pal),
31 size(s),
32 priority(p) {}
33
34 uint8_t x;
35 uint8_t y;
38 uint16_t id;
39 uint8_t palette;
40 bool size;
41 uint8_t priority;
42 uint8_t z;
43};
44
46 AnimationGroup() = default;
47 AnimationGroup(uint8_t fs, uint8_t fe, uint8_t fsp, std::string fn)
48 : frame_name(fn), frame_start(fs), frame_end(fe), frame_speed(fsp) {}
49
50 std::string frame_name;
51 uint8_t frame_start;
52 uint8_t frame_end;
53 uint8_t frame_speed;
54 std::vector<OamTile> Tiles;
55};
56
58 UserRoutine(std::string n, std::string c) : name(n), code(c) {}
59
60 std::string name;
61 std::string code;
62 int Count;
63};
64
65struct SubEditor {
66 std::vector<AnimationGroup> Frames;
67 std::vector<UserRoutine> user_routines;
68};
69
72 std::string Text;
73};
74
75struct ZSprite {
76 public:
77 absl::Status Load(const std::string& filename) {
78 std::ifstream fs(filename, std::ios::binary);
79 if (!fs.is_open()) {
80 return absl::NotFoundError("File not found");
81 }
82
83 std::vector<char> buffer(std::istreambuf_iterator<char>(fs), {});
84
85 int animation_count = *reinterpret_cast<int32_t*>(&buffer[0]);
86 int offset = sizeof(int);
87
88 for (int i = 0; i < animation_count; i++) {
89 std::string aname = std::string(&buffer[offset]);
90 offset += aname.size() + 1;
91 uint8_t afs = *reinterpret_cast<uint8_t*>(&buffer[offset]);
92 offset += sizeof(uint8_t);
93 uint8_t afe = *reinterpret_cast<uint8_t*>(&buffer[offset]);
94 offset += sizeof(uint8_t);
95 uint8_t afspeed = *reinterpret_cast<uint8_t*>(&buffer[offset]);
96 offset += sizeof(uint8_t);
97
98 animations.push_back(AnimationGroup(afs, afe, afspeed, aname));
99 }
100 // RefreshAnimations();
101
102 int frame_count = *reinterpret_cast<int32_t*>(&buffer[offset]);
103 offset += sizeof(int);
104 for (int i = 0; i < frame_count; i++) {
105 // editor.Frames[i] = new Frame();
106 editor.Frames.emplace_back();
107 // editor.AddUndo(i);
108 int tCount = *reinterpret_cast<int*>(&buffer[offset]);
109 offset += sizeof(int);
110
111 for (int j = 0; j < tCount; j++) {
112 ushort tid = *reinterpret_cast<ushort*>(&buffer[offset]);
113 offset += sizeof(ushort);
114 uint8_t tpal = *reinterpret_cast<uint8_t*>(&buffer[offset]);
115 offset += sizeof(uint8_t);
116 bool tmx = *reinterpret_cast<bool*>(&buffer[offset]);
117 offset += sizeof(bool);
118 bool tmy = *reinterpret_cast<bool*>(&buffer[offset]);
119 offset += sizeof(bool);
120 uint8_t tprior = *reinterpret_cast<uint8_t*>(&buffer[offset]);
121 offset += sizeof(uint8_t);
122 bool tsize = *reinterpret_cast<bool*>(&buffer[offset]);
123 offset += sizeof(bool);
124 uint8_t tx = *reinterpret_cast<uint8_t*>(&buffer[offset]);
125 offset += sizeof(uint8_t);
126 uint8_t ty = *reinterpret_cast<uint8_t*>(&buffer[offset]);
127 offset += sizeof(uint8_t);
128 uint8_t tz = *reinterpret_cast<uint8_t*>(&buffer[offset]);
129 offset += sizeof(uint8_t);
130 OamTile to(tx, ty, tmx, tmy, tid, tpal, tsize, tprior);
131 to.z = tz;
132 editor.Frames[i].Tiles.push_back(to);
133 }
134 }
135
136 // all sprites properties
137 property_blockable.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
138 offset += sizeof(bool);
139 property_canfall.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
140 offset += sizeof(bool);
142 *reinterpret_cast<bool*>(&buffer[offset]);
143 offset += sizeof(bool);
144 property_customdeath.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
145 offset += sizeof(bool);
146 property_damagesound.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
147 offset += sizeof(bool);
149 *reinterpret_cast<bool*>(&buffer[offset]);
150 offset += sizeof(bool);
152 *reinterpret_cast<bool*>(&buffer[offset]);
153 offset += sizeof(bool);
154 property_fast.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
155 offset += sizeof(bool);
156 property_harmless.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
157 offset += sizeof(bool);
158 property_impervious.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
159 offset += sizeof(bool);
161 *reinterpret_cast<bool*>(&buffer[offset]);
162 offset += sizeof(bool);
164 *reinterpret_cast<bool*>(&buffer[offset]);
165 offset += sizeof(bool);
166 property_interaction.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
167 offset += sizeof(bool);
168 property_isboss.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
169 offset += sizeof(bool);
170 property_persist.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
171 offset += sizeof(bool);
172 property_shadow.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
173 offset += sizeof(bool);
174 property_smallshadow.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
175 offset += sizeof(bool);
176 property_statis.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
177 offset += sizeof(bool);
178 property_statue.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
179 offset += sizeof(bool);
180 property_watersprite.IsChecked = *reinterpret_cast<bool*>(&buffer[offset]);
181 offset += sizeof(bool);
182
184 std::to_string(*reinterpret_cast<uint8_t*>(&buffer[offset]));
185 offset += sizeof(uint8_t);
187 std::to_string(*reinterpret_cast<uint8_t*>(&buffer[offset]));
188 offset += sizeof(uint8_t);
190 std::to_string(*reinterpret_cast<uint8_t*>(&buffer[offset]));
191 offset += sizeof(uint8_t);
193 std::to_string(*reinterpret_cast<uint8_t*>(&buffer[offset]));
194 offset += sizeof(uint8_t);
196 std::to_string(*reinterpret_cast<uint8_t*>(&buffer[offset]));
197 offset += sizeof(uint8_t);
199 std::to_string(*reinterpret_cast<uint8_t*>(&buffer[offset]));
200 offset += sizeof(uint8_t);
201
202 if (offset != buffer.size()) {
203 property_sprname.Text = std::string(&buffer[offset]);
204 offset += property_sprname.Text.size() + 1;
205
206 int actionL = buffer[offset];
207 offset += sizeof(int);
208 for (int i = 0; i < actionL; i++) {
209 std::string a = std::string(&buffer[offset]);
210 offset += a.size() + 1;
211 std::string b = std::string(&buffer[offset]);
212 offset += b.size() + 1;
213 userRoutines.push_back(UserRoutine(a, b));
214 }
215 }
216
217 if (offset != buffer.size()) {
218 property_sprid.Text = std::string(&buffer[offset]);
219 fs.close();
220 }
221
222 // UpdateUserRoutines();
223 // userroutinesListbox.SelectedIndex = 0;
224 // RefreshScreen();
225
226 return absl::OkStatus();
227 }
228
229 absl::Status Save(const std::string& filename) {
230 std::ofstream fs(filename, std::ios::binary);
231 if (fs.is_open()) {
232 // Write data to the file
233 fs.write(reinterpret_cast<const char*>(animations.size()), sizeof(int));
234 for (const AnimationGroup& anim : animations) {
235 fs.write(anim.frame_name.c_str(), anim.frame_name.size() + 1);
236 fs.write(reinterpret_cast<const char*>(&anim.frame_start),
237 sizeof(uint8_t));
238 fs.write(reinterpret_cast<const char*>(&anim.frame_end),
239 sizeof(uint8_t));
240 fs.write(reinterpret_cast<const char*>(&anim.frame_speed),
241 sizeof(uint8_t));
242 }
243
244 fs.write(reinterpret_cast<const char*>(editor.Frames.size()),
245 sizeof(int));
246 for (int i = 0; i < editor.Frames.size(); i++) {
247 fs.write(reinterpret_cast<const char*>(editor.Frames[i].Tiles.size()),
248 sizeof(int));
249
250 for (int j = 0; j < editor.Frames[i].Tiles.size(); j++) {
251 fs.write(reinterpret_cast<const char*>(&editor.Frames[i].Tiles[j].id),
252 sizeof(ushort));
253 fs.write(
254 reinterpret_cast<const char*>(&editor.Frames[i].Tiles[j].palette),
255 sizeof(uint8_t));
256 fs.write(reinterpret_cast<const char*>(
257 &editor.Frames[i].Tiles[j].mirror_x),
258 sizeof(bool));
259 fs.write(reinterpret_cast<const char*>(
260 &editor.Frames[i].Tiles[j].mirror_y),
261 sizeof(bool));
262 fs.write(reinterpret_cast<const char*>(
263 &editor.Frames[i].Tiles[j].priority),
264 sizeof(uint8_t));
265 fs.write(
266 reinterpret_cast<const char*>(&editor.Frames[i].Tiles[j].size),
267 sizeof(bool));
268 fs.write(reinterpret_cast<const char*>(&editor.Frames[i].Tiles[j].x),
269 sizeof(uint8_t));
270 fs.write(reinterpret_cast<const char*>(&editor.Frames[i].Tiles[j].y),
271 sizeof(uint8_t));
272 fs.write(reinterpret_cast<const char*>(&editor.Frames[i].Tiles[j].z),
273 sizeof(uint8_t));
274 }
275 }
276
277 // Write other properties
278 fs.write(reinterpret_cast<const char*>(&property_blockable.IsChecked),
279 sizeof(bool));
280 fs.write(reinterpret_cast<const char*>(&property_canfall.IsChecked),
281 sizeof(bool));
282 fs.write(
283 reinterpret_cast<const char*>(&property_collisionlayer.IsChecked),
284 sizeof(bool));
285 fs.write(reinterpret_cast<const char*>(&property_customdeath.IsChecked),
286 sizeof(bool));
287 fs.write(reinterpret_cast<const char*>(&property_damagesound.IsChecked),
288 sizeof(bool));
289 fs.write(reinterpret_cast<const char*>(&property_deflectarrows.IsChecked),
290 sizeof(bool));
291 fs.write(
292 reinterpret_cast<const char*>(&property_deflectprojectiles.IsChecked),
293 sizeof(bool));
294 fs.write(reinterpret_cast<const char*>(&property_fast.IsChecked),
295 sizeof(bool));
296 fs.write(reinterpret_cast<const char*>(&property_harmless.IsChecked),
297 sizeof(bool));
298 fs.write(reinterpret_cast<const char*>(&property_impervious.IsChecked),
299 sizeof(bool));
300 fs.write(
301 reinterpret_cast<const char*>(&property_imperviousarrow.IsChecked),
302 sizeof(bool));
303 fs.write(
304 reinterpret_cast<const char*>(&property_imperviousmelee.IsChecked),
305 sizeof(bool));
306 fs.write(reinterpret_cast<const char*>(&property_interaction.IsChecked),
307 sizeof(bool));
308 fs.write(reinterpret_cast<const char*>(&property_isboss.IsChecked),
309 sizeof(bool));
310 fs.write(reinterpret_cast<const char*>(&property_persist.IsChecked),
311 sizeof(bool));
312 fs.write(reinterpret_cast<const char*>(&property_shadow.IsChecked),
313 sizeof(bool));
314 fs.write(reinterpret_cast<const char*>(&property_smallshadow.IsChecked),
315 sizeof(bool));
316 fs.write(reinterpret_cast<const char*>(&property_statis.IsChecked),
317 sizeof(bool));
318 fs.write(reinterpret_cast<const char*>(&property_statue.IsChecked),
319 sizeof(bool));
320 fs.write(reinterpret_cast<const char*>(&property_watersprite.IsChecked),
321 sizeof(bool));
322
323 fs.write(reinterpret_cast<const char*>(&property_prize.Text),
324 sizeof(uint8_t));
325 fs.write(reinterpret_cast<const char*>(&property_palette.Text),
326 sizeof(uint8_t));
327 fs.write(reinterpret_cast<const char*>(&property_oamnbr.Text),
328 sizeof(uint8_t));
329 fs.write(reinterpret_cast<const char*>(&property_hitbox.Text),
330 sizeof(uint8_t));
331 fs.write(reinterpret_cast<const char*>(&property_health.Text),
332 sizeof(uint8_t));
333 fs.write(reinterpret_cast<const char*>(&property_damage.Text),
334 sizeof(uint8_t));
335
336 fs.write(sprName.c_str(), sprName.size() + 1);
337
338 fs.write(reinterpret_cast<const char*>(userRoutines.size()), sizeof(int));
339 for (const UserRoutine& userR : userRoutines) {
340 fs.write(userR.name.c_str(), userR.name.size() + 1);
341 fs.write(userR.code.c_str(), userR.code.size() + 1);
342 }
343
344 fs.write(reinterpret_cast<const char*>(&property_sprid.Text),
345 sizeof(property_sprid.Text));
346
347 fs.close();
348 }
349
350 return absl::OkStatus();
351 }
352
353 std::string sprName;
354 std::vector<AnimationGroup> animations;
355 std::vector<UserRoutine> userRoutines;
357
379
386
388};
389
390} // namespace zsprite
391} // namespace editor
392} // namespace app
393} // namespace yaze
394
395#endif // YAZE_APP_EDITOR_SPRITE_ZSPRITE_H
unsigned short ushort
Definition constants.h:119
Definition common.cc:21
AnimationGroup(uint8_t fs, uint8_t fe, uint8_t fsp, std::string fn)
Definition zsprite.h:47
OamTile(uint8_t x, uint8_t y, bool mx, bool my, uint16_t id, uint8_t pal, bool s, uint8_t p)
Definition zsprite.h:23
std::vector< AnimationGroup > Frames
Definition zsprite.h:66
std::vector< UserRoutine > user_routines
Definition zsprite.h:67
UserRoutine(std::string n, std::string c)
Definition zsprite.h:58
std::vector< UserRoutine > userRoutines
Definition zsprite.h:355
SpriteProperty property_imperviousmelee
Definition zsprite.h:369
SpriteProperty property_imperviousarrow
Definition zsprite.h:368
SpriteProperty property_collisionlayer
Definition zsprite.h:360
absl::Status Save(const std::string &filename)
Definition zsprite.h:229
std::vector< AnimationGroup > animations
Definition zsprite.h:354
SpriteProperty property_deflectprojectiles
Definition zsprite.h:364
SpriteProperty property_deflectarrows
Definition zsprite.h:363
absl::Status Load(const std::string &filename)
Definition zsprite.h:77