From: knghtbrd Date: Thu, 1 Jan 2004 16:50:10 +0000 (+0000) Subject: INCOMPLETE SDL video support (don't use it!) No input, no multiple video X-Git-Tag: xonotic-v0.1.0preview~6167 X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=commitdiff_plain;h=101310bcdc27bfc91fb84cf3e399ad26b1382b5c INCOMPLETE SDL video support (don't use it!) No input, no multiple video modes, no proper shutdown. May not even compile anywher but my mac as of yet. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@3779 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/makefile b/makefile index 24a397bc..e77985f0 100644 --- a/makefile +++ b/makefile @@ -64,6 +64,7 @@ COMMONOBJECTS= $(CLIENTOBJECTS) $(SERVEROBJECTS) $(SHAREDOBJECTS) # built to give the executable a proper date string OBJ_GLX= builddate.c sys_linux.o vid_glx.o $(OBJ_LINUXCD) $(OBJ_LINUXSOUND) $(COMMONOBJECTS) OBJ_DED= builddate.c sys_linux.o vid_null.o cd_null.o snd_null.o $(COMMONOBJECTS) +OBJ_SDL= builddate.c sys_sdl.o vid_sdl.o cd_null.o snd_null.o $(COMMONOBJECTS) OBJ_WGL_EXE= builddate.c sys_win.o vid_wgl.o conproc.o cd_shared.o cd_win.o snd_win.o snd_dma.o snd_mix.o snd_mem.o ogg.o $(COMMONOBJECTS) OBJ_DED_EXE= builddate.c sys_linux.o vid_null.o cd_null.o snd_null.o $(COMMONOBJECTS) @@ -87,6 +88,7 @@ DO_CC=$(CC) $(CFLAGS) -c $< -o $@ # on the linker line, and that -lm must always be last LDFLAGS_GLX=-ldl -lm LDFLAGS_DED=-ldl -lm +LDFLAGS_SDL=-ldl -lm LDFLAGS_WGL_EXE=-mwindows -lwinmm -lwsock32 -luser32 -lgdi32 -ldxguid -ldinput -lcomctl32 LDFLAGS_DED_EXE=-mconsole -lwinmm -lwsock32 LDFLAGS_DEBUG=-g -ggdb @@ -95,6 +97,7 @@ LDFLAGS_RELEASE= EXE_GLX=darkplaces-glx EXE_DED=darkplaces-dedicated +EXE_SDL=darkplaces-sdl EXE_WGL_EXE=darkplaces.exe EXE_DED_EXE=darkplaces-dedicated.exe @@ -125,6 +128,9 @@ help: @echo "* $(MAKE) ded-debug : make dedicated server (debug version)" @echo "* $(MAKE) ded-profile : make dedicated server (profile version)" @echo "* $(MAKE) ded-release : make dedicated server (release version)" + @echo "* $(MAKE) sdl-debug : make SDL client (debug version)" + @echo "* $(MAKE) sdl-profile : make SDL client (profile version)" + @echo "* $(MAKE) sdl-release : make SDL client (release version)" @echo "* $(MAKE) exedebug : make WGL and dedicated binaries (debug versions)" @echo "* $(MAKE) exeprofile : make WGL and dedicated binaries (profile versions)" @echo "* $(MAKE) exerelease : make WGL and dedicated binaries (release versions)" @@ -172,6 +178,15 @@ ded-profile : ded-release : $(MAKE) bin-release EXE="$(EXE_DED)" LDFLAGS_COMMON="$(LDFLAGS_DED)" CFLAGS_COMMON="$(CFLAGS_NONEXECOMMON)" +sdl-debug : + $(MAKE) bin-debug EXE="$(EXE_SDL)" LDFLAGS_COMMON="$(LDFLAGS_SDL)" CFLAGS_COMMON="$(CFLAGS_NONEXECOMMON)" + +sdl-profile : + $(MAKE) bin-profile EXE="$(EXE_SDL)" LDFLAGS_COMMON="$(LDFLAGS_SDL)" CFLAGS_COMMON="$(CFLAGS_NONEXECOMMON)" + +sdl-release : + $(MAKE) bin-release EXE="$(EXE_SDL)" LDFLAGS_COMMON="$(LDFLAGS_SDL)" CFLAGS_COMMON="$(CFLAGS_NONEXECOMMON)" + wglexe-debug : $(MAKE) bin-debug EXE="$(EXE_WGL_EXE)" LDFLAGS_COMMON="$(LDFLAGS_WGL_EXE)" CFLAGS_COMMON="$(CFLAGS_EXECOMMON)" @@ -215,9 +230,18 @@ bin-release : vid_glx.o: vid_glx.c $(DO_CC) -I/usr/X11R6/include +vid_sdl.o: vid_sdl.c + $(DO_CC) `sdl-config --cflags` + +sys_sdl.o: sys_sdl.c + $(DO_CC) `sdl-config --cflags` + .c.o: $(DO_CC) +$(EXE_SDL): $(OBJ_SDL) + $(DO_LD) `sdl-config --libs` + $(EXE_GLX): $(OBJ_GLX) $(DO_LD) -L/usr/X11R6/lib -lX11 -lXext -lXxf86dga -lXxf86vm $(LINUXSOUNDLIB) diff --git a/sys_sdl.c b/sys_sdl.c new file mode 100644 index 00000000..bb236494 --- /dev/null +++ b/sys_sdl.c @@ -0,0 +1,237 @@ + +#ifdef WIN32 +#include "conio.h" +#else +#include +#include +#include +#endif + +#include + +#include "quakedef.h" + +#include + +#ifdef WIN32 +cvar_t sys_usetimegettime = {CVAR_SAVE, "sys_usetimegettime", "1"}; +#endif + + + +// ======================================================================= +// General routines +// ======================================================================= + +void Sys_Quit (void) +{ + Host_Shutdown(); +#ifndef WIN32 + fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY); +#endif + fflush(stdout); + exit(0); +} + +void Sys_Error (const char *error, ...) +{ + va_list argptr; + char string[1024]; + +// change stdin to non blocking +#ifndef WIN32 + fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY); +#endif + + va_start (argptr,error); + vsnprintf (string, sizeof (string), error, argptr); + va_end (argptr); + fprintf(stderr, "Error: %s\n", string); + + Host_Shutdown (); + exit (1); +} + +void Sys_Print(const char *text) +{ + printf("%s", text); +} + +double Sys_DoubleTime (void) +{ + static int first = true; + static double oldtime = 0.0, curtime = 0.0; + double newtime; +#ifdef WIN32 + // LordHavoc: note to people modifying this code, DWORD is specifically defined as an unsigned 32bit number, therefore the 65536.0 * 65536.0 is fine. + if (sys_usetimegettime.integer) + { + static int firsttimegettime = true; + // timeGetTime + // platform: + // Windows 95/98/ME/NT/2000/XP + // features: + // reasonable accuracy (millisecond) + // issues: + // wraps around every 47 days or so (but this is non-fatal to us, odd times are rejected, only causes a one frame stutter) + + // make sure the timer is high precision, otherwise different versions of windows have varying accuracy + if (firsttimegettime) + { + timeBeginPeriod (1); + firsttimegettime = false; + } + + newtime = (double) timeGetTime () / 1000.0; + } + else + { + // QueryPerformanceCounter + // platform: + // Windows 95/98/ME/NT/2000/XP + // features: + // very accurate (CPU cycles) + // known issues: + // does not necessarily match realtime too well (tends to get faster and faster in win98) + // wraps around occasionally on some platforms (depends on CPU speed and probably other unknown factors) + double timescale; + LARGE_INTEGER PerformanceFreq; + LARGE_INTEGER PerformanceCount; + + if (!QueryPerformanceFrequency (&PerformanceFreq)) + Sys_Error ("No hardware timer available"); + QueryPerformanceCounter (&PerformanceCount); + + #ifdef __BORLANDC__ + timescale = 1.0 / ((double) PerformanceFreq.u.LowPart + (double) PerformanceFreq.u.HighPart * 65536.0 * 65536.0); + newtime = ((double) PerformanceCount.u.LowPart + (double) PerformanceCount.u.HighPart * 65536.0 * 65536.0) * timescale; + #else + timescale = 1.0 / ((double) PerformanceFreq.LowPart + (double) PerformanceFreq.HighPart * 65536.0 * 65536.0); + newtime = ((double) PerformanceCount.LowPart + (double) PerformanceCount.HighPart * 65536.0 * 65536.0) * timescale; + #endif + } +#else + struct timeval tp; + gettimeofday(&tp, NULL); + newtime = (double) tp.tv_sec + tp.tv_usec / 1000000.0; +#endif + + if (first) + { + first = false; + oldtime = newtime; + } + + if (newtime < oldtime) + { + // warn if it's significant + if (newtime - oldtime < -0.01) + Con_Printf("Sys_DoubleTime: time stepped backwards (went from %f to %f, difference %f)\n", oldtime, newtime, newtime - oldtime); + } + else + curtime += newtime - oldtime; + oldtime = newtime; + + return curtime; +} + +char *Sys_ConsoleInput(void) +{ + if (cls.state == ca_dedicated) + { + static char text[256]; + int len = 0; +#ifdef WIN32 + int c; + + // read a line out + while (_kbhit ()) + { + c = _getch (); + putch (c); + if (c == '\r') + { + text[len] = 0; + putch ('\n'); + len = 0; + return text; + } + if (c == 8) + { + if (len) + { + putch (' '); + putch (c); + len--; + text[len] = 0; + } + continue; + } + text[len] = c; + len++; + text[len] = 0; + if (len == sizeof (text)) + len = 0; + } +#else + fd_set fdset; + struct timeval timeout; + FD_ZERO(&fdset); + FD_SET(0, &fdset); // stdin + timeout.tv_sec = 0; + timeout.tv_usec = 0; + if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset)) + { + len = read (0, text, sizeof(text)); + if (len >= 1) + { + // rip off the \n and terminate + text[len-1] = 0; + return text; + } + } +#endif + } + return NULL; +} + +void Sys_Sleep(void) +{ +#ifdef WIN32 + Sleep (1); +#else + usleep(1); +#endif +} + +int SDL_main (int argc, char *argv[]) +{ + double frameoldtime, framenewtime; + + signal(SIGFPE, SIG_IGN); + + com_argc = argc; + com_argv = argv; + +#ifndef WIN32 + fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); +#endif + + Sys_Shared_EarlyInit(); + + Host_Init(); + + Sys_Shared_LateInit(); + + frameoldtime = Sys_DoubleTime () - 0.1; + while (1) + { + // find time spent rendering last frame + framenewtime = Sys_DoubleTime (); + + Host_Frame (framenewtime - frameoldtime); + + frameoldtime = framenewtime; + } + return 0; +} diff --git a/vid_sdl.c b/vid_sdl.c new file mode 100644 index 00000000..0c83c410 --- /dev/null +++ b/vid_sdl.c @@ -0,0 +1,147 @@ +/* +Copyright (C) 2003 T. Joseph Carter + +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. +*/ + +#include +#include + +#include "quakedef.h" + +// Tell startup code that we have a client +int cl_available = true; + +static SDL_Surface *screen; + +void *GL_GetProcAddress(const char *name) +{ + void *p = NULL; + p = SDL_GL_GetProcAddress(name); + return p; +} + +void VID_Init (void) +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + printf ("Failed to init video: %s\n", SDL_GetError()); +} + +int VID_InitMode(int fullscreen, int width, int height, int bpp) +{ + int i; + int flags = SDL_OPENGL; + char *drivername; + +#ifdef WIN32 + drivername = "opengl32.dll"; +#elif defined(__APPLE__) && defined(__MACH__) + drivername = "OpenGL.framework"; +#else + drivername = "libGL.so.1"; +#endif + + i = COM_CheckParm("-gl_driver"); + if (i && i < com_argc - 1) + drivername = com_argv[i + 1]; + if (!SDL_GL_LoadLibrary(drivername)) + { + Con_Printf("Unable to load GL driver \"%s\"\n: ", drivername, SDL_GetError()); + return false; + } + + qglGetString = GL_GetProcAddress("glGetString"); + + // Knghtbrd: should do platform-specific extension string function here + + if (qglGetString == NULL) + { + VID_Shutdown(); + Con_Printf("Required OpenGL function glGetString not found\n"); + return false; + } + +// if (fullscreen) +// flags |= SDL_FULLSCREEN; + + SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 1); + SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 1); + SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 1); + SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); + + screen = SDL_SetVideoMode(width, height, bpp, flags); + if (screen == NULL) + { + Con_Printf("Failed to set video mode to %ix%i: %s\n", width, height, SDL_GetError); + VID_Shutdown(); + return false; + } + + gl_renderer = qglGetString(GL_RENDERER); + gl_vendor = qglGetString(GL_VENDOR); + gl_version = qglGetString(GL_VERSION); + gl_extensions = qglGetString(GL_EXTENSIONS); + gl_platform = "SDL"; + // Knghtbrd: should assign platform-specific extensions here + gl_platformextensions = ""; + + GL_Init(); + + vid_hidden = false; + return true; +} + +void VID_Shutdown (void) +{ +// SDL_Quit(); +} + +int VID_SetGamma (unsigned short *ramps) +{ + return false; +} + +int VID_GetGamma (unsigned short *ramps) +{ + return false; +} + +void VID_GetWindowSize (int *x, int *y, int *width, int *height) +{ + *x = *y = 0; + *width = screen->w; + *height = screen->h; +} + +void VID_Finish (void) +{ + qglFinish(); + SDL_GL_SwapBuffers(); +} + +void IN_Commands (void) +{ +} + +void IN_Move (usercmd_t *cmd) +{ +} + +void Sys_SendKeyEvents (void) +{ +} +