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