spingle/source/sdl/main_sdl.c

161 lines
3.8 KiB
C
Raw Normal View History

2019-11-24 20:45:15 -08:00
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2005 John Fitzgibbons and others
Copyright (C) 2007-2008 Kristian Duske
Copyright (C) 2010-2014 QuakeSpasm developers
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.
*/
2019-12-02 07:07:37 -08:00
#include "q_defs.h"
2019-11-24 20:45:15 -08:00
#include <SDL.h>
#include <stdio.h>
/* need at least SDL_2.0.0 */
2019-11-25 17:40:18 -08:00
#define SDL_MIN_X 2
#define SDL_MIN_Y 0
#define SDL_MIN_Z 0
#define SDL_REQUIREDVERSION (SDL_VERSIONNUM(SDL_MIN_X,SDL_MIN_Y,SDL_MIN_Z))
#define SDL_NEW_VERSION_REJECT (SDL_VERSIONNUM(3,0,0))
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
static void Sys_AtExit(void)
2019-11-24 20:45:15 -08:00
{
SDL_Quit();
}
2019-11-25 17:40:18 -08:00
static void Sys_InitSDL(void)
2019-11-24 20:45:15 -08:00
{
SDL_version v;
SDL_version *sdl_version = &v;
SDL_GetVersion(&v);
2019-12-02 07:01:47 -08:00
Sys_Printf("SDL version %" PRIi32 ".%" PRIi32 ".%" PRIi32 "\n", sdl_version->major, sdl_version->minor, sdl_version->patch);
2019-11-25 17:40:18 -08:00
if(SDL_VERSIONNUM(sdl_version->major, sdl_version->minor, sdl_version->patch) < SDL_REQUIREDVERSION)
2019-12-02 07:01:47 -08:00
Sys_Error("Need at least v%" PRIi32 ".%" PRIi32 ".%" PRIi32 " of SDL to run. Exiting.", SDL_MIN_X, SDL_MIN_Y, SDL_MIN_Z);
2019-11-25 17:40:18 -08:00
if(SDL_VERSIONNUM(sdl_version->major, sdl_version->minor, sdl_version->patch) >= SDL_NEW_VERSION_REJECT)
2019-12-02 07:01:47 -08:00
Sys_Error("Detected SDL is too new. Exiting.");
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(SDL_Init(0) < 0)
2019-11-24 20:45:15 -08:00
Sys_Error("Couldn't init SDL: %s", SDL_GetError());
2019-12-02 07:01:47 -08:00
2019-11-24 20:45:15 -08:00
atexit(Sys_AtExit);
}
#define DEFAULT_MEMORY (256 * 1024 * 1024) // ericw -- was 72MB (64-bit) / 64MB (32-bit)
2019-11-25 17:40:18 -08:00
static quakeparms_t parms;
2019-11-24 20:45:15 -08:00
// On OS X we call SDL_main from the launcher, but SDL2 doesn't redefine main
// as SDL_main on OS X anymore, so we do it ourselves.
2019-12-02 04:24:20 -08:00
#if PLATFORM_IS(OSX)
2019-11-24 20:45:15 -08:00
#define main SDL_main
#endif
2019-11-25 16:49:58 -08:00
int32_t main(int32_t argc, char *argv[])
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
int32_t t;
double time, oldtime, newtime;
2019-11-24 20:45:15 -08:00
host_parms = &parms;
parms.basedir = ".";
parms.argc = argc;
parms.argv = argv;
parms.errstate = 0;
COM_InitArgv(parms.argc, parms.argv);
isDedicated = (COM_CheckParm("-dedicated") != 0);
2019-11-25 17:40:18 -08:00
Sys_InitSDL();
2019-11-24 20:45:15 -08:00
Sys_Init();
parms.memsize = DEFAULT_MEMORY;
2019-11-25 17:40:18 -08:00
if(COM_CheckParm("-heapsize"))
2019-11-24 20:45:15 -08:00
{
t = COM_CheckParm("-heapsize") + 1;
2019-11-25 17:40:18 -08:00
if(t < com_argc)
2019-11-24 20:45:15 -08:00
parms.memsize = Q_atoi(com_argv[t]) * 1024;
}
2019-11-25 17:40:18 -08:00
parms.membase = malloc(parms.memsize);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(!parms.membase)
2019-12-02 07:01:47 -08:00
Sys_Error("Not enough memory free for heap. You need 256MiB.\n");
2019-11-24 20:45:15 -08:00
2019-12-02 07:01:47 -08:00
Sys_Printf(ENGINE_NAME " " VERSION " (c) id Software, John Fitzgibbons, "
"SleepwalkR, Baker, Ozkan Sezer, Eric Wasylishen, "
"Alison Watson, et al.\n");
2019-11-24 20:45:15 -08:00
Host_Init();
oldtime = Sys_DoubleTime();
2019-11-25 17:40:18 -08:00
if(isDedicated)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
while(1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
newtime = Sys_DoubleTime();
2019-11-24 20:45:15 -08:00
time = newtime - oldtime;
2019-11-25 17:40:18 -08:00
while(time < sys_ticrate.value)
2019-11-24 20:45:15 -08:00
{
SDL_Delay(1);
2019-11-25 17:40:18 -08:00
newtime = Sys_DoubleTime();
2019-11-24 20:45:15 -08:00
time = newtime - oldtime;
}
2019-11-25 17:40:18 -08:00
Host_Frame(time);
2019-11-24 20:45:15 -08:00
oldtime = newtime;
}
}
else
2019-11-25 17:40:18 -08:00
while(1)
2019-11-24 20:45:15 -08:00
{
2019-11-25 17:40:18 -08:00
/* If we have no input focus at all, sleep a bit */
if(!VID_HasMouseOrInputFocus() || cl.paused)
{
SDL_Delay(16);
}
/* If we're minimised, sleep a bit more */
if(VID_IsMinimized())
{
scr_skipupdate = 1;
SDL_Delay(32);
}
else
{
scr_skipupdate = 0;
}
newtime = Sys_DoubleTime();
time = newtime - oldtime;
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
Host_Frame(time);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
if(time < sys_throttle.value && !cls.timedemo)
SDL_Delay(1);
2019-11-24 20:45:15 -08:00
2019-11-25 17:40:18 -08:00
oldtime = newtime;
}
2019-11-24 20:45:15 -08:00
return 0;
}