Skip to content

Commit f13b3ca

Browse files
committed
build command makes yar from recipe + images
1 parent 1f4ab85 commit f13b3ca

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

include/common.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ char *FileLoadAsString(const char *fn);
1616
bool FileIsLoaded(const char *fn, const void *data);
1717
char *FileGetDirectory(const char *fn);
1818

19+
void FilePutBE32(FILE *file, uint32_t value);
20+
1921
uint32_t U32read(const void *src);
2022

2123
char *Strdup(const char *str);

include/recipe.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct RecipeItem
1818
int height;
1919
enum n64texconv_fmt fmt;
2020
enum n64texconv_bpp bpp;
21+
unsigned int endOffset;
2122
};
2223

2324
struct Recipe
@@ -27,6 +28,7 @@ struct Recipe
2728
char *yarName;
2829
char *imageDir;
2930
struct RecipeItem *head;
31+
int count;
3032
};
3133

3234
struct Recipe *RecipeRead(const char *filename);

src/common.c

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ bool FileIsLoaded(const char *fn, const void *data)
6262
return true;
6363
}
6464

65+
void FilePutBE32(FILE *file, uint32_t value)
66+
{
67+
fputc(value >> 24, file);
68+
fputc(value >> 16, file);
69+
fputc(value >> 8, file);
70+
fputc(value >> 0, file);
71+
}
72+
6573
char *FileGetDirectory(const char *fn)
6674
{
6775
char *out = Strdup(fn);

src/recipe.c

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct Recipe *RecipeRead(const char *filename)
5858
int width;
5959
int height;
6060

61+
recipe->count += 1;
62+
6163
assert(this);
6264

6365
if (sscanf(tmp, "%dx%d,%[^,]", &width, &height, fmt) != 3)

src/z64yartool.c

+88
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
#include "common.h"
1818
#include "yar.h" // from z64compress
19+
#include "yaz.h" // from z64compress
1920
#include "n64texconv.h" // from z64convert
2021
#include "recipe.h"
2122
#include "stb_image_write.h"
23+
#include "stb_image.h"
2224

2325
struct YarEntry
2426
{
@@ -180,6 +182,92 @@ static int YarDump(const char *input)
180182

181183
static int YarBuild(const char *input)
182184
{
185+
struct Recipe *recipe = RecipeRead(input);
186+
void *buffer = malloc(512 * 1024); // 512 KiB is plenty
187+
void *yazCtx = yazCtx_new();
188+
FILE *out;
189+
unsigned int rel;
190+
191+
assert(buffer);
192+
assert(recipe);
193+
194+
if (!(out = fopen(recipe->yarName, "wb")))
195+
{
196+
fprintf(stderr, "failed to open '%s' for writing\n", recipe->yarName);
197+
exit(EXIT_FAILURE);
198+
}
199+
200+
// header
201+
FilePutBE32(out, (recipe->count + 1) * sizeof(uint32_t));
202+
for (int i = 0; i < recipe->count; ++i)
203+
FilePutBE32(out, 0);
204+
rel = ftell(out);
205+
206+
for (struct RecipeItem *this = recipe->head; this; this = this->next)
207+
{
208+
const char *imgFn = this->imageFilename;
209+
const char *errmsg = 0;
210+
void *pix;
211+
int w;
212+
int h;
213+
int unused;
214+
unsigned int sz;
215+
216+
// load image
217+
if (!(pix = stbi_load(imgFn, &w, &h, &unused, STBI_rgb_alpha)))
218+
{
219+
fprintf(stderr, "failed to load image '%s'\n", imgFn);
220+
exit(EXIT_FAILURE);
221+
}
222+
223+
// assert no size change
224+
if (this->width != w || this->height != h)
225+
{
226+
fprintf(stderr, "'%s' image unexpected dimensions\n", imgFn);
227+
exit(EXIT_FAILURE);
228+
}
229+
230+
// convert to n64 pixel format
231+
if ((errmsg = n64texconv_to_n64(pix, pix, 0, -1, this->fmt, this->bpp, w, h, &sz)))
232+
{
233+
fprintf(stderr, "'%s' conversion error: %s\n", imgFn, errmsg);
234+
exit(EXIT_FAILURE);
235+
}
236+
237+
// compress
238+
if (yazenc(pix, sz, buffer, &sz, yazCtx))
239+
{
240+
fprintf(stderr, "compression error\n");
241+
exit(EXIT_FAILURE);
242+
}
243+
244+
// write
245+
if (fwrite(buffer, 1, sz, out) != sz)
246+
{
247+
fprintf(stderr, "error writing to file '%s'\n", recipe->yarName);
248+
exit(EXIT_FAILURE);
249+
}
250+
251+
// alignment
252+
while (ftell(out) & 3)
253+
fputc(0, out);
254+
255+
// used for header later
256+
this->endOffset = ftell(out) - rel;
257+
258+
// cleanup
259+
free(pix);
260+
}
261+
262+
// header
263+
fseek(out, 4, SEEK_SET);
264+
for (struct RecipeItem *this = recipe->head; this; this = this->next)
265+
FilePutBE32(out, this->endOffset);
266+
267+
fclose(out);
268+
RecipeFree(recipe);
269+
yazCtx_free(yazCtx);
270+
free(buffer);
183271
return EXIT_SUCCESS;
184272
}
185273

0 commit comments

Comments
 (0)