2001-04-15 04:18:22 +00:00
|
|
|
/*
|
|
|
|
vid_sdl.c
|
|
|
|
|
|
|
|
Video driver for Sam Lantinga's Simple DirectMedia Layer
|
|
|
|
|
|
|
|
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
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-04-15 04:18:22 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
#include "QF/console.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/vid.h"
|
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
#include "context_sdl.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
#include "d_iface.h"
|
2012-02-17 07:13:56 +00:00
|
|
|
#include "vid_internal.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
|
2004-01-20 03:47:27 +00:00
|
|
|
#ifdef _WIN32 // FIXME: evil hack to get full DirectSound support with SDL
|
2001-04-15 04:18:22 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <SDL_syswm.h>
|
|
|
|
HWND mainwindow;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The original defaults
|
2002-08-21 00:05:27 +00:00
|
|
|
#define BASEWIDTH 320
|
|
|
|
#define BASEHEIGHT 200
|
2001-04-15 04:18:22 +00:00
|
|
|
|
|
|
|
byte *VGA_pagebase;
|
2001-11-02 06:35:32 +00:00
|
|
|
int VGA_width, VGA_height, VGA_rowbytes, VGA_bufferrowbytes = 0;
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2002-08-21 00:05:27 +00:00
|
|
|
SDL_Surface *screen = NULL;
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
// Define GLAPIENTRY to a useful value
|
|
|
|
#ifndef GLAPIENTRY
|
|
|
|
# ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
# define GLAPIENTRY WINAPI
|
|
|
|
# undef LoadImage
|
|
|
|
# else
|
|
|
|
# ifdef APIENTRY
|
|
|
|
# define GLAPIENTRY APIENTRY
|
|
|
|
# else
|
|
|
|
# define GLAPIENTRY
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void (*set_vid_mode) (Uint32 flags);
|
|
|
|
|
|
|
|
static void (GLAPIENTRY *qfglFinish) (void);
|
|
|
|
static int use_gl_procaddress = 0;
|
|
|
|
static cvar_t *gl_driver;
|
|
|
|
|
2013-01-13 05:52:19 +00:00
|
|
|
static byte cached_palette[256 * 3];
|
|
|
|
static int update_palette;
|
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
static void *
|
|
|
|
QFGL_ProcAddress (const char *name, qboolean crit)
|
|
|
|
{
|
|
|
|
void *glfunc = NULL;
|
|
|
|
|
|
|
|
Sys_MaskPrintf (SYS_VID, "DEBUG: Finding symbol %s ... ", name);
|
|
|
|
|
|
|
|
glfunc = SDL_GL_GetProcAddress (name);
|
|
|
|
if (glfunc) {
|
|
|
|
Sys_MaskPrintf (SYS_VID, "found [%p]\n", glfunc);
|
|
|
|
return glfunc;
|
|
|
|
}
|
|
|
|
Sys_MaskPrintf (SYS_VID, "not found\n");
|
|
|
|
|
|
|
|
if (crit) {
|
|
|
|
if (strncmp ("fxMesa", name, 6) == 0) {
|
|
|
|
Sys_Printf ("This target requires a special version of Mesa with "
|
|
|
|
"support for Glide and SVGAlib.\n");
|
|
|
|
Sys_Printf ("If you are in X, try using a GLX or SGL target.\n");
|
|
|
|
}
|
|
|
|
Sys_Error ("Couldn't load critical OpenGL function %s, exiting...",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sdlgl_set_vid_mode (Uint32 flags)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
flags |= SDL_OPENGL;
|
|
|
|
|
|
|
|
// Setup GL Attributes
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
// SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 0); // Try for 0, 8
|
|
|
|
// SDL_GL_SetAttribute (SDL_GL_STEREO, 1); // Someday...
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
int k;
|
|
|
|
int color[5] = {32, 24, 16, 15, 0};
|
|
|
|
int rgba[5][4] = {
|
|
|
|
{8, 8, 8, 0},
|
|
|
|
{8, 8, 8, 8},
|
|
|
|
{5, 6, 5, 0},
|
|
|
|
{5, 5, 5, 0},
|
|
|
|
{5, 5, 5, 1},
|
|
|
|
};
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_RED_SIZE, rgba[i][0]);
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgba[i][1]);
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, rgba[i][2]);
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, rgba[i][3]);
|
|
|
|
|
|
|
|
for (j = 0; j < 5; j++) {
|
|
|
|
for (k = 32; k >= 16; k -= 8) {
|
|
|
|
SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, k);
|
|
|
|
if ((screen = SDL_SetVideoMode (viddef.width, viddef.height,
|
|
|
|
color[j], flags)))
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sys_Error ("Couldn't set video mode: %s", SDL_GetError ());
|
|
|
|
SDL_Quit ();
|
|
|
|
|
|
|
|
success:
|
|
|
|
viddef.numpages = 2;
|
|
|
|
|
|
|
|
viddef.init_gl ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sdlgl_end_rendering (void)
|
|
|
|
{
|
|
|
|
qfglFinish ();
|
|
|
|
SDL_GL_SwapBuffers ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sdl_load_gl (void)
|
|
|
|
{
|
|
|
|
viddef.get_proc_address = QFGL_ProcAddress;
|
|
|
|
viddef.end_rendering = sdlgl_end_rendering;
|
|
|
|
set_vid_mode = sdlgl_set_vid_mode;
|
|
|
|
|
|
|
|
if (SDL_GL_LoadLibrary (gl_driver->string) != 0)
|
|
|
|
Sys_Error ("Couldn't load OpenGL library %s!", gl_driver->string);
|
|
|
|
|
|
|
|
use_gl_procaddress = 1;
|
|
|
|
|
|
|
|
qfglFinish = QFGL_ProcAddress ("glFinish", true);
|
|
|
|
}
|
2001-11-02 06:35:32 +00:00
|
|
|
|
2012-04-12 04:57:05 +00:00
|
|
|
static void
|
2013-01-13 05:52:19 +00:00
|
|
|
sdl_update_palette (const byte *palette)
|
2001-04-15 04:18:22 +00:00
|
|
|
{
|
|
|
|
SDL_Color colors[256];
|
2002-08-21 06:00:08 +00:00
|
|
|
int i;
|
2001-04-15 04:18:22 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
colors[i].r = *palette++;
|
|
|
|
colors[i].g = *palette++;
|
|
|
|
colors[i].b = *palette++;
|
|
|
|
}
|
|
|
|
SDL_SetColors (screen, colors, 0, 256);
|
|
|
|
}
|
|
|
|
|
2013-01-13 05:52:19 +00:00
|
|
|
static void
|
|
|
|
VID_SetPalette (const byte *palette)
|
|
|
|
{
|
|
|
|
if (memcmp (cached_palette, palette, sizeof (cached_palette))) {
|
|
|
|
memcpy (cached_palette, palette, sizeof (cached_palette));
|
|
|
|
update_palette = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-21 03:48:02 +00:00
|
|
|
static void
|
|
|
|
do_screen_buffer (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
static void
|
|
|
|
sdl_set_vid_mode (Uint32 flags)
|
|
|
|
{
|
|
|
|
// Initialize display
|
|
|
|
if (!(screen = SDL_SetVideoMode (viddef.width, viddef.height, 8, flags)))
|
|
|
|
Sys_Error ("VID: Couldn't set video mode: %s", SDL_GetError ());
|
|
|
|
|
|
|
|
// now know everything we need to know about the buffer
|
|
|
|
VGA_width = viddef.width;
|
|
|
|
VGA_height = viddef.height;
|
|
|
|
viddef.do_screen_buffer = do_screen_buffer;
|
|
|
|
VGA_pagebase = viddef.buffer = screen->pixels;
|
|
|
|
VGA_rowbytes = viddef.rowbytes = screen->pitch;
|
|
|
|
viddef.conbuffer = viddef.buffer;
|
|
|
|
viddef.conrowbytes = viddef.rowbytes;
|
|
|
|
viddef.direct = 0;
|
|
|
|
|
|
|
|
VID_InitBuffers (); // allocate z buffer and surface cache
|
|
|
|
}
|
|
|
|
|
2001-04-15 04:18:22 +00:00
|
|
|
void
|
2012-02-01 10:52:13 +00:00
|
|
|
VID_Init (byte *palette, byte *colormap)
|
2001-04-15 04:18:22 +00:00
|
|
|
{
|
|
|
|
Uint32 flags;
|
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
viddef.load_gl = sdl_load_gl;
|
2012-04-12 04:57:05 +00:00
|
|
|
viddef.set_palette = VID_SetPalette;
|
2012-04-12 07:47:37 +00:00
|
|
|
set_vid_mode = sdl_set_vid_mode;
|
2012-04-12 04:57:05 +00:00
|
|
|
|
2001-04-15 04:18:22 +00:00
|
|
|
// Load the SDL library
|
2001-08-25 05:54:21 +00:00
|
|
|
if (SDL_Init (SDL_INIT_VIDEO) < 0)
|
2001-04-15 04:18:22 +00:00
|
|
|
Sys_Error ("VID: Couldn't load SDL: %s", SDL_GetError ());
|
|
|
|
|
2012-04-10 04:27:53 +00:00
|
|
|
R_LoadModule ();
|
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
viddef.numpages = 1;
|
|
|
|
viddef.colormap8 = colormap;
|
|
|
|
viddef.fullbright = 256 - viddef.colormap8[256 * VID_GRADES];
|
|
|
|
|
2001-04-15 04:18:22 +00:00
|
|
|
// Set up display mode (width and height)
|
|
|
|
VID_GetWindowSize (BASEWIDTH, BASEHEIGHT);
|
|
|
|
|
|
|
|
// Set video width, height and flags
|
|
|
|
flags = (SDL_SWSURFACE | SDL_HWPALETTE);
|
2012-04-12 07:47:37 +00:00
|
|
|
if (vid_fullscreen->int_val) {
|
2001-04-15 04:18:22 +00:00
|
|
|
flags |= SDL_FULLSCREEN;
|
2012-04-12 07:47:37 +00:00
|
|
|
#ifndef _WIN32 // Don't annoy Mesa/3dfx folks
|
|
|
|
// doesn't hurt if not using a gl renderer
|
|
|
|
// FIXME: Maybe this could be put in a different spot, but I don't
|
|
|
|
// know where. Anyway, it's to work around a 3Dfx Glide bug.
|
|
|
|
// Cvar_SetValue (in_grab, 1); // Needs #include "QF/input.h"
|
|
|
|
putenv ((char *)"MESA_GLX_FX=fullscreen");
|
|
|
|
} else {
|
|
|
|
putenv ((char *)"MESA_GLX_FX=window");
|
|
|
|
#endif
|
|
|
|
}
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
set_vid_mode (flags);
|
2012-02-01 10:52:13 +00:00
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
VID_SDL_GammaCheck ();
|
2001-04-15 04:18:22 +00:00
|
|
|
VID_InitGamma (palette);
|
2012-04-12 04:57:05 +00:00
|
|
|
viddef.set_palette (viddef.palette);
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
viddef.initialized = true;
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2002-08-21 06:00:08 +00:00
|
|
|
SDL_ShowCursor (0); // hide the mouse pointer
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2004-01-20 03:47:27 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-21 06:00:08 +00:00
|
|
|
// FIXME: EVIL thing - but needed for win32 until
|
|
|
|
// SDL_sound works better - without this DirectSound fails.
|
2001-04-15 04:18:22 +00:00
|
|
|
|
2002-08-21 06:00:08 +00:00
|
|
|
// SDL_GetWMInfo(&info);
|
|
|
|
// mainwindow=info.window;
|
|
|
|
mainwindow=GetActiveWindow();
|
2001-04-15 04:18:22 +00:00
|
|
|
#endif
|
|
|
|
|
2012-04-12 07:47:37 +00:00
|
|
|
viddef.recalc_refdef = 1; // force a surface cache flush
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VID_Init_Cvars ()
|
|
|
|
{
|
|
|
|
SDL_Init_Cvars ();
|
|
|
|
gl_driver = Cvar_Get ("gl_driver", GL_DRIVER, CVAR_ROM, NULL,
|
|
|
|
"The OpenGL library to use. (path optional)");
|
2001-04-15 04:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VID_Update (vrect_t *rects)
|
|
|
|
{
|
2012-02-15 12:49:13 +00:00
|
|
|
static SDL_Rect *sdlrects;
|
|
|
|
static int num_sdlrects;
|
2002-08-21 06:00:08 +00:00
|
|
|
int i, n;
|
2001-04-15 04:18:22 +00:00
|
|
|
vrect_t *rect;
|
|
|
|
|
2013-01-13 05:52:19 +00:00
|
|
|
if (update_palette) {
|
|
|
|
update_palette = 0;
|
|
|
|
sdl_update_palette (cached_palette);
|
|
|
|
}
|
2001-04-15 04:18:22 +00:00
|
|
|
// Two-pass system, since Quake doesn't do it the SDL way...
|
|
|
|
|
|
|
|
// First, count the number of rectangles
|
|
|
|
n = 0;
|
2012-01-06 00:52:14 +00:00
|
|
|
for (rect = rects; rect; rect = rect->next)
|
2001-04-15 04:18:22 +00:00
|
|
|
++n;
|
|
|
|
|
2012-02-15 12:49:13 +00:00
|
|
|
if (n > num_sdlrects) {
|
|
|
|
num_sdlrects = n;
|
|
|
|
sdlrects = realloc (sdlrects, n * sizeof (SDL_Rect));
|
|
|
|
if (!sdlrects)
|
|
|
|
Sys_Error ("Out of memory!");
|
|
|
|
}
|
|
|
|
|
2001-04-15 04:18:22 +00:00
|
|
|
// Second, copy them to SDL rectangles and update
|
|
|
|
i = 0;
|
2012-01-06 00:52:14 +00:00
|
|
|
for (rect = rects; rect; rect = rect->next) {
|
2001-04-15 04:18:22 +00:00
|
|
|
sdlrects[i].x = rect->x;
|
|
|
|
sdlrects[i].y = rect->y;
|
|
|
|
sdlrects[i].w = rect->width;
|
|
|
|
sdlrects[i].h = rect->height;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
SDL_UpdateRects (screen, n, sdlrects);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-15 12:49:13 +00:00
|
|
|
D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
|
2001-04-15 04:18:22 +00:00
|
|
|
{
|
|
|
|
Uint8 *offset;
|
|
|
|
|
|
|
|
if (!screen)
|
|
|
|
return;
|
|
|
|
if (x < 0)
|
|
|
|
x = screen->w + x - 1;
|
|
|
|
offset = (Uint8 *) screen->pixels + y * screen->pitch + x;
|
|
|
|
while (height--) {
|
|
|
|
memcpy (offset, pbitmap, width);
|
|
|
|
offset += screen->pitch;
|
|
|
|
pbitmap += width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
D_EndDirectRect (int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
if (!screen)
|
|
|
|
return;
|
|
|
|
if (x < 0)
|
|
|
|
x = screen->w + x - 1;
|
|
|
|
SDL_UpdateRect (screen, x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VID_LockBuffer (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VID_UnlockBuffer (void)
|
|
|
|
{
|
|
|
|
}
|