PocketMatrix forums

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

EasyCE / ASurfaces...

Dave Johnston · 31 May 2002, 17:35 UTC
Anyone know if there is a limitation to the size of an image an ASurface can handle?

I'm having real problems trying to load a 240x360 uncompressed 16bit TGA using the default constructor. Tried 256x256, and 128x128 and 96x96 - all resulted in crashing at runtime.

However, I have no problem with the 16x16 and 32x32 sprites I'm using.

Interestingly, a 480x640-sized TGA worked without crashing, but the buffer appeared to be filled with zeros - i.e., the image was black.

Any ideas?
Hm, looks like a bug in the ASurface class. I'll check it out when I find some time. In the meantime, you may check this: Someone mentioned that the ASurface constructor only works correct for square surfaces. That doesn't explain your crashes on 128x128 surfaces of course, but if you want to have a look at the sources yourself, that may be a good hint to find the problem.

BTW, in general, EasyCE is not supported anymore. I am working full-time on it's successor, the Overloaded Game Foundation. If anyone wants to develop EasyCE further, you are welcome to take the project from where it is now.

- Jacco.
Dave Johnston · 02 Jun 2002, 13:51 UTC
Well, I've looked into the class, but haven't seen anything obviously wrong - but then I'm not as a talented programmer as some!

BTW, are those numerous 'delete array' which should be 'delete [] array' occurances on the EasyCE code one of your 'bad coding habits' mentioned on the Overloaded update?
Ehm yeah, that's one of my bad coding habbits. :) Jaap corrected all of those in the latest version of the Overloaded Game Foundation btw.
Dave Johnston · 03 Jun 2002, 13:02 UTC
Not meaning to degrade you as a programmer, but what other habits are there which may be useful in improving the EasyCE code? I guess the array one is the most obvious, as are perhaps some naming conventions (hey, I'm a Java programmer brought up by Microsoft Java books...) :)

I've already implemented some useful game classes into EasyCE - like CBaseEntity, CBaseHud, CWorld... (spot the Half-Life SDK influence), so perhaps it could go some way towards updating EasyCE for those who don't want to move to Overloaded.
Hm, if I knew what those bad habbits are, I would correct them. :)
Dave Johnston · 03 Jun 2002, 13:56 UTC
Oops, didn't think of that :)
Dave Johnston · 07 Jun 2002, 10:05 UTC
Now my exams are over (phew), I had a closer look at the crashing problem.

I managed to find out exactly which line the crash occurs, and indeed a fix to stop the crashing, but also prevent the loading of the TGA. I am totally puzzled by it.

I managed to trace the bug into the tgaload method. this is from line 580 in surface.cpp:

Code:
bool ASurface::tgaload( char* a_File, int w, int h, PIXEL* dest )
{
int size = w * 2 * h + 20;
BYTE* tgabuff = new BYTE[size];
FILE* tga = fopen( fullpath( a_File ), "rb" );
if (!tga) return false;
//fullpath(a_File);
fread( tgabuff, 1, size, tga );
fclose( tga );
delete [] tga;


Now, that code by itself causes an access violation and crashes. However, uncomment the 'fullpath(a_File)' line (which I added), and the crash does not occur. I checked out the fullpath method, but don't understand the CE API stuff, so I couldn't take it any further.

Anyone have any ideas?

Even if I do keep the fullpatch method present, the TGA load still gives me a completely black image - suggesting the image is never loaded at all. I'll investigate this more today.
Well if fullpath would return a bad result, or if you pass it a bad path or whatever, the FILE* should become zero because the file could not be opened. I never encountered problems with fullpath though; I'm using it all the time...
Dave Johnston · 07 Jun 2002, 11:46 UTC
I use a logging system to find out what values are passed between things (since the debugger is pretty dangerous on my Jornada!) - each time fullpath has returned a perfectly acceptable result. I can't see how just adding one more execution of the fullpath method stops that particular crash.

I've been looking further, and found another crash in the routine that loads the TGA data into the buffer. I'm guessing one of two things:
1. I have a b0rked EasyCE installation
2. It's trying to load data from a buffer that doesnt exist (couldnt open the file, but carried on trying to read).

Still need to look into it more...
1. Check return values. Always. The return value from fread should give you the number of bytes read. If it's not equal to size, the read failed, or the file is screwy.

2. Check your input data, if you can. Check its file size against the size calculated in the function, at the very least.

3. The afore-mentioned size calculation...

If, in this case, the TGA file actually is a Targa file... then it's potentially going to be larger than:

(2*w*h)+20

... at least according to a cursory glance at the Targa file spec. that I have here.

(The TGA loader really *should* check things like the TGA type, ID Length, etc).

4. If I were you, I'd consider using the windows CreateFile API for opening files and loading them - that at least would give you better error returns (unless you really want it very portable).

5. Finally, the error, I suspect:

With the line

delete [] tga;

You're deleting a buffer that you haven't declared. FClose deals with closing the file pointer, etc - you shouldn't.

From the API:

System-allocated buffers are released when the stream is closed.


Cheers,

Refractor
Dave Johnston · 07 Jun 2002, 13:26 UTC
refractor wrote:
1. Check return values. Always. The return value from fread should give you the number of bytes read. If it's not equal to size, the read failed, or the file is screwy.

2. Check your input data, if you can. Check its file size against the size calculated in the function, at the very least.


Didn't think of that at all. Nice one - thanks!

refractor wrote:
5. Finally, the error, I suspect:

With the line

delete [] tga;

You're deleting a buffer that you haven't declared. FClose deals with closing the file pointer, etc - you shouldn't.


Well, the original code was 'delete tga' - not my addition. I just corrected it to reflect the array.
What array?

If that is indeed the end of the function, you want to remove the tgabuff array, not tga (which isn't an array).

But, if if that is the end of the function, you haven't copied anything into your destination.
Oh, you should destroy the tgabuff buffer before the return upon file open failure, or perhaps more sensibly, allocate it after the file has successfully opened... oh, and check it to make sure it allocated correctly. :)
Are you sure the original code deletes tga, not tgabuff? I can't remember fixing it, and the Overloaded libraries have a nice delete tgabuff there...
Dave Johnston · 07 Jun 2002, 14:53 UTC
I'll do a diff a bit later and work out what I've changed and forgotten about then :)

For the moment, it seems to be working - half the time. For some peculiar reason I can load some files, and not others (fopen gives null). In particular I still can't load my 480x640 bitmap - maybe a memory restriction, but I'll have to dig even deeper. Perhaps some splitting needs to be done.

Anyway, I'll check that out later. Right now I have to work out why my sprite cache is loading sprites twice before caching them... grrrr programming can be so much fun.
Dave Johnston · 07 Jun 2002, 16:45 UTC
Ok, I feel I should now eat my hat. However I don't have a hat. Maybe a large wooden spoon would be far more fitting.

Indeed, the delete stuff was something I made up it seems, along with a few other changes that I never remember doing either. A Diff revealed lots of weird things that I can't remember doing, but clearly did. Fixing them back to the original surface.cpp fixed the problem!

But my cache is still broken, and some image drawing code I just wrote. :)

On the plus side, England beat Argentina, but I can't say I contributed...
Dave Johnston · 07 Jun 2002, 20:10 UTC
Incidentally, I still had to change the tgaload code back to this:

(only first few lines shown)
Code:
bool ASurface::tgaload( char* a_File, int w, int h, PIXEL* dest )
{
int size = w * 2 * h + 20;
BYTE* tgabuff = new BYTE[size];
fullpath(a_File);
FILE* tga = fopen( fullpath( a_File ), "rb" );

fullpath(a_File);

if (!tga) return false;

fullpath(a_File);

fclose( tga );


... in order to get it to work with all the TGAs. If I don't have the various fullpaths(), 'tga' becomes null, fread fails, and my program crashes. Some TGAs load without this, but some don't... apparently. Weird!
Tga's that you point to using a relative path should fail without the fullpath thing. :) Some background info, in case you didn't know: On the PC, you can safely assume that 'test.tga' is in the same dir as your application executable.

And, '../graphics/test.tga' is understood also. On the PPC, you loose these 'relative paths'. So I use a trick: The fullpath function. This function kindly asks the PocketPC OS for the path of the current application. It then appends the 'relative path', and now it can be safely loaded on the PPC.

Note that this is quite an ugly distinction between coding for PPC and PC. An unneccessary distinction, as far as I can see.

- Jacco.
Dave Johnston · 08 Jun 2002, 10:12 UTC
I get that much, I worked it out! My question is - in my code, you'll see I have several calls to fullpath on lines 5, 8 and 12. Their return values are just discarded - essentially these calls shouldn't need to be there, and indeed aren't in the basic EasyCE source. Yet, to get some of my TGAs to load, they have to be there to avoid a crash.
Dave Johnston · 08 Jun 2002, 20:18 UTC
Argh this is going to kill me!

2 hours ago it was loading all my TGAs no problem. Now it's decided not to load some of my HUD graphics. Even went to my build from earlier and it won't load the same files either.

Probably a CE/eVC bug, but if this keeps up I might as well give up. It's bad enough having to modify it for myself, let alone once for every individual gamer who wants to play it...
draklava · 08 Jun 2002, 21:39 UTC — same problem
Dave please keep us updated with your status... I was having a similar problem but now my graphics always seem to load - I'm curious what updates are needed if any

Phantom - is it still correct to use

delete tgabuff

or is delete [] tgabuff needed ?

I'm having a strange problem where my app crashes on win32 desktop when I load an image a second time (like they play a game, exit and start a new game it tries to reload a 488x85 pixel image and then crashes)

I clean up the ASurface pointer after the exit game and reallocate on start game...

this works fine on PPC it only crashes on the desktop - weird !
Dave Johnston · 09 Jun 2002, 15:54 UTC
Whoa, now things have got REALLY weird. fullpath() is now returning the full file name of my logfile, even when it is passed the correct filename for the TGA (ie, fullpath("image.tga") returns '\dir\logfile.txt' instead of '\dir\image.tga').

Very very strange. I even managed to hang the PPC (needing soft reset) and stop it from drawing every other line on the screen (and then hang) just by calling fullpath() earlier!
How does fullpath() know that the amount of memory pointed to by the passed parameter is large enough to safely prepend the full path name?

To be honest I've never seen the code for this library, nor have I used it, but a function that makes an assumption about the amount of memory referenced by a pointer parameter is just asking for trouble.

BTW, the rule regarding which version of delete to use depends on which version of operator new is used to allocate the block. In the code that Dave posted, he's used the array allocator to allocate tgabuff. Therefore he should use the matching array deallocator (delete[]).
It does not append to the passed string, it returns the full path. (potential memory leak: Who frees that string? :) ).