Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_Image.h>
  3. #include <SDL_ttf.h>
  4.  
  5. #include <string>
  6. #include <vector>
  7.  
  8. #define SCREEN_WIDTH 400
  9. #define SCREEN_HEIGHT 400
  10. #define SCREEN_BPP 32
  11.  
  12. #define CHAR_VELOCITY 22
  13.  
  14. SDL_Surface* Screen = NULL;
  15. SDL_Surface* Skin = NULL;
  16. SDL_Surface* BackGround = NULL;
  17.  
  18. SDL_Surface* Load_Image(std::string filename, bool Color_Key = false, int R=0, int G=0, int B=0);
  19. void Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL);
  20.  
  21. enum STATE
  22. {
  23.     E_STATE_STAND,
  24.     E_STATE_WALK_R,
  25.     E_STATE_DEATH,
  26.     E_COUNT
  27. };
  28.  
  29. class Character
  30. {
  31.     public:
  32.         Character();
  33.  
  34.         int
  35.             X,
  36.             Y,
  37.             VX,
  38.             VY,
  39.             Frame;
  40.        
  41.         STATE
  42.             state;
  43.  
  44.         std::vector< std::vector<SDL_Rect> > sprite;
  45.  
  46.         void UpdateState(STATE);
  47.  
  48.         int AddFrame(STATE,int,int,int,int);
  49.         int AddFrameEx(STATE,int=0,int=0,int=0,int=0);
  50. };
  51.  
  52. void Character::UpdateState(STATE newstate)
  53. {
  54.     Frame = 0;
  55.     state = newstate;
  56. }
  57.  
  58. int Character::AddFrameEx(STATE state, int x, int y, int w, int h)
  59. {
  60.     sprite[state].push_back(SDL_Rect());
  61.  
  62.     int
  63.         index = sprite[state].size()-1,
  64.         previous = index-1;
  65.  
  66.     if(!index)
  67.     {
  68.         sprite[state][index].x = x;
  69.         sprite[state][index].y = y;
  70.         sprite[state][index].w = w;
  71.         sprite[state][index].h = h;
  72.        
  73.         return index;
  74.     }
  75.     sprite[state][index].x = sprite[state][previous].x + x;
  76.     sprite[state][index].y = sprite[state][previous].y + y;
  77.     sprite[state][index].w = sprite[state][previous].w + w;
  78.     sprite[state][index].h = sprite[state][previous].h + h;
  79.  
  80.     return index;
  81. }
  82.  
  83. int Character::AddFrame(STATE state, int x, int y, int w, int h)
  84. {
  85.     sprite[state].push_back(SDL_Rect());
  86.  
  87.     int
  88.         index = sprite[state].size()-1;
  89.  
  90.     sprite[state][index].x = x;
  91.     sprite[state][index].y = y;
  92.     sprite[state][index].w = w;
  93.     sprite[state][index].h = h;
  94.  
  95.     return index;
  96. }
  97.  
  98. Character Mario;
  99.  
  100.  
  101. int main(int argc, char * args[])
  102. {
  103.     if(SDL_Init(SDL_INIT_EVERYTHING) != -1)
  104.     {
  105.         Screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
  106.  
  107.         if(Screen)
  108.         {
  109.             SDL_WM_SetCaption("Kyoshiro's SDL tests",NULL);
  110.  
  111.             BackGround = Load_Image("background.jpg");
  112.             Skin = Load_Image("mario.png");
  113.  
  114.             //Mario.AddFrame(E_STATE_WALK_R, 195,265,40,45); //old anim
  115.  
  116.             Mario.AddFrame(E_STATE_DEATH, 0,35,40,40);
  117.  
  118.             Mario.AddFrame(E_STATE_STAND, 195,265,40,45);
  119.  
  120.             int ff = Mario.AddFrame(E_STATE_WALK_R, 318,70,40,45);
  121.             Mario.AddFrameEx(E_STATE_WALK_R, Mario.sprite[E_STATE_WALK_R][ff].w);
  122.  
  123.  
  124.             while(true)
  125.             {
  126.                 SDL_Event Event;
  127.  
  128.                 int
  129.                     TX = Mario.X + Mario.VX,
  130.                     TY = Mario.Y + Mario.VY;
  131.  
  132.                 if(!(SCREEN_WIDTH-Mario.sprite[Mario.state][Mario.Frame].w >= TX && TX >= 0))
  133.                 {
  134.                     Mario.X = (TX > 0)?(SCREEN_WIDTH-Mario.sprite[Mario.state][Mario.Frame].w):(0);
  135.                 }
  136.                 else
  137.                 {
  138.                     Mario.X = TX;
  139.                 }
  140.  
  141.                 if(!(SCREEN_HEIGHT-Mario.sprite[Mario.state][Mario.Frame].h >= TY && TY >= 0))
  142.                 {
  143.                     Mario.Y = (TY > 0)?(SCREEN_HEIGHT-Mario.sprite[Mario.state][Mario.Frame].h):(0);
  144.                 }
  145.                 else
  146.                 {
  147.                     Mario.Y = TY;
  148.                 }
  149.  
  150.                 Apply_Surface(0,0,BackGround,Screen);
  151.                 Apply_Surface(Mario.X,Mario.Y,Skin,Screen,&Mario.sprite[Mario.state][Mario.Frame++]);
  152.  
  153.                 SDL_Flip(Screen);
  154.  
  155.                 while(SDL_PollEvent(&Event))
  156.                 {
  157.                     if(Event.type == SDL_KEYUP)
  158.                     {
  159.                         switch(Event.key.keysym.sym)
  160.                         {
  161.                             case SDLK_DOWN: Mario.VY -= CHAR_VELOCITY; break;
  162.                             case SDLK_UP: Mario.VY += CHAR_VELOCITY; break;
  163.                             case SDLK_LEFT: Mario.VX += CHAR_VELOCITY; break;
  164.                             case SDLK_RIGHT: Mario.VX -= CHAR_VELOCITY; break;
  165.                         }
  166.                     }
  167.                     if(Event.type == SDL_KEYDOWN)
  168.                     {
  169.                         switch(Event.key.keysym.sym)
  170.                         {
  171.                             //for testing
  172.                             case SDLK_F1: Mario.UpdateState(E_STATE_WALK_R); break;
  173.                             case SDLK_F2: Mario.UpdateState(E_STATE_STAND); break;
  174.                             case SDLK_F3: Mario.UpdateState(E_STATE_DEATH); break;
  175.  
  176.                             case SDLK_DOWN: Mario.VY += CHAR_VELOCITY; break;
  177.                             case SDLK_UP: Mario.VY -= CHAR_VELOCITY; break;
  178.                             case SDLK_LEFT: Mario.VX -= CHAR_VELOCITY; break;
  179.                             case SDLK_RIGHT: Mario.VX += CHAR_VELOCITY; break;
  180.                         }
  181.                     }
  182.                     if(Event.type == SDL_QUIT)
  183.                     {
  184.                         SDL_FreeSurface(Skin);
  185.                         SDL_FreeSurface(BackGround);
  186.  
  187.                         SDL_Quit();
  188.                         return 1;
  189.                     }
  190.                 }
  191.                 if(Mario.Frame >= Mario.sprite[Mario.state].size()) Mario.Frame = 0; //another issue ... iterators?
  192.  
  193.                 SDL_Delay(200);
  194.             }
  195.            
  196.         }
  197.     }
  198.     return 1;
  199. }
  200.  
  201. Character::Character()
  202. {
  203.     Frame = NULL,
  204.     X = NULL,
  205.     Y = NULL,
  206.     VX = NULL,
  207.     VY = NULL,
  208.     state = E_STATE_STAND;
  209.  
  210.  
  211.     for(int i = 0; i < E_COUNT; i++)
  212.     {
  213.         sprite.push_back(std::vector<SDL_Rect>());
  214.     }
  215. }
  216.  
  217. SDL_Surface* Load_Image(std::string filename, bool Color_Key, int R, int G, int B)
  218. {
  219.     SDL_Surface* loadedImage = NULL;
  220.     SDL_Surface* optimizedImage = NULL;
  221.    
  222.     loadedImage = IMG_Load(filename.c_str());
  223.    
  224.     if(loadedImage)
  225.     {
  226.         optimizedImage = SDL_DisplayFormat(loadedImage);
  227.         SDL_FreeSurface(loadedImage);
  228.    
  229.         if(optimizedImage)
  230.         {
  231.             if(Color_Key)
  232.             {
  233.                 Uint32 ColorKey = SDL_MapRGB(optimizedImage->format,R,G,B);
  234.                 SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, ColorKey);
  235.             }
  236.         }
  237.     }
  238.     return optimizedImage;
  239. }
  240.  
  241. void Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
  242. {
  243.     SDL_Rect offset;
  244.    
  245.     offset.x = x;
  246.     offset.y = y;
  247.    
  248.     SDL_BlitSurface(source, clip, destination, &offset );
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement