ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
util.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 MUTATIONOFJB_UTIL_H
23 #define MUTATIONOFJB_UTIL_H
24 
25 #include "common/rect.h"
26 
27 #include "graphics/managed_surface.h"
28 #include "graphics/surface.h"
29 
30 namespace Common {
31 class String;
32 }
33 
34 namespace MutationOfJB {
35 
36 void reportFileMissingError(const char *fileName);
37 Common::String toUpperCP895(const Common::String &str);
38 
39 // Taken from ManagedSurface::clip.
40 template<typename SurfaceType>
41 bool clipBounds(Common::Rect &srcBounds, Common::Rect &destBounds, SurfaceType &destSurf) {
42  if (destBounds.left >= destSurf.w || destBounds.top >= destSurf.h ||
43  destBounds.right <= 0 || destBounds.bottom <= 0)
44  return false;
45 
46  // Clip the bounds if source is too big to fit into destination.
47  if (destBounds.right > destSurf.w) {
48  srcBounds.right -= destBounds.right - destSurf.w;
49  destBounds.right = destSurf.w;
50  }
51 
52  if (destBounds.bottom > destSurf.h) {
53  srcBounds.bottom -= destBounds.bottom - destSurf.h;
54  destBounds.bottom = destSurf.h;
55  }
56 
57  if (destBounds.top < 0) {
58  srcBounds.top += -destBounds.top;
59  destBounds.top = 0;
60  }
61 
62  if (destBounds.left < 0) {
63  srcBounds.left += -destBounds.left;
64  destBounds.left = 0;
65  }
66 
67  return true;
68 }
69 
70 template<typename BlitOp>
71 void blit_if(const Graphics::Surface &src, const Common::Rect &srcRect, Graphics::Surface &dest, const Common::Point &destPos, BlitOp blitOp) {
72  Common::Rect srcBounds = srcRect;
73  Common::Rect destBounds(destPos.x, destPos.y, destPos.x + srcRect.width(), destPos.y + srcRect.height());
74 
75  assert(srcRect.isValidRect());
76  assert(dest.format == src.format);
77 
78  if (!clipBounds(srcBounds, destBounds, dest))
79  return;
80 
81  for (int y = 0; y < srcBounds.height(); ++y) {
82  const byte *srcP = reinterpret_cast<const byte *>(src.getBasePtr(srcBounds.left, srcBounds.top + y));
83  const byte *srcEndP = srcP + srcBounds.width();
84  byte *destP = reinterpret_cast<byte *>(dest.getBasePtr(destBounds.left, destBounds.top + y));
85 
86  while (srcP != srcEndP) {
87  const byte newColor = blitOp(*srcP, *destP);
88  if (*destP != newColor) {
89  *destP = newColor;
90  }
91  ++srcP;
92  ++destP;
93  }
94  }
95 }
96 
97 template<typename BlitOp>
98 void blit_if(const Graphics::Surface &src, const Common::Rect &srcRect, Graphics::ManagedSurface &dest, const Common::Point &destPos, BlitOp blitOp) {
99  Common::Rect srcBounds = srcRect;
100  Common::Rect destBounds(destPos.x, destPos.y, destPos.x + srcRect.width(), destPos.y + srcRect.height());
101 
102  assert(srcRect.isValidRect());
103  assert(dest.format == src.format);
104 
105  if (!clipBounds(srcBounds, destBounds, dest))
106  return;
107 
108  Graphics::Surface destSurf = dest.getSubArea(destBounds); // This will invalidate the rectangle.
109  blit_if(src, srcRect, destSurf, Common::Point(0, 0), blitOp);
110 }
111 
112 template<typename BlitOp>
113 void blit_if(const Graphics::Surface &src, Graphics::Surface &dest, const Common::Point &destPos, BlitOp blitOp) {
114  blit_if(src, Common::Rect(0, 0, src.w, src.h), dest, destPos, blitOp);
115 }
116 
117 template<typename BlitOp>
118 void blit_if(const Graphics::Surface &src, Graphics::ManagedSurface &dest, const Common::Point &destPos, BlitOp blitOp) {
119  blit_if(src, Common::Rect(0, 0, src.w, src.h), dest, destPos, blitOp);
120 }
121 
122 }
123 
124 #endif
Definition: managed_surface.h:51
Definition: str.h:59
Definition: surface.h:67
int16 h
Definition: surface.h:76
Surface getSubArea(const Common::Rect &area)
Definition: managed_surface.h:772
int16 right
Definition: rect.h:146
Definition: rect.h:144
const void * getBasePtr(int x, int y) const
Definition: surface.h:138
int16 width() const
Definition: rect.h:192
Definition: animationdecoder.h:36
Definition: algorithm.h:29
Definition: rect.h:45
int16 left
Definition: rect.h:145
int16 x
Definition: rect.h:46
bool isValidRect() const
Definition: rect.h:338
int16 y
Definition: rect.h:47
int16 w
Definition: surface.h:71
PixelFormat format
Definition: surface.h:95
PixelFormat & format
Definition: managed_surface.h:126
int16 height() const
Definition: rect.h:193