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 > Loading gfx from FAT, source code or tutorial?[solved]

#166493 - Emmanuel - Sun Feb 08, 2009 9:31 pm

Can someone post some code here loading a grit gfx from FAT? I'm TOTALLY clueless how to do it myself... the libnds example just shows the directory structure... any tutorial would also be welcome... I don't even know how I should make my directory structure to work properly...

Last edited by Emmanuel on Tue Feb 10, 2009 1:39 pm; edited 1 time in total

#166497 - ChronoDK - Sun Feb 08, 2009 10:23 pm

Maybe this will help you get started. This is how I load sprites from FAT. I'm sure someone is able to write better and faster code - and if so, please show us :)

Code:

   //The sprite and palette memory addresses
   u16* spritePalMain = &SPRITE_PALETTE[m_palIndex * 16];
   u16* spriteMemMain = &SPRITE_GFX[m_tileIndex * 16];

   //Read sprite palette file to memory
   infile.open( ("/coolsprite.pal.bin").c_str(), std::ifstream::binary );
   for (int i = 0; i < 16; i++) {
      u16 x = 0;
      infile.read((char*)&x, 2);
      spritePalMain[i] = x;
   }
   infile.close();
   infile.clear();
   
   //Read sprite data from file to sprite memory
   infile.open( ("/coolsprite.img.bin").c_str(), std::ifstream::binary );
   infile.seekg(0, std::ifstream::end);
   long size = infile.tellg();
   infile.seekg(0);
    for (int i = 0; i < size/2; i++) {
      u16 x = 0;
      infile.read((char*)&x, 2);
      spriteMemMain[i] = x;
   }
   infile.close();
   infile.clear();

#166524 - Emmanuel - Mon Feb 09, 2009 11:40 am

I stilll don't get how opening a file lets the program know it's variables...
for example:
I include
#include "title_up.h"

and in the code I:

Code:
dmaCopyHalfWords(DMA_CHANNEL,
                     title_upBitmap, /* This variable is generated for us by
                                       * grit. */
                     (uint8 *)BG_BMP_RAM_SUB(0), /* Our address for main
                                               * background 3 */
                     title_upBitmapLen); /* This length (in bytes) is generated
                                           * from grit. */


How does reading from fat tells the program those variables:
title_upBitmap and title_upBitmapLen

please help, I don't want to use PAlib due to something that is so basic...

which includes will I need? can someone post more source? like perhaps a full source file that loads gfx from FAT? and generates a working .nds... which file type should I have grit export into?

#166535 - elhobbs - Mon Feb 09, 2009 2:23 pm

that is because it doesn't. ChronoDK posted code that shows reading from a file. your example is for using grit or a similiar application that preprocesses the graphic file and embeds it in the nds file as data - the header file is generated by the tool.

#166556 - ChronoDK - Mon Feb 09, 2009 6:29 pm

I can give you more code. These are just snippets taken out - some includes and hardware init functions are missing, but inserting it into your own project should be doable. Just place the files you want to load at the root of your flashcart. The ones I'm using below are 8-bit bitmaps converted with grit.

Code:

#include <fat>
#include <fstream>

   fatInitDefault()

   videoSetMode( MODE_5_2D | DISPLAY_BG2_ACTIVE | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_1D );
   vramSetBankA(VRAM_A_MAIN_BG);
   vramSetBankB(VRAM_B_MAIN_BG);

   //File path
   std::string path = "/";

   //Main BG2
   int bg2 = bgInit(2, BgType_Bmp8, BgSize_B8_512x512, 0, 0);
   bgSetPriority(bg2, 1 );
   REG_BG2PA = 1 << 8; REG_BG2PB = 0; REG_BG2PC = 0; REG_BG2PD = 1 << 8; REG_BG2X = 0; REG_BG2Y = 0;

   //Read BG2 palette file to memory
   std::ifstream infile( (path + "my512x512bitmap.pal.bin").c_str(), std::ifstream::binary );
   u16 color = 0;
   for (int i = 0; i < 256; i++) {
      infile.read((char*)&color, 2);
      BG_PALETTE[i] = color;
   }
   infile.close();
   infile.clear();

   //Read 8-bit 512x512 256 color bitmap from file
   u16* buffer = (u16*)bgGetGfxPtr( bg2 );
   infile.open( (path + "my512x512bitmap.img.bin").c_str(), std::ifstream::binary );
   for(int iy = 0; iy < 512; iy++) {
      for(int ix = 0; ix < 512 / 2; ix++) {
         u16 x = 0;
         infile.read((char*)&x, 2);
         buffer[ix + iy * 256] = x;
      }
   }
   infile.close();
   infile.clear();

#166580 - Emmanuel - Tue Feb 10, 2009 1:39 pm

Thanks, now I got it