Sign in to follow this  
neon_prannock

My first SDL game

Recommended Posts

I'm learning SDL and I've just made this game. It´s very simple: you can control a little smile on the screen, with the arrows, and when the smile colides with the ball bouncering around, the game ends. Please test it and give your opinion! ;-) Download Game

Share this post


Link to post
Share on other sites
I could download it, so it must be running again...

You should try to expand the game, then it would be fun to play.
Right now I would say it was more or less like an alpha version, but not bad at all...

Share this post


Link to post
Share on other sites
Copy paste the link dont click because if you click it wont work. Another thing is that the app runs 2x slower once you move the smiley. If you dont everything runs faster.!

Share this post


Link to post
Share on other sites
Here's the source code (without comments)


#include <stdio.h>
#include <stdlib.h>

#include <SDL/SDL.h>

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *smile;
SDL_Surface *screen;

int xpos=25,ypos=20,xx=1,yy=1,apos=25,bpos=300,key;

int InitImages()
{
back = SDL_LoadBMP("bg.bmp");
image = SDL_LoadBMP("ball.bmp");
SDL_SetColorKey(image, SDL_SRCCOLORKEY | SDL_RLEACCEL ,SDL_MapRGB(image->format,0,0,0) );
smile = SDL_LoadBMP("smile.bmp");
SDL_SetColorKey(smile, SDL_SRCCOLORKEY | SDL_RLEACCEL ,SDL_MapRGB(smile->format,255,255,255) );
return 0;
}

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

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 DrawBG()
{
DrawIMG(back, 0, 0);
}

void DrawScene()
{
DrawIMG(back, xpos-1, ypos-1, 51, 51, xpos-1, ypos-1);
DrawIMG(image, xpos, ypos);

SDL_Flip(screen);
}

void DrawSmile()
{
DrawIMG(back, apos-1, bpos-1, 31, 31, apos-1, bpos-1);
DrawIMG(smile,apos,bpos);

SDL_Flip(screen);
}

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

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(700,400,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 700x400 video: %s\n", SDL_GetError());
exit(1);
}

InitImages();
DrawBG();
DrawSmile();

int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}

xpos=xpos+xx;
ypos=ypos+yy;
if ((xpos==644)|(xpos==0))
{
xx=xx*-1;
}
if ((ypos==344)|(ypos==0))
{
yy=yy*-1;
}

DrawScene();

if (((apos+30>xpos)&&(apos<xpos+50))&&((bpos+30>ypos)&&(bpos<ypos+50)))
{
exit(1);
}

keys = SDL_GetKeyState(NULL);
if ( (keys[SDLK_UP])&&(bpos!=6) ) { bpos -= 1;key=1; }
if ( (keys[SDLK_DOWN])&&(bpos!=365) ) { bpos += 1;key=1; }
if ( (keys[SDLK_LEFT])&&(apos!=6) ) { apos -= 1;key=1; }
if ( (keys[SDLK_RIGHT])&&(apos!=665) ) { apos += 1;key=1; }
if (key==1)
{
DrawSmile();
key=0;
}
}

return 0;
}




And I'm already making an Pong type game. Thanks for the comments! ;-)

Any sugestion on the code, and how to make it faster?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes, use a backbuffer. That should do the trick.

Share this post


Link to post
Share on other sites
Hey, Good job man!

I've only just rigged up the SDL library last night, so this is the first SDL code I've looked at, And even w/o comments, it's nice and easy and straight foward, and I'll further disect it to learn how to program myself some SDL soon enough.

On a more relevant note, the problem is your updating the screen once per loop when your not pressing a key;
DrawScene()
and twice when you are;
DrawScene()
and
DrawSmile()

Just chuck the contents of
DrawSmile()

into the bottom of
DrawScene()

and get rid of all reference to
DrawSmile()

(The one line down the bottom of your code, or the whole if statement if you like)

This way the consistancy isn't changing depending on wether your pressing a key or not.

BTW, that other guys idea probebly works too, but hey, I'm new to this.

and most importantly; GOOD LUCK!

Share this post


Link to post
Share on other sites
Quote:
Original post by Toadhead
the program closes directly after I opened the program once compiled..

Can anyone tell me why?

do you have a copy of SDL.dll in the working directory?

Share this post


Link to post
Share on other sites
Nice [smile]!!

I tried compiling the source at first with gcc, since you used c header's. I received a few errors so I tried g++ and it compiled without as much as a warning. You should use the new c++ header files (cstdlib, etc). Other than that very good job! Seems like pong would be the next step.

Share this post


Link to post
Share on other sites
Thanks for all the comments and sugestions!!!

I'm already working on a pong clone. The images are very basic so I have to ask someone to draw them for me.

I have to try your sugestions to make the game with a smooth speed. I just didn't understand what a backbuffer is. Can someone explain me?
What about the code appearence? I've never taken I class in programming, so I'm not used to making the code for someone to understand it...

Share this post


Link to post
Share on other sites
Quote:
Original post by neon_prannock
I have to try your sugestions to make the game with a smooth speed. I just didn't understand what a backbuffer is. Can someone explain me?


I don't know what a backbuffer is either, but like I said, your calling SDL_Flip(screen) once per loop, or twice if your holding a key. Just merge DrawScene()and DrawSmile() (the two functions with SDL_Flip(screen) in them) and only have SDL_Flip(screen) once at the end of the function, that way it's only being called once per loop, not twice if your holding a key.

Share this post


Link to post
Share on other sites
Quote:
Original post by neon_prannock
I have to try your sugestions to make the game with a smooth speed.

In your main game loop, put in the following code:

// Tick Delay
// Delays each tick so that there is smooth motion.
// To set the amount of delay, change the 30 to something else.
// This number is roughly in milliseconds.
static Uint32 next_time; // The scheduled next tick time
Uint32 now; // The current tick time
now = SDL_GetTicks(); // Get the time
if(next_time > now) // Check if the scheduled time is now
SDL_Delay(next_time - now); // If it is, delay
next_time += 30; // Reschedule the next delay time



That should provide smooth motion.

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