embeding images/fonts into the elf file
Posted: Sat Aug 15, 2009 11:06 pm
Hello, me again :)
I was needing to put some fonts and textures into my elf file, and I did it in that way:
I took the original code of the font/texture loading functions and exchanged the fread()/fseek() calls to memcpy()/increment/decrement counter calls, using a counter to simulate the file position counter.
I used bin2c to convert the .fnt and .bmp files into a char array.
It worked great for bmp textures, but I wasn't so lucky with the fonts...
I didn't want to use romfs, and instead I preferred to use this method, that should work. (PCSX2 freezes when I try to use them :(, but work great with the modified texture ones... )
Look at the modified font loading functions, with the original parts commented:
I was needing to put some fonts and textures into my elf file, and I did it in that way:
I took the original code of the font/texture loading functions and exchanged the fread()/fseek() calls to memcpy()/increment/decrement counter calls, using a counter to simulate the file position counter.
I used bin2c to convert the .fnt and .bmp files into a char array.
It worked great for bmp textures, but I wasn't so lucky with the fonts...
I didn't want to use romfs, and instead I preferred to use this method, that should work. (PCSX2 freezes when I try to use them :(, but work great with the modified texture ones... )
Look at the modified font loading functions, with the original parts commented:
Code: Select all
GSFONT *gsKit_init_font_buffer(u8 type, unsigned char *path, int size)
{
GSFONT *gsFont = calloc(1,sizeof(GSFONT));
gsFont->Texture = calloc(1,sizeof(GSTEXTURE));
if(path)
{
gsFont->Path = path;//calloc(1,strlen(path));
/*strcpy(gsFont->Path, path);*/
}
gsFont->Type = type;
return gsFont;
}
int gsKit_font_buffer_upload(GSGLOBAL *gsGlobal, GSFONT *gsFont, int size)
{
int i;
if( gsFont->Type == GSKIT_FTYPE_FNT )
{
if( gsKit_texture_fnt_buffer(gsGlobal, gsFont, size) == -1 )
{
printf("Error uploading font!\n");
return -1;
}
gsFont->Additional=malloc( 0x100 );
for (i=0; i<0x100; i++) {
gsFont->Additional[i] = gsFont->CharWidth;
}
return 0;
}
else return -1;
}
int gsKit_texture_fnt_buffer(GSGLOBAL *gsGlobal, GSFONT *gsFont, int buffersize)
{
u32 *mem;
int size = 0;
int vramsize = 0;
int i, m=0;
char* buffer = gsFont->Path;
//FILE* File = fopen(gsFont->Path, "r");
/*if (File == NULL)
{
printf("Failed to load font: %s\n", gsFont->Path);
return -1;
}
fseek(File, 4, SEEK_SET);*/
m=4;
/*if(fread(&gsFont->Texture->Width, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->Texture->Width, &buffer[m], 4);
m+=4;
/*if(fread(&gsFont->Texture->Height, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->Texture->Height, &buffer[m], 4);
m+=4;
/*if(fread(&gsFont->Texture->PSM, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->Texture->PSM, &buffer[m], 4);
m+=4;
/*if(fread(&gsFont->HChars, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->HChars, &buffer[m], 4);
m+=4;
/*if(fread(&gsFont->VChars, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->VChars, &buffer[m], 4);
m+=4;
/*if(fread(&gsFont->CharWidth, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->CharWidth, &buffer[m], 4);
m+=4;
/*if(fread(&gsFont->CharHeight, 4, 1, File) <= 0)
{
printf("Could not load font: %s\n", gsFont->Path);
return -1;
}*/
memcpy(&gsFont->CharHeight, &buffer[m], 4);
m+=4;
//fseek(File, 288, SEEK_SET);
m=288;
gsFont->Texture->Filter = GS_FILTER_NEAREST;
size = gsKit_texture_size_ee(gsFont->Texture->Width, gsFont->Texture->Height, gsFont->Texture->PSM);
vramsize = gsKit_texture_size(gsFont->Texture->Width, gsFont->Texture->Height, gsFont->Texture->PSM);
gsFont->Texture->Mem = memalign(128, size);
gsFont->Texture->Vram = gsKit_vram_alloc(gsGlobal, vramsize, GSKIT_ALLOC_USERBUFFER);
if(gsFont->Texture->Vram == GSKIT_ALLOC_ERROR)
{
printf("VRAM Allocation Failed. Will not upload texture.\n");
return -1;
}
/*if(fread(gsFont->Texture->Mem, size, 1, File) <= 0)
{
printf("Font might be bad: %s\n", gsFont->Path);
}
fclose(File);*/
memcpy(&gsFont->Texture->Mem, &buffer[m], size);
m+=4;
if (gsFont->Texture->PSM != 0) {
printf("Unsupported fnt PSM %d\n", gsFont->Texture->PSM);
}
mem = (u32*)gsFont->Texture->Mem;
for (i=0; i<size/4; i++) {
if (mem[i] == 0xFF00FFFF) {
mem[i] = 0;
} else {
u32 c = (mem[i] & 0x00FF0000) >> 16;
mem[i] = 0x80000000 | (c) | (c<<8) | (c<<16);
}
}
gsKit_texture_upload(gsGlobal, gsFont->Texture);
free(gsFont->Texture->Mem);
return 0;
}