SDL Help

Hi guys,

Looking for a little help with SDL. I am able to do some simple SDL programing such as moving an image, hit detection etc.

However, I have been wondering how to mimic a bouncing ball. The function that comes to mind is a y=abs(cos(x)) with a simple iterator which decreases the peak after each jump.

But that's not even the issue. My problem is blitting doubles onto the screen instead of integers. Does anyone know how to go about this? It does not even have to be a y=cos(x). It could be y=x^2. I just need to know how to graph smooth curves with SDL_BlitSurface function instead of the simple lines I have been doing so far.

Thanks,

Mike
The function that comes to mind is a y=abs(cos(x)) with a simple iterator which decreases the peak after each jump.


A more practical approach is to have a gravity constant that is applied to the ball each frame.

Any object that moves should have 2 basic parts:

1) An x,y position
2) An x,y speed

Every update (typically every frame) you apply motion by adding the speed to the position, then you apply acceleration and other forces to the speed (ie, if the player hits a wall and you want them to stop, you force their speed to zero).

In the case of a bouncing ball, you would do 2 things:

1) apply gravity by adding a constant gravitational value to the ball's y speed every frame.

2) when the ball hits the ground, you negate its Y speed (having it bounce up again), but apply some kind of dampener to absorb some of the shock. IE: multiplying by 1.0 would mean it bounces the same height every time... less would mean it bounces less, and greater would mean it bounces higher and higher each time.


Try to avoid using trig functions like sin/cos unless you are sure it's the best (or only) way to solve the problem at hand.

But that's not even the issue. My problem is blitting doubles onto the screen instead of integers.


Well you should be drawing strings, not integers. In which case all you have to do is convert a double to a string and then print the string... done more or less the same way you'd convert an integer to a string.

1
2
3
4
5
6
7
8
9
10
11
12
//assuming you have a function like this:
void printstring( const char* str );  // which prints the given string

//  with stringstream:
stringstream ss;
ss << yourdouble;
printstring( ss.str().c_str() );

// with c-style strings and sprintf
char buffer[30];
sprintf("%f", yourdouble );
printstring( buffer );
Thanks Disch,


This is what I have so far: (Of course this is just the interface)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Ball{

public:
	Ball(SDL_Surface *im,int x, int y, int sp);
	void show();
	void move();

private:
	SDL_Rect box;
	SDL_Surface *image;
	int dx, dy, speed;

};






You are telling me I should make dx,dy double then use stringstream to plug the values into SDL_BlitSurface?

Last edited on
Well... how are you drawing text? (or are you?) Are you able to print a basic string to the screen with SDL?

Note that SDL does not have any built-in way to print text by default, but it does have an add-on lib which allows for font rendering (I forget the exact name, SDL_TTF or something?) If you are not using that lib, you'll have to create a surface that has a bitmapped font, then draw each character in the string as a single rectangle to be blitted.


I recommend getting that SDL addon lib. Either that, or ditch SDL entirely (blitting is quite an outdated concept -- SDL's API is extremely dated) for something more modern which has this functionality built in. I often recommend SFML - even though it's a bit more difficult to install.
I don't think the OP is talking about drawing text. I think what he means is that he wants to draw images using double coordinates.

You can store all the coordinates as doubles and only convert them to int when you call SDL_BlitSurface.
Aahhhhh, you're probably right.


Looks like I completely understood.

Then OP, yeah, forget about all this string stuff. Just do a simple cast to convert the double to an int:

1
2
3
(int)your_double  // <- C-style cast
//...or...
static_cast<int>(your_double) //<- C++ style static_cast 
Topic archived. No new replies allowed.