ScummVM API documentation
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
30namespace Common {
31class String;
32}
33
34namespace MutationOfJB {
35
36void reportFileMissingError(const char *fileName);
37Common::String toUpperCP895(const Common::String &str);
38
39// Taken from ManagedSurface::clip.
40template<typename SurfaceType>
41bool 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
70template<typename BlitOp>
71void 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
97template<typename BlitOp>
98void 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
112template<typename BlitOp>
113void 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
117template<typename BlitOp>
118void 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: str.h:59
Definition: managed_surface.h:51
PixelFormat & format
Definition: managed_surface.h:137
Surface getSubArea(const Common::Rect &area)
Definition: managed_surface.h:978
Definition: algorithm.h:29
T y
Definition: rect.h:49
T x
Definition: rect.h:48
Definition: rect.h:144
T right
Definition: rect.h:171
T height() const
Definition: rect.h:218
bool isValidRect() const
Definition: rect.h:392
T left
Definition: rect.h:170
T width() const
Definition: rect.h:217
Definition: rect.h:536
Definition: surface.h:67
int16 h
Definition: surface.h:76
const void * getBasePtr(int x, int y) const
Definition: surface.h:140
int16 w
Definition: surface.h:71
PixelFormat format
Definition: surface.h:95