Jump to content
  • Advertisement
Sign in to follow this  
neon_prannock

My first SDL game

This topic is 4993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!