/*
draw.c
this is the only file outside the refresh that touches the vid buffer
Copyright (C) 1996-1997 Id Software, Inc.
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:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id: draw.c,v 1.1.1.1 2001/03/06 09:33:19 lordhavoc Exp $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include "client.h"
#include "console.h"
#include "d_iface.h"
#include "draw.h"
#include "quakefs.h"
#include "sound.h"
#include "sys.h"
#include "vid.h"
typedef struct {
vrect_t rect;
int width;
int height;
byte *ptexbytes;
int rowbytes;
} rectdesc_t;
static rectdesc_t r_rectdesc;
byte *draw_chars; // 8*8 graphic characters
qpic_t *draw_disc;
qpic_t *draw_backtile;
cvar_t *cl_verstring;
//=============================================================================
/* Support Routines */
typedef struct cachepic_s {
char name[MAX_QPATH];
cache_user_t cache;
} cachepic_t;
#define MAX_CACHED_PICS 128
cachepic_t menu_cachepics[MAX_CACHED_PICS];
int menu_numcachepics;
qpic_t *
Draw_PicFromWad (char *name)
{
return W_GetLumpName (name);
}
/*
Draw_ClearCache
This is a no-op in software targets
*/
void
Draw_ClearCache (void)
{
}
/*
Draw_CachePic
*/
qpic_t *
Draw_CachePic (char *path)
{
cachepic_t *pic;
int i;
qpic_t *dat;
for (pic = menu_cachepics, i = 0; i < menu_numcachepics; pic++, i++)
if (!strcmp (path, pic->name))
break;
if (i == menu_numcachepics) {
if (menu_numcachepics == MAX_CACHED_PICS)
Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
menu_numcachepics++;
strcpy (pic->name, path);
}
dat = Cache_Check (&pic->cache);
if (dat)
return dat;
//
// load the pic from disk
//
COM_LoadCacheFile (path, &pic->cache);
dat = (qpic_t *) pic->cache.data;
if (!dat) {
Sys_Error ("Draw_CachePic: failed to load %s", path);
}
SwapPic (dat);
return dat;
}
void
Draw_TextBox (int x, int y, int width, int lines)
{
qpic_t *p;
int cx, cy;
int n;
// draw left side
cx = x;
cy = y;
p = Draw_CachePic ("gfx/box_tl.lmp");
Draw_Pic (cx, cy, p);
p = Draw_CachePic ("gfx/box_ml.lmp");
for (n = 0; n < lines; n++) {
cy += 8;
Draw_Pic (cx, cy, p);
}
p = Draw_CachePic ("gfx/box_bl.lmp");
Draw_Pic (cx, cy + 8, p);
// draw middle
cx += 8;
while (width > 0) {
cy = y;
p = Draw_CachePic ("gfx/box_tm.lmp");
Draw_Pic (cx, cy, p);
p = Draw_CachePic ("gfx/box_mm.lmp");
for (n = 0; n < lines; n++) {
cy += 8;
if (n == 1)
p = Draw_CachePic ("gfx/box_mm2.lmp");
Draw_Pic (cx, cy, p);
}
p = Draw_CachePic ("gfx/box_bm.lmp");
Draw_Pic (cx, cy + 8, p);
width -= 2;
cx += 16;
}
// draw right side
cy = y;
p = Draw_CachePic ("gfx/box_tr.lmp");
Draw_Pic (cx, cy, p);
p = Draw_CachePic ("gfx/box_mr.lmp");
for (n = 0; n < lines; n++) {
cy += 8;
Draw_Pic (cx, cy, p);
}
p = Draw_CachePic ("gfx/box_br.lmp");
Draw_Pic (cx, cy + 8, p);
}
/*
Draw_Init
*/
void
Draw_Init (void)
{
draw_chars = W_GetLumpName ("conchars");
draw_disc = W_GetLumpName ("disc");
draw_backtile = W_GetLumpName ("backtile");
r_rectdesc.width = draw_backtile->width;
r_rectdesc.height = draw_backtile->height;
r_rectdesc.ptexbytes = draw_backtile->data;
r_rectdesc.rowbytes = draw_backtile->width;
cl_verstring =
Cvar_Get ("cl_verstring", PROGRAM " " VERSION, CVAR_NONE, "Client version string");
}
void
Draw_Init_Cvars (void)
{
}
/*
Draw_Character
Draws one 8*8 graphics character with 0 being transparent.
It can be clipped to the top of the screen to allow the console to be
smoothly scrolled off.
*/
void
Draw_Character (int x, int y, int num)
{
byte *source;
int drawline;
int row, col;
num &= 255;
if (y <= -8)
return; // totally off screen
if (y > vid.height - 8 || x < 0 || x > vid.width - 8)
return;
if (num < 0 || num > 255)
return;
row = num >> 4;
col = num & 15;
source = draw_chars + (row << 10) + (col << 3);
if (y < 0) { // clipped
drawline = 8 + y;
source -= 128 * y;
y = 0;
} else
drawline = 8;
switch(r_pixbytes)
{
case 1:
{
byte *dest = (byte *) vid.conbuffer + y * vid.conrowbytes + x;
while (drawline--) {
if (source[0])
dest[0] = source[0];
if (source[1])
dest[1] = source[1];
if (source[2])
dest[2] = source[2];
if (source[3])
dest[3] = source[3];
if (source[4])
dest[4] = source[4];
if (source[5])
dest[5] = source[5];
if (source[6])
dest[6] = source[6];
if (source[7])
dest[7] = source[7];
source += 128;
dest += vid.conrowbytes;
}
}
break;
case 2:
{
unsigned short *dest = (unsigned short *) vid.conbuffer + y * (vid.conrowbytes >> 1) + x;
while (drawline--) {
if (source[0])
dest[0] = d_8to16table[source[0]];
if (source[1])
dest[1] = d_8to16table[source[1]];
if (source[2])
dest[2] = d_8to16table[source[2]];
if (source[3])
dest[3] = d_8to16table[source[3]];
if (source[4])
dest[4] = d_8to16table[source[4]];
if (source[5])
dest[5] = d_8to16table[source[5]];
if (source[6])
dest[6] = d_8to16table[source[6]];
if (source[7])
dest[7] = d_8to16table[source[7]];
source += 128;
dest += (vid.conrowbytes >> 1);
}
}
break;
case 4:
{
unsigned int *dest = (unsigned int *) vid.conbuffer + y * (vid.conrowbytes >> 2) + x;
while (drawline--) {
if (source[0])
dest[0] = d_8to24table[source[0]];
if (source[1])
dest[1] = d_8to24table[source[1]];
if (source[2])
dest[2] = d_8to24table[source[2]];
if (source[3])
dest[3] = d_8to24table[source[3]];
if (source[4])
dest[4] = d_8to24table[source[4]];
if (source[5])
dest[5] = d_8to24table[source[5]];
if (source[6])
dest[6] = d_8to24table[source[6]];
if (source[7])
dest[7] = d_8to24table[source[7]];
source += 128;
dest += (vid.conrowbytes >> 2);
}
}
break;
default:
Sys_Error("Draw_Character: unsupported r_pixbytes %i\n", r_pixbytes);
}
}
/*
Draw_String
*/
void
Draw_String (int x, int y, char *str)
{
while (*str) {
Draw_Character (x, y, *str++);
x += 8;
}
}
/*
Draw_AltString
*/
void
Draw_AltString (int x, int y, char *str)
{
while (*str) {
Draw_Character (x, y, (*str++) | 0x80);
x += 8;
}
}
void
Draw_Pixel (int x, int y, byte color)
{
switch(r_pixbytes)
{
case 1:
((byte *) vid.conbuffer)[y * vid.conrowbytes + x] = color;
break;
case 2:
((unsigned short *) vid.conbuffer)[y * (vid.conrowbytes >> 1) + x] = d_8to16table[color];
break;
case 4:
((unsigned int *) vid.conbuffer)[y * (vid.conrowbytes >> 2) + x] = d_8to24table[color];
break;
default:
Sys_Error("Draw_Pixel: unsupported r_pixbytes %i\n", r_pixbytes);
}
}
void
Draw_Crosshair (void)
{
int x, y;
extern cvar_t *crosshair, *cl_crossx, *cl_crossy, *crosshaircolor;
extern vrect_t scr_vrect;
byte c = crosshaircolor->int_val;
if (crosshair->int_val == 2) {
x = scr_vrect.x + scr_vrect.width / 2 + cl_crossx->int_val;
y = scr_vrect.y + scr_vrect.height / 2 + cl_crossy->int_val;
Draw_Pixel (x - 1, y, c);
Draw_Pixel (x - 3, y, c);
Draw_Pixel (x + 1, y, c);
Draw_Pixel (x + 3, y, c);
Draw_Pixel (x, y - 1, c);
Draw_Pixel (x, y - 3, c);
Draw_Pixel (x, y + 1, c);
Draw_Pixel (x, y + 3, c);
} else if (crosshair->int_val)
Draw_Character (scr_vrect.x + scr_vrect.width / 2 - 4 +
cl_crossx->int_val,
scr_vrect.y + scr_vrect.height / 2 - 4 +
cl_crossy->int_val, '+');
}
/*
Draw_Pic
*/
void
Draw_Pic (int x, int y, qpic_t *pic)
{
byte *source, tbyte;
int v, u;
if (x < 0 || (unsigned int) (x + pic->width) > vid.width || y < 0 ||
(unsigned int) (y + pic->height) > vid.height) {
Sys_Error ("Draw_Pic: bad coordinates");
}
source = pic->data;
switch(r_pixbytes)
{
case 1:
{
byte *dest = (byte *) vid.buffer + y * vid.rowbytes + x;
if (pic->width & 7) { // general
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u++)
if ((tbyte = source[u]) != TRANSPARENT_COLOR)
dest[u] = tbyte;
dest += vid.rowbytes;
source += pic->width;
}
} else { // unwound
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u += 8) {
if ((tbyte = source[u]) != TRANSPARENT_COLOR)
dest[u] = tbyte;
if ((tbyte = source[u + 1]) != TRANSPARENT_COLOR)
dest[u + 1] = tbyte;
if ((tbyte = source[u + 2]) != TRANSPARENT_COLOR)
dest[u + 2] = tbyte;
if ((tbyte = source[u + 3]) != TRANSPARENT_COLOR)
dest[u + 3] = tbyte;
if ((tbyte = source[u + 4]) != TRANSPARENT_COLOR)
dest[u + 4] = tbyte;
if ((tbyte = source[u + 5]) != TRANSPARENT_COLOR)
dest[u + 5] = tbyte;
if ((tbyte = source[u + 6]) != TRANSPARENT_COLOR)
dest[u + 6] = tbyte;
if ((tbyte = source[u + 7]) != TRANSPARENT_COLOR)
dest[u + 7] = tbyte;
}
dest += vid.rowbytes;
source += pic->width;
}
}
}
break;
case 2:
{
unsigned short *dest = (unsigned short *) vid.buffer + y * (vid.rowbytes >> 1) + x;
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u++) {
tbyte = source[u];
if (tbyte != TRANSPARENT_COLOR)
dest[u] = d_8to16table[tbyte];
}
dest += vid.rowbytes >> 1;
source += pic->width;
}
}
break;
case 4:
{
unsigned int *dest = (unsigned int *) vid.buffer + y * (vid.rowbytes >> 2) + x;
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u++) {
tbyte = source[u];
if (tbyte != TRANSPARENT_COLOR)
dest[u] = d_8to24table[tbyte];
}
dest += vid.rowbytes >> 2;
source += pic->width;
}
}
break;
default:
Sys_Error("Draw_Pic: unsupported r_pixbytes %i\n", r_pixbytes);
}
}
/*
Draw_SubPic
*/
void
Draw_SubPic (int x, int y, qpic_t *pic, int srcx, int srcy, int width,
int height)
{
byte *source;
int v, u;
if ((x < 0) ||
(x + width > vid.width) || (y < 0) || (y + height > vid.height)) {
Sys_Error ("Draw_SubPic: bad coordinates");
}
source = pic->data + srcy * pic->width + srcx;
switch (r_pixbytes)
{
case 1:
{
byte *dest = (byte *) vid.buffer + y * vid.rowbytes + x;
for (v = 0; v < height; v++) {
memcpy (dest, source, width);
dest += vid.rowbytes;
source += pic->width;
}
}
break;
case 2:
{
unsigned short *dest = (unsigned short *) vid.buffer + y * (vid.rowbytes >> 1) + x;
for (v = 0; v < height; v++, dest += vid.rowbytes >> 1, source += pic->width)
for (u = 0; u < width; u++)
dest[u] = d_8to16table[source[u]];
}
break;
case 4:
{
unsigned int *dest = (unsigned int *) vid.buffer + y * (vid.rowbytes >> 2) + x;
for (v = 0; v < height; v++, dest += vid.rowbytes >> 2, source += pic->width)
for (u = 0; u < width; u++)
dest[u] = d_8to24table[source[u]];
}
break;
default:
Sys_Error("Draw_SubPic: unsupported r_pixbytes %i\n", r_pixbytes);
}
}
/*
Draw_TransPicTranslate
*/
void
Draw_TransPicTranslate (int x, int y, qpic_t *pic, byte * translation)
{
byte *source, tbyte;
int v, u;
if (x < 0 || (unsigned int) (x + pic->width) > vid.width || y < 0 ||
(unsigned int) (y + pic->height) > vid.height) {
Sys_Error ("Draw_TransPic: bad coordinates");
}
source = pic->data;
switch(r_pixbytes)
{
case 1:
{
byte *dest = (byte *) vid.buffer + y * vid.rowbytes + x;
if (pic->width & 7) { // general
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u++)
if ((tbyte = source[u]) != TRANSPARENT_COLOR)
dest[u] = translation[tbyte];
dest += vid.rowbytes;
source += pic->width;
}
} else { // unwound
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u += 8) {
if ((tbyte = source[u]) != TRANSPARENT_COLOR)
dest[u] = translation[tbyte];
if ((tbyte = source[u + 1]) != TRANSPARENT_COLOR)
dest[u + 1] = translation[tbyte];
if ((tbyte = source[u + 2]) != TRANSPARENT_COLOR)
dest[u + 2] = translation[tbyte];
if ((tbyte = source[u + 3]) != TRANSPARENT_COLOR)
dest[u + 3] = translation[tbyte];
if ((tbyte = source[u + 4]) != TRANSPARENT_COLOR)
dest[u + 4] = translation[tbyte];
if ((tbyte = source[u + 5]) != TRANSPARENT_COLOR)
dest[u + 5] = translation[tbyte];
if ((tbyte = source[u + 6]) != TRANSPARENT_COLOR)
dest[u + 6] = translation[tbyte];
if ((tbyte = source[u + 7]) != TRANSPARENT_COLOR)
dest[u + 7] = translation[tbyte];
}
dest += vid.rowbytes;
source += pic->width;
}
}
}
break;
case 2:
{
unsigned short *dest = (unsigned short *) vid.buffer + y * (vid.rowbytes >> 1) + x;
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u++) {
tbyte = source[u];
if (tbyte != TRANSPARENT_COLOR)
dest[u] = d_8to16table[translation[tbyte]];
}
dest += vid.rowbytes >> 1;
source += pic->width;
}
}
break;
case 4:
{
unsigned int *dest = (unsigned int *) vid.buffer + y * (vid.rowbytes >> 2) + x;
for (v = 0; v < pic->height; v++) {
for (u = 0; u < pic->width; u++) {
tbyte = source[u];
if (tbyte != TRANSPARENT_COLOR)
dest[u] = d_8to24table[translation[tbyte]];
}
dest += vid.rowbytes >> 1;
source += pic->width;
}
}
break;
default:
Sys_Error("Draw_TransPicTranslate: unsupported r_pixbytes %i\n", r_pixbytes);
}
}
/*
Draw_ConsoleBackground
*/
void
Draw_ConsoleBackground (int lines)
{
int x, y, v;
byte *src;
int f, fstep;
qpic_t *conback;
conback = Draw_CachePic ("gfx/conback.lmp");
// draw the pic
switch(r_pixbytes)
{
case 1:
{
byte *dest = vid.conbuffer;
for (y = 0; y < lines; y++, dest += vid.conrowbytes) {
v = (vid.conheight - lines + y) * 200 / vid.conheight;
src = conback->data + v * 320;
if (vid.conwidth == 320)
memcpy (dest, src