ScummVM API documentation
managed_surface.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 GRAPHICS_MANAGED_SURFACE_H
23 #define GRAPHICS_MANAGED_SURFACE_H
24 
25 #include "graphics/pixelformat.h"
26 #include "graphics/surface.h"
27 #include "graphics/transform_struct.h"
28 #include "common/types.h"
29 #include "graphics/blit.h"
30 
31 #define MS_RGB(R,G,B) (uint32)(((R) << 24) | ((G) << 16) | ((B) << 8) | 0xff)
32 #define MS_ARGB(A,R,G,B) (uint32)(((R) << 24) | ((G) << 16) | ((B) << 8) | (A))
33 
34 namespace Graphics {
35 
36 class Palette;
37 
52 private:
56  Surface _innerSurface;
57 
62  DisposeAfterUse::Flag _disposeAfterUse;
63 
68  ManagedSurface *_owner;
69 
74  Common::Point _offsetFromOwner;
75 
80  uint _transparentColor;
81  bool _transparentColorSet;
82 
86  Palette *_palette;
87 protected:
91  void blitFromInner(const Surface &src, const Common::Rect &srcRect,
92  const Common::Rect &destRect, const Palette *srcPalette);
93 
97  void transBlitFromInner(const Surface &src, const Common::Rect &srcRect,
98  const Common::Rect &destRect, uint32 transColor, bool flipped, uint32 overrideColor,
99  uint32 srcAlpha, const Palette *srcPalette, const Palette *dstPalette,
100  const Surface *mask, bool maskOnly);
101 public:
105  bool clip(Common::Rect& srcBounds, Common::Rect& destBounds) const {
106  return _innerSurface.clip(srcBounds, destBounds);
107  }
108 
109 public:
110  int16 &w;
111  int16 &h;
112  int32 &pitch;
114 public:
118  ManagedSurface();
119 
127  ManagedSurface(const ManagedSurface &surf);
128 
132  ManagedSurface(int width, int height);
133 
137  ManagedSurface(int width, int height, const Graphics::PixelFormat &pixelFormat);
138 
142  ManagedSurface(ManagedSurface &surf, const Common::Rect &bounds);
143 
150  ManagedSurface(Surface *surf, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES);
151 
155  ManagedSurface(const Surface *surf);
156 
160  virtual ~ManagedSurface();
161 
169  operator const Surface &() const { return _innerSurface; }
170 
178  const Surface &rawSurface() const { return _innerSurface; }
179  Surface *surfacePtr() { return &_innerSurface; }
180 
187 
191  bool empty() const { return w == 0 || h == 0 || _innerSurface.getPixels() == nullptr; }
192 
196  DisposeAfterUse::Flag disposeAfterUse() const { return _disposeAfterUse; }
197 
206  inline uint32 getPixel(int x, int y) const {
207  return _innerSurface.getPixel(x, y);
208  }
209 
217  inline void setPixel(int x, int y, uint32 pixel) {
218  return _innerSurface.setPixel(x, y, pixel);
219  }
220 
229  inline const void *getBasePtr(int x, int y) const {
230  return _innerSurface.getBasePtr(x, y);
231  }
232 
241  inline void *getBasePtr(int x, int y) {
242  return _innerSurface.getBasePtr(x, y);
243  }
244 
248  inline void *getPixels() { return _innerSurface.getPixels(); }
250  inline const void *getPixels() const { return _innerSurface.getPixels(); }
251 
255  virtual void setPixels(void *newPixels);
256 
260  virtual void create(int16 width, int16 height);
261 
265  virtual void create(int16 width, int16 height, const PixelFormat &pixelFormat);
266 
276  virtual void create(ManagedSurface &surf, const Common::Rect &bounds);
277 
283  virtual void free();
284 
288  virtual void clearDirtyRects() {}
289 
294  virtual void addDirtyRect(const Common::Rect &r);
295 
300  const Common::Point getOffsetFromOwner() const { return _offsetFromOwner; }
301 
305  const Common::Rect getBounds() const {
306  return Common::Rect(0, 0, this->w, this->h);
307  }
308 
312  void blitFrom(const Surface &src, const Palette *srcPalette = nullptr);
313 
317  void blitFrom(const Surface &src, const Common::Point &destPos, const Palette *srcPalette = nullptr);
318 
322  void blitFrom(const Surface &src, const Common::Rect &srcRect,
323  const Common::Point &destPos, const Palette *srcPalette = nullptr);
324 
328  void blitFrom(const Surface &src, const Common::Rect &srcRect,
329  const Common::Rect &destRect, const Palette *srcPalette = nullptr);
330 
334  void blitFrom(const ManagedSurface &src, const Common::Rect &srcRect,
335  const Common::Rect &destRect);
336 
340  void blitFrom(const ManagedSurface &src);
341 
345  void blitFrom(const ManagedSurface &src, const Common::Point &destPos);
346 
350  void blitFrom(const ManagedSurface &src, const Common::Rect &srcRect,
351  const Common::Point &destPos);
352 
364  void transBlitFrom(const Surface &src, uint32 transColor = 0, bool flipped = false,
365  uint32 overrideColor = 0, uint32 srcAlpha = 0xff, const Palette *srcPalette = nullptr);
366 
379  void transBlitFrom(const Surface &src, const Common::Point &destPos,
380  uint32 transColor = 0, bool flipped = false, uint32 overrideColor = 0, uint32 srcAlpha = 0xff, const Palette *srcPalette = nullptr);
381 
390  void transBlitFrom(const Surface &src, const Common::Point &destPos,
391  const ManagedSurface &mask, const Palette *srcPalette = nullptr);
392 
401  void transBlitFrom(const Surface &src, const Common::Point &destPos,
402  const Surface &mask, const Palette *srcPalette = nullptr);
403 
417  void transBlitFrom(const Surface &src, const Common::Rect &srcRect, const Common::Point &destPos,
418  uint32 transColor = 0, bool flipped = false, uint32 overrideColor = 0, uint32 srcAlpha = 0xff, const Palette *srcPalette = nullptr);
419 
429  void transBlitFrom(const Surface &src, const Common::Rect &srcRect, const Common::Rect &destRect, const Palette *srcPalette);
430 
447  void transBlitFrom(const Surface &src, const Common::Rect &srcRect, const Common::Rect &destRect,
448  uint32 transColor = 0, bool flipped = false, uint32 overrideColor = 0, uint32 srcAlpha = 0xff,
449  const Surface *mask = nullptr, bool maskOnly = false, const Palette *srcPalette = nullptr);
450 
461  void transBlitFrom(const ManagedSurface &src, uint32 transColor = 0, bool flipped = false,
462  uint32 overrideColor = 0, uint32 srcAlpha = 0xff);
463 
475  void transBlitFrom(const ManagedSurface &src, const Common::Point &destPos,
476  uint32 transColor = 0, bool flipped = false, uint32 overrideColor = 0, uint32 srcAlpha = 0xff);
477 
485  void transBlitFrom(const ManagedSurface &src, const Common::Point &destPos,
486  const ManagedSurface &mask);
487 
500  void transBlitFrom(const ManagedSurface &src, const Common::Rect &srcRect, const Common::Point &destPos,
501  uint32 transColor = 0, bool flipped = false, uint32 overrideColor = 0, uint32 srcAlpha = 0xff);
502 
518  void transBlitFrom(const ManagedSurface &src, const Common::Rect &srcRect, const Common::Rect &destRect,
519  uint32 transColor = 0, bool flipped = false, uint32 overrideColor = 0, uint32 srcAlpha = 0xff,
520  const Surface *mask = nullptr, bool maskOnly = false);
521 
525  void rawBlitFrom(const ManagedSurface &src, const Common::Rect &srcRect,
526  const Common::Point &destPos) {
527  blitFromInner(src._innerSurface, srcRect, Common::Rect(destPos.x, destPos.y, destPos.x + srcRect.width(),
528  destPos.y + srcRect.height()), src._palette);
529  }
530 
536  static inline bool isBlendBlitPixelFormatSupported(const PixelFormat &src, const PixelFormat &dst) {
538  }
539 
555  const int posX = 0, const int posY = 0,
556  const int flipping = FLIP_NONE,
557  const Common::Rect *srcRect = nullptr,
558  const uint colorMod = MS_ARGB(255, 255, 255, 255),
559  const int width = -1, const int height = -1,
560  const TSpriteBlendMode blend = BLEND_NORMAL,
561  const AlphaType alphaType = ALPHA_FULL);
563  const int posX = 0, const int posY = 0,
564  const int flipping = FLIP_NONE,
565  const Common::Rect *srcRect = nullptr,
566  const uint colorMod = MS_ARGB(255, 255, 255, 255),
567  const int width = -1, const int height = -1,
568  const TSpriteBlendMode blend = BLEND_NORMAL,
569  const AlphaType alphaType = ALPHA_FULL);
570 
574  void clear(uint32 color = 0);
575 
579  void markAllDirty();
580 
586  void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height) {
587  _innerSurface.copyRectToSurface(buffer, srcPitch, destX, destY, width, height);
588  }
589 
595  void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect) {
596  _innerSurface.copyRectToSurface(srcSurface, destX, destY, subRect);
597  }
598 
604  void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key) {
605  _innerSurface.copyRectToSurfaceWithKey(buffer, srcPitch, destX, destY, width, height, key);
606  }
607 
613  void copyRectToSurfaceWithKey(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect, uint32 key) {
614  _innerSurface.copyRectToSurfaceWithKey(srcSurface, destX, destY, subRect, key);
615  }
616 
621  void copyFrom(const ManagedSurface &surf);
622 
627  void copyFrom(const Surface &surf);
628 
632  void drawLine(int x0, int y0, int x1, int y1, uint32 color) {
633  _innerSurface.drawLine(x0, y0, x1, y1, color);
634  addDirtyRect(Common::Rect(MIN(x0, x1), MIN(y0, y1), MAX(x0, x1 + 1), MAX(y0, y1 + 1)));
635  }
636 
640  void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color) {
641  _innerSurface.drawThickLine(x0, y0, x1, y1, penX, penY, color);
642  addDirtyRect(Common::Rect(MIN(x0, x1 + penX), MIN(y0, y1 + penY), MAX(x0, x1 + penX), MAX(y0, y1 + penY)));
643  }
644 
648  void hLine(int x, int y, int x2, uint32 color) {
649  _innerSurface.hLine(x, y, x2, color);
650  addDirtyRect(Common::Rect(x, y, x2 + 1, y + 1));
651  }
652 
656  void vLine(int x, int y, int y2, uint32 color) {
657  _innerSurface.vLine(x, y, y2, color);
658  addDirtyRect(Common::Rect(x, y, x + 1, y2 + 1));
659  }
660 
664  void fillRect(Common::Rect r, uint32 color) {
665  _innerSurface.fillRect(r, color);
666  addDirtyRect(r);
667  }
668 
672  void frameRect(const Common::Rect &r, uint32 color) {
673  _innerSurface.frameRect(r, color);
674  addDirtyRect(r);
675  }
676 
682  addDirtyRect(area);
683  return _innerSurface.getSubArea(area);
684  }
685 
695  void convertToInPlace(const PixelFormat &dstFormat) {
696  _innerSurface.convertToInPlace(dstFormat);
697  }
698 
710  void convertToInPlace(const PixelFormat &dstFormat, const byte *palette, uint16 paletteCount) {
711  _innerSurface.convertToInPlace(dstFormat, palette, paletteCount);
712  }
713 
717  uint getTransparentColor() const { return _transparentColor; }
718 
722  void setTransparentColor(uint32 color) {
723  _transparentColor = color;
724  _transparentColorSet = true;
725  }
726 
731  _transparentColorSet = false;
732  }
733 
737  bool hasTransparentColor() const {
738  return _transparentColorSet;
739  }
740 
744  void clearPalette();
745 
749  bool hasPalette() const;
750 
754  void grabPalette(byte *colors, uint start, uint num) const;
755 
759  void setPalette(const byte *colors, uint start, uint num);
760 };
762 } // End of namespace Graphics
763 
764 #endif
void clear(uint32 color=0)
Definition: managed_surface.h:51
void convertToInPlace(const PixelFormat &dstFormat)
Definition: managed_surface.h:695
const Common::Rect getBounds() const
Definition: managed_surface.h:305
static PixelFormat getSupportedPixelFormat()
Definition: blit.h:337
int16 & w
Definition: managed_surface.h:110
uint getTransparentColor() const
Definition: managed_surface.h:717
Definition: surface.h:66
Common::Rect blendBlitTo(ManagedSurface &target, const int posX=0, const int posY=0, const int flipping=FLIP_NONE, const Common::Rect *srcRect=nullptr, const uint colorMod=(uint32)(((255)<< 24)|((255)<< 16)|((255)<< 8)|(255)), const int width=-1, const int height=-1, const TSpriteBlendMode blend=BLEND_NORMAL, const AlphaType alphaType=ALPHA_FULL)
Renders this surface onto target.
virtual void addDirtyRect(const Common::Rect &r)
bool clip(Common::Rect &srcBounds, Common::Rect &destBounds) const
Definition: managed_surface.h:105
Surface getSubArea(const Common::Rect &area)
void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key)
Definition: managed_surface.h:604
virtual void create(int16 width, int16 height)
void rawBlitFrom(const ManagedSurface &src, const Common::Rect &srcRect, const Common::Point &destPos)
Definition: managed_surface.h:525
Definition: pixelformat.h:138
void convertToInPlace(const PixelFormat &dstFormat)
Definition: surface.h:358
Surface getSubArea(const Common::Rect &area)
Definition: managed_surface.h:681
static bool isBlendBlitPixelFormatSupported(const PixelFormat &src, const PixelFormat &dst)
Definition: managed_surface.h:536
virtual void clearDirtyRects()
Definition: managed_surface.h:288
int32 & pitch
Definition: managed_surface.h:112
void * getBasePtr(int x, int y)
Definition: managed_surface.h:241
DisposeAfterUse::Flag disposeAfterUse() const
Definition: managed_surface.h:196
void setTransparentColor(uint32 color)
Definition: managed_surface.h:722
void clearTransparentColor()
Definition: managed_surface.h:730
const void * getBasePtr(int x, int y) const
Definition: managed_surface.h:229
void transBlitFromInner(const Surface &src, const Common::Rect &srcRect, const Common::Rect &destRect, uint32 transColor, bool flipped, uint32 overrideColor, uint32 srcAlpha, const Palette *srcPalette, const Palette *dstPalette, const Surface *mask, bool maskOnly)
void drawLine(int x0, int y0, int x1, int y1, uint32 color)
Definition: managed_surface.h:632
Definition: rect.h:144
void copyRectToSurfaceWithKey(const void *buffer, int srcPitch, int destX, int destY, int width, int height, uint32 key)
void transBlitFrom(const Surface &src, uint32 transColor=0, bool flipped=false, uint32 overrideColor=0, uint32 srcAlpha=0xff, const Palette *srcPalette=nullptr)
bool empty() const
Definition: managed_surface.h:191
void vLine(int x, int y, int y2, uint32 color)
Definition: managed_surface.h:656
void copyFrom(const ManagedSurface &surf)
void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color)
Definition: managed_surface.h:640
bool hasTransparentColor() const
Definition: managed_surface.h:737
bool clip(Common::Rect &srcBounds, Common::Rect &destBounds) const
void blitFrom(const Surface &src, const Palette *srcPalette=nullptr)
int16 & h
Definition: managed_surface.h:111
const void * getBasePtr(int x, int y) const
Definition: surface.h:137
int16 width() const
Definition: rect.h:191
void hLine(int x, int y, int x2, uint32 color)
void copyRectToSurface(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect)
Definition: managed_surface.h:595
void * getPixels()
Definition: managed_surface.h:248
void drawThickLine(int x0, int y0, int x1, int y1, int penX, int penY, uint32 color)
Definition: formatinfo.h:28
Definition: rect.h:45
const Surface & rawSurface() const
Definition: managed_surface.h:178
void drawLine(int x0, int y0, int x1, int y1, uint32 color)
void frameRect(const Common::Rect &r, uint32 color)
const Common::Point getOffsetFromOwner() const
Definition: managed_surface.h:300
void setPixel(int x, int y, int pixel)
Definition: surface.h:182
int16 x
Definition: rect.h:46
void fillRect(Common::Rect r, uint32 color)
Definition: managed_surface.h:664
void frameRect(const Common::Rect &r, uint32 color)
Definition: managed_surface.h:672
int16 y
Definition: rect.h:47
void blitFromInner(const Surface &src, const Common::Rect &srcRect, const Common::Rect &destRect, const Palette *srcPalette)
T MIN(T a, T b)
Definition: util.h:59
void grabPalette(byte *colors, uint start, uint num) const
ManagedSurface & operator=(const ManagedSurface &surf)
PixelFormat & format
Definition: managed_surface.h:113
T MAX(T a, T b)
Definition: util.h:62
void vLine(int x, int y, int y2, uint32 color)
void hLine(int x, int y, int x2, uint32 color)
Definition: managed_surface.h:648
uint32 getPixel(int x, int y) const
Definition: managed_surface.h:206
Simple class for handling a palette data.
Definition: palette.h:39
void copyRectToSurfaceWithKey(const Graphics::Surface &srcSurface, int destX, int destY, const Common::Rect subRect, uint32 key)
Definition: managed_surface.h:613
void fillRect(Common::Rect r, uint32 color)
void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height)
void setPalette(const byte *colors, uint start, uint num)
const void * getPixels() const
Definition: managed_surface.h:250
uint32 getPixel(int x, int y) const
Definition: surface.h:161
void setPixel(int x, int y, uint32 pixel)
Definition: managed_surface.h:217
void convertToInPlace(const PixelFormat &dstFormat, const byte *palette, uint16 paletteCount)
Definition: managed_surface.h:710
virtual void setPixels(void *newPixels)
const void * getPixels() const
Definition: surface.h:107
int16 height() const
Definition: rect.h:192
void copyRectToSurface(const void *buffer, int srcPitch, int destX, int destY, int width, int height)
Definition: managed_surface.h:586
Definition: display_client.h:78