ScummVM API documentation
sprite.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef DRACI_SPRITE_H
23 #define DRACI_SPRITE_H
24 
25 #include "common/scummsys.h"
26 #include "common/rect.h"
27 
28 namespace Draci {
29 
30 enum DrawableType {
31  kDrawableText,
32  kDrawableSprite
33 };
34 
35 struct Displacement {
36  int relX, relY;
37  double extraScaleX, extraScaleY;
38 };
39 
40 extern const Displacement kNoDisplacement;
41 
42 class Surface;
43 class Font;
44 
45 class Drawable {
46 public:
47  virtual void draw(Surface *surface, bool markDirty, int relX, int relY) const = 0;
48  virtual void drawReScaled(Surface *surface, bool markDirty, const Displacement &displacement) const = 0;
49 
50  virtual ~Drawable() {}
51 
52  uint getWidth() const { return _width; }
53  uint getHeight() const { return _height; }
54 
55  uint getScaledWidth() const { return _scaledWidth; }
56  uint getScaledHeight() const { return _scaledHeight; }
57 
58  void setScaled(uint width, uint height) {
59  _scaledWidth = width;
60  _scaledHeight = height;
61  }
62 
63  int getX() const { return _x; }
64  int getY() const { return _y; }
65 
66  void setX(int x) { _x = x; }
67  void setY(int y) { _y = y; }
68 
69  void setDelay(int delay) { _delay = delay; }
70  int getDelay() const { return _delay; }
71 
72  virtual Common::Rect getRect(const Displacement &displacement) const = 0;
73 
74  virtual DrawableType getType() const = 0;
75 
76 protected:
77  uint _width;
78  uint _height;
79  uint _scaledWidth;
81  int _x, _y;
82 
86  int _delay;
87 };
88 
102 class Sprite : public Drawable {
103 public:
104  // Takes ownership of raw_data.
105  Sprite(uint16 width, uint16 height, byte *raw_data, int x, int y, bool columnwise);
106 
107  // It doesn't take ownership of sprite_data. If columnwise is false,
108  // it internally points to the original buffer (which has lifetime at
109  // least as long as this sprite, assumming that it comes from a cached
110  // BArchive file), otherwise it allocates its own buffer with the
111  // transposed image.
112  Sprite(const byte *sprite_data, uint16 length, int x, int y, bool columnwise);
113 
114  ~Sprite() override;
115 
116  void draw(Surface *surface, bool markDirty, int relX, int relY) const override;
117  void drawReScaled(Surface *surface, bool markDirty, const Displacement &displacement) const override;
118 
119  void setMirrorOn() { _mirror = true; }
120  void setMirrorOff() { _mirror = false; }
121 
122  Common::Rect getRect(const Displacement &displacement) const override;
123 
124  const byte *getBuffer() const { return _data; }
125  int getPixel(int x, int y, const Displacement &displacement) const;
126 
127  DrawableType getType() const override { return kDrawableSprite; }
128 
129 private:
130  bool _ownsData;
131  const byte *_data;
132  bool _mirror;
133 };
134 
135 class Text : public Drawable {
136 
137 public:
138  Text(const Common::String &str, const Font *font, byte fontColor,
139  int x, int y, uint spacing);
140  ~Text() override {}
141 
142  void setText(const Common::String &str);
143  void setColor(byte fontColor) { _color = fontColor; }
144  void setSpacing(uint spacing) { _spacing = spacing; }
145  void setFont(const Font *font);
146 
147  void repeatedlySplitLongLines(uint maxWidth);
148 
149  uint getLength() const { return _length; }
150 
151  void draw(Surface *surface, bool markDirty, int relX, int relY) const override;
152 
153  // drawReScaled just calls draw so that Text can be accessed through a Drawable pointer.
154  // Text scaling does not need to be handled.
155  void drawReScaled(Surface *surface, bool markDirty, const Displacement &displacement) const override { draw(surface, markDirty, displacement.relX, displacement.relY); }
156  Common::Rect getRect(const Displacement &displacement) const override;
157 
158  DrawableType getType() const override { return kDrawableText; }
159 private:
160  void splitLinesLongerThan(uint maxWidth);
161 
162  Common::String _text;
163  uint _length;
164  uint8 _color;
165  uint _spacing;
166  const Font *_font;
167 };
168 
169 } // End of namespace Draci
170 
171 #endif // DRACI_SPRITE_H
Definition: font.h:54
Definition: str.h:59
Definition: rect.h:144
uint _height
Height of the sprite.
Definition: sprite.h:78
uint _scaledWidth
Scaled width of the sprite.
Definition: sprite.h:79
int _y
Sprite coordinates.
Definition: sprite.h:81
uint _width
Width of the sprite.
Definition: sprite.h:77
Definition: sprite.h:102
uint _scaledHeight
Scaled height of the sprite.
Definition: sprite.h:80
int _delay
Definition: sprite.h:86
Definition: surface.h:31
Definition: animation.h:30
Definition: sprite.h:45
Definition: sprite.h:35
Definition: sprite.h:135