]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - common.c
renamed snd_linux.c to snd_oss.c
[xonotic/darkplaces.git] / common.c
index ed4e4a7212b34d46827ec90d8c94d395504f8b6e..a3baf1955ff57267f42001e484eb5fde28ea5841 100644 (file)
--- a/common.c
+++ b/common.c
@@ -585,12 +585,21 @@ void MSG_WriteString (sizebuf_t *sb, char *s)
 
 void MSG_WriteCoord (sizebuf_t *sb, float f)
 {
-       MSG_WriteShort (sb, (int)(f*8));
+       if (dpprotocol)
+               MSG_WriteFloat(sb, f);
+       else
+               MSG_WriteShort (sb, (int)(f*8.0f + 0.5f));
 }
 
+void MSG_WritePreciseAngle (sizebuf_t *sb, float f)
+{
+       MSG_WriteShort (sb, (int)(f*(65536.0f/360.0f) + 0.5f) & 65535);
+}
+
+// LordHavoc: round to nearest value, rather than rounding down, fixes crosshair problem
 void MSG_WriteAngle (sizebuf_t *sb, float f)
 {
-       MSG_WriteByte (sb, ((int)f*256/360) & 255);
+       MSG_WriteByte (sb, (int)(f*(256.0f/360.0f) + 0.5f) & 255);
 }
 
 //
@@ -722,6 +731,14 @@ char *MSG_ReadString (void)
        return string;
 }
 
+float MSG_ReadCoord (void)
+{
+       if (dpprotocol)
+               return MSG_ReadFloat();
+       else
+               return MSG_ReadShort() * (1.0f/8.0f);
+}
+
 /*
 float MSG_ReadCoord (void)
 {
@@ -732,8 +749,12 @@ float MSG_ReadAngle (void)
 {
        return MSG_ReadChar() * (360.0f/256.0f);
 }
-*/
 
+float MSG_ReadPreciseAngle (void)
+{
+       return MSG_ReadShort() * (360.0f/65536);
+}
+*/
 
 
 //===========================================================================
@@ -768,7 +789,7 @@ void *SZ_GetSpace (sizebuf_t *buf, int length)
        if (buf->cursize + length > buf->maxsize)
        {
                if (!buf->allowoverflow)
-                       Host_Error ("SZ_GetSpace: overflow without allowoverflow set - use -zone on the commandline for more zone memory, default: 512k (quake original default was 48k)");
+                       Host_Error ("SZ_GetSpace: overflow without allowoverflow set - use -zone on the commandline for more zone memory, default: 128k (quake original default was 48k)");
                
                if (length > buf->maxsize)
                        Host_Error ("SZ_GetSpace: %i is > full buffer size", length);
@@ -872,13 +893,15 @@ void COM_FileBase (char *in, char *out)
        while (s != in && *s != '.')
                s--;
        
-       for (s2 = s ; *s2 && *s2 != '/' ; s2--)
+       // LordHavoc: EWW bug bug bug bug bug... added previously missing s2 != in (yes, could go hunting through mem for /)
+       for (s2 = s ; s2 != in && *s2 && *s2 != '/' ; s2--)
        ;
        
        if (s-s2 < 2)
                strcpy (out,"?model?");
        else
        {
+               // LordHavoc: FIXME: examine this
                s--;
                strncpy (out,s2+1, s-s2);
                out[s-s2] = 0;
@@ -1144,6 +1167,38 @@ void COM_InitArgv (int argc, char **argv)
 }
 
 
+unsigned int qmalloctotal_alloc, qmalloctotal_alloccount, qmalloctotal_free, qmalloctotal_freecount;
+
+void *qmalloc(unsigned int size)
+{
+       unsigned int *mem;
+       qmalloctotal_alloc += size;
+       qmalloctotal_alloccount++;
+       mem = malloc(size+sizeof(unsigned int));
+       *mem = size;
+       return (void *)(mem + 1);
+}
+
+void qfree(void *mem)
+{
+       unsigned int *m;
+       m = mem;
+       m--; // back up to size
+       qmalloctotal_free += *m; // size
+       qmalloctotal_freecount++;
+       free(m);
+}
+
+void GL_TextureStats_PrintTotal(void);
+extern int hunk_low_used, hunk_high_used, hunk_size;
+void COM_Memstats_f(void)
+{
+       Con_Printf("%i malloc calls totalling %i bytes (%.4gMB)\n%i free calls totalling %i bytes (%.4gMB)\n%i bytes (%.4gMB) currently allocated\n", qmalloctotal_alloccount, qmalloctotal_alloc, qmalloctotal_alloc / 1048576.0, qmalloctotal_freecount, qmalloctotal_free, qmalloctotal_free / 1048576.0, qmalloctotal_alloc - qmalloctotal_free, (qmalloctotal_alloc - qmalloctotal_free) / 1048576.0);
+       GL_TextureStats_PrintTotal();
+       Con_Printf ("%i bytes (%.4gMB) of %.4gMB hunk in use\n", hunk_low_used + hunk_high_used, (hunk_low_used + hunk_high_used) / 1048576.0, hunk_size / 1048576.0);
+}
+
+
 /*
 ================
 COM_Init
@@ -1178,6 +1233,7 @@ void COM_Init (char *basedir)
        Cvar_RegisterVariable (&registered);
        Cvar_RegisterVariable (&cmdline);
        Cmd_AddCommand ("path", COM_Path_f);
+       Cmd_AddCommand ("memstats", COM_Memstats_f);
 
        COM_InitFilesystem ();
        COM_CheckRegistered ();
@@ -1262,7 +1318,7 @@ typedef struct
        int             dirlen;
 } dpackheader_t;
 
-// LordHavoc: was 2048
+// LordHavoc: was 2048, increased to 16384 and changed info[MAX_PACK_FILES] to a temporary malloc to avoid stack overflows
 #define MAX_FILES_IN_PACK       16384
 
 char    com_cachedir[MAX_OSPATH];
@@ -1578,25 +1634,24 @@ byte *COM_LoadFile (char *path, int usehunk, qboolean quiet)
 // extract the filename base name for hunk tag
        COM_FileBase (path, base);
        
-       if (usehunk == 1)
-               buf = Hunk_AllocName (len+1, base);
-       else if (usehunk == 2)
-               buf = Hunk_TempAlloc (len+1);
-       else if (usehunk == 0)
-               buf = Z_Malloc (len+1);
-       else if (usehunk == 3)
-               buf = Cache_Alloc (loadcache, len+1, base);
-       else if (usehunk == 4)
+       switch (usehunk)
        {
-               if (len+1 > loadsize)
-                       buf = Hunk_TempAlloc (len+1);
-               else
-                       buf = loadbuf;
+       case 1:
+               buf = Hunk_AllocName (len+1, va("%s (file)", path));
+               break;
+//     case 0:
+//             buf = Z_Malloc (len+1);
+//             break;
+       case 3:
+               buf = Cache_Alloc (loadcache, len+1, base);
+               break;
+       case 5:
+               buf = qmalloc (len+1);
+               break;
+//     default:
+//             Sys_Error ("COM_LoadFile: bad usehunk");
+//             break;
        }
-       else if (usehunk == 5)
-               buf = malloc (len+1);
-       else
-               Sys_Error ("COM_LoadFile: bad usehunk");
 
        if (!buf)
                Sys_Error ("COM_LoadFile: not enough space for %s", path);
@@ -1614,11 +1669,6 @@ byte *COM_LoadHunkFile (char *path, qboolean quiet)
        return COM_LoadFile (path, 1, quiet);
 }
 
-byte *COM_LoadTempFile (char *path, qboolean quiet)
-{
-       return COM_LoadFile (path, 2, quiet);
-}
-
 // LordHavoc: returns malloc'd memory
 byte *COM_LoadMallocFile (char *path, qboolean quiet)
 {
@@ -1631,18 +1681,6 @@ void COM_LoadCacheFile (char *path, struct cache_user_s *cu, qboolean quiet)
        COM_LoadFile (path, 3, quiet);
 }
 
-// uses temp hunk if larger than bufsize
-byte *COM_LoadStackFile (char *path, void *buffer, int bufsize, qboolean quiet)
-{
-       byte    *buf;
-       
-       loadbuf = (byte *)buffer;
-       loadsize = bufsize;
-       buf = COM_LoadFile (path, 4, quiet);
-       
-       return buf;
-}
-
 /*
 =================
 COM_LoadPackFile
@@ -1661,7 +1699,8 @@ pack_t *COM_LoadPackFile (char *packfile)
        int                             numpackfiles;
        pack_t                  *pack;
        int                             packhandle;
-       dpackfile_t             info[MAX_FILES_IN_PACK];
+       // LordHavoc: changed from stack array to temporary malloc, allowing huge pack directories
+       dpackfile_t             *info;
        unsigned short          crc;
 
        if (Sys_FileOpenRead (packfile, &packhandle) == -1)
@@ -1684,8 +1723,9 @@ pack_t *COM_LoadPackFile (char *packfile)
        if (numpackfiles != PAK0_COUNT)
                com_modified = true;    // not the original file
 
-       newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "packfile");
+       newfiles = Hunk_AllocName (numpackfiles * sizeof(packfile_t), "pack file-table");
 
+       info = qmalloc(sizeof(*info)*MAX_FILES_IN_PACK);
        Sys_FileSeek (packhandle, header.dirofs);
        Sys_FileRead (packhandle, (void *)info, header.dirlen);
 
@@ -1705,8 +1745,9 @@ pack_t *COM_LoadPackFile (char *packfile)
                newfiles[i].filepos = LittleLong(info[i].filepos);
                newfiles[i].filelen = LittleLong(info[i].filelen);
        }
+       qfree(info);
 
-       pack = Hunk_Alloc (sizeof (pack_t));
+       pack = Hunk_AllocName (sizeof (pack_t), packfile);
        strcpy (pack->filename, packfile);
        pack->handle = packhandle;
        pack->numfiles = numpackfiles;
@@ -1737,7 +1778,7 @@ void COM_AddGameDirectory (char *dir)
 //
 // add the directory to the search path
 //
-       search = Hunk_Alloc (sizeof(searchpath_t));
+       search = Hunk_AllocName (sizeof(searchpath_t), "pack info");
        strcpy (search->filename, dir);
        search->next = com_searchpaths;
        com_searchpaths = search;
@@ -1751,10 +1792,10 @@ void COM_AddGameDirectory (char *dir)
                pak = COM_LoadPackFile (pakfile);
                if (!pak)
                        break;
-               search = Hunk_Alloc (sizeof(searchpath_t));
+               search = Hunk_AllocName (sizeof(searchpath_t), "pack info");
                search->pack = pak;
                search->next = com_searchpaths;
-               com_searchpaths = search;               
+               com_searchpaths = search;
        }
 
 //
@@ -1839,7 +1880,7 @@ void COM_InitFilesystem (void)
 
 //
 // -path <dir or packfile> [<dir or packfile>] ...
-// Fully specifies the exact serach path, overriding the generated one
+// Fully specifies the exact search path, overriding the generated one
 //
        i = COM_CheckParm ("-path");
        if (i)
@@ -1851,7 +1892,7 @@ void COM_InitFilesystem (void)
                        if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
                                break;
                        
-                       search = Hunk_Alloc (sizeof(searchpath_t));
+                       search = Hunk_AllocName (sizeof(searchpath_t), "pack info");
                        if ( !strcmp(COM_FileExtension(com_argv[i]), "pak") )
                        {
                                search->pack = COM_LoadPackFile (com_argv[i]);
@@ -1897,4 +1938,3 @@ int COM_FileExists(char *filename)
 
        return false;
 }
-