void wad_close (wad_t *wad) { int i; if (wad->modified) { if (wad->numlumps > wad->old_numlumps) { Qseek (wad->handle, 0, SEEK_END); wad->header.infotableofs = Qtell (wad->handle); } 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); } Qseek (wad->handle, wad->header.infotableofs, SEEK_SET); Qwrite (wad->handle, wad->lumps, wad->numlumps * sizeof (wad->lumps[0])); wad->header.infotableofs = LittleLong (wad->header.infotableofs); wad->header.numlumps = LittleLong (wad->numlumps); Qseek (wad->handle, 0, SEEK_SET); Qwrite (wad->handle, &wad->header, sizeof (wad->header)); Qseek (wad->handle, 0, SEEK_END); } wad_del (wad); }
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; }
int addTreeNode(n **ref, q *list, int data) { n *nw = (n *)malloc(1*sizeof(n)); nw->data = data; nw->leftc = NULL; nw->rightc = NULL; nw->parent = NULL; printf("%x\t", nw); if(list->front == list->rear) { *ref = nw; Qadd(list, nw); return 1; } else { n *parent = Qseek(list); if(parent->leftc == NULL) { parent->leftc = nw; nw->parent = parent; Qadd(list, nw); return 1; } else if(parent->rightc == NULL) { parent->rightc = nw; nw->parent = parent; Qadd(list, nw); Qremove(list); return 1; } } }
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; }
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; }
/* we've opened the trackmap file from the quake resources * go through it, and make ourselves a tracklist map */ static int Load_Tracklist (void) { QFile *oggfile = NULL; char *buffile = NULL; int size; /* kill off the old tracklist, and make sure we're not playing anything */ I_OGGMus_Shutdown (); ogglistvalid = false; mus_enabled = false; if (!mus_ogglist || strequal (mus_ogglist->string, "none")) { return -1; // bail if we don't have a valid filename } oggfile = QFS_FOpenFile (mus_ogglist->string); if (!oggfile) { Sys_Printf ("Mus_OggInit: open of file \"%s\" failed\n", mus_ogglist->string); return -1; } if (!oggfile) { return -1; } /* rewind the stream */ Qseek (oggfile, 0, SEEK_SET); size = Qfilesize (oggfile); buffile = calloc (size+10, sizeof (char)); Qread (oggfile, buffile, size); tracklist = PL_GetPropertyList (buffile); if (!tracklist || PL_Type (tracklist) != QFDictionary) { Sys_Printf ("Malformed or empty tracklist file. check mus_ogglist\n"); return -1; } free (buffile); Qclose (oggfile); ogglistvalid = true; mus_enabled = true; return 0; }
static int load_progs (const char *name) { QFile *file; int i, size; char buff[5]; Hash_FlushTable (func_tab); file = open_file (name, &size); if (!file) { perror (name); return 0; } Qread (file, buff, 4); buff[4] = 0; Qseek (file, 0, SEEK_SET); if (!strcmp (buff, QFO)) { qfo = qfo_read (file); Qclose (file); if (!qfo) return 0; if (!need_progs) return 1; convert_qfo (); } else { pr.progs_name = name; PR_LoadProgsFile (&pr, file, size, 1, 0); Qclose (file); if (!pr.progs) return 0; PR_LoadStrings (&pr); PR_ResolveGlobals (&pr); PR_LoadDebug (&pr); } for (i = 0; i < pr.progs->numfunctions; i++) { // don't bother with builtins if (pr.pr_functions[i].first_statement > 0) Hash_AddElement (func_tab, &pr.pr_functions[i]); } return 1; }
int wad_add_data (wad_t *wad, const char *lumpname, byte type, const void *data, int bytes) { lumpinfo_t *pf; lumpinfo_t dummy; 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; } 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 = bytes; Qwrite (wad->handle, data, bytes); 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; }
/* LoadTGA */ byte * LoadTGA (QFile *fin) { int columns, rows, numPixels; byte *pixbuf; int row, column; unsigned char red = 0, green = 0, blue = 0, alphabyte = 0; TargaHeader targa_header; byte *targa_rgba; targa_header.id_length = Qgetc (fin); targa_header.colormap_type = Qgetc (fin); targa_header.image_type = Qgetc (fin); targa_header.colormap_index = fgetLittleShort (fin); targa_header.colormap_length = fgetLittleShort (fin); targa_header.colormap_size = Qgetc (fin); targa_header.x_origin = fgetLittleShort (fin); targa_header.y_origin = fgetLittleShort (fin); targa_header.width = fgetLittleShort (fin); targa_header.height = fgetLittleShort (fin); targa_header.pixel_size = Qgetc (fin); targa_header.attributes = Qgetc (fin); if (targa_header.image_type != 2 && targa_header.image_type != 10) Sys_Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n"); if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24)) Sys_Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n"); columns = targa_header.width; rows = targa_header.height; numPixels = columns * rows; targa_rgba = malloc (numPixels * 4); if (targa_header.id_length != 0) Qseek (fin, targa_header.id_length, SEEK_CUR); // skip TARGA image // comment if (targa_header.image_type == 2) { // Uncompressed, RGB images for (row = rows - 1; row >= 0; row--) { pixbuf = targa_rgba + row * columns * 4; for (column = 0; column < columns; column++) { switch (targa_header.pixel_size) { case 24: blue = Qgetc (fin); green = Qgetc (fin); red = Qgetc (fin); *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; *pixbuf++ = 255; break; case 32: blue = Qgetc (fin); green = Qgetc (fin); red = Qgetc (fin); alphabyte = Qgetc (fin); *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; *pixbuf++ = alphabyte; break; } } } } else if (targa_header.image_type == 10) { // Runlength encoded RGB // images unsigned char packetHeader, packetSize, j; for (row = rows - 1; row >= 0; row--) { pixbuf = targa_rgba + row * columns * 4; for (column = 0; column < columns;) { packetHeader = Qgetc (fin); packetSize = 1 + (packetHeader & 0x7f); if (packetHeader & 0x80) { // run-length packet switch (targa_header.pixel_size) { case 24: blue = Qgetc (fin); green = Qgetc (fin); red = Qgetc (fin); alphabyte = 255; break; case 32: blue = Qgetc (fin); green = Qgetc (fin); red = Qgetc (fin); alphabyte = Qgetc (fin); break; } for (j = 0; j < packetSize; j++) { *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; *pixbuf++ = alphabyte; column++; if (column == columns) { // run spans across rows column = 0; if (row > 0) row--; else goto breakOut; pixbuf = targa_rgba + row * columns * 4; } } } else { // non run-length packet for (j = 0; j < packetSize; j++) { switch (targa_header.pixel_size) { case 24: blue = Qgetc (fin); green = Qgetc (fin); red = Qgetc (fin); *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; *pixbuf++ = 255; break; case 32: blue = Qgetc (fin); green = Qgetc (fin); red = Qgetc (fin); alphabyte = Qgetc (fin); *pixbuf++ = red; *pixbuf++ = green; *pixbuf++ = blue; *pixbuf++ = alphabyte; break; } column++; if (column == columns) { // pixel packet run spans // across rows column = 0; if (row > 0) row--; else goto breakOut; pixbuf = targa_rgba + row * columns * 4; } } } } breakOut:; } } Qclose (fin); return targa_rgba; }
int SND_Load (sfx_t *sfx) { char *realname; char buf[4]; QFile *file; sfx->touch = sfx->retain = snd_fail; sfx->release = snd_noop; sfx->close = snd_noop; sfx->open = snd_open_fail; file = QFS_FOpenFile (sfx->name); if (!file) { Sys_Printf ("Couldn't load %s\n", sfx->name); return -1; } sfx->open = snd_open; if (!strequal (qfs_foundfile.realname, sfx->name)) { realname = strdup (qfs_foundfile.realname); } else { realname = (char *) sfx->name; // won't free if realname == sfx->name } Qread (file, buf, 4); Qseek (file, 0, SEEK_SET); #ifdef HAVE_VORBIS if (strnequal ("OggS", buf, 4)) { Sys_MaskPrintf (SYS_DEV, "SND_Load: ogg file\n"); if (SND_LoadOgg (file, sfx, realname) == -1) goto bail; return 0; } #endif #ifdef HAVE_FLAC if (strnequal ("fLaC", buf, 4)) { Sys_MaskPrintf (SYS_DEV, "SND_Load: flac file\n"); if (SND_LoadFLAC (file, sfx, realname) == -1) goto bail; return 0; } #endif #ifdef HAVE_WILDMIDI if (strnequal ("MThd", buf, 4)) { Sys_MaskPrintf (SYS_DEV, "SND_Load: midi file\n"); if (SND_LoadMidi (file, sfx, realname) == -1) goto bail; return 0; } #endif if (strnequal ("RIFF", buf, 4)) { Sys_MaskPrintf (SYS_DEV, "SND_Load: wav file\n"); if (SND_LoadWav (file, sfx, realname) == -1) goto bail; return 0; } bail: Qclose (file); if (realname != sfx->name) free (realname); return -1; }