Skip to content

Commit b112aeb

Browse files
committed
Add latest changes to create gfx .dat files,
add s3dat_setindexlen and s3dat_seqindexlen to s3dat.h, add gen_gui example that use this changes to generate completly new dat files
1 parent 4b4bb94 commit b112aeb

File tree

6 files changed

+173
-14
lines changed

6 files changed

+173
-14
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ target_link_libraries(s3dat s3util)
3434
add_executable(cli cli.c)
3535
target_link_libraries(cli s3dat)
3636

37+
add_executable(gen_gui gen_gui.c)
38+
target_link_libraries(gen_gui s3dat m)
39+
3740
add_executable(invert invert.c)
3841
target_link_libraries(invert s3dat)
3942

cli.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ int main() {
2727

2828
int i = 0;
2929
struct dirent* ent;
30-
while((gfx_dir && (ent = readdir(gfx_dir)) != NULL) || (snd_dir && (ent = readdir(snd_dir)) != NULL)) {
30+
bool gfx = false;
31+
32+
while((gfx_dir && (ent = readdir(gfx_dir)) != NULL && (gfx = true)) || (snd_dir && (ent = readdir(snd_dir)) != NULL && (gfx = false))) {
3133
if(ent->d_name[0] == '.') continue;
3234
if(i != 0) printf("\n");
3335

3436
int len = strlen(ent->d_name);
3537
char name[len+5];
36-
if(len > 15) {
38+
if(gfx) {
3739
name[0] = 'G';
3840
name[1] = 'F';
3941
name[2] = 'X';

gen_gui.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <s3dat_ext.h>
2+
#include <math.h>
3+
4+
void create_color_gui_handler(s3dat_extracthandler_t* me, s3dat_res_t* res, s3util_exception_t** throws) {
5+
res->res = s3dat_new_bitmap(me->parent, 64, 64, throws);
6+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(me->parent), throws, __FILE__, __func__, __LINE__);
7+
8+
s3util_color_t color = {0, 0, 0, 0xFF};
9+
for(uint16_t x = 0;x != 64;x++) {
10+
for(uint16_t y = 0;y != 64;y++) {
11+
if(x != 0) color.red = abs(32-x)*8;
12+
color.green = (cos(x/8.0*M_PI)+1)*127;
13+
color.blue = (cos(y/4.0*M_PI)+1)*127;
14+
s3dat_bmpdata(res->res)[y*64+x] = color;
15+
}
16+
}
17+
}
18+
19+
s3util_exception_t* ex;
20+
int main() {
21+
s3dat_t* datfile = s3dat_new_malloc();
22+
23+
s3dat_setindexlen(datfile, s3dat_gui, 1);
24+
25+
s3dat_extracthandler_t* createhandler = s3dat_new_exhandler(datfile, &ex);
26+
s3util_catch_exception(&ex);
27+
28+
s3dat_extracthandler_t* packhandler = s3dat_new_exhandler(datfile, &ex);
29+
s3util_catch_exception(&ex);
30+
31+
createhandler->call = create_color_gui_handler;
32+
packhandler->call = s3dat_pack_handler;
33+
34+
s3dat_add_extracthandler(datfile, createhandler);
35+
s3dat_add_extracthandler(datfile, packhandler);
36+
37+
s3dat_writefile_name(datfile, "GFX/color_gui.dat", &ex);
38+
s3util_catch_exception(&ex);
39+
40+
s3dat_delete(datfile);
41+
}

src/s3dat.h

+5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ void s3dat_init_name(s3dat_t* handle, char* name); //name must be vaild until s3
102102
void s3dat_init_fd(s3dat_t* handle, uint32_t* file); //file must be vaild until s3dat_readfile/s3dat_writefile has ended
103103
bool s3dat_init_ioset(s3dat_t* handle, void* io_arg, s3util_ioset_t* ioset, bool use_openclose_func); // io_arg mst be valid until s3dat_readfile/s3dat_writefile has ended
104104

105+
// use this if you want to write a dat file
106+
// never call s3dat_readfile or s3dat_extract on a s3dat_t that has or will call this functions, it will cause memory leaks and undefinded behavior
107+
bool s3dat_setindexlen(s3dat_t* handle, s3dat_content_type type, uint16_t indexlen);
108+
bool s3dat_setseqindexlen(s3dat_t* handle, s3dat_content_type type, uint16_t index, uint16_t seqindexlen); // only strings can use more than 8 bit.
109+
105110
void s3dat_writefile(s3dat_t* handle, s3util_exception_t** throws);
106111
void s3dat_readfile(s3dat_t* handle, s3util_exception_t** throws);
107112

src/structure.c

+71
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,77 @@ uint32_t s3dat_seqaddr(s3dat_t* handle, uint16_t seq, uint32_t index, s3dat_cont
578578
return 0;
579579
}
580580
}
581+
bool s3dat_setindexlen(s3dat_t* handle, s3dat_content_type type, uint16_t indexlen) {
582+
s3dat_index_t* index = NULL;
583+
s3dat_seq_index_t* seqindex = NULL;
584+
585+
switch(type) {
586+
case s3dat_settler:
587+
seqindex = handle->settler_index;
588+
break;
589+
case s3dat_torso:
590+
seqindex = handle->torso_index;
591+
break;
592+
case s3dat_shadow:
593+
seqindex = handle->shadow_index;
594+
break;
595+
case s3dat_string:
596+
seqindex = handle->string_index;
597+
break;
598+
case s3dat_landscape:
599+
index = handle->landscape_index;
600+
break;
601+
case s3dat_gui:
602+
index = handle->gui_index;
603+
break;
604+
case s3dat_animation:
605+
index = handle->animation_index;
606+
break;
607+
case s3dat_palette:
608+
index = handle->palette_index;
609+
break;
610+
default:
611+
break;
612+
}
613+
614+
if(index) {
615+
index->len = indexlen;
616+
} else if(seqindex) {
617+
seqindex->sequences = s3util_alloc_func(s3dat_memset(handle), sizeof(s3dat_index_t)*indexlen, NULL);
618+
if(seqindex->sequences == NULL) return false;
619+
seqindex->len = indexlen;
620+
} else {
621+
return false;
622+
}
623+
624+
return true;
625+
}
626+
627+
bool s3dat_setseqindexlen(s3dat_t* handle, s3dat_content_type type, uint16_t index, uint16_t seqindexlen) {
628+
s3dat_seq_index_t* seqindex = NULL;
629+
630+
switch(type) {
631+
case s3dat_settler:
632+
seqindex = handle->settler_index;
633+
break;
634+
case s3dat_torso:
635+
seqindex = handle->torso_index;
636+
break;
637+
case s3dat_shadow:
638+
seqindex = handle->shadow_index;
639+
break;
640+
case s3dat_string:
641+
seqindex = handle->string_index;
642+
break;
643+
default:
644+
return false;
645+
break;
646+
}
647+
if(seqindex->len <= index) return false;
648+
649+
seqindex->sequences[index].len = type == s3dat_string ? index : (index&0xFF);
650+
return true;
651+
}
581652

582653
uint32_t s3dat_palette_width(s3dat_t* handle) {
583654
return handle->palette_line_length;

src/write.c

+49-12
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ void s3dat_write_packed(s3dat_t* handle, s3dat_res_t* res, uint32_t* pos, uint32
121121
s3dat_unref(res->res);
122122
}
123123

124-
void s3dat_internal_export_seq_index(s3dat_t* handle, s3dat_seq_index_t* index, uint32_t* seq_pos, uint32_t pos, uint32_t* append_pos, s3util_exception_t** throws) {
125-
s3dat_res_t res = {0, 0, index->type, NULL};
124+
void s3dat_internal_export_seq_index(s3dat_t* handle, s3dat_seq_index_t* index, s3dat_content_type type, uint32_t* seq_pos, uint32_t pos, uint32_t* append_pos, s3util_exception_t** throws) {
125+
s3dat_res_t res = {0, 0, type, NULL};
126126

127127
for(uint16_t s = 0;s != index->len;s++) {
128128
res.first_index = s;
@@ -144,10 +144,10 @@ void s3dat_internal_export_seq_index(s3dat_t* handle, s3dat_seq_index_t* index,
144144
}
145145
}
146146

147-
void s3dat_internal_export_index(s3dat_t* handle, s3dat_index_t* index, uint32_t pos, uint32_t* append_pos, s3util_exception_t** throws) {
147+
void s3dat_internal_export_index(s3dat_t* handle, s3dat_index_t* index, s3dat_content_type type, uint32_t pos, uint32_t* append_pos, s3util_exception_t** throws) {
148148
uint32_t data_positions[index->len];
149149

150-
s3dat_res_t res = {0, 0, index->type, NULL};
150+
s3dat_res_t res = {0, 0, type, NULL};
151151

152152
for(uint16_t i = 0;i != index->len;i++) {
153153
res.first_index = i;
@@ -156,13 +156,50 @@ void s3dat_internal_export_index(s3dat_t* handle, s3dat_index_t* index, uint32_t
156156
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
157157
}
158158

159-
s3dat_internal_write_index(handle, pos, index->type, index->len, data_positions, throws);
159+
s3dat_internal_write_index(handle, pos, type, index->len, data_positions, throws);
160160
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
161161

162162
s3dat_internal_seek_func(handle, *append_pos, S3UTIL_SEEK_SET, throws);
163163
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
164164
}
165165

166+
void s3dat_writefile_name(s3dat_t* handle, char* name, s3util_exception_t** throws) {
167+
s3dat_init_name(handle, name);
168+
s3dat_writefile(handle, throws);
169+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
170+
}
171+
172+
void s3dat_writefile_fd(s3dat_t* handle, uint32_t* file, s3util_exception_t** throws) {
173+
s3dat_init_fd(handle, file);
174+
s3dat_writefile(handle, throws);
175+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
176+
}
177+
178+
void s3dat_writefile_func(s3dat_t* handle, void* arg,
179+
bool (*read_func) (void*, void*, size_t),
180+
bool (*write_func) (void*, void*, size_t),
181+
size_t (*size_func) (void*),
182+
size_t (*pos_func) (void*),
183+
bool (*seek_func) (void*, uint32_t, int),
184+
void* (*open_func) (void*, bool),
185+
void (*close_func) (void*),
186+
void* (*fork_func) (void*),
187+
s3util_exception_t** throws) {
188+
s3dat_init_func(handle, arg, read_func, write_func, size_func, pos_func, seek_func, open_func, close_func, fork_func);
189+
s3dat_writefile(handle, throws);
190+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
191+
}
192+
193+
void s3dat_writefile_ioset(s3dat_t* handle, void* io_arg, s3util_ioset_t* ioset, bool use_openclose_func, s3util_exception_t** throws) {
194+
if(!s3dat_init_ioset(handle, io_arg, ioset, use_openclose_func)) {
195+
s3util_throw(s3dat_memset(handle), throws, S3UTIL_EXCEPTION_IOSET, __FILE__, __func__, __LINE__);
196+
return;
197+
}
198+
199+
s3dat_writefile(handle, throws);
200+
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
201+
}
202+
166203
void s3dat_writefile(s3dat_t* handle, s3util_exception_t** throws) {
167204
if(s3dat_ioset(handle)->open_func != NULL) s3dat_ioset(handle)->arg = s3dat_ioset(handle)->open_func(s3dat_ioset(handle)->arg, true);
168205

@@ -249,25 +286,25 @@ void s3dat_writefile(s3dat_t* handle, s3util_exception_t** throws) {
249286
s3dat_internal_seek_func(handle, append_pos, S3UTIL_SEEK_SET, throws);
250287
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
251288

252-
s3dat_internal_export_index(handle, handle->landscape_index, landscape_pos, &append_pos, throws);
289+
s3dat_internal_export_index(handle, handle->landscape_index, s3dat_landscape, landscape_pos, &append_pos, throws);
253290
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
254291

255-
s3dat_internal_export_index(handle, handle->gui_index, gui_pos, &append_pos, throws);
292+
s3dat_internal_export_index(handle, handle->gui_index, s3dat_gui, gui_pos, &append_pos, throws);
256293
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
257294

258-
s3dat_internal_export_seq_index(handle, handle->settler_index, settler_seq_pos, settler_pos, &append_pos, throws);
295+
s3dat_internal_export_seq_index(handle, handle->settler_index, s3dat_settler, settler_seq_pos, settler_pos, &append_pos, throws);
259296
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
260297

261-
s3dat_internal_export_seq_index(handle, handle->torso_index, torso_seq_pos, torso_pos, &append_pos, throws);
298+
s3dat_internal_export_seq_index(handle, handle->torso_index, s3dat_torso, torso_seq_pos, torso_pos, &append_pos, throws);
262299
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
263300

264-
s3dat_internal_export_seq_index(handle, handle->shadow_index, shadow_seq_pos, shadow_pos, &append_pos, throws);
301+
s3dat_internal_export_seq_index(handle, handle->shadow_index, s3dat_shadow, shadow_seq_pos, shadow_pos, &append_pos, throws);
265302
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
266303

267-
s3dat_internal_export_index(handle, handle->animation_index, animation_pos, &append_pos, throws);
304+
s3dat_internal_export_index(handle, handle->animation_index, s3dat_animation, animation_pos, &append_pos, throws);
268305
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
269306

270-
s3dat_internal_export_index(handle, handle->palette_index, palette_pos, &append_pos, throws);
307+
s3dat_internal_export_index(handle, handle->palette_index, s3dat_palette, palette_pos, &append_pos, throws);
271308
S3UTIL_HANDLE_EXCEPTION(s3dat_memset(handle), throws, __FILE__, __func__, __LINE__);
272309

273310
s3dat_internal_seek_func(handle, 48, S3UTIL_SEEK_SET, throws);

0 commit comments

Comments
 (0)