Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <SDL_Image.h>
- #include <SDL_ttf.h>
- #include <string>
- #include <vector>
- #define SCREEN_WIDTH 400
- #define SCREEN_HEIGHT 400
- #define SCREEN_BPP 32
- #define CHAR_VELOCITY 22
- SDL_Surface* Screen = NULL;
- SDL_Surface* Skin = NULL;
- SDL_Surface* BackGround = NULL;
- SDL_Surface* Load_Image(std::string filename, bool Color_Key = false, int R=0, int G=0, int B=0);
- void Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL);
- enum STATE
- {
- E_STATE_STAND,
- E_STATE_WALK_R,
- E_STATE_DEATH,
- E_COUNT
- };
- class Character
- {
- public:
- Character();
- int
- X,
- Y,
- VX,
- VY,
- Frame;
- STATE
- state;
- std::vector< std::vector<SDL_Rect> > sprite;
- void UpdateState(STATE);
- int AddFrame(STATE,int,int,int,int);
- int AddFrameEx(STATE,int=0,int=0,int=0,int=0);
- };
- void Character::UpdateState(STATE newstate)
- {
- Frame = 0;
- state = newstate;
- }
- int Character::AddFrameEx(STATE state, int x, int y, int w, int h)
- {
- sprite[state].push_back(SDL_Rect());
- int
- index = sprite[state].size()-1,
- previous = index-1;
- if(!index)
- {
- sprite[state][index].x = x;
- sprite[state][index].y = y;
- sprite[state][index].w = w;
- sprite[state][index].h = h;
- return index;
- }
- sprite[state][index].x = sprite[state][previous].x + x;
- sprite[state][index].y = sprite[state][previous].y + y;
- sprite[state][index].w = sprite[state][previous].w + w;
- sprite[state][index].h = sprite[state][previous].h + h;
- return index;
- }
- int Character::AddFrame(STATE state, int x, int y, int w, int h)
- {
- sprite[state].push_back(SDL_Rect());
- int
- index = sprite[state].size()-1;
- sprite[state][index].x = x;
- sprite[state][index].y = y;
- sprite[state][index].w = w;
- sprite[state][index].h = h;
- return index;
- }
- Character Mario;
- int main(int argc, char * args[])
- {
- if(SDL_Init(SDL_INIT_EVERYTHING) != -1)
- {
- Screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
- if(Screen)
- {
- SDL_WM_SetCaption("Kyoshiro's SDL tests",NULL);
- BackGround = Load_Image("background.jpg");
- Skin = Load_Image("mario.png");
- //Mario.AddFrame(E_STATE_WALK_R, 195,265,40,45); //old anim
- Mario.AddFrame(E_STATE_DEATH, 0,35,40,40);
- Mario.AddFrame(E_STATE_STAND, 195,265,40,45);
- int ff = Mario.AddFrame(E_STATE_WALK_R, 318,70,40,45);
- Mario.AddFrameEx(E_STATE_WALK_R, Mario.sprite[E_STATE_WALK_R][ff].w);
- while(true)
- {
- SDL_Event Event;
- int
- TX = Mario.X + Mario.VX,
- TY = Mario.Y + Mario.VY;
- if(!(SCREEN_WIDTH-Mario.sprite[Mario.state][Mario.Frame].w >= TX && TX >= 0))
- {
- Mario.X = (TX > 0)?(SCREEN_WIDTH-Mario.sprite[Mario.state][Mario.Frame].w):(0);
- }
- else
- {
- Mario.X = TX;
- }
- if(!(SCREEN_HEIGHT-Mario.sprite[Mario.state][Mario.Frame].h >= TY && TY >= 0))
- {
- Mario.Y = (TY > 0)?(SCREEN_HEIGHT-Mario.sprite[Mario.state][Mario.Frame].h):(0);
- }
- else
- {
- Mario.Y = TY;
- }
- Apply_Surface(0,0,BackGround,Screen);
- Apply_Surface(Mario.X,Mario.Y,Skin,Screen,&Mario.sprite[Mario.state][Mario.Frame++]);
- SDL_Flip(Screen);
- while(SDL_PollEvent(&Event))
- {
- if(Event.type == SDL_KEYUP)
- {
- switch(Event.key.keysym.sym)
- {
- case SDLK_DOWN: Mario.VY -= CHAR_VELOCITY; break;
- case SDLK_UP: Mario.VY += CHAR_VELOCITY; break;
- case SDLK_LEFT: Mario.VX += CHAR_VELOCITY; break;
- case SDLK_RIGHT: Mario.VX -= CHAR_VELOCITY; break;
- }
- }
- if(Event.type == SDL_KEYDOWN)
- {
- switch(Event.key.keysym.sym)
- {
- //for testing
- case SDLK_F1: Mario.UpdateState(E_STATE_WALK_R); break;
- case SDLK_F2: Mario.UpdateState(E_STATE_STAND); break;
- case SDLK_F3: Mario.UpdateState(E_STATE_DEATH); break;
- case SDLK_DOWN: Mario.VY += CHAR_VELOCITY; break;
- case SDLK_UP: Mario.VY -= CHAR_VELOCITY; break;
- case SDLK_LEFT: Mario.VX -= CHAR_VELOCITY; break;
- case SDLK_RIGHT: Mario.VX += CHAR_VELOCITY; break;
- }
- }
- if(Event.type == SDL_QUIT)
- {
- SDL_FreeSurface(Skin);
- SDL_FreeSurface(BackGround);
- SDL_Quit();
- return 1;
- }
- }
- if(Mario.Frame >= Mario.sprite[Mario.state].size()) Mario.Frame = 0; //another issue ... iterators?
- SDL_Delay(200);
- }
- }
- }
- return 1;
- }
- Character::Character()
- {
- Frame = NULL,
- X = NULL,
- Y = NULL,
- VX = NULL,
- VY = NULL,
- state = E_STATE_STAND;
- for(int i = 0; i < E_COUNT; i++)
- {
- sprite.push_back(std::vector<SDL_Rect>());
- }
- }
- SDL_Surface* Load_Image(std::string filename, bool Color_Key, int R, int G, int B)
- {
- SDL_Surface* loadedImage = NULL;
- SDL_Surface* optimizedImage = NULL;
- loadedImage = IMG_Load(filename.c_str());
- if(loadedImage)
- {
- optimizedImage = SDL_DisplayFormat(loadedImage);
- SDL_FreeSurface(loadedImage);
- if(optimizedImage)
- {
- if(Color_Key)
- {
- Uint32 ColorKey = SDL_MapRGB(optimizedImage->format,R,G,B);
- SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, ColorKey);
- }
- }
- }
- return optimizedImage;
- }
- void Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
- {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, clip, destination, &offset );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement