The Greatest screenshot evar!

Published December 13, 2005
Advertisement


Ok, so maybe it does look like an astronaut standing on a hill of chocolate chip cookies[grin]

So yeah, this is my new game project. This screenshot doesn't show off the coolest part of the game yet, animation. That's right, the player sprite is now almost completely animated (except for a jumping frame).

I did come across an interesting bug when writing the animation code, though.

See, to animate my player character, I first created a multi-dimensional array of bitmaps to hold the sprites:

SDL_Surface *player_sprite[2][MAX_FRAME];

MAX_FRAME = 3, by the way.

The first dimension holds the sprites for each direction the player might be facing, which for this game is only right and left. The second dimension holds the max amount of sprite frames in the animation.

Now there was my animation code:
if(m_FrameCount >= m_FrameDelay){   if(m_CurFrame >= MAX_FRAME)      m_CurFrame = 0;   else      m_CurFrame++;      m_FrameCount = 0;}


When I ran the program the animation would spin when the player ran right and flicker when it ran left. I thought this was really odd, and it took me quite awhile to find the problem.

Turns out I was trying to access an index pass the bounds of the array. I quickly fixed the problem and now it looks great. However, I would've expected a bug like this to crash the program with an access violation, but it didn't even crash it in the debugger. Does VC++ perform some sort of bounds checking or something like that? that is the only thing I can think of.

Also, does anyone know how to make a SDL program wait for V-Sync? I'm trying to make the scrolling on the game smoother.

Anyways, I'll keep working at it, but it is coming along very nicely so far.
Previous Entry No screenshots!
0 likes 3 comments

Comments

nilkn
Might I suggest using a more object-oriented approach to sprite animation?

For example, firstly you could separate a sprite from it's animations. Hence, you would have an animation class and a sprite class, the latter of which will contain a list (use SC++L!) of the former (perhaps stored in a std::map so each may be associated with a descriptive name rather than a non-descriptive integer).

Secondly, an animation might consist of a list of frames. A frame, furthermore, might have properties such as image handle (or pointer, or whatever) and display time (this way you can make certain frames display longer for more realistic animations).

Anyway, other than these minor suggestions, it's looking good!
December 13, 2005 05:13 PM
Jesse Chounard
I don't think Visual Studio is doing any bounds checking for you. I think you're probably just reading the variable you declared after the array.

Crashes mostly happen when a process tries to access memory that it doesn't have rights to.
December 13, 2005 05:38 PM
choffstein
Well, you can read any point in memory, as long as it is within your access block. Generally you will get garbage, but it is only when you try to access memory outside your allocated block (as in, allocated by the OS for you application to run in) will you break things.

C++ and C will go wherever you want. Absolutely no bounds checking or anything like that. You can even set a pointer to an integer value that is read in by the user and try to read the data at that point in memory.

Whether that is a "feature" or a "bug," I will let you decide.
December 13, 2005 08:31 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement