Quick Question in SDL

Started by
16 comments, last by kontrolkl 18 years, 8 months ago
Ok, I'm making my pong game in SDL, i'm now at the point to move the ball around. Give it a random vector at start and calculate the next vecto when it's hit something depending of the angle... I was wondering how exactly how i do that in SDL, i got no trouble animating the bar and setting up limit, but how can i animate an object always ( until it goes on the "win limit" ) If anyone has explanation or maybe a tutorials i would be happy. And also I would like also how exactly work the "colision" between the ball and the bar, since the limit on the side are static it's not hard to say you can't go any further but since the bar keep moving it's a bit harder. thank
Advertisement
i'v came up with something like that but it doens't seem to be working exactly like i want, the ball move way to fast.

void Move_Ball(int x, int y){	while ( !Check_Win(xpos3, ypos3) )	{		xpos3 +=2;		ypos3 +=2;	}}bool Check_Win(int x, int y){	bool bOk = false;	if ( y <= 0 )	{		bOk = true;		iPlayer1Score++;	}	else	{		if ( y >= 640)		{			bOk = true;			iPlayer2Score++;		}	}	return bOk;}


If anyone have another idea ^^
OR maybe how do i add a small timer that would move the ball each 0.5 second of pause?
I found the funtion SDL_GetTicks(); and i think i could use it to make the ball move slower, but I'm not exactly sure how to use that, if someone know about it could you explain me please?


Now look like that
void Move_Ball(int x, int y){	float fTicks = SDL_GetTicks();	while ( !Check_Win(xpos3, ypos3)   )	{		if ( SDL_GetTicks() > fTicks+0.0001 )		{			xpos3 +=25;			ypos3 +=25;			fTicks = SDL_GetTicks();		}	}}

1) Don't just post four times in your own topic, use the edit feature.

2) You may want to check out this article on frame based movement.
Quote:Original post by Ekim_Gram
1) Don't just post four times in your own topic, use the edit feature.

2) You may want to check out this article on frame based movement.


1. Sorry, tough people wouldn't see if i only edit i'll just say i did edit in title next time.

2. I took a look on your link, but that doens't seem to help me, i'm not sure what it do exactly, and how to make it work like i want.

I found 2 things thatm might help me :

Uint32 SDL_GetTicks(void);
void SDL_Delay(Uint32 ms);

but i don't know how to make one of them work corectly.

Don't use delay.

The following is an except from my Tetris clone

