Skip to content

Instantly share code, notes, and snippets.

@banthar
Created July 16, 2010 23:57
Show Gist options
  • Save banthar/479081 to your computer and use it in GitHub Desktop.
Save banthar/479081 to your computer and use it in GitHub Desktop.
tiny game
#include <SDL.h>
#include <SDL_image.h>
typedef struct {int x,y;}Point;
int main()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *background=IMG_Load("game.png");
Point cell={160,128};
Point offset={background->w-cell.x,background->h-cell.y};
SDL_Surface* display=SDL_SetVideoMode(cell.x,cell.y,0,0);
int x=0;
int y=0;
while(1)
{
SDL_Event e;
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
return 0;
case SDL_KEYDOWN:
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
return 0;
case SDLK_LEFT:
x-=cell.x;
break;
case SDLK_RIGHT:
x+=cell.x;
break;
case SDLK_UP:
y-=cell.y;
break;
case SDLK_DOWN:
y+=cell.y;
break;
default:
break;
}
if(x<0)
x=0;
if(y<0)
y=0;
if(x>offset.x)
x=offset.x;
if(y>offset.y)
y=offset.y;
break;
}
}
SDL_BlitSurface(background,&(SDL_Rect){x,y,display->w,display->h},display,NULL);
SDL_Flip(display);
}
}
@kthakore
Copy link

This is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment