scummvm/engines/twine/menu/interface.cpp

212 lines
4.5 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) {}
bool Interface::drawLine(int32 x0, int32 y0, int32 x1, int32 y1, uint8 color) {
// always from left to right
if (x0 > x1) {
SWAP(x0, x1);
SWAP(y0, y1);
2020-11-05 19:02:19 +00:00
}
2020-10-14 12:20:38 +00:00
const int32 cright = _clip.right;
const int32 cleft = _clip.left;
const int32 cbottom = _clip.bottom;
const int32 ctop = _clip.top;
uint16 clipFlags;
do {
if (x0 > cright || x1 < cleft) {
return false;
}
int32 dx = x1 - x0;
int32 dy = y1 - y0;
2020-10-14 12:20:38 +00:00
clipFlags = 0;
2020-10-14 12:20:38 +00:00
if (x0 < cleft) {
clipFlags |= 0x100;
2020-11-05 19:02:19 +00:00
}
2020-10-14 12:20:38 +00:00
if (y0 < ctop) {
clipFlags |= 0x800;
} else if (y0 > cbottom) {
clipFlags |= 0x400;
2020-10-14 12:20:38 +00:00
}
if (x1 > cright) {
clipFlags |= 0x2;
}
if (y1 < ctop) {
clipFlags |= 0x8;
} else if (y1 > cbottom) {
clipFlags |= 0x4;
}
if (clipFlags & (clipFlags >> 8)) {
return false;
2020-10-14 12:20:38 +00:00
}
if (clipFlags) {
if (clipFlags & 0x100) {
y0 += ((cleft - x0) * dy) / dx;
x0 = cleft;
} else if (clipFlags & 0x800) {
x0 += ((ctop - y0) * dx) / dy;
y0 = ctop;
} else if (clipFlags & 0x400) {
x0 += ((cbottom - y0) * dx) / dy;
y0 = cbottom;
} else if (clipFlags & 0x2) {
y1 = (((cright - x0) * dy) / dx) + y0;
x1 = cright;
} else if (clipFlags & 0x8) {
x1 = (((ctop - y0) * dx) / dy) + x0;
y1 = ctop;
} else if (clipFlags & 0x4) {
x1 = (((cbottom - y0) * dx) / dy) + x0;
y1 = cbottom;
}
}
} while (clipFlags);
int16 lineOffset = (int16)_engine->width();
x1 -= x0;
y1 -= y0;
if (y1 < 0) {
lineOffset = -lineOffset;
y1 = -y1;
2020-10-14 12:20:38 +00:00
}
byte *out = (byte *)_engine->_frontVideoBuffer.getBasePtr(x0, y0);
if (x1 < y1) {
// vertical
SWAP(x1, y1);
int32 dy = x1 << 1;
y0 = x1;
y1 <<= 1;
x1++;
2020-10-14 12:20:38 +00:00
do {
*out = color;
y0 -= y1;
out += lineOffset;
if (y0 < 0) {
y0 += dy;
out++;
2020-10-14 12:20:38 +00:00
}
} while (--x1);
} else {
// horizontal
int32 dy = x1 << 1;
y0 = x1;
y1 <<= 1;
x1++;
2020-10-14 12:20:38 +00:00
do {
*out++ = color;
y0 -= y1;
if (y0 < 0) {
y0 += dy;
out += lineOffset;
2020-10-14 12:20:38 +00:00
}
} while (--x1);
2020-10-14 12:20:38 +00:00
}
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
}
void Interface::drawFilledRect(const Common::Rect &rect, uint8 colorIndex) { // Box
if (!rect.isValidRect()) {
return;
}
Common::Rect clipped(rect.left, rect.top, rect.right + 1, rect.bottom + 1);
if (_clip.isValidRect()) {
clipped.clip(_clip);
}
_engine->_frontVideoBuffer.fillRect(clipped, 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
}
void Interface::memoClip() {
_memoClip = _clip;
2020-10-14 12:20:38 +00:00
}
void Interface::restoreClip() {
_clip = _memoClip;
2020-10-14 12:20:38 +00:00
}
void Interface::unsetClip() {
2021-07-31 13:44:15 +00:00
_clip = _engine->rect();
2020-10-14 12:20:38 +00:00
}
} // namespace TwinE