PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

LoadImage(const uint8_t* pBegin, const uint8_t* pEnd )

In my isometrical game I have a lot of tiles which I store in separate files, each time I load a level, I load only tiles needed for this level.
To restrict access to my files for user I decided to use some *.dat files for different tile sets. To start I just tried to write a code for loading images using LoadImage(const uint8_t* pBegin, const uint8_t* pEnd );

I started from a simple loading a gif file to buffer and then creating Surface from buffer contents, but the code I use loads an uncomplite image, whats wrong?

Code:
typedef unsigned __int8/*unsigned char*/ ui8;

bool LibGen::GameInit()
{

int count, total = 0;
ui8 buffer[32000];
FILE *stream;
TCHAR FullFileName[MAX_PATH];
TCHAR FullPath[MAX_PATH];
wcscpy(FullFileName, _T("in.gif") );
if (PocketPC::GetFullPathName(FullFileName, FullPath)==0){return false;}
if( (stream = _wfopen( FullPath, _T("r") )) == NULL ){return false;}

//Just trying to read byte by byte
while( !feof( stream ) )
{
count = fread( &buffer;[total], sizeof( ui8 ), 1, stream );
total += count;
}
fclose( stream );
inSurf = LoadImage(pDisplay, &buffer;[0], &buffer;[total-1] );
return true;
}


What is wrong? :(
Change:

Code:

inSurf = LoadImage(pDisplay, &buffer;[0], &buffer;[total-1] );


to :

Code:

inSurf = LoadImage(pDisplay, &buffer;[0], &buffer;[total] );
Thank you, Thierry.
But before posting this topic I've already tried this, doesn't work.
Also I've tried this one and it also !!! doesn't work even it puts all (I suppose) the data into the buffer:

Code:
int count, total = 0;
ui8 buffer[32000];
FILE *stream;
TCHAR FullFileName[MAX_PATH];
TCHAR FullPath[MAX_PATH];
wcscpy(FullFileName, _T("in.gif") );
if (PocketPC::GetFullPathName(FullFileName, FullPath)==0){return false;}
if( (stream = _wfopen( FullPath, _T("r") )) == NULL ){return false;}

//Just trying to read byte by byte
while( !feof( stream ) )
{
count = fread( &buffer;[total], sizeof( ui8 ), 20000, stream );
total += count;
}
fclose( stream );
inSurf = LoadImage(pDisplay, &buffer;[0], &buffer;[total] );


It's a miracle but this one works:

Code:
int count, total = 0;
ui8 buffer[32000];
FILE *stream;
TCHAR FullFileName[MAX_PATH];
TCHAR FullPath[MAX_PATH];
wcscpy(FullFileName, _T("in.gif") );
if (PocketPC::GetFullPathName(FullFileName, FullPath)==0){return false;}
if( (stream = _wfopen( FullPath, _T("r") )) == NULL ){return false;}

//Just trying to read byte by byte
while( !feof( stream ) )
{
count = fread( &buffer;[total], sizeof( ui8 ), 32000, stream );
total += count;
}
fclose( stream );
inSurf = LoadImage(pDisplay, &buffer;[0], &buffer;[31999] );


It is very strange - I try to read more than in file and it works!
Maybe nobody used it before :?
For me it's a mystery.
Forgot to say:
When I initialize buffer with zeros:

Code:
ui8 buffer[32000];
for (ui8 k=0; k<32000; k++)
{
buffer[k] = 0;
}
//Loading buffer like in the last variant, that works...
//...

inSurf = LoadImage(pDisplay, &buffer[0], &buffer[31999] );


it hangs the system.
And, of course my in.gif is only 1006 bytes. So buffer [32000] is enogh (and the third variant works, when I use other that doest - they also load not only incomplite image it is also have a wrong pallet)
Wouldn't it be just easier to call LoadImage()?

Code:

inSurf = LoadImage( _T("in.gif") );
:D Yes.
But as I've mentioned above I need to build up one file containing some tile set, for example office.dat.
And then to load images form it (not all but only needed).
Sure to load them I'll need to use this:
inSurf = LoadImage(pDisplay, &buffer;[x], &buffer;[y] );

Of course now I use the easy one LoadImage( _T("some.gif") ), but I draw now some more tiles, and it would be not only safety, but also useful(some of them less than a 1024 bytes) to store them in one data file.
And please don't say me about intresources or one big surface. :lol:

Have you tested your function:
Surface* LoadImage( const uint8_t* pBegin, const uint8_t* pEnd );
from an image.h file.

If yes, then what code you used for testing and, I suppose, loading an image?
BIGBEN wrote:Have you tested your function:
Surface* LoadImage( const uint8_t* pBegin, const uint8_t* pEnd );
from an image.h file.


Sure did... It's the only way to load an image in PocketFrog.

I am unfamiliar with _wfopen. Assuming it works like fopen(), can you make sure you are opening it in binary mode "rb"?

What about using a memory mapped file (check how I implemented LoadImage( filename )).
Thank you Thierry for your help.
This "rb" method gave an effect, suppose because wchar is 2 bytes long or something.

I'll use your functions from internal.h and image.h
I'll create two separete files (just to be safety)
one is a header to library (plain text Num_of_files, filename, size; filename, size...), other would be a mix of files one after another.


Than I'll make mapping and everything other nedded objects. I'll get pointer to the beginning of file and pass something like this to PF function:
Surface = LoadImage(mappingBegin + size_of_previous_sub_files, mappingBegin + size_of_sub_file + size_of_previous_sub_files)
It should work

Thanks a lot.
After searching the net about structure of data files I found that it is useful to put my data in one file with a stepoffbits value in the header of the file, for fast access to the real data just from reading the first 16 bits of the header. Everything works perfect. :)
Thank you Thierry for your advice not to use buffering when it is absolutely not necessary )
And BTW, if you like shooters another one is coming, it is an action RPG advanture game. (I'm developing it around a year) It is called !PYROMANIA! and coming soon (I hope) :D
BIGBEN wrote:And BTW, if you like shooters another one is coming, it is an action RPG advanture game. (I'm developing it around a year) It is called !PYROMANIA! and coming soon (I hope) :D


That's awesome! "Pyromania" is a perfect name for a shooter =)
BIGBEN wrote:Thank you Thierry for your help.
This "rb" method gave an effect, suppose because wchar is 2 bytes long or something.


Just to let you know, opeing a file with "rb" opens the file in "binary" mode so when you read from it you will get exactly what is in the file.

However, if you open a file with just "r" it defaults to using "text" mode and when you read a carriage return character (0x0d) it automatically adds a "line feed" (0x0a) character to the stream.
I know how a "rb" method could be important.
Because I had some problems with saving my game .sav files properly. Even I called:

Code:
typedef unsigned __int16/*unsigned short*/ ui16;
//...
fwrite(&SomeShort;, sizeof(ui16), 1, myfile);


but using only "w" generated sometimes errors hard for me to find in an output file, even I use a good hex editor. This file was some bytes bigger than my normal save file.
Even if you call this function it does not set the proper binary data into the file, if you use text mode access (I don't know have it any matter that I use unicode mode (for TCHAR) for reading and writing a file - _tfopen()).
Now sometimes I use wb and a+ and a+b to write text and binary data to the file, and thanks to the fseek :^) when reading different data from a file.
But thank you, Guest, anyway.

P.S. I was amazed - it is very fast to load data from my library files, thanks to Thierry's memory direct access advice, for loading surfaces. I'm so happy!