2004-04-12 21:40:49 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:20:17 +00:00
|
|
|
* Copyright (C) 2004-2005 The ScummVM project
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
* The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Misc. graphics routines
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Line drawing code utilizes Bresenham's run-length slice algorithm
|
|
|
|
// described in "Michael Abrash's Graphics Programming Black Book",
|
|
|
|
// Coriolis Group Books, 1997
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-06-25 21:47:37 +00:00
|
|
|
#include "saga/saga.h"
|
|
|
|
#include "saga/gfx.h"
|
2005-01-20 13:59:12 +00:00
|
|
|
#include "saga/interface.h"
|
2004-06-25 21:47:37 +00:00
|
|
|
|
|
|
|
#include "common/system.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2004-11-24 00:14:21 +00:00
|
|
|
Gfx::Gfx(OSystem *system, int width, int height, GameDetector &detector) : _system(system) {
|
2004-10-27 21:32:28 +00:00
|
|
|
SURFACE back_buf;
|
2004-05-02 00:00:39 +00:00
|
|
|
|
2004-11-24 00:14:21 +00:00
|
|
|
_system->beginGFXTransaction();
|
|
|
|
_vm->initCommonGFX(detector);
|
|
|
|
_system->initSize(width, height);
|
|
|
|
_system->endGFXTransaction();
|
2004-05-02 00:00:39 +00:00
|
|
|
|
|
|
|
debug(0, "Init screen %dx%d", width, height);
|
2004-07-31 12:39:26 +00:00
|
|
|
// Convert surface data to R surface data
|
2004-10-30 22:13:48 +00:00
|
|
|
back_buf.pixels = calloc(1, width * height);
|
|
|
|
back_buf.w = width;
|
|
|
|
back_buf.h = height;
|
|
|
|
back_buf.pitch = width;
|
2004-11-26 00:57:29 +00:00
|
|
|
back_buf.bytesPerPixel = 1;
|
2004-05-02 00:00:39 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
back_buf.clip_rect.left = 0;
|
|
|
|
back_buf.clip_rect.top = 0;
|
|
|
|
back_buf.clip_rect.right = width;
|
|
|
|
back_buf.clip_rect.bottom = height;
|
2004-05-02 00:00:39 +00:00
|
|
|
|
|
|
|
// Set module data
|
2004-10-27 21:32:28 +00:00
|
|
|
_back_buf = back_buf;
|
2004-08-02 11:27:50 +00:00
|
|
|
_init = 1;
|
2004-05-02 15:44:19 +00:00
|
|
|
|
|
|
|
// For now, always show the mouse cursor.
|
2005-01-15 13:41:57 +00:00
|
|
|
setCursor();
|
2004-11-24 00:14:21 +00:00
|
|
|
_system->showMouse(true);
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
~Gfx() {
|
2004-10-30 22:13:48 +00:00
|
|
|
free(GfxModule.r_back_buf->pixels);
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
int drawPalette(SURFACE *dst_s) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int color = 0;
|
|
|
|
|
2004-10-04 23:09:38 +00:00
|
|
|
Rect pal_rect;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
for (y = 0; y < 16; y++) {
|
2004-04-25 15:14:46 +00:00
|
|
|
pal_rect.top = (y * 8) + 4;
|
|
|
|
pal_rect.bottom = pal_rect.top + 8;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
for (x = 0; x < 16; x++) {
|
2004-04-25 15:14:46 +00:00
|
|
|
pal_rect.left = (x * 8) + 4;
|
|
|
|
pal_rect.right = pal_rect.left + 8;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-04-22 01:38:27 +00:00
|
|
|
drawRect(dst_s, pal_rect, color);
|
2004-04-12 21:40:49 +00:00
|
|
|
color++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-21 06:47:21 +00:00
|
|
|
// TODO: I've fixed at least one clipping bug here, but I have a feeling there
|
|
|
|
// are several more.
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// * Copies a rectangle from a raw 8 bit pixel buffer to the specified surface.
|
|
|
|
// The buffer is of width 'src_w' and height 'src_h'. The rectangle to be
|
|
|
|
// copied is defined by 'src_rect'.
|
|
|
|
// The rectangle is copied to the destination surface at point 'dst_pt'.
|
|
|
|
// - If dst_pt is NULL, the buffer is rectangle is copied to the destination
|
|
|
|
// origin.
|
|
|
|
// - If src_rect is NULL, the entire buffer is copied./
|
|
|
|
// - The surface must match the logical dimensions of the buffer exactly.
|
2004-10-27 21:32:28 +00:00
|
|
|
// - Returns FAILURE on error
|
2004-10-30 22:34:08 +00:00
|
|
|
int bufToSurface(SURFACE *ds, const byte *src, int src_w, int src_h,
|
2004-10-04 23:09:38 +00:00
|
|
|
Rect *src_rect, Point *dst_pt) {
|
2004-04-30 23:02:23 +00:00
|
|
|
const byte *read_p;
|
|
|
|
byte *write_p;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int row;
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect s;
|
2004-04-12 21:40:49 +00:00
|
|
|
int d_x, d_y;
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect clip;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int dst_off_x, dst_off_y;
|
|
|
|
int src_off_x, src_off_y;
|
|
|
|
int src_draw_w, src_draw_h;
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clamp source rectangle to source buffer
|
2004-04-12 21:40:49 +00:00
|
|
|
if (src_rect != NULL) {
|
2004-10-22 07:21:17 +00:00
|
|
|
src_rect->clip(src_w, src_h);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
s = *src_rect;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2004-04-25 16:06:22 +00:00
|
|
|
s.left = 0;
|
|
|
|
s.top = 0;
|
2004-10-22 07:21:17 +00:00
|
|
|
s.right = src_w;
|
|
|
|
s.bottom = src_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.width() <= 0 || s.height() <= 0) {
|
|
|
|
// Empty or negative region
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Get destination origin and clip rectangle
|
2004-04-12 21:40:49 +00:00
|
|
|
if (dst_pt != NULL) {
|
|
|
|
d_x = dst_pt->x;
|
|
|
|
d_y = dst_pt->y;
|
|
|
|
} else {
|
|
|
|
d_x = 0;
|
|
|
|
d_y = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clip = ds->clip_rect;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (clip.left == clip.right) {
|
|
|
|
clip.left = 0;
|
2004-10-30 22:13:48 +00:00
|
|
|
clip.right = ds->w;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (clip.top == clip.bottom) {
|
|
|
|
clip.top = 0;
|
2004-10-30 22:13:48 +00:00
|
|
|
clip.bottom = ds->h;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip source rectangle to destination surface
|
2004-04-12 21:40:49 +00:00
|
|
|
dst_off_x = d_x;
|
|
|
|
dst_off_y = d_y;
|
2004-04-25 16:06:22 +00:00
|
|
|
src_off_x = s.left;
|
|
|
|
src_off_y = s.top;
|
2004-10-22 07:21:17 +00:00
|
|
|
src_draw_w = s.width();
|
|
|
|
src_draw_h = s.height();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to left edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (d_x < clip.left) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (d_x <= (-src_draw_w)) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off left edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
src_off_x += (clip.left - d_x);
|
|
|
|
src_draw_w -= (clip.left - d_x);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
dst_off_x = clip.left;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to top edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (d_y < clip.top) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (d_y >= (-src_draw_h)) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off top edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
src_off_y += (clip.top - d_y);
|
|
|
|
src_draw_h -= (clip.top - d_y);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
dst_off_y = clip.top;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to right edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if (d_x >= clip.right) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off right edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if ((d_x + src_draw_w) > clip.right) {
|
|
|
|
src_draw_w = clip.right - d_x;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to bottom edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-21 06:47:21 +00:00
|
|
|
if (d_y > clip.bottom) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off bottom edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if ((d_y + src_draw_h) > clip.bottom) {
|
|
|
|
src_draw_h = clip.bottom - d_y;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Transfer buffer data to surface
|
2004-04-12 21:40:49 +00:00
|
|
|
read_p = (src + src_off_x) + (src_w * src_off_y);
|
2004-10-30 22:13:48 +00:00
|
|
|
write_p = ((byte *)ds->pixels + dst_off_x) + (ds->pitch * dst_off_y);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
for (row = 0; row < src_draw_h; row++) {
|
|
|
|
memcpy(write_p, read_p, src_draw_w);
|
|
|
|
|
2004-10-30 22:13:48 +00:00
|
|
|
write_p += ds->pitch;
|
2004-04-12 21:40:49 +00:00
|
|
|
read_p += src_w;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
int bufToBuffer(byte *dst_buf, int dst_w, int dst_h, const byte *src,
|
2004-10-04 23:09:38 +00:00
|
|
|
int src_w, int src_h, Rect *src_rect, Point *dst_pt) {
|
2004-04-30 23:02:23 +00:00
|
|
|
const byte *read_p;
|
|
|
|
byte *write_p;
|
2004-04-12 21:40:49 +00:00
|
|
|
int row;
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect s;
|
2004-04-12 21:40:49 +00:00
|
|
|
int d_x, d_y;
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect clip;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int dst_off_x, dst_off_y;
|
|
|
|
int src_off_x, src_off_y;
|
|
|
|
int src_draw_w, src_draw_h;
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clamp source rectangle to source buffer
|
2004-04-12 21:40:49 +00:00
|
|
|
if (src_rect != NULL) {
|
2004-10-22 07:21:17 +00:00
|
|
|
src_rect->clip(src_w, src_h);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
s.left = src_rect->left;
|
|
|
|
s.top = src_rect->top;
|
|
|
|
s.right = src_rect->right;
|
|
|
|
s.bottom = src_rect->bottom;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2004-04-25 16:06:22 +00:00
|
|
|
s.left = 0;
|
|
|
|
s.top = 0;
|
2004-10-22 07:21:17 +00:00
|
|
|
s.right = src_w;
|
|
|
|
s.bottom = src_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.width() <= 0 || s.height() <= 0) {
|
|
|
|
// Empty or negative region
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Get destination origin and clip rectangle
|
2004-04-12 21:40:49 +00:00
|
|
|
if (dst_pt != NULL) {
|
|
|
|
d_x = dst_pt->x;
|
|
|
|
d_y = dst_pt->y;
|
|
|
|
} else {
|
|
|
|
d_x = 0;
|
|
|
|
d_y = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clip.left = 0;
|
|
|
|
clip.top = 0;
|
2004-10-22 07:21:17 +00:00
|
|
|
clip.right = dst_w;
|
|
|
|
clip.bottom = dst_h;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip source rectangle to destination surface
|
2004-04-12 21:40:49 +00:00
|
|
|
dst_off_x = d_x;
|
|
|
|
dst_off_y = d_y;
|
2004-04-25 16:06:22 +00:00
|
|
|
src_off_x = s.left;
|
|
|
|
src_off_y = s.top;
|
2004-10-22 07:21:17 +00:00
|
|
|
src_draw_w = s.width();
|
|
|
|
src_draw_h = s.height();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to left edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (d_x < clip.left) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (d_x <= (-src_draw_w)) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off left edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
src_off_x += (clip.left - d_x);
|
|
|
|
src_draw_w -= (clip.left - d_x);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
dst_off_x = clip.left;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to top edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (d_y < clip.top) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (d_y >= (-src_draw_h)) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off top edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
src_off_y += (clip.top - d_y);
|
|
|
|
src_draw_h -= (clip.top - d_y);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
dst_off_y = clip.top;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to right edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if (d_x >= clip.right) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off right edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if ((d_x + src_draw_w) > clip.right) {
|
|
|
|
src_draw_w = clip.right - d_x;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to bottom edge
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if (d_y >= clip.bottom) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off bottom edge
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if ((d_y + src_draw_h) > clip.bottom) {
|
|
|
|
src_draw_h = clip.bottom - d_y;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Transfer buffer data to surface
|
2004-04-12 21:40:49 +00:00
|
|
|
read_p = (src + src_off_x) + (src_w * src_off_y);
|
|
|
|
write_p = (dst_buf + dst_off_x) + (dst_w * dst_off_y);
|
|
|
|
|
|
|
|
for (row = 0; row < src_draw_h; row++) {
|
|
|
|
memcpy(write_p, read_p, src_draw_w);
|
|
|
|
|
|
|
|
write_p += dst_w;
|
|
|
|
read_p += src_w;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Fills a rectangle in the surface ds from point 'p1' to point 'p2' using
|
|
|
|
// the specified color.
|
2005-04-22 01:38:27 +00:00
|
|
|
int drawRect(SURFACE *ds, Rect &dst_rect, int color) {
|
|
|
|
dst_rect.clip(ds->w, ds->h);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-04-22 01:38:27 +00:00
|
|
|
if (!dst_rect.isValidRect()) {
|
|
|
|
// Empty or negative region
|
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-11-25 23:51:52 +00:00
|
|
|
|
2005-04-22 01:38:27 +00:00
|
|
|
ds->fillRect(dst_rect, color);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
int drawFrame(SURFACE *ds, const Point *p1, const Point *p2, int color) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int min_x;
|
|
|
|
int max_x;
|
|
|
|
int min_y;
|
|
|
|
int max_y;
|
|
|
|
|
|
|
|
assert((ds != NULL) && (p1 != NULL) && (p2 != NULL));
|
2004-11-26 00:24:25 +00:00
|
|
|
|
|
|
|
min_x = MIN(p1->x, p2->x);
|
|
|
|
max_x = MAX(p1->x, p2->x);
|
|
|
|
min_y = MIN(p1->y, p2->y);
|
|
|
|
max_y = MAX(p1->y, p2->y);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-11-25 23:51:52 +00:00
|
|
|
ds->frameRect(Common::Rect(min_x, min_y, max_x+1, max_y+1), color);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
int drawPolyLine(SURFACE *ds, const Point *pts, int pt_ct, int draw_color) {
|
2004-04-12 21:40:49 +00:00
|
|
|
assert((ds != NULL) & (pts != NULL));
|
|
|
|
|
|
|
|
if (pt_ct < 3) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-06-09 00:42:16 +00:00
|
|
|
for (int i = 1; i < pt_ct; i++)
|
|
|
|
ds->drawLine(pts[i].x, pts[i].y, pts[i - 1].x, pts[i - 1].y, draw_color);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-06-09 00:42:16 +00:00
|
|
|
ds->drawLine(pts[pt_ct - 1].x, pts[pt_ct - 1].y, pts[0].x, pts[0].y, draw_color);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
int getClipInfo(CLIPINFO *clipinfo) {
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect s;
|
2004-04-12 21:40:49 +00:00
|
|
|
int d_x, d_y;
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect clip;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if (clipinfo == NULL) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (clipinfo->dst_pt != NULL) {
|
|
|
|
d_x = clipinfo->dst_pt->x;
|
|
|
|
d_y = clipinfo->dst_pt->y;
|
|
|
|
} else {
|
|
|
|
d_x = 0;
|
|
|
|
d_y = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
s = *clipinfo->src_rect;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clip = *clipinfo->dst_rect;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip source rectangle to destination surface
|
2004-04-12 21:40:49 +00:00
|
|
|
clipinfo->dst_draw_x = d_x;
|
|
|
|
clipinfo->dst_draw_y = d_y;
|
2004-04-25 16:06:22 +00:00
|
|
|
clipinfo->src_draw_x = s.left;
|
|
|
|
clipinfo->src_draw_y = s.top;
|
2004-10-22 07:21:17 +00:00
|
|
|
clipinfo->draw_w = s.right - s.left;
|
|
|
|
clipinfo->draw_h = s.bottom - s.top;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
clipinfo->nodraw = 0;
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to left edge
|
2004-04-25 16:06:22 +00:00
|
|
|
if (d_x < clip.left) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (d_x <= -(clipinfo->draw_w)) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off left edge
|
2004-04-12 21:40:49 +00:00
|
|
|
clipinfo->nodraw = 1;
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clipinfo->src_draw_x += (clip.left - d_x);
|
|
|
|
clipinfo->draw_w -= (clip.left - d_x);
|
|
|
|
clipinfo->dst_draw_x = clip.left;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to top edge
|
2004-04-25 16:06:22 +00:00
|
|
|
if (d_y < clip.top) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (d_y <= -(clipinfo->draw_h)) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off top edge
|
2004-04-12 21:40:49 +00:00
|
|
|
clipinfo->nodraw = 1;
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clipinfo->src_draw_y += (clip.top - d_y);
|
|
|
|
clipinfo->draw_h -= (clip.top - d_y);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clipinfo->dst_draw_y = clip.top;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to right edge
|
2004-10-22 07:21:17 +00:00
|
|
|
if (d_x >= clip.right) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off right edge
|
2004-04-12 21:40:49 +00:00
|
|
|
clipinfo->nodraw = 1;
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if ((d_x + clipinfo->draw_w) > clip.right) {
|
|
|
|
clipinfo->draw_w = clip.right - d_x;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to bottom edge
|
2004-10-22 07:21:17 +00:00
|
|
|
if (d_y >= clip.bottom) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// dst rect completely off bottom edge
|
2004-04-12 21:40:49 +00:00
|
|
|
clipinfo->nodraw = 1;
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-22 07:21:17 +00:00
|
|
|
if ((d_y + clipinfo->draw_h) > clip.bottom) {
|
|
|
|
clipinfo->draw_h = clip.bottom - d_y;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
int clipLine(SURFACE *ds, const Point *src_p1, const Point *src_p2,
|
2004-10-04 23:09:38 +00:00
|
|
|
Point *dst_p1, Point *dst_p2) {
|
|
|
|
const Point *n_p1;
|
|
|
|
const Point *n_p2;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
Common::Rect clip;
|
2004-04-25 15:14:46 +00:00
|
|
|
int left, top, right, bottom;
|
2004-04-12 21:40:49 +00:00
|
|
|
int dx, dy;
|
|
|
|
|
|
|
|
float m;
|
|
|
|
int y_icpt_l, y_icpt_r;
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
clip = ds->clip_rect;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Normalize points by x
|
|
|
|
if (src_p1->x < src_p2->x) {
|
2004-04-12 21:40:49 +00:00
|
|
|
n_p1 = src_p1;
|
|
|
|
n_p2 = src_p2;
|
|
|
|
} else {
|
|
|
|
n_p1 = src_p2;
|
|
|
|
n_p2 = src_p1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_p1->x = n_p1->x;
|
|
|
|
dst_p1->y = n_p1->y;
|
|
|
|
dst_p2->x = n_p2->x;
|
|
|
|
dst_p2->y = n_p2->y;
|
|
|
|
|
2004-04-25 15:14:46 +00:00
|
|
|
left = n_p1->x;
|
|
|
|
top = n_p1->y;
|
|
|
|
right = n_p2->x;
|
|
|
|
bottom = n_p2->y;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 15:14:46 +00:00
|
|
|
dx = right - left;
|
|
|
|
dy = bottom - top;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-25 15:14:46 +00:00
|
|
|
if (left < 0) {
|
|
|
|
if (right < 0) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// Line completely off left edge
|
2004-04-12 21:40:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to left edge
|
2004-04-25 15:14:46 +00:00
|
|
|
m = ((float)bottom - top) / (right - left);
|
|
|
|
y_icpt_l = (int)(top - (left * m) + 0.5f);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
dst_p1->x = 0;
|
|
|
|
dst_p1->y = y_icpt_l;
|
|
|
|
}
|
|
|
|
|
2004-04-25 16:06:22 +00:00
|
|
|
if (bottom > clip.right) {
|
|
|
|
if (left > clip.right) {
|
2004-05-01 07:32:48 +00:00
|
|
|
// Line completely off right edge
|
2004-04-12 21:40:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-05-01 07:32:48 +00:00
|
|
|
// Clip to right edge
|
2004-04-25 15:14:46 +00:00
|
|
|
m = ((float)top - bottom) / (right - left);
|
2004-04-25 16:06:22 +00:00
|
|
|
y_icpt_r = (int)(top - ((clip.right - left) * m) + 0.5f);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
dst_p1->y = y_icpt_r;
|
2004-04-25 16:06:22 +00:00
|
|
|
dst_p2->x = clip.right;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
SURFACE *Gfx::getBackBuffer() {
|
2004-08-02 11:27:50 +00:00
|
|
|
return &_back_buf;
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int Gfx::setPalette(SURFACE *surface, PALENTRY *pal) {
|
2004-05-02 00:00:39 +00:00
|
|
|
int i;
|
|
|
|
byte *ppal;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0, ppal = _cur_pal; i < PAL_ENTRIES; i++, ppal += 4) {
|
2005-01-27 20:07:04 +00:00
|
|
|
ppal[0] = pal[i].red;
|
|
|
|
ppal[1] = pal[i].green;
|
|
|
|
ppal[2] = pal[i].blue;
|
2004-05-02 00:00:39 +00:00
|
|
|
ppal[3] = 0;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
_system->setPalette(_cur_pal, 0, PAL_ENTRIES);
|
2004-05-02 00:00:39 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
|
2004-08-01 11:48:53 +00:00
|
|
|
int Gfx::getCurrentPal(PALENTRY *src_pal) {
|
2004-05-02 00:00:39 +00:00
|
|
|
int i;
|
|
|
|
byte *ppal;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0, ppal = _cur_pal; i < PAL_ENTRIES; i++, ppal += 4) {
|
2004-05-02 00:00:39 +00:00
|
|
|
src_pal[i].red = ppal[0];
|
|
|
|
src_pal[i].green = ppal[1];
|
|
|
|
src_pal[i].blue = ppal[2];
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int Gfx::palToBlack(SURFACE *surface, PALENTRY *src_pal, double percent) {
|
2004-05-02 00:00:39 +00:00
|
|
|
int i;
|
|
|
|
//int fade_max = 255;
|
|
|
|
int new_entry;
|
|
|
|
byte *ppal;
|
|
|
|
|
|
|
|
double fpercent;
|
|
|
|
|
|
|
|
if (percent > 1.0) {
|
|
|
|
percent = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exponential fade
|
|
|
|
fpercent = percent * percent;
|
|
|
|
|
|
|
|
fpercent = 1.0 - fpercent;
|
|
|
|
|
|
|
|
// Use the correct percentage change per frame for each palette entry
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0, ppal = _cur_pal; i < PAL_ENTRIES; i++, ppal += 4) {
|
2004-05-02 00:00:39 +00:00
|
|
|
new_entry = (int)(src_pal[i].red * fpercent);
|
|
|
|
|
|
|
|
if (new_entry < 0) {
|
|
|
|
ppal[0] = 0;
|
|
|
|
} else {
|
|
|
|
ppal[0] = (byte) new_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_entry = (int)(src_pal[i].green * fpercent);
|
|
|
|
|
|
|
|
if (new_entry < 0) {
|
|
|
|
ppal[1] = 0;
|
|
|
|
} else {
|
|
|
|
ppal[1] = (byte) new_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_entry = (int)(src_pal[i].blue * fpercent);
|
|
|
|
|
|
|
|
if (new_entry < 0) {
|
|
|
|
ppal[2] = 0;
|
|
|
|
} else {
|
|
|
|
ppal[2] = (byte) new_entry;
|
|
|
|
}
|
|
|
|
ppal[3] = 0;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
_system->setPalette(_cur_pal, 0, PAL_ENTRIES);
|
2004-05-02 00:00:39 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int Gfx::blackToPal(SURFACE *surface, PALENTRY *src_pal, double percent) {
|
2004-05-02 00:00:39 +00:00
|
|
|
int new_entry;
|
|
|
|
double fpercent;
|
|
|
|
int color_delta;
|
|
|
|
int best_wdelta = 0;
|
|
|
|
int best_windex = 0;
|
|
|
|
int best_bindex = 0;
|
|
|
|
int best_bdelta = 1000;
|
|
|
|
byte *ppal;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (percent > 1.0) {
|
|
|
|
percent = 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exponential fade
|
|
|
|
fpercent = percent * percent;
|
|
|
|
|
|
|
|
fpercent = 1.0 - fpercent;
|
|
|
|
|
|
|
|
// Use the correct percentage change per frame for each palette entry
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0, ppal = _cur_pal; i < PAL_ENTRIES; i++, ppal += 4) {
|
2004-05-02 00:00:39 +00:00
|
|
|
new_entry = (int)(src_pal[i].red - src_pal[i].red * fpercent);
|
|
|
|
|
|
|
|
if (new_entry < 0) {
|
|
|
|
ppal[0] = 0;
|
|
|
|
} else {
|
|
|
|
ppal[0] = (byte) new_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_entry = (int)(src_pal[i].green - src_pal[i].green * fpercent);
|
|
|
|
|
|
|
|
if (new_entry < 0) {
|
|
|
|
ppal[1] = 0;
|
|
|
|
} else {
|
|
|
|
ppal[1] = (byte) new_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_entry = (int)(src_pal[i].blue - src_pal[i].blue * fpercent);
|
|
|
|
|
|
|
|
if (new_entry < 0) {
|
|
|
|
ppal[2] = 0;
|
|
|
|
} else {
|
|
|
|
ppal[2] = (byte) new_entry;
|
|
|
|
}
|
|
|
|
ppal[3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the best white and black color indices again
|
|
|
|
if (percent >= 1.0) {
|
2004-10-27 21:32:28 +00:00
|
|
|
for (i = 0, ppal = _cur_pal; i < PAL_ENTRIES; i++, ppal += 4) {
|
2004-05-02 00:00:39 +00:00
|
|
|
color_delta = ppal[0];
|
|
|
|
color_delta += ppal[1];
|
|
|
|
color_delta += ppal[2];
|
|
|
|
|
|
|
|
if (color_delta < best_bdelta) {
|
|
|
|
best_bindex = i;
|
|
|
|
best_bdelta = color_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (color_delta > best_wdelta) {
|
|
|
|
best_windex = i;
|
|
|
|
best_wdelta = color_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
_system->setPalette(_cur_pal, 0, PAL_ENTRIES);
|
2004-05-02 00:00:39 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-05-02 00:00:39 +00:00
|
|
|
}
|
|
|
|
|
2004-11-20 00:05:50 +00:00
|
|
|
void Gfx::showCursor(bool state) {
|
|
|
|
updateCursor();
|
|
|
|
g_system->showMouse(state);
|
|
|
|
}
|
|
|
|
|
2005-01-15 13:41:57 +00:00
|
|
|
void Gfx::setCursor() {
|
2004-05-02 16:32:28 +00:00
|
|
|
// Set up the mouse cursor
|
2005-01-20 13:59:12 +00:00
|
|
|
const byte A = kITEColorLightGrey;
|
|
|
|
const byte B = kITEColorWhite;
|
|
|
|
|
|
|
|
const byte cursor_img[CURSOR_W * CURSOR_H] = {
|
|
|
|
0, 0, 0, A, 0, 0, 0,
|
|
|
|
0, 0, 0, A, 0, 0, 0,
|
|
|
|
0, 0, 0, A, 0, 0, 0,
|
2005-01-21 23:18:32 +00:00
|
|
|
A, A, A, B, A, A, A,
|
2005-01-20 13:59:12 +00:00
|
|
|
0, 0, 0, A, 0, 0, 0,
|
|
|
|
0, 0, 0, A, 0, 0, 0,
|
|
|
|
0, 0, 0, A, 0, 0, 0,
|
2004-05-02 16:32:28 +00:00
|
|
|
};
|
|
|
|
|
2005-01-15 13:41:57 +00:00
|
|
|
_system->setMouseCursor(cursor_img, CURSOR_W, CURSOR_H, 3, 3, 0);
|
2004-05-02 16:32:28 +00:00
|
|
|
}
|
|
|
|
|
2004-10-30 22:34:08 +00:00
|
|
|
bool hitTestPoly(const Point *points, unsigned int npoints, const Point& test_point) {
|
2004-10-08 01:22:39 +00:00
|
|
|
int yflag0;
|
|
|
|
int yflag1;
|
|
|
|
bool inside_flag = false;
|
|
|
|
unsigned int pt;
|
|
|
|
|
2004-10-08 19:58:49 +00:00
|
|
|
const Point *vtx0 = &points[npoints - 1];
|
|
|
|
const Point *vtx1 = &points[0];
|
2004-10-08 01:22:39 +00:00
|
|
|
|
|
|
|
yflag0 = (vtx0->y >= test_point.y);
|
|
|
|
for (pt = 0; pt < npoints; pt++, vtx1++) {
|
|
|
|
yflag1 = (vtx1->y >= test_point.y);
|
|
|
|
if (yflag0 != yflag1) {
|
|
|
|
if (((vtx1->y - test_point.y) * (vtx0->x - vtx1->x) >=
|
|
|
|
(vtx1->x - test_point.x) * (vtx0->y - vtx1->y)) == yflag1) {
|
|
|
|
inside_flag = !inside_flag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yflag0 = yflag1;
|
|
|
|
vtx0 = vtx1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inside_flag;
|
|
|
|
}
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
} // End of namespace Saga
|