SDL Sprite positioning

Hi everyone,

I'm currently making a simple Fragger-type game using SDL and Box2D.

At the moment, I'm currently trying to draw an explosion animation at the same set of co-ordinates as the grenade which the player throws (after a certain amount of time has elapsed).

I'm having trouble with this however, as I'm uncertain on how to draw one sprite's position at another sprite's co-ordinates.

Can anyone give me some pointers please? Thanks a lot.
I only dabbled with SDL for a tiny bit and don't remember the exact implementation of sprites, but an easy solution would just be to change the position of your explosion sprite to that of the grenade.
Correct me if you're attempting something else.
Just take the nade's coordinates, and use them for the explosion sprites.
Was that so hard?
Thanks EssGeEich you seem like a great help!!
hi blacksheep, that is what I was thinking too but I am having problems attempting that. I am also making the same sort of game and I am having the same problem. I want my grenade to explode after a certain amout of time but when I try pass the co-ordinates of the grenades position for the explosion I encounter problems.



if anybody could help I would be grateful
Last edited on
@ essgeeich
At the risk of being kicked out of this forum, I feel I should say that it's snide, condescending and patronising comments such as "Was that so hard" that make asking for help online increasingly frustrating. Elitist bullshit like this turn beginners off and make you look like a gigantic clown.

@BlackSheep
Thanks for your suggestion, I've tried changing the explosion sprite's position to the grenade's but it seems to insist on drawing itself in its original position! I'll stay at it though and let you know how I get on.
Ok, sorry for that. I just tought it was quite simple.
Well, a guideline of what you may want to do is:
1. Take the center of the Grenade (x = (Rect.Left + Rect.Right) / 2, y = (Rect.Top + Rect.Bottom) / 2)
2. Build a Rectangle around that center (First set top to 0, bottom to the pic height, left to 0, and right to the pic width, then add x to both left and right, and add y to both top and bottom)
3. Check you are passing the Right parameters in the Right place.
@ EssGeEich - Apology accepted. Sorry if I responded a bit harshly but I've had a lot of frustrating experiences on forums.

Thanks for your tips too. I'm going to try and process them now and see if I can implement it in code. I'll let you know how it goes.
All right. Anyways I've been a bit rude too.
The most probable thing that's wrong is maybe some wrong call to SDL_BlitSurface. I mean, maybe you should check twice the parameters, and check you didn't invert the Clipping Rectangles. This may help you in case you aren't sure: http://www.lazyfoo.net/SDL_tutorials/lesson06/index.php
Hi EssGeEich

Thanks for your help. I've followed Lazy Foo's tutorials quite closely but I haven't really seen much in it pertinent to this issue.

Here's my call for blitting the grenade:
apply_surface(grenadepos.x*PTM_RATIO,grenadepos.y*PTM_RATIO, rotate_grenade, screen, grenadeAngle);

And here's my call for blitting the explosion:

SDL_BlitSurface(explosion, &exSrc, screen, &exSprite);


The apply_surface() function is the following:

1
2
3
4
5
6
7
8
9
10
11
12
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, float angle)
{
    //Temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Get the offsets
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}


Thanks again for your efforts ;)
Hey again. Sorry I was at work. Just looked at the SDL_BlitSurface docs and your apply_surface function looks good to me. Are you sure you're passing the right x and y parameters when you blit the explosion?

1
2
3
SDL_BlitSurface(explosion, &exSrc, screen, &exSprite);
//Here exSprite.x and exSprite.y should be the positions of the grenade at impact. (Perhaps with a little
//adjustment to position the center correctly) 


As a side note, the angle parameter doesn't look like it's being used in your apply_surface code.
Thanks BlackSheep. i finally sorted it out. What was happening was I was initialising exSprite.x and exSprite.y to two arbitrary screen co-ords at the beginning of the main game loop, so obviously they were reverting to these values with each update. Very embarassing!

A big thanks to both EssGeEich and BlackSheep for your help and suggestions.
Topic archived. No new replies allowed.