ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
allegro_bitmap.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 //=============================================================================
23 //
24 // Allegro lib based bitmap
25 //
26 // TODO: probably should be moved to the Engine; check again when (if) it is
27 // clear that AGS.Native does not need allegro for drawing.
28 //
29 //=============================================================================
30 
31 #ifndef AGS_SHARED_GFX_ALLEGRO_BITMAP_H
32 #define AGS_SHARED_GFX_ALLEGRO_BITMAP_H
33 
34 #include "graphics/screen.h"
35 #include "ags/lib/allegro.h" // BITMAP
36 #include "ags/shared/core/types.h"
37 #include "ags/shared/gfx/bitmap.h"
38 #include "ags/shared/util/string.h"
39 
40 namespace AGS3 {
41 namespace AGS {
42 namespace Shared {
43 
44 class Bitmap {
45 public:
46  Bitmap();
47  Bitmap(int width, int height, int color_depth = 0);
48  Bitmap(Bitmap *src, const Rect &rc);
49  Bitmap(BITMAP *al_bmp, bool shared_data);
50  ~Bitmap();
51 
52  // Allocate new bitmap.
53  // NOTE: color_depth is in BITS per pixel (i.e. 8, 16, 24, 32...).
54  // NOTE: in all of these color_depth may be passed as 0 in which case a default
55  // color depth will be used (as previously set for the system).
56  // TODO: color_depth = 0 is used to call Allegro's create_bitmap, which uses
57  // some global color depth setting; not sure if this is OK to use for generic class,
58  // revise this in future
59  bool Create(int width, int height, int color_depth = 0);
60  // Create Bitmap and clear to transparent color
61  bool CreateTransparent(int width, int height, int color_depth = 0);
62  // Creates a sub-bitmap of the given bitmap; the sub-bitmap is a reference to
63  // particular region inside a parent.
64  // WARNING: the parent bitmap MUST be kept in memory for as long as sub-bitmap exists!
65  bool CreateSubBitmap(Bitmap *src, const Rect &rc);
66  // Resizes existing sub-bitmap within the borders of its parent
67  bool ResizeSubBitmap(int width, int height);
68  // Creates a plain copy of the given bitmap, optionally converting to a different color depth;
69  // pass color depth 0 to keep the original one.
70  bool CreateCopy(Bitmap *src, int color_depth = 0);
71  // TODO: this is a temporary solution for plugin support
72  // Wraps a raw allegro BITMAP object, optionally owns it (will delete on disposal)
73  bool WrapAllegroBitmap(BITMAP *al_bmp, bool shared_data);
74  // Deallocate bitmap
75  void Destroy();
76 
77  bool LoadFromFile(const String &filename) {
78  return LoadFromFile(filename.GetCStr());
79  }
80  bool LoadFromFile(const char *filename);
81  bool LoadFromFile(PACKFILE *pf);
82  bool SaveToFile(const String &filename, const void *palette) {
83  return SaveToFile(filename.GetCStr(), palette);
84  }
85  bool SaveToFile(Common::WriteStream &out, const void *palette);
86  bool SaveToFile(const char *filename, const void *palette);
87 
88  // TODO: This is temporary solution for cases when we cannot replace
89  // use of raw BITMAP struct with Bitmap
90  inline BITMAP *GetAllegroBitmap() {
91  return _alBitmap;
92  }
93 
94  // Is this a "normal" bitmap created by application which data can be directly accessed for reading and writing
95  inline bool IsMemoryBitmap() const {
96  return true;
97  }
98  // Is this a video bitmap
99  inline bool IsVideoBitmap() const {
100  return dynamic_cast<Graphics::Screen *>(_alBitmap) != nullptr;
101  }
102  // Is this a linear bitmap, the one that can be accessed linearly within each scanline
103  inline bool IsLinearBitmap() const {
104  return true;
105  }
106 
107  // Is this a subbitmap, referencing a part of another, bigger one?
108  inline bool isSubBitmap() const {
109  return _alBitmap->isSubBitmap();
110  }
111 
112  // Do both bitmaps share same data (usually: subbitmaps, or parent/subbitmap)
113  inline bool IsSameBitmap(Bitmap *other) const {
114  return is_same_bitmap(_alBitmap, other->_alBitmap) != 0;
115  }
116 
117  // Checks if bitmap cannot be used
118  inline bool IsNull() const {
119  return !_alBitmap;
120  }
121  // Checks if bitmap has zero size: either width or height (or both) is zero
122  inline bool IsEmpty() const {
123  return GetWidth() == 0 || GetHeight() == 0;
124  }
125  inline int GetWidth() const {
126  return _alBitmap->w;
127  }
128  inline int GetHeight() const {
129  return _alBitmap->h;
130  }
131  inline Size GetSize() const {
132  return Size(_alBitmap->w, _alBitmap->h);
133  }
134  // Get sub-bitmap's offset position within its parent
135  inline Point GetSubOffset() const {
136  Common::Point pt = _alBitmap->getOffsetFromOwner();
137  return Point(pt.x, pt.y);
138  }
139  inline int GetColorDepth() const {
140  return bitmap_color_depth(_alBitmap);
141  }
142  // BPP: bytes per pixel
143  inline int GetBPP() const {
144  return (GetColorDepth() + 7) / 8;
145  }
146 
147  // CHECKME: probably should not be exposed, see comment to GetData()
148  inline int GetDataSize() const {
149  return GetWidth() * GetHeight() * GetBPP();
150  }
151  // Gets scanline length in bytes (is the same for any scanline)
152  inline int GetLineLength() const {
153  return GetWidth() * GetBPP();
154  }
155 
156  // TODO: replace with byte *
157  // Gets a pointer to underlying graphic data
158  // FIXME: actually not a very good idea, since there's no 100% guarantee the scanline positions in memory are sequential
159  inline const unsigned char *GetData() const {
160  return _alBitmap->getPixels();
161  }
162 
163  // Get scanline for direct reading
164  inline const unsigned char *GetScanLine(int index) const {
165  assert(index >= 0 && index < GetHeight());
166  return _alBitmap->getBasePtr(0, index);
167  }
168  inline unsigned char *GetScanLine(int index) {
169  assert(index >= 0 && index < GetHeight());
170  return (unsigned char *)_alBitmap->getBasePtr(0, index);
171  }
172 
173  // Get bitmap's mask color (transparent color)
174  inline color_t GetMaskColor() const {
175  return bitmap_mask_color(_alBitmap);
176  }
177 
178  // Converts AGS color-index into RGB color according to the bitmap format.
179  // TODO: this method was added to the Bitmap class during large refactoring,
180  // but that's a mistake, because in retrospect is has nothing to do with
181  // bitmap itself and should rather be a part of the game data logic.
182  color_t GetCompatibleColor(color_t color);
183 
184  //=========================================================================
185  // Clipping
186  // TODO: consider implementing push-pop clipping stack logic.
187  //=========================================================================
188  void SetClip(const Rect &rc);
189  void ResetClip();
190  Rect GetClip() const;
191 
192  //=========================================================================
193  // Blitting operations (drawing one bitmap over another)
194  //=========================================================================
195  // Draw other bitmap over current one
196  void Blit(Bitmap *src, int dst_x = 0, int dst_y = 0, BitmapMaskOption mask = kBitmap_Copy);
197  void Blit(Bitmap *src, int src_x, int src_y, int dst_x, int dst_y, int width, int height, BitmapMaskOption mask = kBitmap_Copy);
198  // Draw other bitmap in a masked mode (kBitmap_Transparency)
199  void MaskedBlit(Bitmap *src, int dst_x, int dst_y);
200  // Draw other bitmap, stretching or shrinking its size to given values
201  void StretchBlt(Bitmap *src, const Rect &dst_rc, BitmapMaskOption mask = kBitmap_Copy);
202  void StretchBlt(Bitmap *src, const Rect &src_rc, const Rect &dst_rc, BitmapMaskOption mask = kBitmap_Copy);
203  // Antia-aliased stretch-blit
204  void AAStretchBlt(Bitmap *src, const Rect &dst_rc, BitmapMaskOption mask = kBitmap_Copy);
205  void AAStretchBlt(Bitmap *src, const Rect &src_rc, const Rect &dst_rc, BitmapMaskOption mask = kBitmap_Copy);
206  // TODO: find more general way to call these operations, probably require pointer to Blending data struct?
207  // Draw bitmap using translucency preset
208  void TransBlendBlt(Bitmap *src, int dst_x, int dst_y);
209  // Draw bitmap using lighting preset
210  void LitBlendBlt(Bitmap *src, int dst_x, int dst_y, int light_amount);
211  // TODO: generic "draw transformed" function? What about mask option?
212  void FlipBlt(Bitmap *src, int dst_x, int dst_y, GraphicFlip flip);
213  void RotateBlt(Bitmap *src, int dst_x, int dst_y, fixed_t angle);
214  void RotateBlt(Bitmap *src, int dst_x, int dst_y, int pivot_x, int pivot_y, fixed_t angle);
215 
216  //=========================================================================
217  // Pixel operations
218  //=========================================================================
219  // Fills the whole bitmap with given color (black by default)
220  void Clear(color_t color = 0);
221  void ClearTransparent();
222  // The PutPixel and GetPixel are supposed to be safe and therefore
223  // relatively slow operations. They should not be used for changing large
224  // blocks of bitmap memory - reading/writing from/to scan lines should be
225  // done in such cases.
226  void PutPixel(int x, int y, color_t color);
227  int GetPixel(int x, int y) const;
228 
229  //=========================================================================
230  // Vector drawing operations
231  //=========================================================================
232  void DrawLine(const Line &ln, color_t color);
233  void DrawTriangle(const Triangle &tr, color_t color);
234  void DrawRect(const Rect &rc, color_t color);
235  void FillRect(const Rect &rc, color_t color);
236  void FillCircle(const Circle &circle, color_t color);
237  // Fills the whole bitmap with given color
238  void Fill(color_t color);
239  void FillTransparent();
240  // Floodfills an enclosed area, starting at point
241  void FloodFill(int x, int y, color_t color);
242 
243  //=========================================================================
244  // Direct access operations
245  //=========================================================================
246  // TODO: think how to increase safety over this (some fixed memory buffer class with iterator?)
247  // Gets scanline for directly writing into it
248  inline unsigned char *GetScanLineForWriting(int index) {
249  assert(index >= 0 && index < GetHeight());
250  return _alBitmap->line[index];
251  }
252  inline unsigned char *GetDataForWriting() {
253  return _alBitmap->line[0];
254  }
255  // Copies buffer contents into scanline
256  void SetScanLine(int index, unsigned char *data, int data_size = -1);
257 
258 private:
259  BITMAP *_alBitmap;
260  bool _isDataOwner;
261 };
262 
263 
264 
265 namespace BitmapHelper {
266 // TODO: revise those functions later (currently needed in a few very specific cases)
267 // NOTE: the resulting object __owns__ bitmap data from now on
268 Bitmap *CreateRawBitmapOwner(BITMAP *al_bmp);
269 // NOTE: the resulting object __does not own__ bitmap data
270 Bitmap *CreateRawBitmapWrapper(BITMAP *al_bmp);
271 } // namespace BitmapHelper
272 
273 } // namespace Shared
274 } // namespace AGS
275 } // namespace AGS3
276 
277 #endif
Definition: achievements_tables.h:27
Definition: allegro_bitmap.h:44
Definition: stream.h:77
Definition: file.h:62
Definition: geometry.h:87
Definition: surface.h:32
Definition: screen.h:48
Definition: geometry.h:314
Definition: geometry.h:118
Definition: rect.h:45
Definition: geometry.h:219
Definition: color.h:49
Definition: string.h:62
int16 x
Definition: rect.h:46
Definition: geometry.h:148
Definition: geometry.h:341
int16 y
Definition: rect.h:47
Definition: ags.h:40