1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <sdl/sdl.h>
#include <sdl_image.h>
#include <string>
SDL_Surface *screen=NULL;
SDL_Surface *object=NULL;
SDL_Rect wall,obj1;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Event event;
SDL_Surface *load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
return optimizedImage;
}
void apply_surface(int x,int y, SDL_Surface *source,SDL_Surface *Destination,SDL_Rect *clip=NULL)
{
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface (source,clip,Destination,&offset);
}
int main(int argc,char *args[]){
int x,y,velx,vely;
x=0;
y=0;
velx=0;
vely=0;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
SDL_WM_SetCaption( "Can I Move the Dot though?", NULL );
wall.h=200;
wall.w=200;
wall.x=100;
wall.y=100;
bool quit = false;
while (quit!=true)
{
if (SDL_PollEvent (&event))
{
if (event.type ==SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:velx-=1; break;
case SDLK_DOWN:velx+=1 ; break;
case SDLK_LEFT:vely-=1 ; break;
case SDLK_RIGHT: vely+=1; break;
}
}
else if (event.type ==SDL_KEYUP)
{
switch (event.key.keysym.sym)
{
case SDLK_UP: velx+=1; break;
case SDLK_DOWN: velx-=1; break;
case SDLK_LEFT: vely+=1; break;
case SDLK_RIGHT: vely-=1; break;
case SDLK_ESCAPE: quit=true;break;
}
}
else if (event.type == SDL_QUIT)
{
quit = true;
}
}
obj1.x = x+=vely;
obj1.y = y+=velx;
obj1.w = 15;
obj1.h = 15;
if ((obj1.x<0)||(obj1.x>SCREEN_WIDTH+obj1.w))
{
x-=vely;
}
else if ( )
{
x-=vely;
}
SDL_FillRect (screen,NULL, SDL_MapRGB( screen->format, 255, 255, 255));
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 77, 77, 77 ) );
SDL_FillRect(screen,&obj1,SDL_MapRGB( screen->format,0,0,0));
SDL_Flip (screen);
}
return 0;
}
|