Exemplo n.º 1
0
Arquivo: pmd.cpp Projeto: histat/dc-nx
// loads octave, icon, and wavetable for a track.
static void load_instrument(FILE *fp, stInstrument *ins)
{
int i;

	ins->octave = fgetc(fp);
	ins->icon = fgetc(fp);
	
	fgeti(fp);
	
	ins->notelength = fgetl(fp);
	ins->volume = fgeti(fp);
	
	for(i=0;i<10;i++)
		fgetc(fp);
	
	// read instrument waveform
	for(i=0;i<WAVE_LENGTH;i++)
		ins->wave[i] = fgetc(fp);
	
	// read envelope
	for(i=0;i<ENV_LENGTH;i++)
		ins->env[i] = fgetc(fp);
}
Exemplo n.º 2
0
/* used internally by sgrle_decompress(). */
unsigned char sgrle_get_next_byte(FILE *fp, unsigned char marker)
{
	// are we currently in a RLE run?
	if (sgrle_runlen)
	{
		// decrease length of RLE run and return the previously
		// read char for the run
		sgrle_runlen--;
		return sgrle_runchar;
	}
	else
	{	// not currently in a RLE run
		sgrle_runchar = fgetc(fp);
		if (sgrle_runchar==marker)
		{  // start of a RLE run
			sgrle_runlen = fgeti(fp);
			sgrle_runchar = fgetc(fp);
			return sgrle_get_next_byte(fp, marker);
		}
		else return sgrle_runchar;
	}
}
Exemplo n.º 3
0
SAVESTATE_t* ReadSave(FILE *ifile) {
	int i;
	int compressed = false;
	int chunk_offset,chunk_count;
	char string[128];
	char temp_save[PATH_MAX];
	SAVESTATE_t *save;
	CHUNK_t *chunk;
	FILE *tmpfile;

	fread(string, 1, 8, ifile);
	string[8] = 0;
	if (strncmp(DETECT_CMP_STR, string, 8) == 0) {
		i = fgetc(ifile);
		GetAppDataString(temp_save, PATH_MAX - 1 - strlen(tmpSuffix));
		strcat(temp_save, tmpSuffix);
		mkstemp(temp_save);
		tmpfile = fopen(temp_save,"wb");
		if (!tmpfile) {
			return nullptr;
		}
		//int error;
		switch(i) {
#ifdef ZLIB_WINAPI
			case ZLIB_CMP:
				{
					int error = inf(ifile,tmpfile);
					fclose(tmpfile);
					ifile = fopen(temp_save,"rb");	//this is not a leak, file gets closed
													// outside of this routine.
					if (!ifile) {
						return nullptr;
					}
					compressed = true;
					fread(string, 1, 8, ifile);
					break;
				}
#endif
			default:
				fclose(tmpfile);
				remove(temp_save);
				return nullptr;
		}
	}
		
	if (strncmp(DETECT_STR, string, 8) != 0){

		if (compressed == true) fclose(ifile);
		return nullptr;
	}		
	
	save = (SAVESTATE_t *) malloc(sizeof(SAVESTATE_t));
	if (!save) {
		if (compressed == true) fclose(ifile);
		return nullptr;
	}

	chunk_offset = fgeti(ifile);
	
	save->version_major = fgeti(ifile);
	save->version_minor = fgeti(ifile);
	save->version_build = fgeti(ifile);


	if (save->version_major != CUR_MAJOR) {
		fclose(ifile);
		free(save);
		return nullptr;
	}

	save->model = fgeti(ifile);

	chunk_count = fgeti(ifile);
	fread(save->author,1,32,ifile);
	fread(save->comment,1,64,ifile);

	fseek(ifile, chunk_offset + 8 + 4, SEEK_SET);
	
	for(i = 0; i < 512; i++) {
		save->chunks[i] = nullptr;
	}
	save->chunk_count = 0;
	for(i = 0; i < chunk_count; i++) {
		string[0]	= fgetc(ifile);
		string[1]	= fgetc(ifile);
		string[2]	= fgetc(ifile);
		string[3]	= fgetc(ifile);
		string[4]	= 0;
		chunk		= NewChunk(save,string);
		chunk->size	= fgeti(ifile);
		chunk->data	= (unsigned char *) malloc(chunk->size);
		fread(chunk->data,1,chunk->size,ifile);
	}
	if (compressed == true) {
		fclose(ifile);
		remove(temp_save);
	}
/* check for read errors... */
	return save;
}
Exemplo n.º 4
0
/** 
 *  \brief loads a 32-bit uncompressed RGBA targa file, and return a pointer to
 *         the raw image data. The width and height of the image are returned as well.
 *  \param filename Name to the file that will used to read the picture
 *  \param image    Pointer to an array that will contain the image data. This array will be allocated
 *  \param width    Width of the picture that will be set after the picture was read
 *  \param height   Same here for the height
 */
