Advertisement
Redxone

[SDL2] Pokemon Style Movement - Player class

May 26th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.49 KB | None | 0 0
  1. // SDLHelper.h
  2.  
  3. struct Image
  4. {
  5.     SDL_Surface* surface;
  6.     SDL_Texture* texture;
  7.     SDL_Rect source;
  8. };
  9.  
  10. void drawImage(SDL_Renderer *r, Image *image, SDL_Rect *pos)
  11. {
  12.     SDL_RenderCopy(r,image->texture,&image->source,pos);
  13. }
  14.  
  15. Image* getImageFromFile(SDL_Renderer *r, char* file)
  16. {
  17.     Image *img = new Image();
  18.     img->surface = IMG_Load(file);
  19.     if(img->surface == NULL)
  20.     {
  21.         std::cout << "File doesn't exist: " << file << std::endl; throw img->surface;
  22.     }
  23.     img->texture = SDL_CreateTextureFromSurface(r,img->surface);
  24.     img->source  = (SDL_Rect) {1,1,img->surface->w,img->surface->h};
  25.  
  26.     return img;
  27. }
  28.  
  29. void disposeImage(Image* img)
  30. {
  31.     SDL_FreeSurface(img->surface);
  32.     SDL_DestroyTexture(img->texture);
  33.     delete img;
  34. }
  35.  
  36. // main.cpp
  37.  
  38. class Player
  39. {
  40. public:
  41.     // Movement vars
  42.     int xvel, yvel, speed, tile, dir;
  43.     bool moving;
  44.     bool firstStep;
  45.     int anim_frame;
  46.     uint32_t ticks, delay, anim;
  47.  
  48.     // Input vars
  49.     struct Buttons
  50.     {
  51.         int BT_FORWARD;
  52.         int BT_BACK;
  53.         int BT_LEFT;
  54.         int BT_RIGHT;
  55.     };
  56.     Buttons buttons;
  57.     uint32_t ActiveButtons;
  58.     enum InputButtons
  59.     {
  60.         BT_FORWARD = 0x01,
  61.         BT_BACK    = 0x02,
  62.         BT_LEFT    = 0x04,
  63.         BT_RIGHT   = 0x08,
  64.     };
  65.     enum Directions
  66.     {
  67.         DIR_DOWN = 0,
  68.         ANIM_DOWN,
  69.         ANIM_DOWN_FLIP,
  70.         DIR_UP,
  71.         ANIM_UP,
  72.         ANIM_UP_FLIP,
  73.         DIR_LEFT,
  74.         ANIM_LEFT,
  75.         DIR_RIGHT,
  76.         ANIM_RIGHT
  77.     };
  78.  
  79.     // Graphics vars
  80.     SDL_Rect unit;
  81.     Image* graphic;
  82.  
  83.  
  84.     Player(int x, int y, int w, int h, int speed, int tiles, char* img)
  85.     {
  86.         this->graphic = new Image();
  87.         this->unit.x = x - (x%tiles);
  88.         this->unit.y = y - (y%tiles);
  89.         this->unit.w = w;
  90.         this->unit.h = h;
  91.         this->graphic = getImageFromFile(window->render,img);
  92.         this->graphic->source.y = 0;
  93.         this->graphic->source.w = w;
  94.         this->graphic->source.h = h;
  95.         this->anim_frame = 0;
  96.         this->setFrame(DIR_DOWN);
  97.  
  98.         this->xvel = this->unit.x;
  99.         this->yvel = this->unit.y;
  100.         this->moving = false;
  101.         this->speed = speed;
  102.         this->anim = 120;
  103.         this->anim_frame = 0;
  104.         this->ticks = SDL_GetTicks();
  105.         this->delay = 50;
  106.         this->tile = tiles;
  107.  
  108.         this->buttons.BT_FORWARD = SDLK_w;
  109.         this->buttons.BT_BACK    = SDLK_s;
  110.         this->buttons.BT_LEFT    = SDLK_a;
  111.         this->buttons.BT_RIGHT   = SDLK_d;
  112.         // Setup default buttons
  113.         this->ActiveButtons = 0;
  114.  
  115.     }
  116.     ~Player()
  117.     {
  118.         disposeImage(this->graphic);
  119.     }
  120.     virtual void animateDirection(int len, int* frames)
  121.     {
  122.         this->setFrame(frames[this->anim_frame]);
  123.         this->anim_frame++;
  124.         if(this->anim_frame > len-1)this->anim_frame = 0;
  125.     }
  126.     virtual void draw()
  127.     {
  128.         drawImage(window->render,this->graphic,&(*this).unit);
  129.     }
  130.     virtual void setFrame(int frame)
  131.     {
  132.         this->graphic->source.x = frame*this->unit.w;
  133.     }
  134.     virtual void changeButtons(int key, bool bSet)
  135.     {
  136.         if(bSet)
  137.         {
  138.             if(key == this->buttons.BT_FORWARD)this->ActiveButtons |= BT_FORWARD;
  139.             if(key == this->buttons.BT_BACK   )this->ActiveButtons |= BT_BACK;
  140.             if(key == this->buttons.BT_LEFT   )this->ActiveButtons |= BT_LEFT;
  141.             if(key == this->buttons.BT_RIGHT  )this->ActiveButtons |= BT_RIGHT;
  142.         }
  143.         else
  144.         {
  145.             if(key == this->buttons.BT_FORWARD)this->ActiveButtons &= ~BT_FORWARD;
  146.             if(key == this->buttons.BT_BACK   )this->ActiveButtons &= ~BT_BACK;
  147.             if(key == this->buttons.BT_LEFT   )this->ActiveButtons &= ~BT_LEFT;
  148.             if(key == this->buttons.BT_RIGHT  )this->ActiveButtons &= ~BT_RIGHT;
  149.         }
  150.     }
  151.     virtual void update(SDL_Event *ev)
  152.     {
  153.         if(ev->type == SDL_KEYDOWN)
  154.         {
  155.             int key = ev->key.keysym.sym;
  156.             changeButtons(key,1);
  157.         }
  158.         if(ev->type == SDL_KEYUP)
  159.         {
  160.             int key = ev->key.keysym.sym;
  161.             changeButtons(key,0);
  162.         }
  163.  
  164.         if( !this->moving && (SDL_GetTicks()-this->ticks) > this->delay)
  165.         {
  166.             if( this->ActiveButtons & BT_FORWARD )
  167.             {
  168.                 if(this->dir == DIR_UP)
  169.                 {
  170.                     this->yvel -= this->tile;
  171.                     this->moving = true;
  172.                 }
  173.                 else
  174.                 {
  175.                    this->setFrame(DIR_UP);
  176.                    this->dir = DIR_UP;
  177.                    this->ticks = SDL_GetTicks();
  178.                    return;
  179.                 }
  180.             }
  181.             else if(this->ActiveButtons & BT_BACK)
  182.             {
  183.                     if(this->dir == DIR_DOWN)
  184.                     {
  185.                         this->yvel += this->tile;
  186.                         this->moving = true;
  187.                     }
  188.                     else
  189.                     {
  190.                         this->setFrame(DIR_DOWN);
  191.                         this->dir = DIR_DOWN;
  192.                         this->ticks = SDL_GetTicks();
  193.                         return;
  194.                     }
  195.             }
  196.             else if(this->ActiveButtons & BT_LEFT)
  197.             {
  198.                 if(this->dir == DIR_LEFT)
  199.                 {
  200.                     this->xvel -= this->tile;
  201.                     this->moving = true;
  202.                 }
  203.                 else
  204.                 {
  205.                     this->setFrame(DIR_LEFT);
  206.                     this->dir = DIR_LEFT;
  207.                     this->ticks = SDL_GetTicks();
  208.                     return;
  209.                 }
  210.             }
  211.             else if(this->ActiveButtons & BT_RIGHT)
  212.             {
  213.                 if(this->dir == DIR_RIGHT)
  214.                 {
  215.                     this->xvel += this->tile;
  216.                     this->moving = true;
  217.                 }
  218.                 else
  219.                 {
  220.                     this->setFrame(DIR_RIGHT);
  221.                     this->dir = DIR_RIGHT;
  222.                     this->ticks = SDL_GetTicks();
  223.                     return;
  224.                 }
  225.             }
  226.         }
  227.  
  228.  
  229.         window->setView( (this->unit.x), (this->unit.y) );
  230.  
  231.         if(this->moving)
  232.         {
  233.             //cout << "Moving..." <<endl;
  234.             //cout << "From X: " << this->unit.x << " To X: " << this->xvel << endl;
  235.             //cout << "From Y: " << this->unit.y << " To Y: " << this->yvel << endl;
  236.             int moved = 0;
  237.  
  238.             if(this->unit.x < this->xvel)
  239.             {
  240.                 if( (SDL_GetTicks() - this->ticks) > this->anim)
  241.                 {
  242.                     int anims[2] = {ANIM_RIGHT,DIR_RIGHT};
  243.                     animateDirection(2,anims);
  244.                     this->ticks = SDL_GetTicks();
  245.                 }
  246.                 this->unit.x += this->speed;
  247.             }
  248.             if(this->unit.x > this->xvel)
  249.             {
  250.                 if( (SDL_GetTicks() - this->ticks) > this->anim)
  251.                 {
  252.                     int anims[2] = {ANIM_LEFT,DIR_LEFT};
  253.                     animateDirection(2,anims);
  254.                     this->ticks = SDL_GetTicks();
  255.                 }
  256.                 this->unit.x -= this->speed;
  257.             }
  258.             if(this->unit.y < this->yvel)
  259.             {
  260.                 if( (SDL_GetTicks() - this->ticks) > this->anim)
  261.                 {
  262.                     int anims[2] = {ANIM_DOWN,DIR_DOWN};
  263.                     if(!this->firstStep) anims[0] = ANIM_DOWN_FLIP;
  264.                     animateDirection(2,anims);
  265.                     this->ticks = SDL_GetTicks();
  266.                 }
  267.                 this->unit.y += this->speed;
  268.             }
  269.             if(this->unit.y > this->yvel)
  270.             {
  271.                 if( (SDL_GetTicks() - this->ticks) > this->anim)
  272.                 {
  273.                     int anims[2] = {ANIM_UP,DIR_UP};
  274.                     if(!this->firstStep)anims[0] = ANIM_UP_FLIP;
  275.                     animateDirection(2,anims);
  276.                     this->ticks = SDL_GetTicks();
  277.                 }
  278.                 this->unit.y -= this->speed;
  279.             }
  280.             if(this->unit.x == this->xvel && this->unit.y == this->yvel)
  281.             {
  282.                 this->anim_frame = 0;
  283.                 this->firstStep = !this->firstStep;
  284.                 this->setFrame(this->dir);
  285.                 this->ticks = SDL_GetTicks();
  286.                 this->moving = false;
  287.             }
  288.         }
  289.     }
  290. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement