aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/minigames/geisha/meter.cpp
blob: e3b9bd1ccfa26a434c3238f192b4d746f12ee649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/util.h"

#include "gob/surface.h"

#include "gob/minigames/geisha/meter.h"

namespace Gob {

namespace Geisha {

Meter::Meter(int16 x, int16 y, int16 width, int16 height, uint8 frontColor,
             uint8 backColor, int32 maxValue, Direction direction) :
	_x(x), _y(y), _width(width), _height(height), _frontColor(frontColor),
	_backColor(backColor), _value(0), _maxValue(maxValue), _direction(direction),
	_needUpdate(true), _surface(0) {

}

Meter::~Meter() {
	delete _surface;
}

int32 Meter::getValue() const {
	return _value;
}

void Meter::setValue(int32 value) {
	value = CLIP<int32>(value, 0, _maxValue);
	if (_value == value)
		return;

	_value = value;
	_needUpdate = true;
}

void Meter::setMaxValue() {
	setValue(_maxValue);
}

void Meter::increase(int32 n) {
	int32 value = CLIP<int32>(_value + n, 0, _maxValue);
	if (_value == value)
		return;

	_value = value;
	_needUpdate = true;
}

void Meter::decrease(int32 n) {
	int32 value = CLIP<int32>(_value - n, 0, _maxValue);
	if (_value == value)
		return;

	_value = value;
	_needUpdate = true;
}

void Meter::draw(Surface &dest, int16 &left, int16 &top, int16 &right, int16 &bottom) {
	if (!_surface) {
		_surface = new Surface(_width, _height, dest.getBPP());
		_needUpdate = true;
	}

	update();

	left   = CLIP<int16>(_x              , 0, dest.getWidth () - 1);
	top    = CLIP<int16>(_y              , 0, dest.getHeight() - 1);
	right  = CLIP<int16>(_x + _width  - 1, 0, dest.getWidth () - 1);
	bottom = CLIP<int16>(_y + _height - 1, 0, dest.getHeight() - 1);

	dest.blit(*_surface, left - _x, top - _y, _width, _height, left, top);
}

void Meter::update() {
	if (!_needUpdate)
		return;

	_needUpdate = false;

	_surface->fill(_backColor);

	int32 n = (int32)floor((((float) _width) / _maxValue * _value) + 0.5);
	if (n <= 0)
		return;

	if (_direction == kFillToLeft)
		_surface->fillRect(_width - n, 0, _width - 1, _height - 1, _frontColor);
	else
		_surface->fillRect(0         , 0, n - 1, _height - 1, _frontColor);
}

} // End of namespace Geisha

} // End of namespace Gob