scummvm/engines/twine/menu/interface.cpp

203 lines
5.7 KiB
C++
Raw Normal View History

/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "twine/menu/interface.h"
2020-11-16 17:01:16 +00:00
#include "graphics/managed_surface.h"
2021-06-03 16:43:07 +00:00
#include "graphics/primitives.h"
#include "twine/twine.h"
namespace TwinE {
Interface::Interface(TwinEEngine *engine) : _engine(engine) {}
2020-10-14 12:20:38 +00:00
const int32 INSIDE = 0; // 0000
const int32 LEFT = 1; // 0001
const int32 RIGHT = 2; // 0010
const int32 TOP = 4; // 0100
const int32 BOTTOM = 8; // 1000
2020-10-14 12:20:38 +00:00
2020-12-29 11:00:49 +00:00
int32 Interface::checkClipping(int32 x, int32 y) const {
2020-10-14 12:20:38 +00:00
int32 code = INSIDE;
2021-07-31 13:44:15 +00:00
if (x < _clip.left) {
code |= LEFT;
2021-07-31 13:44:15 +00:00
} else if (x > _clip.right) {
code |= RIGHT;
2020-11-05 19:02:19 +00:00
}
2021-07-31 13:44:15 +00:00
if (y < _clip.top) {
code |= TOP;
2021-07-31 13:44:15 +00:00
} else if (y > _clip.bottom) {
code |= BOTTOM;
2020-11-05 19:02:19 +00:00
}
2020-10-14 12:20:38 +00:00
return code;
}
bool Interface::drawLine(int32 startWidth, int32 startHeight, int32 endWidth, int32 endHeight, uint8 lineColor) {
2020-10-14 12:20:38 +00:00
// draw line from left to right
if (startWidth > endWidth) {
2020-12-23 09:50:50 +00:00
SWAP(endWidth, startWidth);
SWAP(endHeight, startHeight);
2020-10-14 12:20:38 +00:00
}
// Perform proper clipping (CohenSutherland algorithm)
int32 outcode0 = checkClipping(startWidth, startHeight);
int32 outcode1 = checkClipping(endWidth, endHeight);
2020-10-14 12:20:38 +00:00
2020-11-16 18:35:33 +00:00
while ((outcode0 | outcode1) != INSIDE) {
if ((outcode0 & outcode1) != INSIDE && outcode0 != INSIDE) {
return false; // Reject lines which are behind one clipping plane
2020-11-05 19:02:19 +00:00
}
2020-10-14 12:20:38 +00:00
// At least one endpoint is outside the clip rectangle; pick it.
2020-12-23 09:37:51 +00:00
const int32 outcodeOut = outcode0 ? outcode0 : outcode1;
2020-10-14 12:20:38 +00:00
int32 x = 0;
int32 y = 0;
if (outcodeOut & TOP) { // point is above the clip rectangle
2021-07-31 13:44:15 +00:00
x = startWidth + (int)((endWidth - startWidth) * (float)(_clip.top - startHeight) / (float)(endHeight - startHeight));
y = _clip.top;
2020-10-14 12:20:38 +00:00
} else if (outcodeOut & BOTTOM) { // point is below the clip rectangle
2021-07-31 13:44:15 +00:00
x = startWidth + (int)((endWidth - startWidth) * (float)(_clip.bottom - startHeight) / (float)(endHeight - startHeight));
y = _clip.bottom;
} else if (outcodeOut & RIGHT) { // point is to the right of clip rectangle
2021-07-31 13:44:15 +00:00
y = startHeight + (int)((endHeight - startHeight) * (float)(_clip.right - startWidth) / (float)(endWidth - startWidth));
x = _clip.right;
} else if (outcodeOut & LEFT) { // point is to the left of clip rectangle
2021-07-31 13:44:15 +00:00
y = startHeight + (int)((endHeight - startHeight) * (float)(_clip.left - startWidth) / (float)(endWidth - startWidth));
x = _clip.left;
2020-10-14 12:20:38 +00:00
}
// Clip the point
if (outcodeOut == outcode0) {
startWidth = x;
startHeight = y;
outcode0 = checkClipping(startWidth, startHeight);
} else {
endWidth = x;
endHeight = y;
outcode1 = checkClipping(endWidth, endHeight);
}
}
int32 pitch = _engine->width();
2020-10-14 12:20:38 +00:00
endWidth -= startWidth;
endHeight -= startHeight;
if (endHeight < 0) {
2020-12-23 09:55:13 +00:00
pitch = -pitch;
2020-10-14 12:20:38 +00:00
endHeight = -endHeight;
}
2021-07-31 13:44:15 +00:00
uint8 *out = (uint8*)_engine->_frontVideoBuffer.getBasePtr(startWidth, startHeight);
_engine->_frontVideoBuffer.addDirtyRect(Common::Rect(startWidth, startHeight, startWidth + endWidth, startHeight + endHeight));
2020-10-14 12:20:38 +00:00
if (endWidth < endHeight) { // significant slope
2020-12-23 09:50:50 +00:00
SWAP(endWidth, endHeight);
2020-12-23 09:37:51 +00:00
const int16 var2 = endWidth << 1;
2020-10-14 12:20:38 +00:00
startHeight = endWidth;
endHeight <<= 1;
endWidth++;
do {
2020-12-23 09:50:50 +00:00
*out = lineColor;
2020-10-14 12:20:38 +00:00
startHeight -= endHeight;
if (startHeight > 0) {
2020-12-23 09:55:13 +00:00
out += pitch;
2020-10-14 12:20:38 +00:00
} else {
startHeight += var2;
2020-12-23 09:55:13 +00:00
out += pitch + 1;
2020-10-14 12:20:38 +00:00
}
} while (--endWidth);
} else { // reduced slope
2020-12-23 09:37:51 +00:00
const int16 var2 = endWidth << 1;
2020-10-14 12:20:38 +00:00
startHeight = endWidth;
endHeight <<= 1;
endWidth++;
do {
2020-12-23 09:50:50 +00:00
*out++ = lineColor;
2020-10-14 12:20:38 +00:00
startHeight -= endHeight;
if (startHeight < 0) {
startHeight += var2;
2020-12-23 09:55:13 +00:00
out += pitch;
2020-10-14 12:20:38 +00:00
}
} while (--endWidth);
}
return true;
2020-10-14 12:20:38 +00:00
}
2020-11-25 20:36:18 +00:00
void Interface::blitBox(const Common::Rect &rect, const Graphics::ManagedSurface &source, Graphics::ManagedSurface &dest) {
Common::Rect r(rect);
r.right += 1;
r.bottom += 1;
dest.blitFrom(source, r, Common::Point(rect.left, rect.top));
2020-10-14 12:20:38 +00:00
}
2020-11-25 15:59:54 +00:00
void Interface::drawTransparentBox(const Common::Rect &rect, int32 colorAdj) {
2021-01-07 21:00:23 +00:00
Common::Rect r = rect;
r.clip(_engine->rect());
if (r.isEmpty()) {
2020-10-14 12:20:38 +00:00
return;
2020-11-05 19:02:19 +00:00
}
2020-10-14 12:20:38 +00:00
2021-07-31 13:44:15 +00:00
uint8 *pos = (uint8*)_engine->_frontVideoBuffer.getBasePtr(0, r.top);
2020-10-14 12:20:38 +00:00
for (int32 y = r.top; y <= r.bottom; ++y) {
for (int32 x = r.left; x <= r.right; ++x) {
2020-12-23 10:16:32 +00:00
const int8 color = (pos[x] & 0x0F) - colorAdj;
const int8 color2 = pos[x] & 0xF0;
if (color < 0) {
2020-12-23 10:16:32 +00:00
pos[x] = color2;
} else {
2020-12-23 10:16:32 +00:00
pos[x] = color + color2;
}
}
2021-07-31 13:44:15 +00:00
pos += _engine->_frontVideoBuffer.pitch;
}
2021-07-31 13:44:15 +00:00
_engine->_frontVideoBuffer.addDirtyRect(r);
2020-10-14 12:20:38 +00:00
}
2021-01-30 13:35:12 +00:00
void Interface::drawFilledRect(const Common::Rect &rect, uint8 colorIndex) {
if (!rect.isValidRect()) {
return;
}
2021-07-31 13:44:15 +00:00
_engine->_frontVideoBuffer.fillRect(Common::Rect(rect.left, rect.top, rect.right + 1, rect.bottom + 1), colorIndex);
2020-10-14 12:20:38 +00:00
}
bool Interface::setClip(const Common::Rect &rect) {
2021-07-31 13:44:15 +00:00
if (!_clip.isValidRect()) {
return false;
}
2021-07-31 13:44:15 +00:00
_clip = rect;
_clip.clip(_engine->rect());
return true;
2020-10-14 12:20:38 +00:00
}
2020-12-23 09:37:22 +00:00
void Interface::saveClip() {
2021-07-31 13:44:15 +00:00
_savedClip = _clip;
2020-10-14 12:20:38 +00:00
}
2020-12-23 09:37:22 +00:00
void Interface::loadClip() {
2021-07-31 13:44:15 +00:00
_clip = _savedClip;
2020-10-14 12:20:38 +00:00
}
void Interface::resetClip() {
2021-07-31 13:44:15 +00:00
_clip = _engine->rect();
2020-10-14 12:20:38 +00:00
}
} // namespace TwinE