2001-04-15 06:43:11 +00:00
|
|
|
/*
|
|
|
|
vid.c
|
|
|
|
|
|
|
|
general video driver functions
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2001-08-27 01:00:03 +00:00
|
|
|
|
2002-09-10 06:35:32 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2001-04-15 06:43:11 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2010-11-27 06:10:34 +00:00
|
|
|
#include "QF/console.h"
|
2001-04-15 06:43:11 +00:00
|
|
|
#include "QF/cvar.h"
|
2011-12-24 01:04:33 +00:00
|
|
|
#include "QF/mathlib.h"
|
2001-04-15 06:43:11 +00:00
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
2021-07-10 09:04:34 +00:00
|
|
|
#include "QF/ui/view.h"
|
2001-08-27 01:00:03 +00:00
|
|
|
|
|
|
|
#include "compat.h"
|
2013-01-27 03:53:58 +00:00
|
|
|
#include "d_iface.h"
|
2012-02-17 07:13:56 +00:00
|
|
|
#include "vid_internal.h"
|
2001-04-15 06:43:11 +00:00
|
|
|
|
2001-08-27 01:00:03 +00:00
|
|
|
/* Software and hardware gamma support */
|
2013-01-16 02:23:47 +00:00
|
|
|
#define viddef (*r_data->vid)
|
2002-06-18 21:41:24 +00:00
|
|
|
cvar_t *vid_gamma;
|
|
|
|
cvar_t *vid_system_gamma;
|
2003-07-27 20:20:20 +00:00
|
|
|
cvar_t *con_width; // FIXME: Try to move with rest of con code
|
|
|
|
cvar_t *con_height; // FIXME: Try to move with rest of con code
|
2001-04-15 06:43:11 +00:00
|
|
|
qboolean vid_gamma_avail; // hardware gamma availability
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE unsigned int d_8to24table[256];
|
2002-06-18 21:41:24 +00:00
|
|
|
|
2001-08-27 01:00:03 +00:00
|
|
|
/* Screen size */
|
2002-06-18 21:41:24 +00:00
|
|
|
cvar_t *vid_width;
|
|
|
|
cvar_t *vid_height;
|
2001-04-15 06:43:11 +00:00
|
|
|
|
2001-09-28 16:54:31 +00:00
|
|
|
cvar_t *vid_fullscreen;
|
2001-08-27 01:00:03 +00:00
|
|
|
|
2021-07-10 09:04:34 +00:00
|
|
|
static view_t conview;
|
|
|
|
|
2001-04-15 06:43:11 +00:00
|
|
|
void
|
|
|
|
VID_GetWindowSize (int def_w, int def_h)
|
|
|
|
{
|
2003-06-21 00:07:59 +00:00
|
|
|
int pnum, conheight;
|
2001-04-15 06:43:11 +00:00
|
|
|
|
2021-01-31 07:01:20 +00:00
|
|
|
vid_width = Cvar_Get ("vid_width", va (0, "%d", def_w), CVAR_NONE, NULL,
|
2001-04-15 06:43:11 +00:00
|
|
|
"screen width");
|
2021-01-31 07:01:20 +00:00
|
|
|
vid_height = Cvar_Get ("vid_height", va (0, "%d", def_h), CVAR_NONE, NULL,
|
2001-04-15 06:43:11 +00:00
|
|
|
"screen height");
|
|
|
|
|
|
|
|
if ((pnum = COM_CheckParm ("-width"))) {
|
|
|
|
if (pnum >= com_argc - 1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("VID: -width <width>");
|
2001-04-15 06:43:11 +00:00
|
|
|
|
|
|
|
Cvar_Set (vid_width, com_argv[pnum + 1]);
|
|
|
|
|
|
|
|
if (!vid_width->int_val)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("VID: Bad window width");
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((pnum = COM_CheckParm ("-height"))) {
|
|
|
|
if (pnum >= com_argc - 1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("VID: -height <height>");
|
2001-04-15 06:43:11 +00:00
|
|
|
|
|
|
|
Cvar_Set (vid_height, com_argv[pnum + 1]);
|
|
|
|
|
|
|
|
if (!vid_height->int_val)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("VID: Bad window height");
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((pnum = COM_CheckParm ("-winsize"))) {
|
|
|
|
if (pnum >= com_argc - 2)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("VID: -winsize <width> <height>");
|
2001-04-15 06:43:11 +00:00
|
|
|
|
|
|
|
Cvar_Set (vid_width, com_argv[pnum + 1]);
|
|
|
|
Cvar_Set (vid_height, com_argv[pnum + 2]);
|
|
|
|
|
|
|
|
if (!vid_width->int_val || !vid_height->int_val)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("VID: Bad window width/height");
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Cvar_SetFlags (vid_width, vid_width->flags | CVAR_ROM);
|
|
|
|
Cvar_SetFlags (vid_height, vid_height->flags | CVAR_ROM);
|
|
|
|
|
2013-01-27 03:53:58 +00:00
|
|
|
// viddef.maxlowwidth = LOW_WIDTH;
|
|
|
|
// viddef.maxlowheight = LOW_HEIGHT;
|
|
|
|
|
2012-02-14 10:47:02 +00:00
|
|
|
viddef.width = vid_width->int_val;
|
|
|
|
viddef.height = vid_height->int_val;
|
2021-07-10 09:04:34 +00:00
|
|
|
viddef.conview = &conview;
|
2003-03-17 05:17:58 +00:00
|
|
|
|
2021-01-31 07:01:20 +00:00
|
|
|
con_width = Cvar_Get ("con_width", va (0, "%d", viddef.width), CVAR_NONE,
|
2012-02-14 10:47:02 +00:00
|
|
|
NULL, "console effective width (GL only)");
|
2003-03-17 05:17:58 +00:00
|
|
|
if ((pnum = COM_CheckParm ("-conwidth"))) {
|
|
|
|
if (pnum >= com_argc - 1)
|
|
|
|
Sys_Error ("VID: -conwidth <width>");
|
2003-07-27 20:20:20 +00:00
|
|
|
Cvar_Set (con_width, com_argv[pnum + 1]);
|
2003-03-17 05:17:58 +00:00
|
|
|
}
|
2009-12-21 12:36:12 +00:00
|
|
|
// make con_width a multiple of 8 and >= 320
|
2021-01-31 07:01:20 +00:00
|
|
|
Cvar_Set (con_width, va (0, "%d", max (con_width->int_val & ~7, 320)));
|
2003-07-27 20:20:20 +00:00
|
|
|
Cvar_SetFlags (con_width, con_width->flags | CVAR_ROM);
|
2021-07-10 09:04:34 +00:00
|
|
|
viddef.conview->xlen = con_width->int_val;
|
2003-07-27 20:20:20 +00:00
|
|
|
|
2021-07-10 09:04:34 +00:00
|
|
|
conheight = (viddef.conview->xlen * viddef.height) / viddef.width;
|
2021-01-31 07:01:20 +00:00
|
|
|
con_height = Cvar_Get ("con_height", va (0, "%d", conheight), CVAR_NONE,
|
|
|
|
NULL, "console effective height (GL only)");
|
2003-06-21 00:07:59 +00:00
|
|
|
if ((pnum = COM_CheckParm ("-conheight"))) {
|
|
|
|
if (pnum >= com_argc - 1)
|
|
|
|
Sys_Error ("VID: -conheight <width>");
|
2003-07-27 20:20:20 +00:00
|
|
|
Cvar_Set (con_height, com_argv[pnum + 1]);
|
2003-06-21 00:07:59 +00:00
|
|
|
}
|
2009-12-21 12:36:12 +00:00
|
|
|
// make con_height >= 200
|
2021-01-31 07:01:20 +00:00
|
|
|
Cvar_Set (con_height, va (0, "%d", max (con_height->int_val, 200)));
|
2003-07-27 20:20:20 +00:00
|
|
|
Cvar_SetFlags (con_height, con_height->flags | CVAR_ROM);
|
2021-07-10 09:04:34 +00:00
|
|
|
viddef.conview->ylen = con_height->int_val;
|
2010-11-27 06:10:34 +00:00
|
|
|
|
|
|
|
Con_CheckResize (); // Now that we have a window size, fix console
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
|
2001-08-27 01:00:03 +00:00
|
|
|
/* GAMMA FUNCTIONS */
|
2001-04-15 06:43:11 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-04-15 06:43:11 +00:00
|
|
|
VID_BuildGammaTable (double gamma)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (gamma == 1.0) { // linear, don't bother with the math
|
|
|
|
for (i = 0; i < 256; i++) {
|
2012-02-26 05:00:15 +00:00
|
|
|
viddef.gammatable[i] = i;
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
double g = 1.0 / gamma;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) { // Build/update gamma lookup table
|
|
|
|
v = (int) ((255.0 * pow ((double) i / 255.0, g)) + 0.5);
|
2012-02-26 05:00:15 +00:00
|
|
|
viddef.gammatable[i] = bound (0, v, 255);
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
VID_UpdateGamma
|
|
|
|
|
|
|
|
This is a callback to update the palette or system gamma whenever the
|
|
|
|
vid_gamma Cvar is changed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
VID_UpdateGamma (cvar_t *vid_gamma)
|
|
|
|
{
|
|
|
|
double gamma = bound (0.1, vid_gamma->value, 9.9);
|
2021-01-23 14:56:33 +00:00
|
|
|
byte *p24;
|
|
|
|
byte *p32;
|
2021-03-25 13:42:16 +00:00
|
|
|
const byte *col;
|
2021-01-23 14:56:33 +00:00
|
|
|
int i;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2012-02-14 10:47:02 +00:00
|
|
|
viddef.recalc_refdef = 1; // force a surface cache flush
|
2001-05-24 15:48:39 +00:00
|
|
|
|
2001-04-15 06:43:11 +00:00
|
|
|
if (vid_gamma_avail && vid_system_gamma->int_val) { // Have system, use it
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_vid, "Setting hardware gamma to %g\n", gamma);
|
2001-04-15 06:43:11 +00:00
|
|
|
VID_BuildGammaTable (1.0); // hardware gamma wants a linear palette
|
|
|
|
VID_SetGamma (gamma);
|
2021-01-23 14:56:33 +00:00
|
|
|
p24 = viddef.palette;
|
|
|
|
p32 = viddef.palette32;
|
|
|
|
col = viddef.basepal;
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
*p32++ = *p24++ = *col++;
|
|
|
|
*p32++ = *p24++ = *col++;
|
|
|
|
*p32++ = *p24++ = *col++;
|
|
|
|
*p32++ = 255;
|
|
|
|
}
|
|
|
|
p32[-1] = 0; // color 255 is transparent
|
2001-04-15 06:43:11 +00:00
|
|
|
} else { // We have to hack the palette
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_vid, "Setting software gamma to %g\n", gamma);
|
2001-04-15 06:43:11 +00:00
|
|
|
VID_BuildGammaTable (gamma);
|
2021-01-23 14:56:33 +00:00
|
|
|
p24 = viddef.palette;
|
|
|
|
p32 = viddef.palette32;
|
|
|
|
col = viddef.basepal;
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
*p32++ = *p24++ = viddef.gammatable[*col++];
|
|
|
|
*p32++ = *p24++ = viddef.gammatable[*col++];
|
|
|
|
*p32++ = *p24++ = viddef.gammatable[*col++];
|
|
|
|
*p32++ = 255;
|
|
|
|
}
|
|
|
|
p32[-1] = 0; // color 255 is transparent
|
2019-07-08 04:40:29 +00:00
|
|
|
viddef.vid_internal->set_palette (viddef.palette); // update with the new palette
|
2001-04-15 06:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
VID_InitGamma
|
|
|
|
|
|
|
|
Initialize the vid_gamma Cvar, and set up the palette
|
|
|
|
*/
|
|
|
|
void
|
2013-01-27 10:57:40 +00:00
|
|
|
VID_InitGamma (const byte *pal)
|
2001-04-15 06:43:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
2012-07-16 13:05:36 +00:00
|
|
|
double gamma = 1.0;
|
2001-04-15 06:43:11 +00:00
|
|
|
|
2012-02-26 05:00:15 +00:00
|
|
|
viddef.gammatable = malloc (256);
|
2012-02-14 10:47:02 +00:00
|
|
|
viddef.basepal = pal;
|
|
|
|
viddef.palette = malloc (256 * 3);
|
2021-01-23 14:56:33 +00:00
|
|
|
viddef.palette32 = malloc (256 * 4);
|
2001-04-15 06:43:11 +00:00
|
|
|
if ((i = COM_CheckParm ("-gamma"))) {
|
|
|
|
gamma = atof (com_argv[i + 1]);
|
|
|
|
}
|
|
|
|
gamma = bound (0.1, gamma, 9.9);
|
|
|
|
|
2021-01-31 07:01:20 +00:00
|
|
|
vid_gamma = Cvar_Get ("vid_gamma", va (0, "%f", gamma), CVAR_ARCHIVE,
|
2001-04-15 06:43:11 +00:00
|
|
|
VID_UpdateGamma, "Gamma correction");
|
|
|
|
|
|
|
|
VID_BuildGammaTable (vid_gamma->value);
|
|
|
|
}
|
2012-02-23 01:06:30 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
VID_InitBuffers (void)
|
|
|
|
{
|
|
|
|
int buffersize, zbuffersize, cachesize = 1;
|
|
|
|
|
|
|
|
// No console scaling in the sw renderer
|
2021-07-10 09:04:34 +00:00
|
|
|
viddef.conview->xlen = viddef.width;
|
|
|
|
viddef.conview->ylen = viddef.height;
|
2012-02-23 01:06:30 +00:00
|
|
|
Con_CheckResize ();
|
|
|
|
|
|
|
|
// Calculate the sizes we want first
|
|
|
|
buffersize = viddef.rowbytes * viddef.height;
|
|
|
|
zbuffersize = viddef.width * viddef.height * sizeof (*viddef.zbuffer);
|
2019-07-08 04:40:29 +00:00
|
|
|
if (viddef.vid_internal->surf_cache_size)
|
|
|
|
cachesize = viddef.vid_internal->surf_cache_size (viddef.width,
|
|
|
|
viddef.height);
|
2012-02-23 01:06:30 +00:00
|
|
|
|
|
|
|
// Free the old z-buffer
|
|
|
|
if (viddef.zbuffer) {
|
|
|
|
free (viddef.zbuffer);
|
|
|
|
viddef.zbuffer = NULL;
|
|
|
|
}
|
|
|
|
// Free the old surface cache
|
|
|
|
if (viddef.surfcache) {
|
2019-07-08 04:40:29 +00:00
|
|
|
if (viddef.vid_internal->flush_caches)
|
|
|
|
viddef.vid_internal->flush_caches ();
|
2012-02-23 01:06:30 +00:00
|
|
|
free (viddef.surfcache);
|
|
|
|
viddef.surfcache = NULL;
|
|
|
|
}
|
2021-03-31 04:05:50 +00:00
|
|
|
if (viddef.vid_internal->init_buffers) {
|
|
|
|
viddef.vid_internal->init_buffers ();
|
2012-02-23 01:06:30 +00:00
|
|
|
} else {
|
|
|
|
// Free the old screen buffer
|
|
|
|
if (viddef.buffer) {
|
|
|
|
free (viddef.buffer);
|
|
|
|
viddef.conbuffer = viddef.buffer = NULL;
|
|
|
|
}
|
|
|
|
// Allocate the new screen buffer
|
|
|
|
viddef.conbuffer = viddef.buffer = calloc (buffersize, 1);
|
|
|
|
if (!viddef.conbuffer) {
|
|
|
|
Sys_Error ("Not enough memory for video mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allocate the new z-buffer
|
|
|
|
viddef.zbuffer = calloc (zbuffersize, 1);
|
|
|
|
if (!viddef.zbuffer) {
|
|
|
|
free (viddef.buffer);
|
|
|
|
viddef.conbuffer = viddef.buffer = NULL;
|
|
|
|
Sys_Error ("Not enough memory for video mode");
|
|
|
|
}
|
|
|
|
// Allocate the new surface cache; free the z-buffer if we fail
|
|
|
|
viddef.surfcache = calloc (cachesize, 1);
|
|
|
|
if (!viddef.surfcache) {
|
|
|
|
free (viddef.buffer);
|
|
|
|
free (viddef.zbuffer);
|
|
|
|
viddef.conbuffer = viddef.buffer = NULL;
|
|
|
|
viddef.zbuffer = NULL;
|
|
|
|
Sys_Error ("Not enough memory for video mode");
|
|
|
|
}
|
|
|
|
|
2019-07-08 04:40:29 +00:00
|
|
|
if (viddef.vid_internal->init_caches)
|
|
|
|
viddef.vid_internal->init_caches (viddef.surfcache, cachesize);
|
2012-02-23 01:06:30 +00:00
|
|
|
}
|
2019-07-08 03:46:22 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
VID_ClearMemory (void)
|
|
|
|
{
|
2019-07-08 04:40:29 +00:00
|
|
|
if (viddef.vid_internal->flush_caches)
|
|
|
|
viddef.vid_internal->flush_caches ();
|
2012-02-23 01:06:30 +00:00
|
|
|
}
|