SDL_BlitSurface problem

Hi, I am currently using the SDL library for some simple game development. I have run into a problem. Ok here goes:

I have a Sprite class, which holds all info about a Sprite, (position,dimensions etc). I also have a method in the Sprite class that looks like this:
1
2
3
4
5
6
7
8
9
void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)
{
    SDL_Rect dstrect,srcrect;
    dstrect.x = getX();
    dstrect.y = getY();
    srcrect.w = getW();
    srcrect.h = getH();
    SDL_BlitSurface(source,NULL,dest,&dstrect);
}

This works perfectly fine, for my player object and the background image. My problem comes when I try to set the source rectangle.

1
2
3
4
5
6
7
8
9
void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)
{
    SDL_Rect dstrect,srcrect;
    dstrect.x = getX();
    dstrect.y = getY();
    srcrect.w = getW();
    srcrect.h = getH();
    SDL_BlitSurface(source,&srcrect,dest,&dstrect);
}

When I do this, none of my surfaces show up! I read the API for the BlitSurface and it says the srcrect uses only the width and height of the source rectangle, and the dstrect only uses the position.

I have set the x,y,width and height of all my sprites using a setBounds() method that I created.

My understanding is that, srcrect will set the width and height of the surface, and dstrect will set the position of that on the destination surface. Is this wrong?

If you need to see any code just let me know. Thanks.
you're only setting the x and y values for the destination rect and the height and width for the source width, you need to set all four for both
Ok, So since I want my source rectangle to have the width and height of my object, and I want its dest coords to be those of that object, can I just use the same rectangle for both? e.g.
1
2
3
4
5
6
7
8
9
void Sprite::Spr_Blit(SDL_Surface* source,SDL_Surface* dest)
{
    SDL_Rect dstrect,srcrect;
    dstrect.x = getX();
    dstrect.y = getY();
    dstrect.w = getW();
    dstrect.h = getH();
    SDL_BlitSurface(source,&dstrect,dest,&dstrect);
}


because according to SDL API, the source rect only uses width and height, and the dest rect only uses x and y
Last edited on
No. The width and height of the dest and source rectanlge will be the same, but your x and y values for the destination rectangle will need to be the x and y position to draw to on the screen, whereas the x and y values for the source rectangle will be where to start copying from in the texture, if your textures are all static, not animated, then this will always be (0,0).

I think you've got the wrong idea of how a sprite should work really, you're calling getX() and similar functions to get the x, y, height and width, but the x and y draw position wont always be the same for all world objects which use that sprite, they should be passed into the draw function. You don't want to load the same texture and create a sprite from it several times in order to have more than one world object which uses it, you should just load each texture once and allow several world objects to use that sprite via some kind of identifier, like a string for the name of the texture or an int for the position in the array/vector/whatever of sprites which you have stored.
I have a player class that inherits from the Sprite class.I created a player sprite. I have key events that can move the sprite around. The getX(),getY() gets the x and y coords of the sprite. Will this not mean that the source will copy from those points and go as far as the width and height? And then draw this at the current position of the player sprite? Does that make sense?
You're mixing together elements of your game world and the graphical representation of it there, try to seperate out the graphics part of your game and just have calls to it from your world object's classes such as the Player class.
Topic archived. No new replies allowed.