gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > .bmp to sprite?

#144573 - spinal_cord - Sun Nov 04, 2007 2:09 am

Doesn't matter, fixed it. I have left the [fixed] code for anyone who wants it though. It loads a 16 colour 32x32 .bmp to a sprite.
Code:

// Includes
#include <PA9.h>       // Include for PA_Lib

void load_bmp_4_sprite(u8 sprite_number, char*filename);

unsigned short sprite0_Pal[16]; // temp palette for sprite
unsigned char sprite0_Sprite[32*32]; // temp image for sprite


// Function: main()
int main(int argc, char ** argv)
{
   PA_Init();    // Initializes PA_Lib
   PA_InitVBL(); // Initializes a standard VBL
   
   fatInitDefault(); //Initialise fat library

   load_bmp_4_sprite(0,"icon.bmp");// load icon.bmp to sprite array
   PA_CreateSprite(0, 0, (void*)sprite0_Sprite, // Blank sprite !
               OBJ_SIZE_32X32, // Sprite size
               1, 0, // Sprite palette number
               100, 0); // In the upper left corner

   PA_LoadSpritePal(0, // Screen
               0, // Palette number
               (void*)sprite0_Pal);   // Palette name
 

   // Infinite loop to keep the program running
   while (1)
   {
   
      PA_WaitForVBL();
   }
   
   return 0;
} // End of main()



void load_bmp_4_sprite(u8 sprite_number, char *filename)
{
    FILE* testRead = fopen (filename, "rb"); //rb = read
   if(testRead)
   {
   fseek(testRead, 54, SEEK_SET); // palette is 54 bytes from start of file
   unsigned char col[4];
   int temp;
   for(temp=0; temp<16; temp++) // read the palette info
   {
      fread(col, sizeof(char), 4, testRead);      
      sprite0_Pal[temp]=PA_RGB(col[2]>>3,col[1]>>3,col[0]>>3);
   }   
   u8 row[16*32];
   fread(row,sizeof(u8),512,testRead); // read the full icon directly from the file
   int tile,x,y,tempx=0,tempy=0,width=16,smp=0;   
   for (tile = 0; tile < 16; tile++){
   for (y = 0; y < 8; y++){    
      for (x = 0; x <4; x++) {
         temp = row[tempx + x + (31-(tempy+y))*width];
         sprite0_Sprite[(tile << 6) + (x*2) + (y << 3)] = (temp&240)>>4;
         sprite0_Sprite[1+(tile << 6) + (x*2) + (y << 3)] = temp&15;
         smp++;
      }
   }// x
   tempx += 4;
   if (tempx >= width) {
      tempy += 8;
      tempx = 0;
   }// y
}// tile

   
   fclose(testRead);
   PA_CreateSprite(0, sprite_number, (void*)sprite0_Sprite, // Blank sprite !
               OBJ_SIZE_32X32, // Sprite size
               1, 0, // Sprite palette number
               100, 0); // In the upper left corner

   PA_LoadSpritePal(0, // Screen
               0, // Palette number
               (void*)sprite0_Pal);   // Palette name

   }// end if testread   

}

_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage