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
|
#include <sdl/sdl.h>
#include <sdl/sdl_image.h>
#include <string>
//SDL_Surface *background=NULL;
SDL_Surface *screen=NULL;
SDL_Surface *object=NULL;
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 velx = 0;
int vely = 0;
SDL_FillRect (screen,NULL, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF));
SDL_Init(SDL_INIT_EVERYTHING);//above if applied makes it not workify//
bool quit = false;
int x = 0;
int y = 0;
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
SDL_WM_SetCaption( "Can I Move the Dot though?", NULL );
object = load_image("object.png");
//background = load_image( );
//apply_surface (SCREEN_WIDTH,SCREEN_HEIGHT,background,screen);
while (quit!=true)
{
if (SDL_PollEvent (&event))
{
if (event.type ==SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP: y -= 10 / 2; break;
case SDLK_DOWN: y +=10 / 2; break;
case SDLK_LEFT: x -= 10/ 2; break;
case SDLK_RIGHT: x += 10 / 2; break;
}
}
else if (event.type ==SDL_KEYUP)
{
switch (event.key.keysym.sym)
{
case SDLK_UP: y += 10 / 2; break;
case SDLK_DOWN: y -=10 / 2; break;
case SDLK_LEFT: x += 10/ 2; break;
case SDLK_RIGHT: x -= 10 / 2; break;
}
}
else if (event.type == SDL_QUIT)
{
quit = true;
}
}
SDL_Delay (1);
apply_surface (velx=velx+x,vely=vely+y,object,screen);
SDL_Flip (screen);
}
SDL_FreeSurface (object);
return 0;
}
|