1 /*
2 	sys_sdl.c
3 
4 	(description)
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the GNU General Public License for more details.
18 
19 	You should have received a copy of the GNU General Public License
20 	along with this program; if not, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #ifdef HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #ifdef HAVE_STRINGS_H
35 # include <strings.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 
41 #ifdef HAVE_FCNTL_H
42 # include <fcntl.h>
43 #else
44 # include <sys/fcntl.h>
45 #endif
46 
47 #include <SDL.h>
48 #include <SDL_main.h>
49 
50 #include "QF/cvar.h"
51 #include "QF/qargs.h"
52 #include "QF/sys.h"
53 
54 #include "client.h"
55 #include "host.h"
56 
57 #ifdef _WIN32
58 # include "winquake.h"
59 #endif
60 
61 int qf_sdl_link;
62 qboolean    isDedicated = false;
63 
64 static void
startup(void)65 startup (void)
66 {
67 #ifdef _WIN32
68 	OSVERSIONINFO vinfo;
69 	Sys_MaskFPUExceptions ();
70 
71 	// make sure the timer is high precision, otherwise NT gets 18ms resolution
72 	timeBeginPeriod (1);
73 
74 	vinfo.dwOSVersionInfoSize = sizeof (vinfo);
75 
76 	if (!GetVersionEx (&vinfo))
77 		Sys_Error ("Couldn't get OS info");
78 
79 	if ((vinfo.dwMajorVersion < 4)
80 		|| (vinfo.dwPlatformId == VER_PLATFORM_WIN32s)) {
81 		Sys_Error ("This version of " PACKAGE_NAME
82 				   " requires at least Win95 or NT 4.0");
83 	}
84 #endif
85 }
86 
87 static void
shutdown_f(void)88 shutdown_f (void)
89 {
90 #ifndef _WIN32
91 	// change stdin to blocking
92 	fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
93 	fflush (stdout);
94 #endif
95 }
96 
97 #ifndef SDL_main
98 # define SDL_main main
99 #endif
100 
101 int
SDL_main(int argc,char * argv[])102 SDL_main (int argc, char *argv[])
103 {
104 	double      time, oldtime, newtime;
105 
106 	startup ();
107 
108 	memset (&host_parms, 0, sizeof (host_parms));
109 
110 	COM_InitArgv (argc, (const char **) argv);
111 	host_parms.argc = com_argc;
112 	host_parms.argv = com_argv;
113 
114 	isDedicated = (COM_CheckParm ("-dedicated") != 0);
115 
116 	Sys_RegisterShutdown (Host_Shutdown);
117 	Sys_RegisterShutdown (shutdown_f);
118 
119 	Host_Init ();
120 
121 #ifndef _WIN32
122 	if (!sys_nostdout->int_val) {
123 		fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK);
124 		Sys_Printf ("Quake -- Version %s\n", NQ_VERSION);
125 	}
126 #endif
127 
128 	oldtime = Sys_DoubleTime () - 0.1;
129 	while (1) {							// Main message loop
130 		// find time spent rendering last frame
131 		newtime = Sys_DoubleTime ();
132 		time = newtime - oldtime;
133 
134 		if (cls.state == ca_dedicated) {	// play vcrfiles at max speed
135 			if (time < sys_ticrate->value && (!vcrFile || recording)) {
136 				usleep (1);
137 				continue;			// not time to run a server-only tic yet
138 			}
139 			time = sys_ticrate->value;
140 		}
141 
142 		if (time > sys_ticrate->value * 2)
143 			oldtime = newtime;
144 		else
145 			oldtime += time;
146 
147 		Host_Frame (time);
148 	}
149 }
150