2013-07-11 17:36:59 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This code is based on the original source code of Lord Avalot d'Argent version 1.3.
|
|
|
|
* Copyright (c) 1994-1995 Mike, Mark and Thomas Thurman.
|
|
|
|
*/
|
|
|
|
|
2013-08-07 23:51:12 +02:00
|
|
|
#include "math.h"
|
|
|
|
|
2013-07-11 17:36:59 +02:00
|
|
|
#include "avalanche/avalanche.h"
|
2013-07-24 17:02:08 +02:00
|
|
|
#include "avalanche/graphics.h"
|
2013-07-11 17:36:59 +02:00
|
|
|
|
|
|
|
#include "common/system.h"
|
2013-07-27 17:22:56 +02:00
|
|
|
#include "common/rect.h"
|
2013-07-11 17:36:59 +02:00
|
|
|
|
|
|
|
#include "engines/util.h"
|
|
|
|
|
|
|
|
#include "graphics/palette.h"
|
|
|
|
|
|
|
|
namespace Avalanche {
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
const byte Graphics::_egaPaletteIndex[16] = {0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63};
|
2013-07-11 17:36:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-24 17:52:57 +02:00
|
|
|
Graphics::Graphics(AvalancheEngine *vm) {
|
2013-07-11 17:36:59 +02:00
|
|
|
_vm = vm;
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
void Graphics::init() {
|
2013-07-24 19:47:41 +02:00
|
|
|
initGraphics(kScreenWidth, kScreenHeight * 2, true); // Doubling the height.
|
2013-07-12 08:41:14 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 64; ++i) {
|
|
|
|
_egaPalette[i][0] = (i >> 2 & 1) * 0xaa + (i >> 5 & 1) * 0x55;
|
|
|
|
_egaPalette[i][1] = (i >> 1 & 1) * 0xaa + (i >> 4 & 1) * 0x55;
|
|
|
|
_egaPalette[i][2] = (i & 1) * 0xaa + (i >> 3 & 1) * 0x55;
|
|
|
|
}
|
2013-07-11 17:36:59 +02:00
|
|
|
|
|
|
|
for (byte i = 0; i < 16; i++)
|
2013-07-12 08:41:14 +02:00
|
|
|
g_system->getPaletteManager()->setPalette(_egaPalette[_egaPaletteIndex[i]], i, 1);
|
2013-07-11 17:36:59 +02:00
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
_surface.create(kScreenWidth, kScreenHeight, ::Graphics::PixelFormat::createFormatCLUT8());
|
2013-07-30 20:33:05 +02:00
|
|
|
|
|
|
|
_magics.create(kScreenWidth, kScreenHeight, ::Graphics::PixelFormat::createFormatCLUT8());
|
2013-08-05 12:19:10 +02:00
|
|
|
|
|
|
|
_screen.create(kScreenWidth, kScreenHeight * 2, ::Graphics::PixelFormat::createFormatCLUT8());
|
2013-08-06 01:17:44 +02:00
|
|
|
|
|
|
|
_scrolls.create(kScreenWidth, kScreenHeight, ::Graphics::PixelFormat::createFormatCLUT8());
|
2013-07-11 17:36:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
Graphics::~Graphics() {
|
2013-07-11 17:36:59 +02:00
|
|
|
_surface.free();
|
2013-07-30 20:33:05 +02:00
|
|
|
_magics.free();
|
2013-07-29 12:22:59 +02:00
|
|
|
_background.free();
|
2013-08-05 12:19:10 +02:00
|
|
|
_screen.free();
|
2013-08-06 01:17:44 +02:00
|
|
|
_scrolls.free();
|
2013-07-11 17:36:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-20 12:16:06 +02:00
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
void Graphics::flesh_colours()
|
2013-07-20 12:16:06 +02:00
|
|
|
{
|
|
|
|
g_system->getPaletteManager()->setPalette(_egaPalette[39], 13, 1);
|
2013-07-20 12:27:52 +02:00
|
|
|
g_system->getPaletteManager()->setPalette(_egaPalette[28], 5, 1);
|
2013-07-20 12:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
byte *Graphics::getPixel(int16 x, int16 y) {
|
2013-07-11 17:36:59 +02:00
|
|
|
return (byte *)_surface.getBasePtr(x, y);
|
|
|
|
}
|
|
|
|
|
2013-07-25 16:26:49 +02:00
|
|
|
void Graphics::drawFrame(int16 x1, int16 y1, int16 x2, int16 y2, int16 color) {
|
|
|
|
_surface.frameRect(Common::Rect(x1, y1, x2, y2), color);
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
void Graphics::drawBar(int16 x1, int16 y1, int16 x2, int16 y2, int16 color) {
|
2013-07-12 10:25:16 +02:00
|
|
|
_surface.fillRect(Common::Rect(x1, y1, x2, y2), color);
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
void Graphics::drawSprite(const SpriteInfo &sprite, byte picnum, int16 x, int16 y) {
|
2013-07-30 01:48:21 +02:00
|
|
|
drawPicture(_background, 0, 10); // TODO: Remove it later, implement otherwise!!! Now it only works with one sprite on the screen.
|
2013-07-19 23:36:58 +02:00
|
|
|
|
2013-07-30 01:48:21 +02:00
|
|
|
/* First we make the pixels of the spirte blank. */
|
|
|
|
for (byte qay = 0; qay < sprite.yl; qay++)
|
|
|
|
for (byte qax = 0; qax < sprite.xl; qax++)
|
|
|
|
if (((*sprite.sil[picnum])[qay][qax / 8] >> ((7 - qax % 8)) & 1) == 0)
|
2013-07-20 10:06:43 +02:00
|
|
|
*getPixel(x + qax, y + qay) = 0;
|
2013-07-30 01:48:21 +02:00
|
|
|
|
2013-07-20 16:58:37 +02:00
|
|
|
/* Then we draw the picture to the blank places. */
|
2013-07-27 10:05:09 +02:00
|
|
|
uint16 i = 0; // Because the original siltype starts at 5!!! See Graphics.h for definition.
|
2013-07-20 10:55:19 +02:00
|
|
|
|
|
|
|
for (byte qay = 0; qay < sprite.yl; qay++)
|
|
|
|
for (int8 plane = 3; plane >= 0; plane--) // The planes are in the opposite way.
|
|
|
|
for (uint16 qax = 0; qax < sprite.xl; qax += 8) {
|
|
|
|
byte pixel = (*sprite.mani[picnum])[i++];
|
|
|
|
for (byte bit = 0; bit < 8; bit++) {
|
|
|
|
byte pixelBit = (pixel >> bit) & 1;
|
|
|
|
*getPixel(x + qax + 7 - bit, y + qay) += (pixelBit << plane);
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 16:07:07 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 03:59:29 +02:00
|
|
|
Common::Point Graphics::drawArc(const ::Graphics::Surface &surface, int16 x, int16 y, int16 stAngle, int16 endAngle, uint16 radius, byte color) {
|
|
|
|
Common::Point endPoint;
|
2013-08-07 23:51:12 +02:00
|
|
|
const double pi = 3.14;
|
|
|
|
const double convfac = pi / 180.0;
|
|
|
|
|
|
|
|
int32 xRadius = radius;
|
|
|
|
int32 yRadius = radius * kScreenWidth / (8 * kScreenHeight); // Just don't ask why...
|
|
|
|
|
|
|
|
if (xRadius == 0)
|
|
|
|
xRadius ++;
|
|
|
|
if (yRadius == 0)
|
|
|
|
yRadius ++;
|
|
|
|
|
|
|
|
/* check for an ellipse with negligable x and y radius */
|
|
|
|
if ((xRadius <= 1) && (yRadius <= 1))
|
|
|
|
{
|
2013-08-08 03:59:29 +02:00
|
|
|
*(byte *)_scrolls.getBasePtr(x, y) = color;
|
|
|
|
endPoint.x = x;
|
|
|
|
endPoint.y = y;
|
|
|
|
return endPoint;
|
2013-08-07 23:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check if valid angles */
|
|
|
|
stAngle = stAngle % 361;
|
|
|
|
endAngle = endAngle % 361;
|
|
|
|
|
|
|
|
/* if impossible angles then swap them! */
|
|
|
|
if (endAngle < stAngle)
|
|
|
|
{
|
|
|
|
uint16 tmpAngle=endAngle;
|
|
|
|
endAngle=stAngle;
|
|
|
|
stAngle=tmpAngle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* approximate the number of pixels required by using the circumference */
|
|
|
|
/* equation of an ellipse. */
|
2013-08-08 03:59:29 +02:00
|
|
|
uint16 numOfPixels=floor(sqrt(3.0)*sqrt(pow(double(xRadius), 2)+pow(double(yRadius), 2)) + 0.5);
|
2013-08-07 23:51:12 +02:00
|
|
|
|
|
|
|
/* Calculate the angle precision required */
|
|
|
|
double delta = 90.0 / numOfPixels;
|
|
|
|
|
|
|
|
/* Always just go over the first 90 degrees. Could be optimized a */
|
|
|
|
/* bit if startAngle and endAngle lie in the same quadrant, left as an */
|
|
|
|
/* exercise for the reader :) (JM) */
|
|
|
|
double j = 0;
|
|
|
|
|
|
|
|
/* calculate stop position, go 1 further than 90 because otherwise */
|
|
|
|
/* 1 pixel is sometimes not drawn (JM) */
|
|
|
|
uint16 deltaEnd = 91;
|
|
|
|
|
2013-08-08 03:59:29 +02:00
|
|
|
// Set the end point.
|
|
|
|
double tempTerm = endAngle * convfac;
|
|
|
|
endPoint.x = floor(xRadius * cos(tempTerm) + 0.5) + x;
|
|
|
|
endPoint.y = floor(yRadius * sin(tempTerm + pi) + 0.5) + y;
|
|
|
|
|
2013-08-07 23:51:12 +02:00
|
|
|
/* Calculate points */
|
|
|
|
int16 xNext = xRadius;
|
|
|
|
int16 yNext = 0;
|
|
|
|
do {
|
|
|
|
int16 xTemp = xNext;
|
|
|
|
int16 yTemp = yNext;
|
|
|
|
/* this is used by both sin and cos */
|
|
|
|
double tempTerm = (j+delta)*convfac;
|
|
|
|
/* Calculate points */
|
|
|
|
xNext = floor(xRadius*cos(tempTerm) + 0.5);
|
|
|
|
yNext = floor(yRadius*sin(tempTerm + pi) + 0.5);
|
|
|
|
|
|
|
|
int16 xp = x + xTemp;
|
|
|
|
int16 xm = x - xTemp;
|
|
|
|
int16 yp = y + yTemp;
|
|
|
|
int16 ym = y - yTemp;
|
|
|
|
if ((j >= stAngle) && (j <= endAngle))
|
|
|
|
*(byte *)_scrolls.getBasePtr(xp,yp) = color;
|
|
|
|
|
|
|
|
if (((180-j) >= stAngle) && ((180-j) <= endAngle))
|
|
|
|
*(byte *)_scrolls.getBasePtr(xm,yp) = color;
|
|
|
|
|
|
|
|
if (((j+180) >= stAngle) && ((j+180) <= endAngle))
|
|
|
|
*(byte *)_scrolls.getBasePtr(xm,ym) = color;
|
|
|
|
|
|
|
|
if (((360-j) >= stAngle) && ((360-j) <= endAngle))
|
|
|
|
*(byte *)_scrolls.getBasePtr(xp,ym) = color;
|
|
|
|
|
|
|
|
j += delta;
|
|
|
|
} while (j <= deltaEnd);
|
2013-08-08 03:59:29 +02:00
|
|
|
|
|
|
|
return endPoint;
|
2013-08-07 23:51:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 00:57:10 +02:00
|
|
|
void Graphics::drawPieSlice(const ::Graphics::Surface &surface, int16 x, int16 y, int16 stAngle, int16 endAngle, uint16 radius, byte color) {
|
|
|
|
while (radius > 0)
|
|
|
|
drawArc(surface, x, y, stAngle, endAngle, radius--, color);
|
2013-08-08 04:05:27 +02:00
|
|
|
*(byte *)surface.getBasePtr(x + 1, y) = color;
|
2013-08-08 00:57:10 +02:00
|
|
|
}
|
2013-08-07 23:51:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-26 13:32:08 +02:00
|
|
|
::Graphics::Surface Graphics::loadPictureGraphic(Common::File &file) {
|
2013-07-26 14:38:06 +02:00
|
|
|
// This function mimics Pascal's getimage().
|
2013-07-26 13:32:08 +02:00
|
|
|
// The height and the width are stored in 2-2 bytes. We have to add 1 to each because Pascal stores the value of them -1.
|
|
|
|
uint16 pictureWidth = file.readUint16LE() + 1;
|
|
|
|
uint16 pictureHeight = file.readUint16LE() + 1;
|
|
|
|
|
|
|
|
::Graphics::Surface picture; // We make a Surface object for the picture itself.
|
|
|
|
|
|
|
|
picture.create(pictureWidth, pictureHeight, ::Graphics::PixelFormat::createFormatCLUT8());
|
|
|
|
|
2013-07-26 14:38:06 +02:00
|
|
|
// Produce the picture. We read it in row-by-row, and every row has 4 planes.
|
2013-07-26 13:32:08 +02:00
|
|
|
for (byte y = 0; y < pictureHeight; y++)
|
|
|
|
for (int8 plane = 3; plane >= 0; plane--) // The planes are in the opposite way.
|
|
|
|
for (uint16 x = 0; x < pictureWidth; x += 8) {
|
|
|
|
byte pixel = file.readByte();
|
|
|
|
for (byte bit = 0; bit < 8; bit++) {
|
|
|
|
byte pixelBit = (pixel >> bit) & 1;
|
|
|
|
*(byte *)picture.getBasePtr(x + 7 - bit, y) += (pixelBit << plane);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return picture;
|
|
|
|
}
|
|
|
|
|
|
|
|
::Graphics::Surface Graphics::loadPictureRow(Common::File &file, uint16 width, uint16 height) {
|
2013-07-26 14:38:06 +02:00
|
|
|
// This function is our own creation, very much like the one above. The main differences are that
|
|
|
|
// we don't read the width and the height from the file, the planes are in a different order
|
|
|
|
// and we read the picture plane-by-plane.
|
|
|
|
|
2013-07-26 13:32:08 +02:00
|
|
|
::Graphics::Surface picture;
|
|
|
|
|
|
|
|
picture.create(width, height, ::Graphics::PixelFormat::createFormatCLUT8());
|
|
|
|
|
|
|
|
for (byte plane = 0; plane < 4; plane++)
|
|
|
|
for (uint16 y = 0; y < height; y++)
|
|
|
|
for (uint16 x = 0; x < width; x += 8) {
|
|
|
|
byte pixel = file.readByte();
|
|
|
|
for (byte i = 0; i < 8; i++) {
|
|
|
|
byte pixelBit = (pixel >> i) & 1;
|
|
|
|
*(byte *)picture.getBasePtr(x + 7 - i, y) += (pixelBit << plane);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return picture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::drawPicture(const ::Graphics::Surface &picture, uint16 destX, uint16 destY) {
|
|
|
|
// Copy the picture to the given place on the screen.
|
|
|
|
for (uint16 y = 0; y < picture.h; y++)
|
|
|
|
for (uint16 x = 0; x < picture.w; x++)
|
|
|
|
*getPixel(x + destX, y + destY) = *(byte *)picture.getBasePtr(x, y);
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:02:08 +02:00
|
|
|
void Graphics::refreshScreen() {
|
2013-07-24 19:47:41 +02:00
|
|
|
// These cycles are for doubling the screen height.
|
2013-08-05 12:19:10 +02:00
|
|
|
for (uint16 y = 0; y < _screen.h / 2; y++)
|
|
|
|
for (uint16 x = 0; x < _screen.w; x++)
|
2013-07-24 19:47:41 +02:00
|
|
|
for (byte j = 0; j < 2; j++)
|
2013-08-05 12:19:10 +02:00
|
|
|
*(byte *)_screen.getBasePtr(x, y * 2 + j) = *(byte *)_surface.getBasePtr(x, y);
|
2013-07-24 19:47:41 +02:00
|
|
|
|
|
|
|
// Now we copy the stretched picture to the screen.
|
2013-08-05 12:19:10 +02:00
|
|
|
g_system->copyRectToScreen(_screen.pixels, _screen.pitch, 0, 0, kScreenWidth, kScreenHeight * 2);
|
2013-07-11 17:36:59 +02:00
|
|
|
g_system->updateScreen();
|
|
|
|
}
|
|
|
|
|
2013-07-24 19:47:41 +02:00
|
|
|
|
2013-07-11 17:36:59 +02:00
|
|
|
} // End of namespace Avalanche
|