float Start_Time = 0;void Drop(){	if((SDL_GetTicks() - Start_Time) > (1000))	{	  Piece.Move(0,1); // Move the piece down	  Start_Time = SDL_GetTicks();	}}


If 1000 miliseconds/1 second has passed, the piece would move down 1 unit (32 pixels in my case).
I understand what you have wrote, and how it should act, but since i did add it to my code it's load my system real bad and the game run, but i can't move anything and when i try to close the window i get an error.

Here's my code

void Move_Ball(int x, int y){	float Start_Time = 0;		//float fTicks = SDL_GetTicks();	while ( !Check_Win(xpos3, ypos3)   )	{		if((SDL_GetTicks() - Start_Time) > (1000))		{			xpos3 +=2;			ypos3 +=2;			Start_Time = SDL_GetTicks();		}	}}


is it because of my while that would be looping so fast that my system become loaded?
Would you be able to post more of your code? I feel that it may be something else.
Sure

#include <SDL.h>#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string>#include <conio.h>using namespace std;void Init_SDL();//void DrawPixel(SDL_Surface *screen, int x, int y,Uint8 R, Uint8 G, Uint8 B);//void Draw_Bg(SDL_Surface *screen);void Slock(SDL_Surface *screen);void Sulock(SDL_Surface *screen);void Init_Images();void Draw_Start();void Draw_BG();void DrawIMG(SDL_Surface *img, int x, int y);void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);void Draw_scenes();bool Check_Limits_Left(int x);bool Check_Limits_Right(int x);void Move_Ball(int x, int y);bool Check_Win(int x, int y);SDL_Surface *screen;SDL_Surface *back;SDL_Surface *Palette1;SDL_Surface *Palette2;SDL_Surface *Balle;SDL_Surface *Start;int xpos=260,ypos=460;int xpos2=260,ypos2=20;int xpos3=260,ypos3=220;Uint32 SDL_GetTicks(void);int iPlayer1Score = 0;int iPlayer2Score = 0;string sLastWinner = "";string sPlayer1 = "Player1";string sPlayer2 = "Player2";int main(int argc, char *argv[]){		char cChoice;			Init_SDL();	Init_Images();	Draw_Start();	Draw_BG();			int iDone=0;		//Uint8* keys2;	  while(iDone == 0)  {	SDL_Event event;    Uint8* keys;	keys = SDL_GetKeyState(NULL);    while ( SDL_PollEvent(&event) )    {      if ( event.type == SDL_QUIT )  {  iDone = 1;  }      if ( event.type == SDL_KEYDOWN )      {        if ( event.key.keysym.sym == SDLK_ESCAPE ) { iDone = 1; }      }    }    	//keys2 = SDL_GetKeyState(NULL);    //if ( keys[SDLK_UP] ) { ypos -= 1; }    //if ( keys[SDLK_DOWN] ) { ypos += 1; }	//Player 1	if ( keys[SDLK_LEFT] && (!Check_Limits_Left(xpos)) ) 	{ 		xpos -= 6; 	}	if ( keys[SDLK_RIGHT]&& (!Check_Limits_Right(xpos)) ) 	{ 		xpos += 6;	}	//Player 2	if ( keys[SDLK_a] && (!Check_Limits_Left(xpos2)) ) 	{ 		xpos2 -= 6; 	}    if ( keys[SDLK_s] && (!Check_Limits_Right(xpos2)) ) 	{ 		xpos2 += 6; 	}    Draw_scenes();	Move_Ball(xpos3,ypos3);  }  return 0;}void Init_SDL(){	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0 )	{		cout << "Can't init SDL" << endl << SDL_GetError();	}	screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);	if ( screen == NULL )	{		cout << "Can't set resolution to 640x480x32" << endl << SDL_GetError();	}}void Slock(SDL_Surface *screen){  if ( SDL_MUSTLOCK(screen) )  {    if ( SDL_LockSurface(screen) < 0 )    {      return;    }  }}void Sulock(SDL_Surface *screen){  if ( SDL_MUSTLOCK(screen) )  {    SDL_UnlockSurface(screen);  }}void Init_Images(){  back = SDL_LoadBMP("BG.bmp");  Palette1 = SDL_LoadBMP("Palette1.bmp");  Palette2 = SDL_LoadBMP("Palette2.bmp");  Balle = SDL_LoadBMP("Balle.bmp");  Start = SDL_LoadBMP("StartMenu.bmp");}void Draw_Start(){	DrawIMG(Start, 0,0);}void Draw_BG(){	Slock(screen);	DrawIMG(back, 0, 0);	Sulock(screen);}void DrawIMG(SDL_Surface *img, int x, int y){  SDL_Rect dest;  dest.x = x;  dest.y = y;  SDL_BlitSurface(img, NULL, screen, &dest);}void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2){  SDL_Rect dest;  dest.x = x;  dest.y = y;  SDL_Rect dest2;  dest2.x = x2;  dest2.y = y2;  dest2.w = w;  dest2.h = h;  SDL_BlitSurface(img, &dest2, screen, &dest);}void Draw_scenes(){ Slock(screen); Draw_BG(); DrawIMG(back,xpos-2,ypos-2,150,160,xpos-2,ypos-2); DrawIMG(Palette1, xpos,ypos); DrawIMG(Palette2, xpos2,ypos2); DrawIMG(Balle, xpos3, ypos3); SDL_Flip(screen); Sulock(screen);}bool Check_Limits_Left(int x){	bool bOk = false;	if ( x <= 0 )	{		bOk = true;	}	return bOk;}bool Check_Limits_Right(int x){	bool bOk = false;	if ( x >= 520 )	{		bOk = true;	}	return bOk;}void Move_Ball(int x, int y){	float Start_Time = 0;		//float fTicks = SDL_GetTicks();	while ( !Check_Win(xpos3, ypos3)   )	{		if((SDL_GetTicks() - Start_Time) > (1000))		{			xpos3 +=2;			ypos3 +=2;			Start_Time = SDL_GetTicks();		}	}}bool Check_Win(int x, int y){	bool bOk = false;	if ( y <= 0 )	{		bOk = true;		iPlayer1Score++;		xpos3 = 260;		ypos3 = 220;		Draw_scenes();	}	else	{		if ( y >= 640)		{			bOk = true;			iPlayer2Score++;			xpos3 = 260;			ypos3 = 220;			Draw_scenes();		}	}	return bOk;}


My code became a bit messy with all the change i tried to fix the speed of the ball, if there is something you don't understand in my logic ask.

This topic is closed to new replies.

Advertisement