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
|
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#include "character.h"
using namespace std;
SDL_Surface *optimage(string filename)
{
SDL_Surface* loadedImage=NULL;
SDL_Surface* optimizedImage;
optimizedImage=NULL;
loadedImage=IMG_Load(filename.c_str());
if(loadedImage!=NULL )
{
optimizedImage=SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
int main(int argc, char** argv)
{
SDL_Surface* screen;
screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
SDL_Event event;
Uint32 start;
bool running=true;
bool arr[4]={0,0,0,0};
character player(optimage("16sprite.png"),20,20,128,192,1,1);
while(running)
{
start=SDL_GetTicks();
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running=false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
arr[0]=1;
break;
case SDLK_LEFT:
arr[1]=1;
break;
case SDLK_DOWN:
arr[2]=1;
break;
case SDLK_RIGHT:
arr[3]=1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
arr[0]=0;
break;
case SDLK_LEFT:
arr[1]=0;
break;
case SDLK_DOWN:
arr[2]=0;
break;
case SDLK_RIGHT:
arr[3]=0;
break;
}
break;
}
}
if(arr[0])
player.moveUp();
if(arr[1])
player.moveLeft();
if(arr[2])
player.moveDown();
if(arr[3])
player.moveRight();
SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));
player.show();
if(1000/30>SDL_GetTicks()-start)
SDL_Delay(1000/30-(SDL_GetTicks()-start));
}
SDL_Quit();
}
|