Sign in to follow this  
tom_mai78101

SDL_image: Clipping and Blitting Sprites in Left and Right Directions, How exactly?

Recommended Posts

Here's the code that's affecting the sprites facing left and/or right.
#include <SDL.h>
#include "Animate.h"

void Protagonist::Stand()
{
	const int x = 202;
	const int y = 4;

	for (int i = 0; i < 6; i++)
	{
		clip[i].x = x + i * 42;
		clip[i].y = y;
		clip[i].w = 34;
		clip[i].h = 41;
	}

	for (int j = 6; j < 12; j++)
	{
		clip[j].x = x + 244 - (j * 42);
		clip[j].y = y;
		clip[j].w = (-34);
		clip[j].h = 41;
	}
	return;
}
I had separated everything else that's not related to bi-directional facings from this. Everything else is working flawlessly, so I don't think there's a need to post them here. I had already declared "SDL_Rect clip[12]" in the class function, which isn't included in this post, so no worries about that. The top "for" loop is to clip sprites out of the sprite sheet that were facing right. By default, all of the sprites in the sprite sheet are facing right, so the top part has no errors. The main problem resides in the bottom "for" loop. I'm not sure how SDL_Rect works for the clipping, but I thought by putting in a negative width value, the clipping would go the opposite direction of a normal clipping, but I was wrong. Does anyone know how to make sprites face towards two directions with just one sprite sheet (having the default direction facing for all sprites)? Thanks in advance.

Share this post


Link to post
Share on other sites
SDL doesn't do "flipping" of sprites on it's own in either horizontal or vertical fashion. If you use OpenGL or DirectX to do your rendering then you can use texture coordinates to "flip" the images. I believe SDL_gfx may have support for such a thing.

If you want to use vanilla SDL and generate the surfaces once at load you could always flip them yourself using pixel operations to reverse the scan lines in memory and then blit the resulting array to a new surface.

In the end this type of "optimization" is not worth the time or effort to get working outside of your own personal learning experience. Disk space to have a sprite sheet contain all directions should not be an issue and surface/texture memory most likely is not an issue either unless you literally have millions of sprites. Even then you would be better off with a good sprite management system than trying to save disk/memory this way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this