int t3f_save_animation_f(T3F_ANIMATION * ap, ALLEGRO_FILE * fp) { int i; ani_header[11] = T3F_ANIMATION_REVISION; // put the version number in al_fwrite(fp, ani_header, 12); al_fwrite16le(fp, ap->bitmaps->count); for(i = 0; i < ap->bitmaps->count; i++) { if(!t3f_save_bitmap_f(fp, ap->bitmaps->bitmap[i])) { printf("failed to save bitmap\n"); return 0; } } al_fwrite16le(fp, ap->frames); for(i = 0; i < ap->frames; i++) { al_fwrite16le(fp, ap->frame[i]->bitmap); t3f_fwrite_float(fp, ap->frame[i]->x); t3f_fwrite_float(fp, ap->frame[i]->y); t3f_fwrite_float(fp, ap->frame[i]->z); t3f_fwrite_float(fp, ap->frame[i]->width); t3f_fwrite_float(fp, ap->frame[i]->height); t3f_fwrite_float(fp, ap->frame[i]->angle); al_fwrite32le(fp, ap->frame[i]->ticks); al_fwrite32le(fp, ap->frame[i]->flags); } al_fwrite32le(fp, ap->flags); return 1; }
static size_t slice_fwrite(ALLEGRO_FILE *f, const void *ptr, size_t size) { SLICE_DATA *slice = al_get_file_userdata(f); if (!(slice->mode & SLICE_WRITE)) { /* no write permissions */ return 0; } if (!(slice->mode & SLICE_EXPANDABLE) && slice->pos + size > slice->size) { /* don't write past the buffer size if not expandable */ size = slice->size - slice->pos; } if (!size) { return 0; } else { /* unbuffered, write directly to parent file */ size_t b = al_fwrite(slice->fp, ptr, size); slice->pos += b; if (slice->pos > slice->size) slice->size = slice->pos; return b; } }
bool t3f_save_vector_object_f(T3F_VECTOR_OBJECT * vp, ALLEGRO_FILE * fp) { unsigned char r, g, b, a; char header[16] = {'T', '3', 'F', 'V'}; int i; if(al_fwrite(fp, header, 16) != 16) { return false; } al_fwrite32le(fp, vp->segments); for(i = 0; i < vp->segments; i++) { t3f_fwrite_float(fp, vp->segment[i]->point[0].x); t3f_fwrite_float(fp, vp->segment[i]->point[0].y); t3f_fwrite_float(fp, vp->segment[i]->point[0].z); t3f_fwrite_float(fp, vp->segment[i]->point[1].x); t3f_fwrite_float(fp, vp->segment[i]->point[1].y); t3f_fwrite_float(fp, vp->segment[i]->point[1].z); al_unmap_rgba(vp->segment[i]->color, &r, &g, &b, &a); al_fputc(fp, r); al_fputc(fp, g); al_fputc(fp, b); al_fputc(fp, a); t3f_fwrite_float(fp, vp->segment[i]->thickness); } return true; }
void CDisplay::UpdateHighScores() { for(int i = 0; i < numHighScores; i++) { long tempScore = atol(highScores[i]); if(score > tempScore) { for(int j = numHighScores - 1; j > i; j--) highScores[j] = highScores[j - 1]; highScores[i] = ltoa(score, new char[16], 10); break; } } ALLEGRO_FILE* file; file = al_fopen("C:\\Users\\Chase\\Documents\\Visual Studio 2010\\Projects\\Tetris\\scores.scr", "w+b"); if(!file) { printf("File scores.scr could not be opened\n"); return; } for(int i = 0; i < numHighScores; i++) { long tempScore = atol(highScores[i]); al_fwrite(file, &tempScore, 8); } al_fclose(file); printf("File written to"); }
int t3f_save_tilemap_f(T3F_TILEMAP * tmp, ALLEGRO_FILE * fp) { int i, j, k; char header[16] = {0}; strcpy(header, "T3F_TILEMAP"); header[15] = 0; al_fwrite(fp, header, 16); al_fwrite16le(fp, tmp->layers); for(i = 0; i < tmp->layers; i++) { al_fwrite16le(fp, tmp->layer[i]->width); al_fwrite16le(fp, tmp->layer[i]->height); for(j = 0; j < tmp->layer[i]->height; j++) { for(k = 0; k < tmp->layer[i]->width; k++) { al_fwrite16le(fp, tmp->layer[i]->data[j][k]); } } t3f_fwrite_float(fp, tmp->layer[i]->x); t3f_fwrite_float(fp, tmp->layer[i]->y); t3f_fwrite_float(fp, tmp->layer[i]->z); t3f_fwrite_float(fp, tmp->layer[i]->scale); t3f_fwrite_float(fp, tmp->layer[i]->speed_x); t3f_fwrite_float(fp, tmp->layer[i]->speed_y); al_fwrite32le(fp, tmp->layer[i]->flags); } al_fwrite32le(fp, tmp->flags); return 1; }
static void pack_object(ALLEGRO_FILE *file, const void *object, size_t len) { /* First write the length of the object, so we know how big to make the slice when it is opened later. */ al_fwrite32le(file, len); al_fwrite(file, object, len); }
static boolean empty_output_buffer(j_compress_ptr cinfo) { struct my_dest_mgr *dest = (void *)cinfo->dest; al_fwrite(dest->fp, dest->buffer, BUFFER_SIZE); dest->pub.next_output_byte = dest->buffer; dest->pub.free_in_buffer = BUFFER_SIZE; return 1; }
int inf(ALLEGRO_FILE *source, ALLEGRO_FILE *dest) { int ret; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; /* allocate inflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); if (ret != Z_OK) return ret; /* decompress until deflate stream ends or end of file */ do { strm.avail_in = al_fread(source, in, CHUNK); if (al_ferror(source)) { (void)inflateEnd(&strm); return Z_ERRNO; } if (strm.avail_in == 0) break; strm.next_in = in; /* run inflate() on input until output buffer not full */ do { strm.avail_out = CHUNK; strm.next_out = out; ret = inflate(&strm, Z_NO_FLUSH); assert(ret != Z_STREAM_ERROR); /* state not clobbered */ switch (ret) { case Z_NEED_DICT: ret = Z_DATA_ERROR; /* and fall through */ case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd(&strm); return ret; } have = CHUNK - strm.avail_out; if (al_fwrite(dest, out, have) != have || al_ferror(dest)) { (void)inflateEnd(&strm); return Z_ERRNO; } } while (strm.avail_out == 0); /* done when inflate() says it's done */ } while (ret != Z_STREAM_END); /* clean up and return */ (void)inflateEnd(&strm); return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; }
int t3f_save_tileset_f(T3F_TILESET * tsp, ALLEGRO_FILE * fp) { int i, j; char header[16] = {0}; strcpy(header, "T3F_TILESET"); header[15] = 0; al_fwrite(fp, header, 16); /* write tile data */ al_fwrite16le(fp, tsp->tiles); for(i = 0; i < tsp->tiles; i++) { t3f_save_animation_f(tsp->tile[i]->ap, fp); al_fwrite32le(fp, tsp->tile[i]->flags); /* write user data */ if(tsp->tile[i]->flags & T3F_TILE_FLAG_USER_DATA) { for(j = 0; j < T3F_TILE_MAX_DATA; j++) { al_fwrite32le(fp, tsp->tile[i]->user_data[j]); } } /* write animation frames */ al_fwrite32le(fp, tsp->tile[i]->frame_list_total); for(j = 0; j < tsp->tile[i]->frame_list_total; j++) { al_fwrite16le(fp, tsp->tile[i]->frame_list[j]); } } /* write tileset data */ al_fwrite32le(fp, tsp->width); al_fwrite32le(fp, tsp->height); al_fwrite32le(fp, tsp->flags); return 1; }
bool t3f_save_vector_font_f(T3F_VECTOR_FONT * vfp, ALLEGRO_FILE * fp) { char header[16] = {'T', '3', 'F', 'V', 'F'}; int i; if(al_fwrite(fp, header, 16) != 16) { return false; } for(i = 0; i < T3F_VECTOR_FONT_MAX_CHARACTERS; i++) { if(vfp->character[i]) { al_fputc(fp, 1); t3f_save_vector_object_f(vfp->character[i]->object, fp); t3f_fwrite_float(fp, vfp->character[i]->width); } else { al_fputc(fp, 0); } } return true; }
static void mixer_pp_callback(void *buf, unsigned int samples, void *userdata) { ALLEGRO_MIXER *mixer = (ALLEGRO_MIXER *)userdata; int nch; int sample_size; if (!saving) return; switch (al_get_mixer_channels(mixer)) { case ALLEGRO_CHANNEL_CONF_1: nch = 1; break; case ALLEGRO_CHANNEL_CONF_2: nch = 2; break; default: /* Not supported. */ return; } sample_size = al_get_audio_depth_size(al_get_mixer_depth(mixer)); al_fwrite(save_fp, buf, nch * samples * sample_size); }
static void term_destination(j_compress_ptr cinfo) { struct my_dest_mgr *dest = (void *)cinfo->dest; al_fwrite(dest->fp, dest->buffer, BUFFER_SIZE - dest->pub.free_in_buffer); }
void mapSaver::writeData(ALLEGRO_FILE* fileStream) { unsigned int offset_settings = 0; unsigned int offset_collision = 0; unsigned int offset_tile = 0; unsigned int offset_background = 0; //Reserve room for the header for (unsigned int i=0; i<4*sizeof(unsigned int); i++) { al_fputc(fileStream,0); } offset_settings = al_ftell(fileStream); //Write map dimensions al_fwrite(fileStream,&_mData.left,sizeof(int)); //Write position on map al_fwrite(fileStream,&_mData.top,sizeof(int)); //Write position on map al_fwrite(fileStream,&_mData.right,sizeof(int)); //Write position on map al_fwrite(fileStream,&_mData.bottom,sizeof(int)); //Write position on map offset_collision = al_ftell(fileStream); int collisionLineList = _mData.getCollisionLineCount(); al_fwrite(fileStream,&collisionLineList, sizeof(int)); //Write number of collision lines for (collisionLineListNode* i = _mData.getRootCollisionLine(); i!=NULL; i=i->next) { //Iterate through all the lines int buffer; buffer = i->line->p1.x; al_fwrite(fileStream,&buffer,sizeof(int)); //Line beginning buffer = i->line->p1.y; al_fwrite(fileStream,&buffer,sizeof(int)); //Line beginning buffer = i->line->p2.x; al_fwrite(fileStream,&buffer,sizeof(int)); //Line end buffer = i->line->p2.y; al_fwrite(fileStream,&buffer,sizeof(int)); //line end al_fwrite(fileStream,&i->line->jumpthrough,sizeof(bool)); //jumpthrough? } offset_tile = al_ftell(fileStream); int layerList = _mData.tileLayers.size(); al_fwrite(fileStream,&layerList, sizeof(int)); //Write number of tile Layers for (unsigned int i=0; i<_mData.tileLayers.size(); i++) { int layoutList = _mData.tileLayers[i]->layouts.size(); al_fwrite(fileStream, &layoutList, sizeof(int)); //Write number of layouts. for (unsigned int j=0; j<layoutList; j++) { int tileList = _mData.tileLayers[i]->layouts[j]->tilePositions.size(); al_fwrite(fileStream,&_mData.tileLayers[i]->layouts[j]->tilesetData->tilesetID, sizeof(short)); //Write ID of tileset al_fwrite(fileStream,&tileList, sizeof(int)); //Number of tiles for (unsigned int c=0; c<tileList; c++) { al_fwrite(fileStream,&_mData.tileLayers[i]->layouts[j]->tilePositions[c]->mapX,sizeof(int)); //Write position on map al_fwrite(fileStream,&_mData.tileLayers[i]->layouts[j]->tilePositions[c]->mapY,sizeof(int)); //Write position on map al_fwrite(fileStream,&_mData.tileLayers[i]->layouts[j]->tilePositions[c]->tileSetX,sizeof(short)); //Write position on tileset (pos*width) al_fwrite(fileStream,&_mData.tileLayers[i]->layouts[j]->tilePositions[c]->tileSetY,sizeof(short)); //Write position on tileset (pos*height) } } } offset_background = al_ftell(fileStream); //Background layers int LayerCount = _mData.backgroundLayers.size(); al_fwrite(fileStream,&LayerCount, sizeof(int)); //Write number of background layers for (int i=0; i<_mData.backgroundLayers.size(); i++) { int buffer; buffer = _mData.backgroundLayers[i]->bgHandle->id; al_fwrite(fileStream,&buffer,sizeof(int)); //BG ID buffer = _mData.backgroundLayers[i]->xPos; al_fwrite(fileStream,&buffer,sizeof(int)); //x position buffer = _mData.backgroundLayers[i]->yPos; al_fwrite(fileStream,&buffer,sizeof(int)); //Y position } al_fseek(fileStream,0,ALLEGRO_SEEK_SET); al_fwrite(fileStream,&offset_settings,sizeof(int)); //Write position on map al_fwrite(fileStream,&offset_collision,sizeof(int)); //Write position on map al_fwrite(fileStream,&offset_tile,sizeof(int)); //Write position on map al_fwrite(fileStream,&offset_background,sizeof(int)); //Write position on map }
int main(int argc, char **argv) { ALLEGRO_AUDIO_RECORDER *r; ALLEGRO_AUDIO_STREAM *s; ALLEGRO_EVENT_QUEUE *q; ALLEGRO_DISPLAY *d; ALLEGRO_FILE *fp = NULL; ALLEGRO_PATH *tmp_path = NULL; int prev = 0; bool is_recording = false; int n = 0; /* number of samples written to disk */ (void) argc; (void) argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (!al_init_primitives_addon()) { abort_example("Unable to initialize primitives addon"); } if (!al_install_keyboard()) { abort_example("Unable to install keyboard"); } if (!al_install_audio()) { abort_example("Unable to initialize audio addon"); } if (!al_init_acodec_addon()) { abort_example("Unable to initialize acodec addon"); } /* Note: increasing the number of channels will break this demo. Other * settings can be changed by modifying the constants at the top of the * file. */ r = al_create_audio_recorder(1000, samples_per_fragment, frequency, audio_depth, ALLEGRO_CHANNEL_CONF_1); if (!r) { abort_example("Unable to create audio recorder"); } s = al_create_audio_stream(playback_fragment_count, playback_samples_per_fragment, frequency, audio_depth, ALLEGRO_CHANNEL_CONF_1); if (!s) { abort_example("Unable to create audio stream"); } al_reserve_samples(0); al_set_audio_stream_playing(s, false); al_attach_audio_stream_to_mixer(s, al_get_default_mixer()); q = al_create_event_queue(); /* Note: the following two options are referring to pixel samples, and have * nothing to do with audio samples. */ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); d = al_create_display(320, 256); if (!d) { abort_example("Error creating display\n"); } al_set_window_title(d, "SPACE to record. P to playback."); al_register_event_source(q, al_get_audio_recorder_event_source(r)); al_register_event_source(q, al_get_audio_stream_event_source(s)); al_register_event_source(q, al_get_display_event_source(d)); al_register_event_source(q, al_get_keyboard_event_source()); al_start_audio_recorder(r); while (true) { ALLEGRO_EVENT e; al_wait_for_event(q, &e); if (e.type == ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT) { /* We received an incoming fragment from the microphone. In this * example, the recorder is constantly recording even when we aren't * saving to disk. The display is updated every time a new fragment * comes in, because it makes things more simple. If the fragments * are coming in faster than we can update the screen, then it will be * a problem. */ ALLEGRO_AUDIO_RECORDER_EVENT *re = al_get_audio_recorder_event(&e); audio_buffer_t input = (audio_buffer_t) re->buffer; int sample_count = re->samples; const int R = sample_count / 320; int i, gain = 0; /* Calculate the volume, and display it regardless if we are actively * recording to disk. */ for (i = 0; i < sample_count; ++i) { if (gain < abs(input[i] - sample_center)) gain = abs(input[i] - sample_center); } al_clear_to_color(al_map_rgb(0,0,0)); if (is_recording) { /* Save raw bytes to disk. Assumes everything is written * succesfully. */ if (fp && n < frequency / (float) samples_per_fragment * max_seconds_to_record) { al_fwrite(fp, input, sample_count * sample_size); ++n; } /* Draw a pathetic visualization. It draws exactly one fragment * per frame. This means the visualization is dependent on the * various parameters. A more thorough implementation would use this * event to copy the new data into a circular buffer that holds a * few seconds of audio. The graphics routine could then always * draw that last second of audio, which would cause the * visualization to appear constant across all different settings. */ for (i = 0; i < 320; ++i) { int j, c = 0; /* Take the average of R samples so it fits on the screen */ for (j = i * R; j < i * R + R && j < sample_count; ++j) { c += input[j] - sample_center; } c /= R; /* Draws a line from the previous sample point to the next */ al_draw_line(i - 1, 128 + ((prev - min_sample_val) / (float) sample_range) * 256 - 128, i, 128 + ((c - min_sample_val) / (float) sample_range) * 256 - 128, al_map_rgb(255,255,255), 1.2); prev = c; } } /* draw volume bar */ al_draw_filled_rectangle((gain / (float) max_sample_val) * 320, 251, 0, 256, al_map_rgba(0, 255, 0, 128)); al_flip_display(); } else if (e.type == ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT) { /* This event is received when we are playing back the audio clip. * See ex_saw.c for an example dedicated to playing streams. */ if (fp) { audio_buffer_t output = al_get_audio_stream_fragment(s); if (output) { /* Fill the buffer from the data we have recorded into the file. * If an error occurs (or end of file) then silence out the * remainder of the buffer and stop the playback. */ const size_t bytes_to_read = playback_samples_per_fragment * sample_size; size_t bytes_read = 0, i; do { bytes_read += al_fread(fp, (uint8_t *)output + bytes_read, bytes_to_read - bytes_read); } while (bytes_read < bytes_to_read && !al_feof(fp) && !al_ferror(fp)); /* silence out unused part of buffer (end of file) */ for (i = bytes_read / sample_size; i < bytes_to_read / sample_size; ++i) { output[i] = sample_center; } al_set_audio_stream_fragment(s, output); if (al_ferror(fp) || al_feof(fp)) { al_drain_audio_stream(s); al_fclose(fp); fp = NULL; } } } } else if (e.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (e.type == ALLEGRO_EVENT_KEY_CHAR) { if (e.keyboard.unichar == 27) { /* pressed ESC */ break; } else if (e.keyboard.unichar == ' ') { if (!is_recording) { /* Start the recording */ is_recording = true; if (al_get_audio_stream_playing(s)) { al_drain_audio_stream(s); } /* Reuse the same temp file for all recordings */ if (!tmp_path) { fp = al_make_temp_file("alrecXXX.raw", &tmp_path); } else { if (fp) al_fclose(fp); fp = al_fopen(al_path_cstr(tmp_path, '/'), "w"); } n = 0; } else { is_recording = false; if (fp) { al_fclose(fp); fp = NULL; } } } else if (e.keyboard.unichar == 'p') { /* Play the previously recorded wav file */ if (!is_recording) { if (tmp_path) { fp = al_fopen(al_path_cstr(tmp_path, '/'), "r"); if (fp) { al_set_audio_stream_playing(s, true); } } } } } } /* clean up */ al_destroy_audio_recorder(r); al_destroy_audio_stream(s); if (fp) al_fclose(fp); if (tmp_path) { al_remove_filename(al_path_cstr(tmp_path, '/')); al_destroy_path(tmp_path); } return 0; }
/* * Decodes map data from a <data> node */ static void decode_layer_data(xmlNode *data_node, ALLEGRO_MAP_LAYER *layer) { char *str = g_strstrip((char *)data_node->children->content); int datalen = layer->width * layer->height; layer->data = (char *)calloc(datalen, sizeof(char)); char *encoding = get_xml_attribute(data_node, "encoding"); if (!encoding) { int i = 0; GSList *tiles = get_children_for_name(data_node, "tile"); GSList *tile_item = tiles; while (tile_item) { xmlNode *tile_node = (xmlNode*)tile_item->data; tile_item = g_slist_next(tile_item); char *gid = get_xml_attribute(tile_node, "gid"); layer->data[i] = atoi(gid); i++; } g_slist_free(tiles); } else if (!strcmp(encoding, "base64")) { // decompress gsize rawlen; unsigned char *rawdata = g_base64_decode(str, &rawlen); // check the compression char *compression = get_xml_attribute(data_node, "compression"); if (compression != NULL) { if (strcmp(compression, "zlib") && strcmp(compression, "gzip")) { fprintf(stderr, "Error: unknown compression format '%s'\n", compression); return; } // set up files used by zlib to decompress the data ALLEGRO_PATH *srcpath; ALLEGRO_FILE *datasrc = al_make_temp_file("XXXXXX", &srcpath); al_fwrite(datasrc, rawdata, rawlen); al_fseek(datasrc, 0, ALLEGRO_SEEK_SET); //al_fclose(datasrc); //datasrc = al_fopen(al_path_cstr(srcpath, ALLEGRO_NATIVE_PATH_SEP), "rb"); ALLEGRO_FILE *datadest = al_make_temp_file("XXXXXX", NULL); // decompress and print an error if it failed int status = inf(datasrc, datadest); if (status) zerr(status); // flush data and get the file length al_fflush(datadest); int len = al_fsize(datadest); // read in the file al_fseek(datadest, 0, ALLEGRO_SEEK_SET); char *data = (char *)calloc(len, sizeof(char)); if (al_fread(datadest, data, len) != len) { fprintf(stderr, "Error: failed to read in map data\n"); return; } // every tile id takes 4 bytes int i; for (i = 0; i<len; i += 4) { int tileid = 0; tileid |= data[i]; tileid |= data[i+1] << 8; tileid |= data[i+2] << 16; tileid |= data[i+3] << 24; layer->data[i/4] = tileid; } /* printf("layer dimensions: %dx%d, data length = %d\n", layer->width, layer->height, len); */ al_destroy_path(srcpath); al_fclose(datasrc); al_fclose(datadest); al_free(data); } else { // TODO: verify that this still works int i; for (i = 0; i<rawlen; i += 4) { int tileid = 0; tileid |= rawdata[i]; tileid |= rawdata[i+1] << 8; tileid |= rawdata[i+2] << 16; tileid |= rawdata[i+3] << 24; layer->data[i/4] = tileid; } } g_free(rawdata); } else if (!strcmp(encoding, "csv")) { int i; for (i = 0; i<datalen; i++) { char *id = strtok((i == 0 ? str : NULL), ","); layer->data[i] = atoi(id); } } else { fprintf(stderr, "Error: unknown encoding format '%s'\n", encoding); } }
/* write_data: * Custom write function to use Allegro packfile routines, * rather than C streams. */ static void write_data(png_structp png_ptr, png_bytep data, png_uint_32 length) { ALLEGRO_FILE *f = (ALLEGRO_FILE *)png_get_io_ptr(png_ptr); if ((png_uint_32) al_fwrite(f, data, length) != length) png_error(png_ptr, "write error (loadpng calling al_fs_entry_write)"); }