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 sprites and/or tiles using libFat

#158018 - Polaris - Tue Jun 03, 2008 4:39 pm

I'm looking into using libFat so that I don't need to have all my assets loaded in RAM at all times. I don't think I'll be doing a project big enough to garauntee a file system anytime soon, but it's good to start getting a hang on it.

So I have been reading a bit on libFat and it seems pretty straight forward, what I don't get is how I load my images into sprite graphics memory or tile memory once I have any given file opened.

When using the cosnt array method, it is pretty easy because the file already has been procesed by another program, but from what I am understanding if I want to load a file using libFat I have to write the routine that will process the image so that the DS can use it, am I right here or I'm missing the point?

About libFat and emulators. I'm using NO$GBA, will I be running into any problems with the emulator not supporting a file system? Because I read something like that, but it wasn't too clear, at least for me.

#158028 - silent_code - Tue Jun 03, 2008 7:35 pm

i can't tell you much about libfat, because until know, although i did succeed to make it work with emus, i still use gbfs for my projects (it's easier for me due to the hardware i use.)

but whit loading, i think i can help.

if you have the desired file open, you can use standard "file reads" (i prefer the c style ones, e.g. fread(), although i'm mostly a oo programmer) to geht the file's contents.

of course you need to know how the data inside the file is organized. if your files are simple (raw binary) .bin files, it's easy: just read the amount of byts that are contained inside the file (you will have to know that beforehand, although it's also possible to find that out on runtime, but will increase loading times.)

if you really don't know how to work with files, i recommend first trying it on the pc.

hope that helped a bit and didn't confuse you too much. ;^)

happy coding! :^)

EDIT:random example found on cplusplus.com:
Code:
/* fread example: read a complete file */
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "myfile.bin" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}

_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158043 - Polaris - Tue Jun 03, 2008 10:15 pm

Actually that was helpfull, I always forget to check the most obvious places, like cplusplus.com (durh!).

So doing something like the obove example and also taking something from this tutorial
http://dev-scene.com/NDS/Tutorials_Day_3#Working_With_Graphics_Files
I should be able to load a bmp file, right?

#158049 - silent_code - Tue Jun 03, 2008 10:59 pm

i personally prefer the .tga format, because the header is less bloaty than .bmp's.

the tgaloader.cpp file of my demo contains some information about the format and what you need to know when dealing with it. so, if you like your information short and to the point, go for it. :^)

NOTE: don't use the provided function directly, as it's written to work with tepples' GBFS. but it should be easy to port back to standard file handling, as there are only two reads or so. :^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158057 - Polaris - Wed Jun 04, 2008 1:02 am

Thanks for the directions. Looks like I have another mini project on the horizon.

I'm shuddering just of thinking this might actually be fun :P

#158069 - Dwedit - Wed Jun 04, 2008 4:01 am

Just convert the graphics before you load them. Then you are reading native DS graphics.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#158081 - silent_code - Wed Jun 04, 2008 10:50 am

Dwedit is right, it's best to convert images (ect.) to a format that is close to the native "format", but for a learning experience and small demos, i think it's ok to use more general formats.

