summary refs log tree commit diff stats
path: root/src/sprite.h
blob: dc26fadff793b4b6660b942120f002187e5cdb0a (plain) (blame)
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
#ifndef SPRITE_H_70503825
#define SPRITE_H_70503825

#include <map>
#include <string_view>
#include <vector>
#include "direction.h"
#include "renderer.h"
#include "vector.h"

struct SpriteFrame {
  SDL_Rect srcRect;
  vec2i center;
  vec2i size;
};

class Sprite {
public:

  Sprite(std::string_view filename, Renderer& renderer);

  int getTextureId() const {
    return textureId_;
  }

  const vec2i& loc() const { return loc_; }

  vec2i& loc() { return loc_; }

  const SpriteFrame& getFrame() const { return frames_.at(animations_[curAnim_][curFrame_]); }

  void setDirection(Direction dir);

  Direction getDirection() const { return curDir_; }

  void setState(std::string state);

  void tickAnim();

private:

  void updateAnimation();

  int textureId_;
  vec2i loc_ { 0, 0 };
  Direction curDir_ = Direction::down;
  std::string state_;
  int curAnim_ = 0;
  int curFrame_ = 0;
  std::vector<SpriteFrame> frames_;
  std::vector<std::vector<int>> animations_;
  std::map<std::string, std::map<Direction, int>> stateDirToAnim_;
};

#endif /* end of include guard: SPRITE_H_70503825 */