-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.h
More file actions
70 lines (60 loc) · 1.54 KB
/
shape.h
File metadata and controls
70 lines (60 loc) · 1.54 KB
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
#ifndef SHAPE_H
#define SHAPE_H
#include <vector>
struct Pixel{
int rowOffset;
int colOffset;
char ch;
};
class Shape{
public:
virtual ~Shape() = default;
virtual std::vector<Pixel> pixels() const = 0;
virtual void advanceFrame() {}
};
class CharShape: public Shape{
char ch;
public:
explicit CharShape(char c): ch{c} {}
std::vector<Pixel> pixels() const override {
return{ Pixel{0, 0, ch} };
}
};
class RectShape: public Shape{
int h, w;
char ch;
public:
RectShape(int height, int width, char c): h{height}, w{width}, ch{c} {}
std::vector<Pixel> pixels() const override {
std::vector<Pixel> pixels;
pixels.reserve(h * w);
for(int r = 0; r < h; ++r){
for(int c = 0; c < w; ++c){
pixels.push_back(Pixel{r, c, ch});
}
}
return pixels;
}
};
class BitmapShape: public Shape{
std::vector<Pixel> data;
public:
BitmapShape(std::vector<Pixel> pixels): data{std::move(pixels)}{}
std::vector<Pixel> pixels() const override {
return data;
}
};
class AnimatedShape: public Shape{
std::vector<std::vector<Pixel>> frames;
int current = 0;
public:
explicit AnimatedShape(std::vector<std::vector<Pixel>> f): frames{std::move(f)} {}
std::vector<Pixel> pixels() const override {
if(frames.empty()) return {};
return frames[current];
}
void advanceFrame() override {
if(!frames.empty()) current = (current + 1) % static_cast<int>(frames.size());
}
};
#endif