hint: texmex sounds like a converter, you should check it out! :^) (i haven't tried it myself, yet.)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158869 - Polaris - Thu Jun 19, 2008 10:37 pm

I'm back at this, had to take a short break.

Just want to make somethings clear, is there any other way other than this http://dev-scene.com/NDS/FCSR_Tutorial to test libFat under NO&GBA?

I'm using R4DS, do I need to follow this instrucctions http://dev-scene.com/NDS/Libfat_DLDI#DLDI_Walkthrough for my .nds to have access to external files, or does the R4 somehow manages that on it's own?

#158882 - dantheman - Fri Jun 20, 2008 5:41 am

You can use DLDIrc to automate the process of making an FCSR image, but you will want to use the updated program made by Dwedit, found at http://forum.gbadev.org/viewtopic.php?t=15369

Replace the "bfi.exe -f=%1 %2" line with "add_to_disk_image.exe %1 %2" in the build.bat folder to make it use the new program.

The R4 automatically patches with DLDI upon execution.

On a side note, when reading that wiki page it seemed awfully familiar until I realized that they had edited a portion of an article I wrote for another wiki. Good to see knowledge being passed around, though perhaps they should re-check the final bullet point before the "Notes" segment as it still vaguely references SNEmulDS.

#158892 - mattlacey - Fri Jun 20, 2008 10:28 am

Yeah the easiest way by far is to convert the files before hand, then they're ready to go.

Personally I use grit, which does all the conversion to tiles (if you want tiles other than bitmaps), with reduction - it finds which ones can just be flipped etc., and writes out both the tile image data into one file, and the map data into another. You can also spit the palette out into a seperate file if you want it.

I'm usually floating about in #dsdev, gimme a shout if you want some help with it

#158907 - silent_code - Fri Jun 20, 2008 3:07 pm

@ Dwedit / dantheman: Perfect, it works flawlessly - thanks a lot for the program / hint! :^D
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158909 - Polaris - Fri Jun 20, 2008 4:05 pm

Awesome as usual, I'll be trying this out soon.

#158984 - josath - Mon Jun 23, 2008 6:36 pm

dantheman wrote:
On a side note, when reading that wiki page it seemed awfully familiar until I realized that they had edited a portion of an article I wrote for another wiki. Good to see knowledge being passed around, though perhaps they should re-check the final bullet point before the "Notes" segment as it still vaguely references SNEmulDS.


I think I originally copied it from a site that encourages piracy, I figured they wouldn't mind :P

#158986 - dantheman - Mon Jun 23, 2008 6:47 pm

I didn't say I minded, just found it amusing, that's all. I was reading it and kept thinking "this sounds exactly like something I'd say!" until it dawned on me that I had written it. Besides, I didn't know about the experimental write-enabled FCSR DLDI file until seeing your version of the page, so I'm glad Polaris linked to it.

#159479 - Polaris - Tue Jul 01, 2008 9:11 pm

Hey again, been a bit lazy on this lately :P

I'm having a truly unexpected problem here, when I run build.bat to create the .img the program complains about not knowing what is IF, something which caught me completely off guard. Should I be running the program from a specific location or can I do it from anywhere I please?

Since I'm here I'll get a bit ahead of myself. Once I manage to patch my .nds properly, how should I change the MakeFile so that it does all this steps automatically each time I build my demo?

#159487 - tepples - Wed Jul 02, 2008 12:23 am

Polaris wrote:
when I run build.bat to create the .img the program complains about not knowing what is IF, something which caught me completely off guard.

What error message are you getting? Do you need help learning how to copy and paste an error message from a command prompt window?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#159502 - Polaris - Wed Jul 02, 2008 2:45 am

This is what i'm getting
Code:
./build.bat: line 2: IF: command not found
./build.bat: line 3: IF: command not found
./build.bat: line 4: syntax error near unexpected token `"tokens=3,4*"'
./build.bat: line 4: `for /f "tokens=3,4*" %%a in ( 'dir /w /s /-C %2 ^| findstr
 "File(s)"') do @set info=%%a'

I got that after removing the first 4 lines of the bat file which I assumed where there just to make the output look pretty. With those in the error is pretty similar
Code:
./build.bat: line 1: @echo: command not found
./build.bat: line 2: REM: command not found
./build.bat: line 3: REM: command not found
./build.bat: line 4: syntax error near unexpected token `('
./build.bat: line 4: `REM by Troy(GPF) http://gpf.dcemu.co.uk'

I really have no clue as to what is happening.

#159546 - thedopefish - Wed Jul 02, 2008 2:12 pm

You're trying to run build.bat with bash (the msys shell), instead of cmd.exe (the normal Windows command-line shell).
_________________
#include <sig.h>

#159557 - Polaris - Wed Jul 02, 2008 5:40 pm

Thanks, and obviously now i'm bumping into another issue I have no clue about. This is what is happening
Code:
Builds a FAT disk image from a given directory.
Missing operand
ECHO is deactivated.
300000 was not expected.

I played a bit with the bat file and after removing the first line that says echo off I got this
Code:
Builds a FAT disk image from a given directory.
IF "image.img" == "" goto ERROR
IF "fcsr" == "" goto ERROR
for /F "tokens=3,4*" %a in ('dir /w /s /-C fcsr | findstr "File(s)"') do @set info=%a set /A size="+/3"
Missing operand.
ECHO is deactivated.
300000 was not expected.

What I noticed is that in the bat file size should be set to %info%+%info%/3, but when I run the program apparently info isn't set and I assume that is why i'm getting the missing operand error, which would be logical as doing size="+/3" is just stupid. I'm also guessing the other error is about size not being set.

Well, atleast i'm not completely clueless, but still have no idea as to how to fix this.

#159568 - silent_code - Wed Jul 02, 2008 6:43 pm

Hey, what language is your OS?
Are you using GPF's latest FCSR (the one on his site?)
Are you using add_to_disk_image instead of BFI?

I am on a German XP(SP2) and I had a simmilar problem. Someone fixed that in the build.bat and now everything is fine.

This is my build.bat with all modifications and language independency (I guess):

Code:
@echo off
REM
REM based on davr http://blog.davr.org/ linux code and shell script
REM by Troy(GPF) http://gpf.dcemu.co.uk
REM

echo Builds a FAT disk image from a given directory.
IF "%1" == "" goto ERROR
IF "%2" == "" goto ERROR
for /f "tokens=3,4*" %%a in ( 'dir /w /s /-C %2 ^| tail -n 2 ^| head -n 1') do @set info=%%a
set /A size="%info%+64"
echo %size%

if /I %size% LSS 10000  ( set /A size="10000" )

echo Creating image
mkdosfs.exe %1 %size%
REM bfi.exe -f=%1 %2
add_to_disk_image.exe %1 %2
REM 'bless' the image so it is recognized by FCSR driver, and sets up SRAM overlay

echo Blessing image
bless.exe %1
echo "FAT12 image built as %1 from /%2"
goto :end

:ERROR
echo usage: build.bat name.img dirname
goto :end

:tobig
echo directory size to large , remove some files and try again
goto :end


:end

_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159589 - Polaris - Wed Jul 02, 2008 8:19 pm

I have a Spanish XP with SP2, i'm using add_to_disk_image.exe and I think I got the latest FCSR, I downloaded it something like 3 weeks ago.

#159593 - silent_code - Wed Jul 02, 2008 8:42 pm

Did you try the batch I posted? You should.
Your cmd shell doesn't have the "Files" entry, that is required by the batch you are using. Instead, it has the Spanish word for "Files". That's the language bug.

FCSR should be ok, anyways, but I don't know, if the language bug has been removed since the last time I checked. It might still be present.
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159596 - Polaris - Wed Jul 02, 2008 8:59 pm

Haven't tried it yet, but I will, thanks.

Edit: Worked like a charm.

Now all I need is for someone to guide me in the process of adding all this stuff into the MakeFile.

#159616 - Polaris - Thu Jul 03, 2008 1:21 am

By now I'm starting to feel a bit thick about this....but anyway. I managed to make all the patching necessary to have my file system running in NO$GBA, manually.

When I tried using DLDIrl to make things less cumbersome the resulting file, for some reason, doesn't work as the one patched manually. I think I did everything I should in the fcsrimage folder of DLDIrl. Replaced BFI.exe with add_to_disk_image.exe and made the necessary changes in the batch file.

The only thing I can think of that is different is that DLDIrc asks me to create a folder with the same name as the .nds to act as root for the file system. So before when opening a file I was doing
Code:
FILE *test = fopen("fat1:/orangeShuttle.tga", "rb");
and now i'm doing
Code:
FILE *test = fopen("fat1:/File_System/orangeShuttle.tga", "rb");
Yes my .nds is called File_System, pretty clever huh?

What could I be missing? I tried replacing the files DLDIrc uses with the ones I used but that didn't change much. The only thing I haven't checked yet is how the program uses padbin.exe. And I haven't checked it because I don't know where to find it.

#159667 - Polaris - Fri Jul 04, 2008 2:53 am

Ok got this working on both hardware and emulator, it was a really dumb mistake. Now back to more interesting things.

So using grit is the way to go when converting files to stuff more easily used in the DS, but just how should I use it when working with a file system? So far the only times I have used grit was from tutorials saying "Just do it" with no real explanation as to what is going on and specially never doing any file reading.

So let's say I set up my Makefiles to use grit on all the images I want to, what options I should be interested in if my intention is to read files with fread() for instance?

I think that asides from all the graphics, map and palette options the most important one would be to export everything as binary files, right? And if that is the case, exactly how should I read binary files? Because they don't seem to have any sort of format, like let's say bmp, png or tga.

#159691 - silent_code - Fri Jul 04, 2008 11:28 am

It's simple: Read in the whole file contents. You have to determine the amount of bytes in the file, then you just read it in where you need it.
Just make sure you do 32bit reads / writes where necessary and work with bytes where needed.

So, if you wanted a bitmap background displayed from file, you could directly read it into the background memory.

An alternative is to allocate a buffer, if a file's content needs to be processed before being copied to the destination.

Other than opening the file in binary read mode ("rb") and using fread(), it's quite simmilar to using compiled arrays (whout a file system.)

Good luck.
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.