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 > NDS and Zlib

#173230 - Gryphon93 - Sun Mar 28, 2010 12:02 pm

(Gulp... First post here...)

Hello, everyone! I wonder if anyone could tell me how to use the zlib in my DS projects. I want to compress some data in my app, but I don't know how to use zlib, especially when it's not a DLL (I've worked with the zlib-DLL earlier). I found the NDSZlib here and I guess it's that one I'm supposed to use. (Or can I use zlib from zlib.net directly?) I appreciate all your help! Thanks in advance!

PS. I've run make install, so it's installed .DS

Edit. Are there any devkitARM examples that uses zlib (or any other examples for the NDS)?

#173243 - wintermute - Sun Mar 28, 2010 8:48 pm

It's probably best if you remove that zlib from your libnds directory, devkitARM has moved on quite considerably since those archives were put together.

Many libraries, like zlib, are in fact completely hardware independent and devkitARM is useful for any platform running on ARM processors. There's a brief explanation of building and installing normal autotools based libraries at http://wiki.devkitpro.org/index.php/portlibs which also links to the prebuilt archives hosted on sourceforge.

Using zlib on the DS is essentially identical to using it on any other platform although you'll obviously need to use libfat for file access.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#173280 - Gryphon93 - Mon Mar 29, 2010 7:13 pm

Thanks. I'll remove the library I installed and follow your instructions. I want to use zlib to compress data in the memory, so I won't need libfat. The examples I've found only show how to compress files though, and I would like examples on how to compress data directly from the memory. It was a while since I used zlib, so I'm a bit rusty. Oh, I forgot to say that I used zlib for another language than C/C++, but I can't remember what language. So, this is something new I have to learn. All help is appreciated! (Learning how to use libraries aren't usually that hard though)

#173293 - sverx - Tue Mar 30, 2010 9:03 am

check compress() :)

#173298 - Gryphon93 - Tue Mar 30, 2010 7:29 pm

Thanks a lot sverx, I'll try it out right away!

#173302 - headspin - Tue Mar 30, 2010 8:16 pm

Here's a function I wrote for TDG

EDIT: Sorry didn't realize you wanted to _compress_ data in memory.

Code:
int decompressFileBuffer(const char *fileName, char *pBuffer, int bufferLen)
{
   FILE *pFile;
   struct stat fileStat;
   size_t result;

   pFile = fopen(fileName, "rb");
   
   if(pFile == NULL)
      return 0;

   if(stat(fileName, &fileStat) != 0)
   {
      fclose(pFile);
      return 0;
   }
   
   void *pZLibBuffer = malloc(fileStat.st_size);
   
   if(pZLibBuffer == NULL)
   {
      fclose(pFile);
      return 0;
   }

   result = fread(pZLibBuffer, 1, fileStat.st_size, pFile);
   
   if(result != (uint) fileStat.st_size)
   {
      free(pZLibBuffer);
      fclose(pFile);
      return 0;
   }
   
   uLongf unCompSize = bufferLen;
   uncompress((Bytef*)pBuffer, &unCompSize, (const Bytef*) pZLibBuffer, fileStat.st_size);

   free(pZLibBuffer);
   fclose(pFile);
   
   return result;
}

_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#173303 - Gryphon93 - Tue Mar 30, 2010 8:34 pm

No worries, headspin. It's a great example and I want to say thank you for it! It's nice and I may have use of it later on.

Edit:
What am I supposed to write in the Makefile to include zlib? I've tried this

Code:

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS   := -libz -lnds9
 
 
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS   :=   $(PORTLIBS) $(LIBNDS)


But it doesn't work. The compiler says that it can't find libz. I can change it to something else, like libza, or something, and of course it doesn't work, but I can still compile the program just fine, without errors. Please help.

I downloaded the portlib from sourceforge and followed the instructions in the wiki as well as I could (like putting the library inside $(DEVKITPRO)/portlibs/arm/. I haven't used Makefiles before, so I'm not sure on how to do this.

#173313 - Sektor - Wed Mar 31, 2010 4:10 am

LIBS := -lnds9 -lz
_________________
GTAMP.com/DS