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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * This code is based on Broken Sword 2.5 engine
25  *
26  * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
27  *
28  * Licensed under GNU GPL v2
29  *
30  */
31 
32 #include "sword25/gfx/bitmap.h"
33 #include "sword25/kernel/outputpersistenceblock.h"
34 #include "sword25/kernel/inputpersistenceblock.h"
35 
36 namespace Sword25 {
37 
Bitmap(RenderObjectPtr<RenderObject> parentPtr,TYPES type,uint handle)38 Bitmap::Bitmap(RenderObjectPtr<RenderObject> parentPtr, TYPES type, uint handle) :
39 	RenderObject(parentPtr, type, handle),
40 	_modulationColor(BS_ARGBMASK),
41 	_scaleFactorX(1.0f),
42 	_scaleFactorY(1.0f),
43 	_flipH(false),
44 	_flipV(false) {
45 }
46 
~Bitmap()47 Bitmap::~Bitmap() {
48 }
49 
setAlpha(int alpha)50 void Bitmap::setAlpha(int alpha) {
51 	if (!isAlphaAllowed()) {
52 		warning("Tried to set alpha value on a bitmap that does not support alpha blending. Call was ignored.");
53 		return;
54 	}
55 
56 	if (alpha < 0 || alpha > 255) {
57 		int oldAlpha = alpha;
58 		if (alpha < 0)
59 			alpha = 0;
60 		if (alpha > 255)
61 			alpha = 255;
62 		warning("Tried to set an invalid alpha value (%d) on a bitmap. Value was changed to %d.", oldAlpha, alpha);
63 
64 		return;
65 	}
66 
67 	uint newModulationColor = (_modulationColor & BS_RGBMASK) | alpha << BS_ASHIFT;
68 	if (newModulationColor != _modulationColor) {
69 		_modulationColor = newModulationColor;
70 		forceRefresh();
71 	}
72 }
73 
setModulationColor(uint modulationColor)74 void Bitmap::setModulationColor(uint modulationColor) {
75 	if (!isColorModulationAllowed()) {
76 		warning("Tried to set modulation color of a bitmap that does not support color modulation. Call was ignored.");
77 		return;
78 	}
79 
80 	uint newModulationColor = (modulationColor & BS_RGBMASK) | (_modulationColor & BS_AMASK);
81 	if (newModulationColor != _modulationColor) {
82 		_modulationColor = newModulationColor;
83 		forceRefresh();
84 	}
85 }
86 
setScaleFactor(float scaleFactor)87 void Bitmap::setScaleFactor(float scaleFactor) {
88 	setScaleFactorX(scaleFactor);
89 	setScaleFactorY(scaleFactor);
90 }
91 
setScaleFactorX(float scaleFactorX)92 void Bitmap::setScaleFactorX(float scaleFactorX) {
93 	if (!isScalingAllowed()) {
94 		warning("Tried to set scale factor of a bitmap that does not support scaling. Call was ignored.");
95 		return;
96 	}
97 
98 	if (scaleFactorX < 0) {
99 		warning("Tried to set scale factor of a bitmap to a negative value. Call was ignored.");
100 		return;
101 	}
102 
103 	if (scaleFactorX != _scaleFactorX) {
104 		_scaleFactorX = scaleFactorX;
105 		_width = static_cast<int>(_originalWidth * _scaleFactorX);
106 		if (_scaleFactorX <= 0.0f)
107 			_scaleFactorX = 0.001f;
108 		if (_width <= 0)
109 			_width = 1;
110 		forceRefresh();
111 	}
112 }
113 
setScaleFactorY(float scaleFactorY)114 void Bitmap::setScaleFactorY(float scaleFactorY) {
115 	if (!isScalingAllowed()) {
116 		warning("Tried to set scale factor of a bitmap that does not support scaling. Call was ignored.");
117 		return;
118 	}
119 
120 	if (scaleFactorY < 0) {
121 		warning("Tried to set scale factor of a bitmap to a negative value. Call was ignored.");
122 		return;
123 	}
124 
125 	if (scaleFactorY != _scaleFactorY) {
126 		_scaleFactorY = scaleFactorY;
127 		_height = static_cast<int>(_originalHeight * scaleFactorY);
128 		if (_scaleFactorY <= 0.0f)
129 			_scaleFactorY = 0.001f;
130 		if (_height <= 0)
131 			_height = 1;
132 		forceRefresh();
133 	}
134 }
135 
setFlipH(bool flipH)136 void Bitmap::setFlipH(bool flipH) {
137 	_flipH = flipH;
138 	forceRefresh();
139 }
140 
setFlipV(bool flipV)141 void Bitmap::setFlipV(bool flipV) {
142 	_flipV = flipV;
143 	forceRefresh();
144 }
145 
persist(OutputPersistenceBlock & writer)146 bool Bitmap::persist(OutputPersistenceBlock &writer) {
147 	bool result = true;
148 
149 	result &= RenderObject::persist(writer);
150 	writer.write(_flipH);
151 	writer.write(_flipV);
152 	writer.write(_scaleFactorX);
153 	writer.write(_scaleFactorY);
154 	writer.write(_modulationColor);
155 	writer.write(_originalWidth);
156 	writer.write(_originalHeight);
157 
158 	return result;
159 }
160 
unpersist(InputPersistenceBlock & reader)161 bool Bitmap::unpersist(InputPersistenceBlock &reader) {
162 	bool result = true;
163 
164 	result &= RenderObject::unpersist(reader);
165 	reader.read(_flipH);
166 	reader.read(_flipV);
167 	reader.read(_scaleFactorX);
168 	reader.read(_scaleFactorY);
169 	reader.read(_modulationColor);
170 	reader.read(_originalWidth);
171 	reader.read(_originalHeight);
172 
173 	forceRefresh();
174 
175 	return reader.isGood() && result;
176 }
177 
178 } // End of namespace Sword25
179