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 > Load textures from fat

#176277 - SchmendrickSchmuck - Sat Jun 11, 2011 11:49 pm

I've been having some trouble trying to load a texture (pcx file) from fat.
Loading an included file is easy enough:

Code:

#include "foo_pcx.h"
sImage image;
loadPCX(foo_pcx, &image);


However, loading from fat doesn't give the expected results:

Code:

fp = fopen("fat:/foo.pcx", "rb");
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* foo_pcx = (char *)malloc(len);
fread(foo_pcx, len, 1, fp);
fclose(fp);
sImage image;
loadPCX(foo_pcx, &image);


Apparently, the 'foo_pcx' in this case doesn't yield the same result in the second as in the first. Was I wrong in assuming they would? How should I load the pcx file from fat in order to use the loadPCX function as I would in the first example?

Thanks
_________________
http://DSLiero.DennisvanZwieten.com - Liero for NDS!

#176282 - Azenris - Sun Jun 12, 2011 11:53 pm

What do you mean doesn't yield same results, garbage image or just blank? Never used loadPCX myself. Is the file definitely opening?
_________________
My Homebrew Games

#176283 - DiscoStew - Mon Jun 13, 2011 3:44 am

Yeah, check if the file handle is valid. Also check the length from ftell.
_________________
DS - It's all about DiscoStew

#176307 - SchmendrickSchmuck - Mon Jun 20, 2011 7:55 pm

Thanks for the replies. It didn't get the same results, as in, the loaded texture was blank (completely white). I don't know why I didn't immediately recognize this as a lack of VRAM memory, but that has been solved now.

Another point I came across here is that fseek (and ftell) returned invalid values (-1). I couldn't for the life of me figure out why, and after changing apparently nothing, that has too been fixed. It has still left me confused, though I can't really complain.
_________________
http://DSLiero.DennisvanZwieten.com - Liero for NDS!

#176317 - ritz - Wed Jun 22, 2011 4:14 pm

Here's some code I used for putting PCX on the background, maybe it'll help:

Code:
u8*
v_readraw (char *filename)
{
   u8 *tp;
   int fp;
   u32 flen;
   struct stat st;

   fp = open(filename, O_RDONLY);
   if (fp == -1)
      return 0;

   if (fstat(fp, &st) == -1)
      return 0;

   flen = st.st_size;

   tp = (u8*) malloc(flen);
   if (!tp)
      { close(fp); return 0; }

   if (read(fp, tp, flen) == -1)
      { close(fp); free(tp); return 0; }

   close(fp);

   return tp; // free(tp);
}

void
v_ldpcxbg (char *sbgimg, int screen)
{
   sImage pcx;
   void *gfxptr, *palptr;

   u8 *tp = v_readraw(sbgimg);
   if (!tp) OOP("v_readraw()");
   if (!loadPCX((u8*)tp, &pcx))
      OOP("loadPCX()");
   free(tp);

   if (screen == 1)
      { gfxptr = (void*)BG_GFX_SUB;
        palptr = (void*)BG_PALETTE_SUB; }
   else   { gfxptr = (void*)BG_GFX;
            palptr = (void*)BG_PALETTE; }

   swiCopy((void*)pcx.image.data8, gfxptr, ((pcx.width*pcx.height)>>2) | COPY_MODE_WORD);
   swiCopy((void*)pcx.palette, palptr, ((256*2)>>2) | COPY_MODE_WORD);

   imageDestroy(&pcx);
}