int main (int argc, char **argv) { double start, stop; dstring_t *portalfile = dstring_new (); QFile *f; start = Sys_DoubleTime (); this_program = argv[0]; DecodeArgs (argc, argv); if (!options.bspfile) { usage (1); Sys_Error ("%s: no bsp file specified.", this_program); } QFS_SetExtension (options.bspfile, ".bsp"); f = Qopen (options.bspfile->str, "rb"); if (!f) Sys_Error ("couldn't open %s for reading.", options.bspfile->str); bsp = LoadBSPFile (f, Qfilesize (f)); Qclose (f); visdata = dstring_new (); dstring_copystr (portalfile, options.bspfile->str); QFS_SetExtension (portalfile, ".prt"); LoadPortals (portalfile->str); uncompressed = calloc (bitbytes_l, portalclusters); CalcVis (); if (options.verbosity >= 0) printf ("c_chains: %i%s\n", c_chains, options.threads > 1 ? " (not reliable)" :""); BSP_AddVisibility (bsp, (byte *) visdata->str, visdata->size); if (options.verbosity >= 0) printf ("visdatasize:%ld compressed from %ld\n", (long) bsp->visdatasize, (long) originalvismapsize); CalcAmbientSounds (); f = Qopen (options.bspfile->str, "wb"); if (!f) Sys_Error ("couldn't open %s for writing.", options.bspfile->str); WriteBSPFile (bsp, f); Qclose (f); stop = Sys_DoubleTime (); if (options.verbosity >= 0) printf ("%5.1f seconds elapsed\n", stop - start); return 0; }
void process_wad_script (const char *name) { char *buf; QFile *file; int bytes; script_t *script; file = Qopen (name, "rt"); if (!file) Sys_Error ("couldn't open %s. %s", name, strerror(errno)); bytes = Qfilesize (file); buf = malloc (bytes + 1); bytes = Qread (file, buf, bytes); buf[bytes] = 0; Qclose (file); dstring_copystr (&destfile, name); dstring_appendstr (&destfile, ".wad"); script = Script_New (); Script_Start (script, name, buf); parse_script (script); if (wadfile) wad_close (wadfile); }
int Sys_FileOpenRead (char *path, int *hndl) { QFile *f; int i; i = findhandle (); f = Qopen(path, "rb"); if (!f) { *hndl = -1; return -1; } sys_handles[i].hFile = f; sys_handles[i].nLen = filelength(f); sys_handles[i].nPos = 0; sys_handles[i].pMap = mmap( 0, sys_handles[i].nLen, PROT_READ, MAP_SHARED, fileno( sys_handles[i].hFile ), 0 ); if (!sys_handles[i].pMap || (sys_handles[i].pMap == (char *)-1)) { printf( "mmap %s failed!", path ); sys_handles[i].pMap = NULL; } *hndl = i; return( sys_handles[i].nLen ); }
qboolean SNDDMA_Init (void) { shm = &sn; memset ((dma_t *) shm, 0, sizeof (*shm)); shm->splitbuffer = 0; shm->channels = 2; shm->submission_chunk = 1; // don't mix less than this # shm->samplepos = 0; // in mono samples shm->samplebits = 16; shm->samples = 16384; // mono samples in buffer shm->speed = 44100; shm->buffer = malloc (shm->samples * shm->channels * shm->samplebits / 8); Con_Printf ("%5d stereo\n", shm->channels - 1); Con_Printf ("%5d samples\n", shm->samples); Con_Printf ("%5d samplepos\n", shm->samplepos); Con_Printf ("%5d samplebits\n", shm->samplebits); Con_Printf ("%5d submission_chunk\n", shm->submission_chunk); Con_Printf ("%5d speed\n", shm->speed); Con_Printf ("0x%x dma buffer\n", (int) shm->buffer); Con_Printf ("%5d total_channels\n", total_channels); if (!(snd_file = Qopen ("qf.raw", "wb"))) return 0; snd_inited = 1; return 1; }
int wad_extract (wad_t *wad, lumpinfo_t *pf) { const char *name = pf->name; size_t count; int len; QFile *file; char buffer[16384]; if (make_parents (name) == -1) return -1; if (!(file = Qopen (name, "wb"))) return -1; Qseek (wad->handle, pf->filepos, SEEK_SET); len = pf->size; while (len) { count = len; if (count > sizeof (buffer)) count = sizeof (buffer); count = Qread (wad->handle, buffer, count); Qwrite (file, buffer, count); len -= count; } Qclose (file); return 0; }
static void write_pcx (image_t *image) { pcx_t *pcx; int pcx_len, i; byte palette[768]; QFile *outfile; outfile = Qopen (options.outf_name, "wb"); if (outfile == NULL) { fprintf (stderr, "Error opening output file %s.\n", options.outf_name); exit (1); } // quick and dirty greyscale palette for (i = 0; i < 256; i++) { palette[i * 3 + 0] = i; palette[i * 3 + 1] = i; palette[i * 3 + 2] = i; } Sys_Init (); Memory_Init (malloc (MEMSIZE), MEMSIZE); pcx = EncodePCX (image->image, image->width, image->height, image->width, palette, false, &pcx_len); if (Qwrite (outfile, pcx, pcx_len) != pcx_len) { fprintf (stderr, "Error writing to %s\n", options.outf_name); exit (1); } Qclose (outfile); }
VISIBLE void Cmd_Exec_File (cbuf_t *cbuf, const char *path, int qfs) { char *f; int len; QFile *file; if (!path || !*path) return; if (qfs) { QFS_FOpenFile (path, &file); } else { char *newpath = Sys_ExpandSquiggle (path); file = Qopen (newpath, "r"); free (newpath); } if (file) { len = Qfilesize (file); f = (char *) malloc (len + 1); if (f) { f[len] = 0; Qread (file, f, len); Cbuf_InsertText (cbuf, f); free (f); } Qclose (file); } }
static QFile * open_file (const char *path, int *len) { QFile *file = Qopen (path, "rbz"); if (!file) return 0; *len = Qfilesize (file); return file; }
int wad_add (wad_t *wad, const char *filename, const char *lumpname, byte type) { lumpinfo_t *pf; lumpinfo_t dummy; QFile *file; char buffer[16384]; int bytes; strncpy (dummy.name, lumpname, 16); dummy.name[15] = 0; pf = Hash_FindElement (wad->lump_hash, &dummy); if (pf) return -1; if (wad->numlumps == wad->lumps_size) { lumpinfo_t *f; wad->lumps_size += 64; f = realloc (wad->lumps, wad->lumps_size * sizeof (lumpinfo_t)); if (!f) return -1; wad->lumps = f; } file = Qopen (filename, "rb"); if (!file) return -1; wad->modified = 1; pf = &wad->lumps[wad->numlumps++]; strncpy (pf->name, lumpname, sizeof (pf->name)); pf->name[sizeof (pf->name) - 1] = 0; Qseek (wad->handle, 0, SEEK_END); pf->filepos = Qtell (wad->handle); pf->type = type; pf->size = 0; while ((bytes = Qread (file, buffer, sizeof (buffer)))) { Qwrite (wad->handle, buffer, bytes); pf->size += bytes; } Qclose (file); if (wad->pad && pf->size & 3) { static char buf[4]; Qwrite (wad->handle, buf, 4 - (pf->size & 3)); } Hash_AddElement (wad->lump_hash, pf); return 0; }
int Sys_FileTime (char *path) { QFile *f; f = Qopen(path, "rb"); if (f) { Qclose(f); return 1; } return -1; }
static void write_file (void) { QFile *file; const char *name; name = va ("%s/%s.lmp", destfile.str, lumpname->str); if (!(file = Qopen (name, "wb"))) Sys_Error ("couldn't open %s. %s", name, strerror(errno)); Qwrite (file, lumpbuffer, lump_p - lumpbuffer); Qclose (file); free (lumpbuffer); lumpbuffer = lump_p = 0; }
int Sys_FileOpenWrite (char *path) { QFile *f; int i; i = findhandle (); f = Qopen(path, "wb"); if (!f) Sys_Error ("Error opening %s: %s", path,strerror(errno)); sys_handles[i] = f; return i; }
int main (int argc, char *argv[]) { QFile *bspfile; bsp_t *bsp; image_t *image; /* Enough args? */ if (argc < 3) { show_help (); return 1; } /* Setup options */ def_options (&options); get_options (&options, argc, argv); show_options (&options); bspfile = Qopen (options.bspf_name, "rbz"); if (bspfile == NULL) { fprintf (stderr, "Error opening bsp file %s.\n", options.bspf_name); return 1; } bsp = LoadBSPFile (bspfile, Qfilesize (bspfile)); Qclose (bspfile); image = render_map (bsp); BSP_Free (bsp); /* Write image */ switch (options.outf_type) { case 0: write_pcx (image); break; case 1: write_png (image); break; } printf ("File written to %s.\n", options.outf_name); /* Close, done! */ free (image->image); free (image); return 0; }
wad_t * wad_open (const char *name) { wad_t *wad = wad_new (name); int i; if (!wad) return 0; wad->handle = Qopen (name, "rbz"); if (!wad->handle) { goto error; } if (Qread (wad->handle, &wad->header, sizeof (wad->header)) != sizeof (wad->header)) { fprintf (stderr, "%s: not a wad file\n", name); errno = 0; goto error; } if (strncmp (wad->header.id, "WAD2", 4)) { fprintf (stderr, "%s: not a wad file\n", name); errno = 0; goto error; } wad->header.infotableofs = LittleLong (wad->header.infotableofs); wad->header.numlumps = LittleLong (wad->header.numlumps); wad->numlumps = wad->header.numlumps; wad->old_numlumps = wad->lumps_size = wad->numlumps; wad->lumps = malloc (wad->lumps_size * sizeof (lumpinfo_t)); if (!wad->lumps) { //fprintf (stderr, "out of memory\n"); goto error; } Qseek (wad->handle, wad->header.infotableofs, SEEK_SET); Qread (wad->handle, wad->lumps, wad->numlumps * sizeof (wad->lumps[0])); for (i = 0; i < wad->numlumps; i++) { wad->lumps[i].filepos = LittleLong (wad->lumps[i].filepos); wad->lumps[i].size = LittleLong (wad->lumps[i].size); //Hash_AddElement (wad->lump_hash, &wad->lumps[i]); } return wad; error: wad_del (wad); return 0; }
int Sys_FileOpenWrite (char *path) { QFile *f; int i; i = findhandle (); f = Qopen(path, "wb"); if (!f) Sys_Error ("Error opening %s: %s", path,strerror(errno)); sys_handles[i].hFile = f; sys_handles[i].nLen = 0; sys_handles[i].nPos = 0; sys_handles[i].pMap = NULL; return i; }
int Sys_FileOpenRead (char *path, int *hndl) { QFile *f; int i; i = findhandle (); f = Qopen(path, "rb"); if (!f) { *hndl = -1; return -1; } sys_handles[i] = f; *hndl = i; return filelength(f); }
wad_t * wad_create (const char *name) { wad_t *wad = wad_new (name); if (!wad) return 0; wad->handle = Qopen (name, "wb"); if (!wad->handle) { wad_del (wad); return 0; } strncpy (wad->header.id, "WAD2", sizeof (wad->header.id)); Qwrite (wad->handle, &wad->header, sizeof (wad->header)); return wad; }
void Sys_DebugLog(char *file, char *fmt, ...) { va_list argptr; static char data[1024]; QFile *stream; unsigned char *p; //int fd; va_start(argptr, fmt); vsnprintf(data, sizeof(data), fmt, argptr); va_end(argptr); // fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666); stream = Qopen(file, "a"); for (p = (unsigned char *) data; *p; p++) { Qputc(stream, trans_table[*p]); } Qclose(stream); /* fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666); write(fd, data, strlen(data)); close(fd); */ }
void LoadTriangleList (char *filename, triangle_t **pptri, int *numtriangles) { QFile *input; char name[256], tex[256]; float start, exitpattern, t; int count, iLevel, magic, i; tf_triangle tri; triangle_t *ptri; t = -FLOAT_START; *((unsigned char *) &exitpattern + 0) = *((unsigned char *) &t + 3); *((unsigned char *) &exitpattern + 1) = *((unsigned char *) &t + 2); *((unsigned char *) &exitpattern + 2) = *((unsigned char *) &t + 1); *((unsigned char *) &exitpattern + 3) = *((unsigned char *) &t + 0); if ((input = Qopen(filename, "rb")) == 0) { fprintf (stderr,"reader: could not open file '%s'\n", filename); exit (0); } iLevel = 0; Qread(input, &magic, sizeof(int)); if (BigLong (magic) != MAGIC) { fprintf (stderr,"File is not a Alias object separated triangle file, " "magic number is wrong.\n"); exit (0); } ptri = malloc (MAXTRIANGLES * sizeof (triangle_t)); *pptri = ptri; while (Qeof(input) == 0) { Qread(input, &start, sizeof (float)); start = BigFloat (start); if (start != exitpattern) { if (start == FLOAT_START) { // Start of an object or group of objects. i = -1; do { // There are probably better ways to read a string from // a file, but this does allow you to do error checking // (which I'm not doing) on a per character basis. i++; Qread(input, &(name[i]), sizeof (char)); } while (name[i] != '\0'); // indent (); // fprintf(stdout,"OBJECT START: %s\n",name); Qread (input, &count, sizeof (int)); count = BigLong (count); ++iLevel; if (count != 0) { // indent(); // fprintf (stdout, "NUMBER OF TRIANGLES: %d\n", count); i = -1; do { i++; Qread (input, &(tex[i]), sizeof (char)); } while (tex[i] != '\0'); // indent(); // fprintf(stdout," Object texture name: '%s'\n",tex); } /* Else (count == 0) this is the start of a group, and */ /* no texture name is present. */ } else if (start == FLOAT_END) { /* End of an object or group. Yes, the name should be */ /* obvious from context, but it is in here just to be */ /* safe and to provide a little extra information for */ /* those who do not wish to write a recursive reader. */ /* Mia culpa. */ iLevel--; i = -1; do { i++; Qread (input, &(name[i]), sizeof (char)); } while (name[i] != '\0'); // indent(); // fprintf(stdout,"OBJECT END: %s\n",name); continue; } } // read the triangles for (i = 0; i < count; ++i) { int j; Qread (input, &tri, sizeof (tf_triangle)); ByteSwapTri (&tri); for (j = 0; j < 3; j++) { int k; for (k = 0; k < 3; k++) { ptri->verts[j][k] = tri.pt[j].p.v[k]; } } ptri++; if ((ptri - *pptri) >= MAXTRIANGLES) Sys_Error ("Error: too many triangles; increase MAXTRIANGLES\n"); } } *numtriangles = ptri - *pptri; Qclose (input); }
static void LoadPortals (char *name) { const char *line; char *err; int numpoints, i, j, k; int read_leafs = 0; int clusternums[2]; cluster_t *cluster; plane_t plane; portal_t *portal; winding_t *winding; QFile *f; if (!strcmp (name, "-")) f = Qdopen (0, "rt"); // create a QFile of stdin else { f = Qopen (name, "r"); if (!f) { printf ("LoadPortals: couldn't read %s\n", name); printf ("No vising performed.\n"); exit (1); } } line = Qgetline (f); if (line && (!strcmp (line, PORTALFILE "\n") || !strcmp (line, PORTALFILE "\r\n"))) { line = Qgetline (f); if (!line || sscanf (line, "%i\n", &portalclusters) != 1) Sys_Error ("LoadPortals: failed to read header"); line = Qgetline (f); if (!line || sscanf (line, "%i\n", &numportals) != 1) Sys_Error ("LoadPortals: failed to read header"); numrealleafs = portalclusters; } else if (line && (!strcmp (line, PORTALFILE_AM "\n") || !strcmp (line, PORTALFILE_AM "\r\n"))) { line = Qgetline (f); if (!line || sscanf (line, "%i\n", &portalclusters) != 1) Sys_Error ("LoadPortals: failed to read header"); line = Qgetline (f); if (!line || sscanf (line, "%i\n", &numportals) != 1) Sys_Error ("LoadPortals: failed to read header"); line = Qgetline (f); if (!line || sscanf (line, "%i\n", &numrealleafs) != 1) Sys_Error ("LoadPortals: failed to read header"); read_leafs = 1; } else { Sys_Error ("LoadPortals: not a portal file"); } if (options.verbosity >= 0) { printf ("%4i portalclusters\n", portalclusters); printf ("%4i numportals\n", numportals); printf ("%4i numrealleafs\n", numrealleafs); } bitbytes = ((portalclusters + 63) & ~63) >> 3; bitlongs = bitbytes / sizeof (long); bitbytes_l = ((numrealleafs + 63) & ~63) >> 3; // each file portal is split into two memory portals, one for each // direction portals = calloc (2 * numportals, sizeof (portal_t)); clusters = calloc (portalclusters, sizeof (cluster_t)); originalvismapsize = numrealleafs * ((numrealleafs + 7) / 8); for (i = 0, portal = portals; i < numportals; i++) { line = Qgetline (f); if (!line) Sys_Error ("LoadPortals: reading portal %i", i); numpoints = strtol (line, &err, 10); if (err == line) Sys_Error ("LoadPortals: reading portal %i", i); line = err; for (j = 0; j < 2; j++) { clusternums[j] = strtol (line, &err, 10); if (err == line) Sys_Error ("LoadPortals: reading portal %i", i); line = err; } if (numpoints > MAX_POINTS_ON_WINDING) Sys_Error ("LoadPortals: portal %i has too many points", i); if ((unsigned) clusternums[0] > (unsigned) portalclusters || (unsigned) clusternums[1] > (unsigned) portalclusters) Sys_Error ("LoadPortals: reading portal %i", i); winding = portal->winding = NewWinding (numpoints); winding->original = true; winding->numpoints = numpoints; for (j = 0; j < numpoints; j++) { // (%ld %ld %ld) while (isspace ((byte) *line)) line++; if (*line++ != '(') Sys_Error ("LoadPortals: reading portal %i", i); for (k = 0; k < 3; k++) { winding->points[j][k] = strtod (line, &err); if (err == line) Sys_Error ("LoadPortals: reading portal %i", i); line = err; } while (isspace ((byte) *line)) line++; if (*line++ != ')') Sys_Error ("LoadPortals: reading portal %i", i); } // calc plane PlaneFromWinding (winding, &plane); // create forward portal cluster = &clusters[clusternums[0]]; if (cluster->numportals == MAX_PORTALS_ON_CLUSTER) Sys_Error ("Cluster with too many portals"); cluster->portals[cluster->numportals] = portal; cluster->numportals++; portal->winding = winding; VectorNegate (plane.normal, portal->plane.normal); portal->plane.dist = -plane.dist; portal->cluster = clusternums[1]; portal++; // create backwards portal cluster = &clusters[clusternums[1]]; if (cluster->numportals == MAX_PORTALS_ON_CLUSTER) Sys_Error ("Cluster with too many portals"); cluster->portals[cluster->numportals] = portal; cluster->numportals++; portal->winding = winding; portal->plane = plane; portal->cluster = clusternums[0]; portal++; } leafcluster = calloc (numrealleafs, sizeof (int)); if (read_leafs) { for (i = 0; i < numrealleafs; i++) { line = Qgetline (f); if (sscanf (line, "%i\n", &leafcluster[i]) != 1) Sys_Error ("LoadPortals: parse error in leaf->cluster " "mappings"); } } else { for (i = 0; i < numrealleafs; i++) leafcluster[i] = i; } Qclose (f); }
static void load_image (const char *name) { QFile *file; tex_t *tex; int i, pixels; byte *s, *d; if (!(file = Qopen (name, "rb"))) Sys_Error ("couldn't open %s. %s", name, strerror(errno)); if (!(tex = LoadPNG (file))) Sys_Error ("couldn't read %s as PNG", name); pixels = tex->width * tex->height; image = malloc (pixels * 4 + sizeof (tex_t)); image->width = tex->width; image->height = tex->height; image->format = tex_rgba; image->palette = 0; switch (tex->format) { case tex_palette: for (i = 0, s = tex->data, d = image->data; i < pixels; i++) { byte *v = tex->palette + *s++ * 3; *d++ = *v++; *d++ = *v++; *d++ = *v++; *d++ = 255; } break; case tex_l: for (i = 0, s = tex->data, d = image->data; i < pixels; i++) { byte l = *s++; *d++ = l; *d++ = l; *d++ = l; *d++ = 255; } break; case tex_a: for (i = 0, s = tex->data, d = image->data; i < pixels; i++) { *d++ = 255; *d++ = 255; *d++ = 255; *d++ = *s++; } break; case tex_la: for (i = 0, s = tex->data, d = image->data; i < pixels; i++) { byte l = *s++; *d++ = l; *d++ = l; *d++ = l; *d++ = *s++; } break; case tex_rgb: for (i = 0, s = tex->data, d = image->data; i < pixels; i++) { *d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = 255; } break; case tex_rgba: memcpy (image->data, tex->data, pixels * 4); break; default: Sys_Error ("unknown texture type"); } }