summary refs log tree commit diff stats
path: root/src/animation.h
blob: 58df616c9ab4b4ab275a896dc40b745cafd35631 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef ANIMATION_H_74EB0901
#define ANIMATION_H_74EB0901

#include "renderer/texture.h"
#include <string>
#include <map>
#include <stdexcept>

class Animation {
public:

  Animation(
    size_t firstFrame,
    size_t numFrames,
    size_t delay) :
      firstFrame_(firstFrame),
      numFrames_(numFrames),
      delay_(delay)
  {
  }

  inline size_t getFirstFrame() const
  {
    return firstFrame_;
  }

  inline size_t getNumFrames() const
  {
    return numFrames_;
  }

  inline size_t getDelay() const
  {
    return delay_;
  }

private:

  size_t firstFrame_;
  size_t numFrames_;
  size_t delay_;
};

class AnimationSet {
public:

  AnimationSet(
    Texture texture,
    int frameWidth,
    int frameHeight,
    int framesAcross);

  void emplaceAnimation(
    std::string animation,
    size_t firstFrame,
    size_t numFrames,
    size_t delay);

  inline const Animation& getAnimation(std::string animation) const
  {
    if (!animations_.count(animation))
    {
      throw std::invalid_argument("Animation does not exist");
    }

    return animations_.at(animation);
  }

  inline const Texture& getTexture() const
  {
    return texture_;
  }

  inline int getFrameWidth() const
  {
    return frameWidth_;
  }

  inline int getFrameHeight() const
  {
    return frameHeight_;
  }

  inline int getFramesAcross() const
  {
    return framesAcross_;
  }

  Rectangle getFrameRect(int frame) const;

private:

  std::map<std::string, Animation> animations_;
  Texture texture_;
  int frameWidth_;
  int frameHeight_;
  int framesAcross_;
};

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