Subversion Repositories Kolibri OS

Rev

Rev 8663 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5131 clevermous 1
#include 
2
#include 
3
#include 
4
#include 
5
#include 
6
#include 
7
#include 
8
#include 
9
#include 
10
#include 
11
#include 
12
#include 
13
#include 
14
#if !defined(__WIN32__) && !defined(_KOLIBRI)
15
#include 
16
#include 
17
#include 
18
#include 
19
#include 
20
#endif
21
 
22
#include "quakedef.h"
23
 
24
qboolean			isDedicated;
25
 
26
int noconinput = 0;
27
 
28
char *basedir = ".";
29
 
30
cvar_t  sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
31
cvar_t  sys_nostdout = {"sys_nostdout","0"};
32
 
33
// =======================================================================
34
// General routines
35
// =======================================================================
36
 
37
void Sys_DebugNumber(int y, int val)
38
{
39
}
40
 
41
void Sys_Printf (char *fmt, ...)
42
{
43
	va_list		argptr;
44
	char		text[1024];
45
 
46
	va_start (argptr,fmt);
47
	vsprintf (text,fmt,argptr);
48
	va_end (argptr);
49
#ifdef _KOLIBRI
50
	__menuet__debug_out(text);
51
#else
52
	fprintf(stderr, "%s", text);
53
#endif
54
 
55
	//Con_Print (text);
56
}
57
 
58
void Sys_Quit (void)
59
{
60
	Host_Shutdown();
61
	exit(0);
62
}
63
 
64
void Sys_Init(void)
65
{
66
#if id386
67
	Sys_SetFPCW();
68
#endif
69
}
70
 
71
#if !id386
72
 
73
/*
74
================
75
Sys_LowFPPrecision
76
================
77
*/
78
void Sys_LowFPPrecision (void)
79
{
80
// causes weird problems on Nextstep
81
}
82
 
83
 
84
/*
85
================
86
Sys_HighFPPrecision
87
================
88
*/
89
void Sys_HighFPPrecision (void)
90
{
91
// causes weird problems on Nextstep
92
}
93
 
94
#endif	// !id386
95
 
96
 
97
void Sys_Error (char *error, ...)
98
{
99
    va_list     argptr;
100
    char        string[1024];
101
 
102
    va_start (argptr,error);
103
    vsprintf (string,error,argptr);
104
    va_end (argptr);
105
#ifdef _KOLIBRI
106
	__menuet__debug_out("Error: ");
107
	__menuet__debug_out(string);
108
	__menuet__debug_out("\n");
109
#else
110
	fprintf(stderr, "Error: %s\n", string);
111
#endif
112
 
113
	Host_Shutdown ();
114
	exit (1);
115
 
116
}
117
 
118
void Sys_Warn (char *warning, ...)
119
{
120
    va_list     argptr;
121
    char        string[1024];
122
 
123
    va_start (argptr,warning);
124
    vsprintf (string,warning,argptr);
125
    va_end (argptr);
126
#ifdef _KOLIBRI
127
	__menuet__debug_out("Warning: ");
128
	__menuet__debug_out(string);
129
#else
130
	fprintf(stderr, "Warning: %s", string);
131
#endif
132
}
133
 
134
/*
135
===============================================================================
136
 
137
FILE IO
138
 
139
===============================================================================
140
*/
141
 
142
#define	MAX_HANDLES		10
143
FILE	*sys_handles[MAX_HANDLES];
144
 
145
int		findhandle (void)
146
{
147
	int		i;
148
 
149
	for (i=1 ; i
150
		if (!sys_handles[i])
151
			return i;
152
	Sys_Error ("out of handles");
153
	return -1;
154
}
155
 
156
/*
157
================
158
Qfilelength
159
================
160
*/
161
static int Qfilelength (FILE *f)
162
{
163
	int		pos;
164
	int		end;
165
 
166
	pos = ftell (f);
167
	fseek (f, 0, SEEK_END);
168
	end = ftell (f);
169
	fseek (f, pos, SEEK_SET);
170
 
171
	return end;
172
}
173
 
174
int Sys_FileOpenRead (char *path, int *hndl)
175
{
176
	FILE	*f;
177
	int		i;
178
 
179
	i = findhandle ();
180
 
181
	f = fopen(path, "rb");
182
	if (!f)
183
	{
184
		*hndl = -1;
185
		return -1;
186
	}
187
	sys_handles[i] = f;
188
	*hndl = i;
189
 
190
	return Qfilelength(f);
191
}
192
 
193
int Sys_FileOpenWrite (char *path)
194
{
195
	FILE	*f;
196
	int		i;
197
 
198
	i = findhandle ();
199
 
200
	f = fopen(path, "wb");
201
	if (!f)
202
		Sys_Error ("Error opening %s: %s", path,strerror(errno));
203
	sys_handles[i] = f;
204
 
205
	return i;
206
}
207
 
208
void Sys_FileClose (int handle)
209
{
210
	if ( handle >= 0 ) {
211
		fclose (sys_handles[handle]);
212
		sys_handles[handle] = NULL;
213
	}
214
}
215
 
216
void Sys_FileSeek (int handle, int position)
217
{
218
	if ( handle >= 0 ) {
219
		fseek (sys_handles[handle], position, SEEK_SET);
220
	}
221
}
222
 
223
int Sys_FileRead (int handle, void *dst, int count)
224
{
225
	char *data;
226
	int size, done;
227
 
228
	size = 0;
229
	if ( handle >= 0 ) {
230
		data = dst;
231
		while ( count > 0 ) {
232
			done = fread (data, 1, count, sys_handles[handle]);
233
			if ( done == 0 ) {
234
				break;
235
			}
236
			data += done;
237
			count -= done;
238
			size += done;
239
		}
240
	}
241
	return size;
242
 
243
}
244
 
245
int Sys_FileWrite (int handle, void *src, int count)
246
{
247
	char *data;
248
	int size, done;
249
 
250
	size = 0;
251
	if ( handle >= 0 ) {
252
		data = src;
253
		while ( count > 0 ) {
254
			done = fread (data, 1, count, sys_handles[handle]);
255
			if ( done == 0 ) {
256
				break;
257
			}
258
			data += done;
259
			count -= done;
260
			size += done;
261
		}
262
	}
263
	return size;
264
}
265
 
266
int	Sys_FileTime (char *path)
267
{
268
	FILE	*f;
269
 
270
	f = fopen(path, "rb");
271
	if (f)
272
	{
273
		fclose(f);
274
		return 1;
275
	}
276
 
277
	return -1;
278
}
279
 
280
void Sys_mkdir (char *path)
281
{
282
#ifdef __WIN32__
283
    mkdir (path);
284
#else
285
    mkdir (path, 0777);
286
#endif
287
}
288
 
289
void Sys_DebugLog(char *file, char *fmt, ...)
290
{
291
    va_list argptr;
292
    static char data[1024];
293
    FILE *fp;
294
 
295
    va_start(argptr, fmt);
296
    vsprintf(data, fmt, argptr);
297
    va_end(argptr);
298
    fp = fopen(file, "a");
299
    fwrite(data, strlen(data), 1, fp);
300
    fclose(fp);
301
}
302
 
303
double Sys_FloatTime (void)
304
{
305
#if defined(_KOLIBRI)
306
	static int starttime = 0;
307
 
308
	if ( ! starttime )
309
		__asm__ __volatile__("int $0x40" : "=a"(starttime) : "a"(26), "b"(9));
310
 
311
        int curtime;
312
        __asm__ __volatile__("int $0x40" : "=a"(curtime) : "a"(26), "b"(9));
313
	return (curtime-starttime)*0.01;
314
#elif defined(__WIN32__)
315
 
316
	static int starttime = 0;
317
 
318
	if ( ! starttime )
319
		starttime = clock();
320
 
321
	return (clock()-starttime)*1.0/1024;
322
 
323
#else
324
 
325
    struct timeval tp;
326
    struct timezone tzp;
327
    static int      secbase;
328
 
329
    gettimeofday(&tp, &tzp);
330
 
331
    if (!secbase)
332
    {
333
        secbase = tp.tv_sec;
334
        return tp.tv_usec/1000000.0;
335
    }
336
 
337
    return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
338
 
339
#endif
340
}
341
 
342
// =======================================================================
343
// Sleeps for microseconds
344
// =======================================================================
345
 
346
static volatile int oktogo;
347
 
348
void alarm_handler(int x)
349
{
350
	oktogo=1;
351
}
352
 
353
byte *Sys_ZoneBase (int *size)
354
{
355
 
356
	char *QUAKEOPT = getenv("QUAKEOPT");
357
 
358
	*size = 0xc00000;
359
	if (QUAKEOPT)
360
	{
361
		while (*QUAKEOPT)
362
			if (tolower(*QUAKEOPT++) == 'm')
363
			{
364
				*size = atof(QUAKEOPT) * 1024*1024;
365
				break;
366
			}
367
	}
368
	return malloc (*size);
369
 
370
}
371
 
372
void Sys_LineRefresh(void)
373
{
374
}
375
 
376
void Sys_Sleep(void)
377
{
378
#ifdef _KOLIBRI
379
	__menuet__delay100(1);
380
#else
381
	SDL_Delay(1);
382
#endif
383
}
384
 
385
#ifndef _KOLIBRI
386
void floating_point_exception_handler(int whatever)
387
{
388
//	Sys_Warn("floating point exception\n");
389
	signal(SIGFPE, floating_point_exception_handler);
390
}
391
#endif
392
 
393
void moncontrol(int x)
394
{
395
}
396
 
397
int main (int c, char **v)
398
{
399
 
400
	double		time, oldtime, newtime;
401
	quakeparms_t parms;
402
	extern int vcrFile;
403
	extern qboolean recording;
404
	static int frame;
405
 
406
	moncontrol(0);
407
 
408
#ifndef _KOLIBRI
409
//	signal(SIGFPE, floating_point_exception_handler);
410
	signal(SIGFPE, SIG_IGN);
411
#endif
412
 
413
	parms.memsize = 8*1024*1024;
414
	parms.membase = malloc (parms.memsize);
415
	parms.basedir = basedir;
416
	parms.cachedir = NULL;
417
 
418
	COM_InitArgv(c, v);
419
	parms.argc = com_argc;
420
	parms.argv = com_argv;
421
 
422
	Sys_Init();
423
 
424
    Host_Init(&parms);
425
 
426
	Cvar_RegisterVariable (&sys_nostdout);
427
 
428
    oldtime = Sys_FloatTime () - 0.1;
429
    while (1)
430
    {
431
// find time spent rendering last frame
432
        newtime = Sys_FloatTime ();
433
        time = newtime - oldtime;
434
 
435
        if (cls.state == ca_dedicated)
436
        {   // play vcrfiles at max speed
437
            if (time < sys_ticrate.value && (vcrFile == -1 || recording) )
438
            {
439
#ifdef _KOLIBRI
440
                __menuet__delay100(1);
441
#else
442
                SDL_Delay (1);
443
#endif
444
                continue;       // not time to run a server only tic yet
445
            }
446
            time = sys_ticrate.value;
447
        }
448
 
449
        if (time > sys_ticrate.value*2)
450
            oldtime = newtime;
451
        else
452
            oldtime += time;
453
 
454
        if (++frame > 10)
455
            moncontrol(1);      // profile only while we do each Quake frame
456
        Host_Frame (time);
457
        moncontrol(0);
458
 
459
// graphic debugging aids
460
        if (sys_linerefresh.value)
461
            Sys_LineRefresh ();
462
    }
463
 
464
}
465
 
466
 
467
/*
468
================
469
Sys_MakeCodeWriteable
470
================
471
*/
472
void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
473
{
474
#ifndef _KOLIBRI
475
	int r;
476
	unsigned long addr;
477
	int psize = getpagesize();
478
 
479
	fprintf(stderr, "writable code %lx-%lx\n", startaddr, startaddr+length);
480
 
481
	addr = startaddr & ~(psize-1);
482
 
483
	r = mprotect((char*)addr, length + startaddr - addr, 7);
484
 
485
	if (r < 0)
486
    		Sys_Error("Protection change failed\n");
487
#endif
488
}
489