bool LoadTGA(const std::string& filename, Uint8 **image, Uint16 &width, Uint16 &height)
{
	TGA_HEADER header;
	FILE *fp;
	uint bytes_per_pixel;
	unsigned long img_data_size;

	// First check if the files exists
	if ((fp=OpenGameFile(filename, "rb")) == NULL)
		return false;
	
	// read the header
	header.identsize = fgetc(fp);
	fgetc(fp);
	header.imagetype = fgetc(fp);
	fgeti(fp); fgeti(fp); fgetc(fp);
	
	header.xstart = fgeti(fp);
	header.ystart = fgeti(fp);
	header.width = fgeti(fp);
	header.height = fgeti(fp);
	header.bpp = fgetc(fp);
	fgetc(fp);
	
	// Check if the header meet our requirements
	if (header.imagetype != TGA_RGB)
	{
		g_pLogFile->textOut(PURPLE,"<br>LoadTGA: " + filename + ": imagetype must be RGBA uncompressed!<br>");
		fclose(fp);
		return false;
	}

	if (header.bpp != 32)
	{
		g_pLogFile->textOut(PURPLE,"<br>LoadTGA: " + filename + ": image bpp must be 32 (RGB w/ alpha channel)<br>");
		fclose(fp);
		return false;
	}
	
	// Set some variables values so the pictures can be used outside this code
	width = header.width;
	height = header.height;
	
	bytes_per_pixel = (header.bpp / 8);
	img_data_size = header.width * header.height * bytes_per_pixel;

	// allocate memory for the image buffer
	*image = new byte [img_data_size];
	if (*image == NULL)
	{
		fclose(fp);
		return false;
	}
	
	// Read the picture data
	const uint elem_read = fread(*image, img_data_size, 1, fp);
	fclose(fp);

	// Check if the picture was read correctly
	if(elem_read != 1)
	{
		delete [] *image;
		*image  = NULL;
		return false;
	}
	else
		return true;
}
Exemplo n.º 5
0
char org_load(char *fname)
{
static const char *magic = "Org-02";
char buf[8];
FILE *fp;
int i, j;

	fp = fileopen(fname, "rb");
	if (!fp) { visible_warning("org_load: no such file: '%s'", fname); return 1; }
	
	for(i=0;i<6;i++) { buf[i] = fgetc(fp); } buf[i] = 0;
	if (strcmp(buf, magic)) { visible_warning("org-load: not an org file (got '%s')", buf); fclose(fp); return 1; }
	stat("%s: %s detected", fname, magic);
	
	fseek(fp, 0x06, SEEK_SET);
	
	song.ms_per_beat = fgeti(fp);
	song.steps_per_bar = fgetc(fp);
	song.beats_per_step = fgetc(fp);
	song.loop_start = fgetl(fp);
	song.loop_end = fgetl(fp);
	
	//song.ms_per_beat = 500;
	//song.loop_start = 64;
	
	if (song.loop_end < song.loop_start)
	{
		visible_warning("org_load: loop end is before loop start");
		fclose(fp);
		return 1;
	}
	
	// compute how long the last beat of a note should be (it should not use up the whole beat)
	song.ms_of_last_beat_of_note = song.ms_per_beat - (int)((double)song.ms_per_beat * 0.1);
	
	// not actually used in this module, but the larger program might want to know this
	song.beats_per_bar = (song.beats_per_step * song.steps_per_bar);
	
	/*lprintf("tempo: %d ms/beat\n", song.ms_per_beat);
	lprintf("beats_per_step: %d\n", song.beats_per_step);
	lprintf("steps_per_bar: %d\n", song.steps_per_bar);
	lprintf("loop begins on beat %d\n", song.loop_start);
	lprintf("loop ends on beat %d\n", song.loop_end);*/
	
	for(i=0;i<16;i++)
	{
		song.instrument[i].pitch = fgeti(fp);
		song.instrument[i].wave = fgetc(fp);
		song.instrument[i].pi = fgetc(fp);
		song.instrument[i].nnotes = fgeti(fp);
		
		if (song.instrument[i].nnotes >= MAX_SONG_LENGTH)
		{
			visible_warning(" * org_load: instrument %d has too many notes! (has %d, max %d)", i, song.instrument[i].nnotes, MAX_SONG_LENGTH);
			fclose(fp);
			return 1;
		}
		
		/*if (song.instrument[i].nnotes)
		{
			lprintf("Instrument %d: ", i);
			lprintf(" Pitch: %d, ", song.instrument[i].pitch);
			lprintf(" Wave: %d, ", song.instrument[i].wave);
			lprintf(" Pi: %d, ", song.instrument[i].pi);
			lprintf(" Nnotes: %d\n", song.instrument[i].nnotes);
		}*/
		
		// substitute unavailable drums
		// credits track for one, has Per02 set which CS didn't actually have, I don't think
		if (i >= 8)
		{
			switch(song.instrument[i].wave)
			{
				case 9: song.instrument[i].wave = 8; break;
			}
		}
	}
	
	for(i=0;i<16;i++)
	{
		for(j=0;j<song.instrument[i].nnotes;j++) song.instrument[i].note[j].beat = fgetl(fp);
		for(j=0;j<song.instrument[i].nnotes;j++) song.instrument[i].note[j].note = fgetc(fp);
		for(j=0;j<song.instrument[i].nnotes;j++) song.instrument[i].note[j].length = fgetc(fp);
		for(j=0;j<song.instrument[i].nnotes;j++) song.instrument[i].note[j].volume = fgetc(fp);
		for(j=0;j<song.instrument[i].nnotes;j++) song.instrument[i].note[j].panning = fgetc(fp);
	}
	
	fclose(fp);
	return init_buffers();
}
Exemplo n.º 6
0
SAVESTATE_t* ReadSave(FILE *ifile) {
	int i;
	int compressed = FALSE;
	int chunk_offset,chunk_count;
	char string[128];
	TCHAR tmpfn[L_tmpnam];
	TCHAR temp_save[MAX_PATH];
	SAVESTATE_t *save;
	CHUNK_t *chunk;
	FILE *tmpfile;

	fread(string, 1, 8, ifile);
	string[8] = 0;
	if (strncmp(DETECT_CMP_STR, string, 8) == 0) {
		i = fgetc(ifile);
#ifdef WINVER
		_ttmpnam_s(tmpfn);
		GetAppDataString(temp_save, sizeof(temp_save));
		StringCbCat(temp_save, sizeof(temp_save), tmpfn);
		_tfopen_s(&tmpfile, temp_save, _T("wb"));
#else
		tmpnam(tmpfn);
		strcpy(temp_save, getenv("appdata"));
		strcat(temp_save, tmpfn);
		tmpfile = fopen(temp_save,"wb");
#endif
		if (!tmpfile) {
			return NULL;
		}
		//int error;
		switch(i) {
#ifdef ZLIB_WINAPI
			case ZLIB_CMP:
				{
					int error = inf(ifile,tmpfile);
					break;
				}
#endif
			default:
				fclose(tmpfile);
#ifdef _WINDOWS
				_tremove(tmpfn);
#else
				remove(tmpfn);
#endif
				return NULL;
		}
		
		fclose(tmpfile);
#ifdef WINVER
		_tfopen_s(&ifile, temp_save, _T("rb"));	//this is not a leak, file gets closed
											// outside of this routine.
#else
		ifile = fopen(temp_save,"rb");	//this is not a leak, file gets closed
										// outside of this routine.
#endif
		if (!ifile) {
			return NULL;
		}
		compressed = TRUE;
		fread(string, 1, 8, ifile);
	}
		
	if (strncmp(DETECT_STR, string, 8) != 0){

		if (compressed == TRUE) fclose(ifile);
		return NULL;
	}		
	
	save = (SAVESTATE_t *) malloc(sizeof(SAVESTATE_t));
	if (!save) {
		if (compressed == TRUE) fclose(ifile);
		return NULL;
	}

	chunk_offset = fgeti(ifile);
	
	save->version_major = fgeti(ifile);
	save->version_minor = fgeti(ifile);
	save->version_build = fgeti(ifile);


	if (save->version_major != CUR_MAJOR) {
		fclose(ifile);
		free(save);
		return NULL;
	}

	save->model = fgeti(ifile);

	chunk_count = fgeti(ifile);
	fread(save->author,1,32,ifile);
	fread(save->comment,1,64,ifile);

	fseek(ifile, chunk_offset + 8 + 4, SEEK_SET);
	
	for(i = 0; i < 512; i++) {
		save->chunks[i] = NULL;
	}
	save->chunk_count = 0;
	for(i = 0; i < chunk_count; i++) {
		string[0]	= fgetc(ifile);
		string[1]	= fgetc(ifile);
		string[2]	= fgetc(ifile);
		string[3]	= fgetc(ifile);
		string[4]	= 0;
		chunk		= NewChunk(save,string);
		chunk->size	= fgeti(ifile);
		chunk->data	= (unsigned char *) malloc(chunk->size);
		fread(chunk->data,1,chunk->size,ifile);
	}
	if (compressed == TRUE) {
		fclose(ifile);
#ifdef _WINDOWS
		_tremove(temp_save);
#else
		remove(temp_save);
#endif
	}
/* check for read errors... */
	return save;
}
Exemplo n.º 7
0
// load savefile #num into the given Profile structure.
bool profile_load(const char *pfname, Profile *file)
{
   int i, curweaponslot;
   FILE *fp = fopen(pfname, "rb");

   memset(file, 0, sizeof(Profile));

   if (!fp)
      return 1;

   if (!fverifystring(fp, "Do041220"))
      goto error;

   file->stage = fgetl(fp);
   file->songno = fgetl(fp);

   file->px = fgetl(fp);
   file->py = fgetl(fp);
   file->pdir = CVTDir(fgetl(fp));

   file->maxhp = fgeti(fp);
   file->num_whimstars = fgeti(fp);
   file->hp = fgeti(fp);

   fgeti(fp);						// unknown value
   curweaponslot = fgetl(fp);		// current weapon (slot, not number, converted below)
   fgetl(fp);						// unknown value
   file->equipmask = fgetl(fp);	// equipped items

   // load weapons
   fseek(fp, PF_WEAPONS_OFFS, SEEK_SET);
   for(i=0;i<MAX_WPN_SLOTS;i++)
   {
      int level, xp, maxammo, ammo;
      int type = fgetl(fp);
      if (!type)
         break;

      level   = fgetl(fp);
      xp      = fgetl(fp);
      maxammo = fgetl(fp);
      ammo    = fgetl(fp);

      file->weapons[type].hasWeapon = true;
      file->weapons[type].level = (level - 1);
      file->weapons[type].xp = xp;
      file->weapons[type].ammo = ammo;
      file->weapons[type].maxammo = maxammo;

      if (i == curweaponslot)
         file->curWeapon = type;
   }

   /* load inventory */
   file->ninventory = 0;
   fseek(fp, PF_INVENTORY_OFFS, SEEK_SET);
   for(i=0;i<MAX_INVENTORY;i++)
   {
      int item = fgetl(fp);
      if (!item)
         break;

      file->inventory[file->ninventory++] = item;
   }

   /* load teleporter slots */
   file->num_teleslots = 0;
   fseek(fp, PF_TELEPORTER_OFFS, SEEK_SET);

   for(i=0;i<NUM_TELEPORTER_SLOTS;i++)
   {
      int slotno   = fgetl(fp);
      int scriptno = fgetl(fp);
      if (slotno == 0)
         break;

      file->teleslots[file->num_teleslots].slotno = slotno;
      file->teleslots[file->num_teleslots].scriptno = scriptno;
      file->num_teleslots++;
   }

   /* load flags */
   fseek(fp, PF_FLAGS_OFFS, SEEK_SET);
   if (!fverifystring(fp, "FLAG"))
   {
      NX_ERR("profile_load: missing 'FLAG' marker\n");
      goto error;
   }

   fresetboolean();
   for(i=0;i<NUM_GAMEFLAGS;i++)
      file->flags[i] = fbooleanread(fp);

   fclose(fp);
   return 0;

error:
   fclose(fp);
   return 1;
}