int main(int argc, char *argv[]) { int samplerate = 44100; int nsamples = samplerate * 240; int bytesize = nsamples * 2; fprintf(stdout, "RIFF"); fputi(bytesize + 36, 4, stdout); // ChunkSize fprintf(stdout, "WAVE"); fprintf(stdout, "fmt "); fputi(16, 4, stdout); // SubChunkSize fputi(1, 2, stdout); // AudioFormat fputi(1, 2, stdout); // NumChannels fputi(samplerate, 4, stdout); // SampleRate fputi(samplerate * 2, 4, stdout); // ByteRate fputi(2, 2, stdout); // BlockAlign fputi(16, 2, stdout); // BitsPerSample fprintf(stdout, "data"); fputi(bytesize, 4, stdout); int maxAmplitude = 0; for (int tn = 0; tn < nsamples; tn++) { int sample = (int) (32767 * dsp(tn)); int asample = abs(sample); if (asample > maxAmplitude) maxAmplitude = asample; fputi(sample, 2, stdout); } fprintf(stderr, "%d", maxAmplitude); return 0; }
void Object:: print(bool opening, int base_i, FILE* f) const { if(opening) { fputi(base_i,f); fprintf(f,"{\n"); } if(members.size()) { auto it = members.cbegin(); auto end = members.cend(); for(;;) { fputi(base_i+inc,f); fprintf(f,"\"%s\":",it->name.data()); if(it->value.kind == ElementKind::object){fprintf(f,"{\n");} else if(it->value.kind == ElementKind::array ){fprintf(f,"[\n");} it->value.print(false,base_i+inc,f); ++it; if(it == end) { break; } fprintf(f,",\n"); } } fprintf(f,"\n"); fputi(base_i,f); fprintf(f,"}"); }
void Array:: print(bool opening, int base_i, FILE* f) const { if(opening) { fputi(base_i,f); fprintf(f,"[\n"); } if(elements.size()) { auto it = elements.cbegin(); auto end = elements.cend(); for(;;) { fputi(base_i+inc,f); if(it->kind == ElementKind::object){fprintf(f,"{\n");} else if(it->kind == ElementKind::array ){fprintf(f,"[\n");} it->print(false,base_i+inc,f); ++it; if(it == end) { break; } fprintf(f,",\n"); } } fprintf(f,"\n"); fputi(base_i,f); fprintf(f,"]"); }
void WriteSave(const char *fn, SAVESTATE_t* save, int compress) { int i; FILE* ofile; FILE* cfile; char temp_save[PATH_MAX]; if (!save) { return; } if (compress == 0) { ofile = fopen(fn, "wb"); } else { GetAppDataString(temp_save, PATH_MAX - 1 - strlen(tmpSuffix)); strcat(temp_save, tmpSuffix); mkstemp(temp_save); ofile = fopen(temp_save,"wb"); } if (!ofile) { return; } fputs(DETECT_STR, ofile); fputi(SAVE_HEADERSIZE, ofile); fputi(save->version_major, ofile); fputi(save->version_minor, ofile); fputi(save->version_build, ofile); fputi(save->model, ofile); fputi(save->chunk_count, ofile); fwrite(save->author, 1,32, ofile); fwrite(save->comment, 1, 64, ofile); for(i = 0; i < save->chunk_count; i++) { fputc(save->chunks[i]->tag[0], ofile); fputc(save->chunks[i]->tag[1], ofile); fputc(save->chunks[i]->tag[2], ofile); fputc(save->chunks[i]->tag[3], ofile); fputi(save->chunks[i]->size,ofile); fwrite(save->chunks[i]->data, 1, save->chunks[i]->size, ofile); } fclose(ofile); if (compress) { cfile = fopen(fn, "wb"); if (!cfile) { return; } ofile = fopen(temp_save,"rb"); if (!ofile) { return; } //int error; fputs(DETECT_CMP_STR, cfile); switch(compress) { #ifdef ZLIB_WINAPI case ZLIB_CMP: { fputc(ZLIB_CMP, cfile); int error = def(ofile, cfile, 9); break; } #endif default: break; } fclose(ofile); fclose(cfile); remove(temp_save); } }
void WriteSave(const TCHAR *fn, SAVESTATE_t* save, int compress) { int i; FILE* ofile; FILE* cfile; TCHAR tmpfn[L_tmpnam]; TCHAR temp_save[MAX_PATH]; if (!save) { _putts(_T("Save was null for write")); return; } if (compress == 0) { #ifdef WINVER _tfopen_s(&ofile, fn, _T("wb")); #else ofile = fopen(fn,"wb"); #endif } else { #ifdef WINVER _ttmpnam_s(tmpfn, sizeof(tmpfn)); GetAppDataString(temp_save, sizeof(temp_save)); StringCbCat(temp_save, sizeof(temp_save), tmpfn); _tfopen_s(&ofile, temp_save, _T("wb")); #else tmpnam(tmpfn); strcpy(temp_save, getenv("appdata")); strcat(temp_save, tmpfn); ofile = fopen(temp_save,"wb"); #endif } if (!ofile) { _putts(_T("Could not open save file for write")); return; } fputs(DETECT_STR, ofile); fputi(SAVE_HEADERSIZE, ofile); fputi(save->version_major, ofile); fputi(save->version_minor, ofile); fputi(save->version_build, ofile); fputi(save->model, ofile); fputi(save->chunk_count, ofile); fwrite(save->author, 1,32, ofile); fwrite(save->comment, 1, 64, ofile); for(i = 0; i < save->chunk_count; i++) { fputc(save->chunks[i]->tag[0], ofile); fputc(save->chunks[i]->tag[1], ofile); fputc(save->chunks[i]->tag[2], ofile); fputc(save->chunks[i]->tag[3], ofile); fputi(save->chunks[i]->size,ofile); fwrite(save->chunks[i]->data, 1, save->chunks[i]->size, ofile); } fclose(ofile); if (compress) { #ifdef WINVER _tfopen_s(&cfile, fn, _T("wb")); #else cfile = fopen(fn,"wb"); #endif if (!cfile) { return; } #ifdef WINVER _tfopen_s(&ofile, temp_save, _T("rb")); #else ofile = fopen(temp_save,"rb"); #endif if (!ofile) { return; } //int error; fputs(DETECT_CMP_STR, cfile); switch(compress) { #ifdef ZLIB_WINAPI case ZLIB_CMP: { fputc(ZLIB_CMP, cfile); int error = def(ofile, cfile, 9); break; } #endif default: break; } fclose(ofile); fclose(cfile); #ifdef _WINDOWS _tremove(temp_save); #else remove(temp_save); #endif } }
bool profile_save(const char *pfname, Profile *file) { int i, slotno = 0, curweaponslot = 0; FILE *fp = fopen(pfname, "wb"); if (!fp) return 1; fputstringnonull("Do041220", fp); fputl(file->stage, fp); fputl(file->songno, fp); fputl(file->px, fp); fputl(file->py, fp); fputl((file->pdir == RIGHT) ? 2:0, fp); fputi(file->maxhp, fp); fputi(file->num_whimstars, fp); fputi(file->hp, fp); fseek(fp, 0x2C, SEEK_SET); fputi(file->equipmask, fp); /* save weapons */ fseek(fp, PF_WEAPONS_OFFS, SEEK_SET); for(i = 0; i < WPN_COUNT; i++) { if (file->weapons[i].hasWeapon) { fputl(i, fp); fputl(file->weapons[i].level + 1, fp); fputl(file->weapons[i].xp, fp); fputl(file->weapons[i].maxammo, fp); fputl(file->weapons[i].ammo, fp); if (i == file->curWeapon) curweaponslot = slotno; slotno++; if (slotno >= MAX_WPN_SLOTS) break; } } if (slotno < MAX_WPN_SLOTS) fputl(0, fp); // 0-type weapon: terminator /* go back and save slot no of current weapon */ fseek(fp, PF_CURWEAPON_OFFS, SEEK_SET); fputl(curweaponslot, fp); /* save inventory */ fseek(fp, PF_INVENTORY_OFFS, SEEK_SET); for(i=0;i<file->ninventory;i++) fputl(file->inventory[i], fp); fputl(0, fp); /* write teleporter slots */ fseek(fp, PF_TELEPORTER_OFFS, SEEK_SET); for(i=0;i<MAX_TELE_SLOTS;i++) { if (i < file->num_teleslots) { fputl(file->teleslots[i].slotno, fp); fputl(file->teleslots[i].scriptno, fp); } else { fputl(0, fp); fputl(0, fp); } } /* write flags */ fseek(fp, PF_FLAGS_OFFS, SEEK_SET); fputstringnonull("FLAG", fp); fresetboolean(); for(i=0;i<NUM_GAMEFLAGS;i++) fbooleanwrite(file->flags[i], fp); fbooleanflush(fp); fclose(fp); return 0; }
/* using the SGRLE algorithm and saves it to file *fp */ void sgrle_compress(FILE *fp, unsigned char *ptr, unsigned long nbytes) { int byt; unsigned long compress_index, run_ahead_index; unsigned int runlength; int readbyt; unsigned int i; fputl(nbytes, fp); compress_index = 0; while(compress_index < nbytes) { // read a byte from the buffer readbyt = ptr[compress_index]; /* is the next byte the same? if so find the length of the run */ if ((compress_index+1 < nbytes) && ptr[compress_index+1]==readbyt) { /* find how long the run is (a run of byte readbyt) */ run_ahead_index = (compress_index + 1); runlength = 1; do { byt = ptr[run_ahead_index]; // the run is over when either the byte is different // or run_ahead_index is at the end of the buffer, // or runlength is approaching FFFF (max possible RLE run length) if (byt != readbyt || run_ahead_index >= nbytes || runlength >= SGRLE_MAXRUNLEN) { break; } run_ahead_index++; runlength++; } while(1); // it takes 4 bytes to code a RLE run, so if the run is less than // 4 bytes, it would actually be smaller if we didn't compress it if (runlength < 4 && readbyt != SGRLE_RLEMARKERNEW) { // RLE run, but too small to bother with for(i=0;i<runlength;i++) fputc(readbyt, fp); } else { // save a RLE run fputc(SGRLE_RLEMARKERNEW, fp); fputi(runlength, fp); fputc(readbyt, fp); } // advance compress_index += runlength; } else { // next byte is different, this is not a run, it's just a single char if (readbyt != SGRLE_RLEMARKERNEW) { fputc(readbyt, fp); } else { // it's a single uncompressed byte which is equal to the RLE marker. // delimit it by placing it in a RLE run of length 1. fputc(SGRLE_RLEMARKERNEW, fp); fputi(1, fp); fputc(readbyt, fp); } compress_index++; } } }