Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SDLHelper.h
- struct Image
- {
- SDL_Surface* surface;
- SDL_Texture* texture;
- SDL_Rect source;
- };
- void drawImage(SDL_Renderer *r, Image *image, SDL_Rect *pos)
- {
- SDL_RenderCopy(r,image->texture,&image->source,pos);
- }
- Image* getImageFromFile(SDL_Renderer *r, char* file)
- {
- Image *img = new Image();
- img->surface = IMG_Load(file);
- if(img->surface == NULL)
- {
- std::cout << "File doesn't exist: " << file << std::endl; throw img->surface;
- }
- img->texture = SDL_CreateTextureFromSurface(r,img->surface);
- img->source = (SDL_Rect) {1,1,img->surface->w,img->surface->h};
- return img;
- }
- void disposeImage(Image* img)
- {
- SDL_FreeSurface(img->surface);
- SDL_DestroyTexture(img->texture);
- delete img;
- }
- // main.cpp
- class Player
- {
- public:
- // Movement vars
- int xvel, yvel, speed, tile, dir;
- bool moving;
- bool firstStep;
- int anim_frame;
- uint32_t ticks, delay, anim;
- // Input vars
- struct Buttons
- {
- int BT_FORWARD;
- int BT_BACK;
- int BT_LEFT;
- int BT_RIGHT;
- };
- Buttons buttons;
- uint32_t ActiveButtons;
- enum InputButtons
- {
- BT_FORWARD = 0x01,
- BT_BACK = 0x02,
- BT_LEFT = 0x04,
- BT_RIGHT = 0x08,
- };
- enum Directions
- {
- DIR_DOWN = 0,
- ANIM_DOWN,
- ANIM_DOWN_FLIP,
- DIR_UP,
- ANIM_UP,
- ANIM_UP_FLIP,
- DIR_LEFT,
- ANIM_LEFT,
- DIR_RIGHT,
- ANIM_RIGHT
- };
- // Graphics vars
- SDL_Rect unit;
- Image* graphic;
- Player(int x, int y, int w, int h, int speed, int tiles, char* img)
- {
- this->graphic = new Image();
- this->unit.x = x - (x%tiles);
- this->unit.y = y - (y%tiles);
- this->unit.w = w;
- this->unit.h = h;
- this->graphic = getImageFromFile(window->render,img);
- this->graphic->source.y = 0;
- this->graphic->source.w = w;
- this->graphic->source.h = h;
- this->anim_frame = 0;
- this->setFrame(DIR_DOWN);
- this->xvel = this->unit.x;
- this->yvel = this->unit.y;
- this->moving = false;
- this->speed = speed;
- this->anim = 120;
- this->anim_frame = 0;
- this->ticks = SDL_GetTicks();
- this->delay = 50;
- this->tile = tiles;
- this->buttons.BT_FORWARD = SDLK_w;
- this->buttons.BT_BACK = SDLK_s;
- this->buttons.BT_LEFT = SDLK_a;
- this->buttons.BT_RIGHT = SDLK_d;
- // Setup default buttons
- this->ActiveButtons = 0;
- }
- ~Player()
- {
- disposeImage(this->graphic);
- }
- virtual void animateDirection(int len, int* frames)
- {
- this->setFrame(frames[this->anim_frame]);
- this->anim_frame++;
- if(this->anim_frame > len-1)this->anim_frame = 0;
- }
- virtual void draw()
- {
- drawImage(window->render,this->graphic,&(*this).unit);
- }
- virtual void setFrame(int frame)
- {
- this->graphic->source.x = frame*this->unit.w;
- }
- virtual void changeButtons(int key, bool bSet)
- {
- if(bSet)
- {
- if(key == this->buttons.BT_FORWARD)this->ActiveButtons |= BT_FORWARD;
- if(key == this->buttons.BT_BACK )this->ActiveButtons |= BT_BACK;
- if(key == this->buttons.BT_LEFT )this->ActiveButtons |= BT_LEFT;
- if(key == this->buttons.BT_RIGHT )this->ActiveButtons |= BT_RIGHT;
- }
- else
- {
- if(key == this->buttons.BT_FORWARD)this->ActiveButtons &= ~BT_FORWARD;
- if(key == this->buttons.BT_BACK )this->ActiveButtons &= ~BT_BACK;
- if(key == this->buttons.BT_LEFT )this->ActiveButtons &= ~BT_LEFT;
- if(key == this->buttons.BT_RIGHT )this->ActiveButtons &= ~BT_RIGHT;
- }
- }
- virtual void update(SDL_Event *ev)
- {
- if(ev->type == SDL_KEYDOWN)
- {
- int key = ev->key.keysym.sym;
- changeButtons(key,1);
- }
- if(ev->type == SDL_KEYUP)
- {
- int key = ev->key.keysym.sym;
- changeButtons(key,0);
- }
- if( !this->moving && (SDL_GetTicks()-this->ticks) > this->delay)
- {
- if( this->ActiveButtons & BT_FORWARD )
- {
- if(this->dir == DIR_UP)
- {
- this->yvel -= this->tile;
- this->moving = true;
- }
- else
- {
- this->setFrame(DIR_UP);
- this->dir = DIR_UP;
- this->ticks = SDL_GetTicks();
- return;
- }
- }
- else if(this->ActiveButtons & BT_BACK)
- {
- if(this->dir == DIR_DOWN)
- {
- this->yvel += this->tile;
- this->moving = true;
- }
- else
- {
- this->setFrame(DIR_DOWN);
- this->dir = DIR_DOWN;
- this->ticks = SDL_GetTicks();
- return;
- }
- }
- else if(this->ActiveButtons & BT_LEFT)
- {
- if(this->dir == DIR_LEFT)
- {
- this->xvel -= this->tile;
- this->moving = true;
- }
- else
- {
- this->setFrame(DIR_LEFT);
- this->dir = DIR_LEFT;
- this->ticks = SDL_GetTicks();
- return;
- }
- }
- else if(this->ActiveButtons & BT_RIGHT)
- {
- if(this->dir == DIR_RIGHT)
- {
- this->xvel += this->tile;
- this->moving = true;
- }
- else
- {
- this->setFrame(DIR_RIGHT);
- this->dir = DIR_RIGHT;
- this->ticks = SDL_GetTicks();
- return;
- }
- }
- }
- window->setView( (this->unit.x), (this->unit.y) );
- if(this->moving)
- {
- //cout << "Moving..." <<endl;
- //cout << "From X: " << this->unit.x << " To X: " << this->xvel << endl;
- //cout << "From Y: " << this->unit.y << " To Y: " << this->yvel << endl;
- int moved = 0;
- if(this->unit.x < this->xvel)
- {
- if( (SDL_GetTicks() - this->ticks) > this->anim)
- {
- int anims[2] = {ANIM_RIGHT,DIR_RIGHT};
- animateDirection(2,anims);
- this->ticks = SDL_GetTicks();
- }
- this->unit.x += this->speed;
- }
- if(this->unit.x > this->xvel)
- {
- if( (SDL_GetTicks() - this->ticks) > this->anim)
- {
- int anims[2] = {ANIM_LEFT,DIR_LEFT};
- animateDirection(2,anims);
- this->ticks = SDL_GetTicks();
- }
- this->unit.x -= this->speed;
- }
- if(this->unit.y < this->yvel)
- {
- if( (SDL_GetTicks() - this->ticks) > this->anim)
- {
- int anims[2] = {ANIM_DOWN,DIR_DOWN};
- if(!this->firstStep) anims[0] = ANIM_DOWN_FLIP;
- animateDirection(2,anims);
- this->ticks = SDL_GetTicks();
- }
- this->unit.y += this->speed;
- }
- if(this->unit.y > this->yvel)
- {
- if( (SDL_GetTicks() - this->ticks) > this->anim)
- {
- int anims[2] = {ANIM_UP,DIR_UP};
- if(!this->firstStep)anims[0] = ANIM_UP_FLIP;
- animateDirection(2,anims);
- this->ticks = SDL_GetTicks();
- }
- this->unit.y -= this->speed;
- }
- if(this->unit.x == this->xvel && this->unit.y == this->yvel)
- {
- this->anim_frame = 0;
- this->firstStep = !this->firstStep;
- this->setFrame(this->dir);
- this->ticks = SDL_GetTicks();
- this->moving = false;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement