]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sys_shared.c
added new method of culling irrelevant entity collisions - a grid of areas
[xonotic/darkplaces.git] / sys_shared.c
index 52a28285a16dc86924b02d8a5468bf9c4e9f5534..5b81ce0ea877c19e7fbb8eed83bbe09c50d2bdf7 100644 (file)
@@ -1,6 +1,14 @@
 
 #include "quakedef.h"
 #include <time.h>
+#ifdef WIN32
+#include <direct.h>
+#else
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#endif
+#include <errno.h>
 
 extern cvar_t  timestamps;
 extern cvar_t  timeformat;
@@ -39,7 +47,7 @@ static char qfont_table[256] = {
        'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
        'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
        '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
-       'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o', 
+       'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
        'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
        'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<'
 };
@@ -49,7 +57,7 @@ extern HANDLE hinput, houtput;
 #endif
 
 #define MAX_PRINT_MSG  16384
-void Sys_Printf (char *fmt, ...)
+void Sys_Printf (const char *fmt, ...)
 {
        va_list         argptr;
        char            start[MAX_PRINT_MSG];   // String we started with
@@ -75,45 +83,180 @@ void Sys_Printf (char *fmt, ...)
        if (sys_nostdout)
                return;
 
-       if (timestamps.value)
+       if (timestamps.integer)
        {
                mytime = time (NULL);
                local = localtime (&mytime);
                strftime (stamp, sizeof (stamp), timeformat.string, local);
-               
+
                snprintf (final, sizeof (final), "%s%s", stamp, start);
        }
        else
                snprintf (final, sizeof (final), "%s", start);
 
-       for (p = (unsigned char *) final; *p; p++)
+       // LordHavoc: make sure the string is terminated
+       final[MAX_PRINT_MSG - 1] = 0;
+       for (p = (unsigned char *) final;*p; p++)
                *p = qfont_table[*p];
 #ifdef WIN32
        if (cls.state == ca_dedicated)
-               WriteFile(houtput, final, strlen (final), &dummy, NULL);        
+               WriteFile(houtput, final, strlen (final), &dummy, NULL);
 #else
-       puts(final);
-#endif
-//     for (p = (unsigned char *) final; *p; p++)
-//             putc (qfont_table[*p], stdout);
-#ifndef WIN32
-       fflush (stdout);
+       printf("%s", final);
 #endif
 }
 
-void Sys_Shared_Init(void)
+// LordHavoc: 256 pak files (was 10)
+#define MAX_HANDLES 256
+QFile *sys_handles[MAX_HANDLES];
+
+int findhandle (void)
 {
-       if (COM_CheckParm("-nostdout"))
-               sys_nostdout = 1;
+       int i;
+
+       for (i = 1;i < MAX_HANDLES;i++)
+               if (!sys_handles[i])
+                       return i;
+       Sys_Error ("out of handles");
+       return -1;
+}
+
+/*
+================
+Sys_FileLength
+================
+*/
+int Sys_FileLength (QFile *f)
+{
+       int pos, end;
+
+       pos = Qtell (f);
+       Qseek (f, 0, SEEK_END);
+       end = Qtell (f);
+       Qseek (f, pos, SEEK_SET);
+
+       return end;
+}
+
+int Sys_FileOpenRead (const char *path, int *handle)
+{
+       QFile *f;
+       int i, retval;
+
+       i = findhandle ();
+
+       f = Qopen(path, "rbz");
+
+       if (!f)
+       {
+               *handle = -1;
+               retval = -1;
+       }
        else
        {
+               sys_handles[i] = f;
+               *handle = i;
+               retval = Sys_FileLength(f);
+       }
+
+       return retval;
+}
+
+int Sys_FileOpenWrite (const char *path)
+{
+       QFile   *f;
+       int             i;
+
+       i = findhandle ();
+
+       f = Qopen(path, "wb");
+       if (!f)
+       {
+               Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno));
+               return 0;
+       }
+       sys_handles[i] = f;
+
+       return i;
+}
+
+void Sys_FileClose (int handle)
+{
+       Qclose (sys_handles[handle]);
+       sys_handles[handle] = NULL;
+}
+
+void Sys_FileSeek (int handle, int position)
+{
+       Qseek (sys_handles[handle], position, SEEK_SET);
+}
+
+int Sys_FileRead (int handle, void *dest, int count)
+{
+       return Qread (sys_handles[handle], dest, count);
+}
+
+int Sys_FileWrite (int handle, void *data, int count)
+{
+       return Qwrite (sys_handles[handle], data, count);
+}
+
+int Sys_FileTime (const char *path)
+{
+#if WIN32
+       QFile *f;
+
+       f = Qopen(path, "rb");
+       if (f)
+       {
+               Qclose(f);
+               return 1;
+       }
+
+       return -1;
+#else
+       struct stat buf;
+
+       if (stat (path,&buf) == -1)
+               return -1;
+
+       return buf.st_mtime;
+#endif
+}
+
+void Sys_mkdir (const char *path)
+{
+#if WIN32
+       _mkdir (path);
+#else
+       mkdir (path, 0777);
+#endif
+}
+
+char engineversion[128];
+
+void Sys_Shared_EarlyInit(void)
+{
+       Memory_Init ();
+
+       COM_InitArgv();
+       COM_InitGameType();
+
 #if defined(__linux__)
-               fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
-               printf ("DarkPlaces Linux   GL %.2f build %3i", (float) VERSION, buildnumber);
+       sprintf (engineversion, "%s Linux %s", gamename, buildstring);
 #elif defined(WIN32)
-               printf ("DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
+       sprintf (engineversion, "%s Windows %s", gamename, buildstring);
 #else
-               printf ("DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
+       sprintf (engineversion, "%s Unknown %s", gamename, buildstring);
 #endif
-       }
+
+       if (COM_CheckParm("-nostdout"))
+               sys_nostdout = 1;
+       else
+               printf("%s\n", engineversion);
 }
+
+void Sys_Shared_LateInit(void)
+{
+}
+