Sign in to follow this  
deathwearer

SDL - Problem with Pong like game(Fixed)

Recommended Posts

deathwearer    367
Hi, I'm curently working on a pong game with SDL, everything is working fine for now but i just got a minor problem. I did set 2 image to act like the "bar" ( the thing that stop and return the ball ) The first one is moving perfectly. The second move also, but he doenst erase is image when it's moving, so it's end up drawing a long line on the screen. here the code.
#include <SDL.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.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_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();


SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *Palette1;
SDL_Surface *Palette2;
SDL_Surface *Balle;

int xpos=320,ypos=460;
int xpos2=320,ypos2=20;
int xpos3=320,ypos3=220;



int main(int argc, char *argv[])
{
	Init_SDL();

	Init_Images();
	Draw_BG();

	int iDone=0;
	Uint8* keys;
	//Uint8* keys2;	

  while(iDone == 0)
  {
    SDL_Event event;

    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; }
      }
    }
    keys = SDL_GetKeyState(NULL);
	//keys2 = SDL_GetKeyState(NULL);
    //if ( keys[SDLK_UP] ) { ypos -= 1; }
    //if ( keys[SDLK_DOWN] ) { ypos += 1; }

    if ( keys[SDLK_LEFT] ) { xpos -= 1; }
    if ( keys[SDLK_RIGHT] ) { xpos += 1; }
	
	
	if ( keys[SDLK_a] ) { xpos2 -= 1; }
    if ( keys[SDLK_s] ) { xpos2 += 1; }

    Draw_scenes();
  }

  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");
}

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);
 DrawIMG(back,xpos-2,ypos-2,150,160,xpos-2,ypos-2);
 DrawIMG(Palette1, xpos,ypos);
 DrawIMG(Palette2, xpos2,ypos2);
 SDL_Flip(screen);
 Sulock(screen);
}



[Edited by - deathwearer on August 3, 2005 2:03:47 PM]

Share this post


Link to post
Share on other sites
Ekim_Gram    418
Call DrawBG() right before everything else in your draw scene function.

Also, there is no need to constantly lock and unlock the drawing surface. I don't see you drawing any pixels directly to your monitor.

Share this post


Link to post
Share on other sites
deathwearer    367
Quote:
Original post by Ekim_Gram
Call DrawBG() right before everything else in your draw scene function.

Also, there is no need to constantly lock and unlock the drawing surface. I don't see you drawing any pixels directly to your monitor.


1. Thank will do it

2. I know, it was because at first i was drawing the screen black with pixel but just did load a BG now.


Share this post


Link to post
Share on other sites
deathwearer    367
First problem is solved, Thank Ekim.

Now i have another problem, i'm trying to set the limit to where the bars can move, i think i got it right ( i assume lol ) but i get an error when compile.


#include <SDL.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.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_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);


SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *Palette1;
SDL_Surface *Palette2;
SDL_Surface *Balle;

int xpos=250,ypos=460;
int xpos2=250,ypos2=20;
int xpos3=250,ypos3=220;



int main(int argc, char *argv[])
{
Init_SDL();

Init_Images();
Draw_BG();

int iDone=0;
Uint8* keys;
//Uint8* keys2;

while(iDone == 0)
{
SDL_Event event;

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; }
}
}
keys = SDL_GetKeyState(NULL);
//keys2 = SDL_GetKeyState(NULL);
//if ( keys[SDLK_UP] ) { ypos -= 1; }
//if ( keys[SDLK_DOWN] ) { ypos += 1; }


if ( keys[SDLK_LEFT] && (!Check_Limits_Left(xpos)) )
{
xpos -= 3;
}

if ( keys[SDLK_RIGHT]&& (!Check_Limits_Right(xpos)) )
{
xpos += 3;
}




if ( keys[SDLK_a] ) { xpos2 -= 3; }
if ( keys[SDLK_s] ) { xpos2 += 3; }

Draw_scenes();
}

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");
}

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);
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 >= 640 )
{
bOk = true;
}

return bOk;
}



Tetris error LNK2019: unresolved external symbol "bool __cdecl Check_Limits_Left(int)" (?Check_Limits_Left@@YA_NH@Z) referenced in function _SDL_main
Tetris fatal error LNK1120: 1 unresolved externals

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