Organize code structure, generate CBMD files without premade headers.

This commit is contained in:
Steveice10
2015-01-23 18:37:10 -08:00
parent 78ee395ea5
commit 9360d55bd5
13 changed files with 245 additions and 103 deletions

81
source/3ds/cbmd.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include "cbmd.h"
#include "lz11.h"
#include <string.h>
#include <malloc.h>
typedef struct {
char magic[4] = {'C', 'B', 'M', 'D'};
u32 zero = 0;
u32 cgfxOffsets[14] = {0};
u8 padding[0x44] = {0};
u32 cwavOffset = 0;
} CBMDHeader;
u8* build_cbmd_data(CBMD cbmd, u32* size, bool bnr) {
u32 headerSize = sizeof(CBMDHeader);
CBMDHeader header;
u8* compressedCGFXs[14] = {0};
u32 compressedCGFXSizes[14] = {0};
u32 offset = headerSize;
for(int i = 0; i < 14; i++) {
if(cbmd.cgfxs[i] != NULL) {
compressedCGFXs[i] = lz11_compress(cbmd.cgfxs[i], cbmd.cgfxSizes[i], &compressedCGFXSizes[i]);
header.cgfxOffsets[i] = offset;
offset += compressedCGFXSizes[i];
}
}
u32 pad = 0;
if(bnr) {
pad = 16 - (offset % 16);
offset += pad;
}
if(cbmd.cwav != NULL) {
header.cwavOffset = offset;
offset += cbmd.cwavSize;
}
u8* output = (u8*) malloc(offset);
u32 pos = 0;
memcpy(output + pos, &header, headerSize);
pos += headerSize;
for(int i = 0; i < 14; i++) {
if(compressedCGFXs[i] != NULL) {
memcpy(output + pos, compressedCGFXs[i], compressedCGFXSizes[i]);
free(compressedCGFXs[i]);
compressedCGFXs[i] = NULL;
pos += compressedCGFXSizes[i];
}
}
if(bnr) {
memset(output + pos, 0, pad);
pos += pad;
}
if(cbmd.cwav != NULL) {
memcpy(output + pos, cbmd.cwav, cbmd.cwavSize);
pos += cbmd.cwavSize;
}
if(size != NULL) {
*size = offset;
}
return output;
}
u8* build_cbmd(CBMD cbmd, u32* size) {
return build_cbmd_data(cbmd, size, false);
}
u8* build_bnr(CBMD cbmd, u32* size) {
return build_cbmd_data(cbmd, size, true);
}