forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

embeding images/fonts into the elf file

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
methos3



Joined: 01 Feb 2008
Posts: 89

PostPosted: Sat Aug 15, 2009 11:06 pm    Post subject: embeding images/fonts into the elf file Reply with quote

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:

Code:

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;
}

Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sun Aug 16, 2009 12:58 am    Post subject: Reply with quote

The problem is

Code:
memcpy(&gsFont->Texture->Mem, &buffer[m], size);


It should be

Code:
memcpy(gsFont->Texture->Mem, &buffer[m], size);


->Mem is a variable pointing to the memory, not an array that is the memory.

Also, the m += 4; after that memcpy should be m+= size;
Back to top
View user's profile Send private message AIM Address
methos3



Joined: 01 Feb 2008
Posts: 89

PostPosted: Sun Aug 16, 2009 9:39 am    Post subject: Reply with quote

Ow, it was it...
I can explain: I didn't pay any attention while doing my custom font loading functions, I was only using ctrl+c and ctrl+v, this explains the lasting '&' and the m+=4 (instead of m+= size). But the m+=4 doesn't matter, since is's after the fclose() call, so I just left +=4.

Now it works, but... a weird thing is happening:
*the left part of each char is being cut (just some pixels) when using arial
*when using lucida, instead of each char, a strange "thing" appears
*I succesfully loaded lucida font from the memory, and from mass:lucida.fnt(using the normal load function), and both worked the same way (the strange "things", none of them being a char)
*I succesfully loaded Arial from the memory, and from the mass:arial.fnt, but the PS2 hangs when I try to write something on the screen using the one loaded from the pendrive.

Maybe it's a problem with the GSKit, I will end up having to download the latest version : (

Well, my doubt was solved, thanks J.F.!
Later!

[edit]
ok... if I don't load mass:lucida.fnt, mass:arial.fnt will work, the same way the embeded arial works. now I've gotta discover what's going on with lucida
and... the black square around the characters won't disappear, even using any king of alpha blending, but it's ok
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group