Unhandled Exception error implementing sprite

Hi all,

So I'm new to C++ and it was all going swimmingly until I hit a wall.

I'm learning C++ in the context of making games, and for my project I'm making a 2D tank battle game. The tanks move horizontally over the horizon, and the players take shots at each other in alternating turns.
I recently got the collision detection working between the sprites and the terrain, but wanted to change the terrain to make it look more realistic (I had up until that point been using a simply green rectangle to represent the terrain). I changed the sprite from the green rectangle to an uneven floor surface and received the following error message:

Unhandled exception at 0x5c59a41b (gfcd.dll) in gfcgame.exe: 0xC0000005: Access violation reading location 0x0000000c

And an arrow pointing to this code in the library:

int GetHeight() { return GetSurface()->h; }

(I'm using a library called GFC, which I think is a library made by my lecturer for this course).

What's really baking my noodle is that when I substitute the sprite which causes this error with another sprite that is already active in the game (e.g. a sprite of one of the tanks) it loads fine and sits in the middle of the screen. However when I try to bring in a new sprite I get the above error. It's as if I've run out of memory to store sprites. I know I'm drawing/updating it fine, because like I say the other sprites are working fine.

If anyone can shed any light on this I would be eternally grateful, it's driving me crazy.

Below is a snippet of the code:


CMyGame::CMyGame(void)
: background1(700, 400, "bg1.bmp", 0),
ground(700, 400, "ground.bmp", 0),
theTank1(CVector(100, 150), "tank.bmp", CColor::White(), GetTime()),
theTurret1(CVector(), "turret.gif", CColor::White(), GetTime()),
theTank2(CVector(1300, 150), "tank2.bmp", CColor::White(), GetTime()),
theTurret2(CVector(), "turret2.gif", CColor::White(), GetTime()),
arrow(CVector(700, 50), "arrow.gif", CColor::Cyan(), GetTime())


It's the "ground.bmp" which is messing me around. If I substitute it for, say, "turret.gif" it loads fine. I've tried the background1 code as you see it above, as well as in the format used for the tanks/turrets/arrow.
Well, clearly loading the image fails. Maybe you made a typo when naming the file or give the function wrong width and height?
That's what I thought at first, but I've run through every possibility I can think of, including your suggestions. I'm not declaring a height taller than the screen, and I'm definitely not spelling the filename incorrectly; I've also tried with both bitmap and gif images. You're starting to see my frustration? :)
Is background1 loaded correctly? If yes, try replacing ground.bmp with a (renamed) copy of bg1 and see how it goes.
Hi hamsterman,

background1 is loading correctly. When I substituted ground.bmp with a renamed copy of bg1 it all loaded fine; this was as I was expecting, as when I load any other sprite besides ground.bmp it works fine. It's only when I load ground.bmp that it freaks out. I've tried totally deleting ground.bmp and creating a new sprite from scratch with a totally different name ("workgoddamnit.bmp for instance!) but to no avail.
I've also tried loading the sprite using the same format as, for example, the tank sprite:

ground(CVector(400, 400), "ground.bmp", CColor::White(), GetTime()),
as opposed to
ground(700, 400, "ground.bmp", 0),
but this doesn't seem to have made any difference either!
So the failure to load ground.bmp is related to it's contents? Or did I misunderstand you?
I've managed to fix it. Turns out my game folder was a complete mess and there was a whole other set of the same sprites in another folder. I was loading the sprites from that folder and not the one I was saving the ground sprite too, so when I thought I was importing the sprite from that folder I was trying to import it from a folder where there was no ground sprite, hence the error. Lesson learned! *hangs head in shame*

Thanks for your help anyway!
Topic archived. No new replies allowed.