characterImageCache* characterImageManager::cacheImage(std::string imageName, std::string dataName) { if (characterImageManager::cacheList.find(imageName)==characterImageManager::cacheList.end()) { //Cache was not previously generated, so generate it characterImageCache* newCache = new characterImageCache(); //int oldFlags = al_get_new_bitmap_flags(); //al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); newCache->image = al_load_bitmap_f(gameEngine::resources::graphics.openSubFile(imageName),".png"); //Load the image to cache //al_set_new_bitmap_flags(oldFlags); ALLEGRO_FILE * imageData = gameEngine::resources::graphics.openSubFile(dataName,false); al_fread(imageData,&newCache->imageOriginX,sizeof(unsigned short)); al_fread(imageData,&newCache->imageOriginY,sizeof(unsigned short)); al_fread(imageData,&newCache->imageRelativeX,sizeof(short)); al_fread(imageData,&newCache->imageRelativeY,sizeof(short)); al_fclose(imageData); newCache->usageCount = 1; //One person is now using it.. cacheList[imageName] = newCache; //Add the cache to the map to.. cache it :p //std::cout << "Loaded image and cached it for first time.." << std::endl; return newCache; }else{ //Cache already exists, so return the existing one //std::cout << "Using previously cached image..." << std::endl; characterImageManager::cacheList[imageName]->usageCount ++; //A new renderer is using this cache return characterImageManager::cacheList[imageName]; } }
static size_t slice_fread(ALLEGRO_FILE *f, void *ptr, size_t size) { SLICE_DATA *slice = al_get_file_userdata(f); if (!(slice->mode & SLICE_READ)) { /* no read permissions */ return 0; } if (!(slice->mode & SLICE_EXPANDABLE) && slice->pos + size > slice->size) { /* don't read past the buffer size if not expandable */ size = slice->size - slice->pos; } if (!size) { return 0; } else { /* unbuffered, read directly from parent file */ size_t b = al_fread(slice->fp, ptr, size); slice->pos += b; if (slice->pos > slice->size) slice->size = slice->pos; return b; } }
static boolean fill_input_buffer(j_decompress_ptr cinfo) { struct my_src_mgr *src = (void *)cinfo->src; src->pub.next_input_byte = src->buffer; src->pub.bytes_in_buffer = al_fread(src->fp, src->buffer, BUFFER_SIZE); return 1; }
bool _al_identify_png(ALLEGRO_FILE *f) { uint8_t x[8]; al_fread(f, x, 8); if (memcmp(x, "\x89PNG\r\n\x1a\n", 8) != 0) return false; return true; }
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *dptr) { AL_OV_DATA *ov = (AL_OV_DATA *)dptr; size_t ret = 0; ret = al_fread(ov->file, ptr, size*nmemb); return ret; }
static int read_callback(void *stream, unsigned char *ptr, int nbytes) { AL_OP_DATA *op = (AL_OP_DATA *)stream; size_t ret = 0; ret = al_fread(op->file, ptr, nbytes); return ret; }
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; }
static int check_if_png(ALLEGRO_FILE *fp) { unsigned char buf[PNG_BYTES_TO_CHECK]; ALLEGRO_ASSERT(fp); if (al_fread(fp, buf, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK) return 0; return (png_sig_cmp(buf, (png_size_t) 0, PNG_BYTES_TO_CHECK) == 0); }
bool _al_identify_jpg(ALLEGRO_FILE *f) { uint8_t x[4]; uint16_t y; y = al_fread16be(f); if (y != 0xffd8) return false; al_fseek(f, 6 - 2, ALLEGRO_SEEK_CUR); al_fread(f, x, 4); if (memcmp(x, "JFIF", 4) != 0) return false; return true; }
int main(int argc, const char *argv[]) { ALLEGRO_FILE *master, *slice; ALLEGRO_PATH *tmp_path; const char *first_string = "Hello, World!"; const char *second_string = "The quick brown fox jumps over the lazy dog."; char buffer[BUFFER_SIZE]; (void) argc, (void) argv; al_init(); master = al_make_temp_file("ex_file_slice_XXXX", &tmp_path); if (!master) { abort_example("Unable to create temporary file\n"); } /* Pack both strings into the master file. */ pack_object(master, first_string, strlen(first_string)); pack_object(master, second_string, strlen(second_string)); /* Seek back to the beginning of the file, as if we had just opened it */ al_fseek(master, 0, ALLEGRO_SEEK_SET); /* Loop through the main file, opening a slice for each object */ while ((slice = get_next_chunk(master))) { /* Note: While the slice is open, we must avoid using the master file! If you were dealing with packed images, this is where you would pass 'slice' to al_load_bitmap_f(). */ if (al_fsize(slice) < BUFFER_SIZE) { /* We could have used al_fgets(), but just to show that the file slice is constrained to the string object, we'll read the entire slice. */ al_fread(slice, buffer, al_fsize(slice)); buffer[al_fsize(slice)] = 0; printf("Chunk of size %d: '%s'\n", (int) al_fsize(slice), buffer); } /* The slice must be closed before the next slice is opened. Closing the slice will advanced the master file to the end of the slice. */ al_fclose(slice); } al_fclose(master); al_remove_filename(al_path_cstr(tmp_path, '/')); return 0; }
T3F_TILEMAP * t3f_load_tilemap_f(ALLEGRO_FILE * fp) { int i, j, k, w, h; T3F_TILEMAP * tmp; char header[16]; al_fread(fp, header, 16); if(strcmp(header, "T3F_TILEMAP")) { return NULL; } tmp = malloc(sizeof(T3F_TILEMAP)); if(!tmp) { return NULL; } switch(header[15]) { case 0: { tmp->layers = al_fread16le(fp); for(i = 0; i < tmp->layers; i++) { tmp->layer[i] = malloc(sizeof(T3F_TILEMAP_LAYER)); w = al_fread16le(fp); h = al_fread16le(fp); tmp->layer[i] = t3f_create_tilemap_layer(w, h); for(j = 0; j < tmp->layer[i]->height; j++) { for(k = 0; k < tmp->layer[i]->width; k++) { tmp->layer[i]->data[j][k] = al_fread16le(fp); } } tmp->layer[i]->x = t3f_fread_float(fp); tmp->layer[i]->y = t3f_fread_float(fp); tmp->layer[i]->z = t3f_fread_float(fp); tmp->layer[i]->scale = t3f_fread_float(fp); tmp->layer[i]->speed_x = t3f_fread_float(fp); tmp->layer[i]->speed_y = t3f_fread_float(fp); tmp->layer[i]->flags = al_fread32le(fp); } tmp->flags = al_fread32le(fp); break; } } return tmp; }
/* vector font IO */ T3F_VECTOR_FONT * t3f_load_vector_font_f(ALLEGRO_FILE * fp) { T3F_VECTOR_FONT * vfp = NULL; T3F_VECTOR_OBJECT * vp; float w; float max_y = 0.0; char header[16] = {0}; int i, j; if(al_fread(fp, header, 16) != 16) { return NULL; } if(stricmp(header, "T3FVF")) { return NULL; } vfp = t3f_create_vector_font(); if(!vfp) { return NULL; } for(i = 0; i < T3F_VECTOR_FONT_MAX_CHARACTERS; i++) { if(al_fgetc(fp)) { vp = t3f_load_vector_object_f(fp); w = t3f_fread_float(fp); t3f_add_vector_character(vfp, i, vp, w); for(j = 0; j < vp->segments; j++) { if(vp->segment[j]->point[0].y > max_y) { max_y = vp->segment[j]->point[0].y; } if(vp->segment[j]->point[1].y > max_y) { max_y = vp->segment[j]->point[1].y; } } } } vfp->height = max_y; return vfp; }
/* vector object IO */ T3F_VECTOR_OBJECT * t3f_load_vector_object_f(ALLEGRO_FILE * fp) { T3F_VECTOR_OBJECT * vp = NULL; T3F_VECTOR_SEGMENT segment; int r, g, b, a; char header[16] = {0}; int i; int segments = 0; if(al_fread(fp, header, 16) != 16) { return NULL; } if(stricmp(header, "T3FV")) { return NULL; } vp = t3f_create_vector_object(); if(!vp) { return NULL; } segments = al_fread32le(fp); for(i = 0; i < segments; i++) { segment.point[0].x = t3f_fread_float(fp); segment.point[0].y = t3f_fread_float(fp); segment.point[0].z = t3f_fread_float(fp); segment.point[1].x = t3f_fread_float(fp); segment.point[1].y = t3f_fread_float(fp); segment.point[1].z = t3f_fread_float(fp); r = al_fgetc(fp); g = al_fgetc(fp); b = al_fgetc(fp); a = al_fgetc(fp); segment.color = al_map_rgba(r, g, b, a); segment.thickness = t3f_fread_float(fp); if(!t3f_add_vector_segment(vp, segment.point[0].x, segment.point[0].y, segment.point[0].z, segment.point[1].x, segment.point[1].y, segment.point[1].z, segment.color, segment.thickness)) { return NULL; } } return vp; }
void CDisplay::ReadFile() { ALLEGRO_FILE* file; file = al_fopen("C:\\Users\\Chase\\Documents\\Visual Studio 2010\\Projects\\Tetris\\scores.scr", "rb"); if(!file) printf("File scores.scr could not be opened\n"); numHighScores = al_fsize(file) / 8; highScores = new char*[numHighScores]; for(int i = 0; i < numHighScores; i++) { long* temp = new long; al_fread(file, temp, 8); highScores[i] = ltoa(*temp, new char[16], 10); } al_fclose(file); printf("File read from, scores: %d\n",numHighScores); }
/* * 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); } }
static AL_VOC_DATA *voc_open(ALLEGRO_FILE *fp) { AL_VOC_DATA *vocdata; char hdrbuf[0x16]; size_t readcount = 0; uint8_t blocktype = 0; uint8_t x = 0; uint16_t timeconstant = 0; uint16_t format = 0; // can be 16-bit in Blocktype 9 (voc version > 1.20 uint16_t vocversion = 0; uint16_t checkver = 0; // must be 1's complement of vocversion + 0x1234 uint32_t blocklength = 0; //length is stored in 3 bites LE, gd byteorder. if (!fp) { ALLEGRO_WARN("voc_open: Failed opening ALLEGRO_FILE"); return NULL; } /* init VOC data */ vocdata = al_malloc(sizeof(AL_VOC_DATA)); memset(vocdata, 0, sizeof(*vocdata)); memset(hdrbuf, 0, sizeof(hdrbuf)); vocdata->file = fp; /* Begin checking the Header info */ readcount = al_fread(fp, hdrbuf, 0x16); if (readcount != 0x16 /*shorter header*/ || !memcmp(hdrbuf, "Creative Voice File\0x1A", 0x14) /*wrong id */ || !memcmp(hdrbuf+0x15 , "\0x00\0x1A", 0x2)) { /*wrong offset */ ALLEGRO_WARN("voc_open: File does not appear to be a valid VOC file"); return NULL; } al_fread(fp, &vocversion, 2); if (vocversion != 0x10A && vocversion != 0x114) { // known ver 1.10 -1.20 ALLEGRO_WARN("voc_open: File is of unknown version"); return NULL; } /* checksum version check */ al_fread(fp, &checkver, 2); if (checkver != ~vocversion + 0x1234) { ALLEGRO_WARN("voc_open: Bad VOC Version Identification Number"); return NULL; } /* * We're at the first datablock, we shall check type and set all the relevant * info in the vocdata structure, including finally the datapos index to the * first valid data byte. */ READNBYTES(fp, blocktype, 1, NULL); READNBYTES(fp, blocklength, 2, NULL); READNBYTES(fp, x, 1, NULL); blocklength += x<<16; switch (blocktype) { case 1: /* blocktype 1 is the most basic header with 1byte format, time * constant and length equal to (datalength + 2). */ blocklength -= 2; READNBYTES(fp, timeconstant, 1, NULL); READNBYTES(fp, format, 1, NULL); vocdata->bits = 8; /* only possible codec for Blocktype 1 */ vocdata->channels = 1; /* block 1 alone means MONO */ vocdata->samplerate = 1000000 / (256 - timeconstant); vocdata->sample_size = 1; /* or better: 1 * 8 / 8 */ /* * Expected number of samples is at LEAST what is in this block. * IIF lentgh 0xFFF there will be a following blocktype 2. * We will deal with this later in load_voc. */ vocdata->samples = blocklength / vocdata->sample_size; vocdata->datapos = al_ftell(fp); break; case 8: /* Blocktype 8 is enhanced data block (mainly for Stereo samples I * guess) that precedes a Blocktype 1, of which the sound infor have * to be ignored. * We skip to the end of the following Blocktype 1 once we get all the * required header info. */ if (blocklength != 4) { ALLEGRO_WARN("voc_open: Got opening Blocktype 8 of wrong length"); return NULL; } READNBYTES(fp, timeconstant, 2, NULL); READNBYTES(fp, format, 1, NULL); READNBYTES(fp, vocdata->channels, 1, NULL); vocdata->channels += 1; /* was 0 for mono, 1 for stereo */ vocdata->bits = 8; /* only possible codec for Blocktype 8 */ vocdata->samplerate = 1000000 / (256 - timeconstant); vocdata->samplerate /= vocdata->channels; vocdata->sample_size = vocdata->channels * vocdata->bits / 8; /* * Now following there is a blocktype 1 which tells us the length of * the data block and all other info are discarded. */ READNBYTES(fp, blocktype, 1, NULL); if (blocktype != 1) { ALLEGRO_WARN("voc_open: Blocktype following type 8 is not 1"); return NULL; } READNBYTES(fp, blocklength, 2, NULL); READNBYTES(fp, x, 1, NULL); blocklength += x<<16; blocklength -= 2; READNBYTES(fp, x, 2, NULL); vocdata->samples = blocklength / vocdata->sample_size; vocdata->datapos = al_ftell(fp); break; case 9: /* * Blocktype 9 is available only for VOC version 1.20 and above. * Deals with 16-bit codecs and stereo and is richier than blocktype 1 * or the BLocktype 8+1 combo * Length is 12 bytes more than actual data. */ blocklength -= 12; READNBYTES(fp, vocdata->samplerate, 4, NULL); // actual samplerate READNBYTES(fp, vocdata->bits, 1, NULL); // actual bits // after compression READNBYTES(fp, vocdata->channels, 1, NULL); // actual channels READNBYTES(fp, format, 2, NULL); if ((vocdata->bits != 8 && vocdata->bits != 16) || (format != 0 && format != 4)) { ALLEGRO_WARN("voc_open: unsupported CODEC in voc data"); return NULL; } READNBYTES(fp, x, 4, NULL); // just skip 4 reserved bytes vocdata->datapos = al_ftell(fp); break; case 2: // case 3: // these cases are just case 4: // ignored in this span case 5: // and wll not return a case 6: // valid VOC data. case 7: // default: ALLEGRO_WARN("voc_open: opening Block is of unsupported type"); return NULL; break; } return vocdata; }
ALLEGRO_SAMPLE *_al_load_voc_f(ALLEGRO_FILE *file) { AL_VOC_DATA *vocdata; ALLEGRO_SAMPLE *sample = NULL; size_t pos = 0; /* where to write in the buffer */ size_t read = 0; /*bytes read during last operation */ char* buffer; size_t bytestoread = 0; bool endofvoc = false; vocdata = al_malloc(sizeof(AL_VOC_DATA)); memset(vocdata, 0, sizeof(*vocdata)); /* * Open file and populate VOC DATA, then create a buffer for the number of * samples of the frst block. * Iterate on the following blocks till EOF or terminator block */ vocdata = voc_open(file); if (!vocdata) return NULL; ALLEGRO_DEBUG("channels %d\n", vocdata->channels); ALLEGRO_DEBUG("word_size %d\n", vocdata->sample_size); ALLEGRO_DEBUG("rate %d\n", vocdata->samplerate); ALLEGRO_DEBUG("first_block_samples %d\n", vocdata->samples); ALLEGRO_DEBUG("first_block_size %d\n", vocdata->samples * vocdata->sample_size); /* * Let's allocate at least the first block's bytes; */ buffer = al_malloc(vocdata->samples * vocdata->sample_size); if (!buffer) { return NULL; } /* * We now need to iterate over data blocks till either we hit end of file * or we find a terminator block. */ bytestoread = vocdata->samples * vocdata->sample_size; while(!endofvoc && !al_feof(vocdata->file)) { uint32_t blocktype = 0; uint32_t x = 0, len = 0; read = al_fread(vocdata->file, buffer, bytestoread); pos += read; READNBYTES(vocdata->file, blocktype, 1, NULL); // read next block type if (al_feof(vocdata->file)) break; switch (blocktype) { case 0:{ /* we found a terminator block */ endofvoc = true; break; } case 2:{ /*we found a continuation block: unlikely but handled */ x = 0; bytestoread = 0; READNBYTES(vocdata->file, bytestoread, 2, NULL); READNBYTES(vocdata->file, x, 1, NULL); bytestoread += x<<16; /* increase subsequently storage */ buffer = al_realloc(buffer, sizeof(buffer) + bytestoread); break; } case 1: // we found a NEW data block starter, I assume this is wrong case 8: // and let the so far read sample data correctly in the case 9:{ // already allocated buffer. endofvoc = true; break; } case 3: /* we found a pause block */ case 4: /* we found a marker block */ case 5: /* we found an ASCII c-string block */ case 6: /* we found a repeat block */ case 7:{ /* we found an end repeat block */ /* all these blocks will be skipped */ unsigned int ii; len = 0; x = 0; READNBYTES(vocdata->file, len, 2, NULL); READNBYTES(vocdata->file, x, 1, NULL); len += x<<16; // this is the length what's left to skip */ for (ii = 0; ii < len ; ++ii) { al_fgetc(vocdata->file); } bytestoread = 0; //should let safely check for the next block */ break; } default: break; } } sample = al_create_sample(buffer, pos, vocdata->samplerate, _al_word_size_to_depth_conf(vocdata->sample_size), _al_count_to_channel_conf(vocdata->channels), true); if (!sample) al_free(buffer); voc_close(vocdata); return sample; }
T3F_ANIMATION * t3f_load_animation_f(ALLEGRO_FILE * fp, const char * fn) { T3F_ANIMATION * ap; int i; char header[12] = {0}; int ver; int fpos = 0; ALLEGRO_STATE old_state; ALLEGRO_BITMAP * bp; al_fread(fp, header, 12); ver = check_header(header); if(ver < 0) { return NULL; } ap = t3f_create_animation(); if(ap) { switch(ver) { case 0: { ap->bitmaps->count = al_fread16le(fp); for(i = 0; i < ap->bitmaps->count; i++) { ap->bitmaps->bitmap[i] = t3f_load_resource_f((void **)(&ap->bitmaps->bitmap[i]), T3F_RESOURCE_TYPE_BITMAP, fp, fn, 1, 0); } ap->frames = al_fread16le(fp); for(i = 0; i < ap->frames; i++) { ap->frame[i] = al_malloc(sizeof(T3F_ANIMATION_FRAME)); if(!ap->frame[i]) { return NULL; } ap->frame[i]->bitmap = al_fread16le(fp); ap->frame[i]->x = t3f_fread_float(fp); ap->frame[i]->y = t3f_fread_float(fp); ap->frame[i]->z = t3f_fread_float(fp); ap->frame[i]->width = t3f_fread_float(fp); ap->frame[i]->height = t3f_fread_float(fp); ap->frame[i]->angle = t3f_fread_float(fp); ap->frame[i]->ticks = al_fread32le(fp); ap->frame[i]->flags = al_fread32le(fp); } ap->flags = al_fread32le(fp); break; } case 1: { ap->bitmaps->count = al_fread16le(fp); for(i = 0; i < ap->bitmaps->count; i++) { fpos = al_ftell(fp); ap->bitmaps->bitmap[i] = t3f_load_resource_f((void **)(&ap->bitmaps->bitmap[i]), T3F_RESOURCE_TYPE_BITMAP, fp, fn, 0, 0); if(!ap->bitmaps->bitmap[i]) { al_fseek(fp, fpos, ALLEGRO_SEEK_SET); al_store_state(&old_state, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); bp = t3f_load_bitmap_f(fp); al_restore_state(&old_state); if(bp) { ap->bitmaps->bitmap[i] = t3f_squeeze_bitmap(bp, NULL, NULL); al_destroy_bitmap(bp); } } else if(al_get_bitmap_flags(ap->bitmaps->bitmap[i]) & ALLEGRO_MEMORY_BITMAP) { bp = t3f_squeeze_bitmap(ap->bitmaps->bitmap[i], NULL, NULL); al_destroy_bitmap(ap->bitmaps->bitmap[i]); ap->bitmaps->bitmap[i] = bp; } if(!ap->bitmaps->bitmap[i]) { return NULL; } } ap->frames = al_fread16le(fp); for(i = 0; i < ap->frames; i++) { ap->frame[i] = al_malloc(sizeof(T3F_ANIMATION_FRAME)); if(!ap->frame[i]) { return NULL; } ap->frame[i]->bitmap = al_fread16le(fp); ap->frame[i]->x = t3f_fread_float(fp); ap->frame[i]->y = t3f_fread_float(fp); ap->frame[i]->z = t3f_fread_float(fp); ap->frame[i]->width = t3f_fread_float(fp); ap->frame[i]->height = t3f_fread_float(fp); ap->frame[i]->angle = t3f_fread_float(fp); ap->frame[i]->ticks = al_fread32le(fp); ap->frame[i]->flags = al_fread32le(fp); } ap->flags = al_fread32le(fp); break; } } } t3f_animation_build_frame_list(ap); return ap; }
static long dfs_getnc(char *ptr, long n, void *f) { return al_fread(f, ptr, n); }
T3F_TILESET * t3f_load_tileset_f(ALLEGRO_FILE * fp, const char * fn) { int i, j; T3F_TILESET * tsp; char header[16]; tsp = t3f_create_tileset(0, 0); if(!tsp) { return NULL; } al_fread(fp, header, 16); if(strcmp(header, "T3F_TILESET")) { t3f_destroy_tileset(tsp); return NULL; } switch(header[15]) { case 0: { /* read tile data */ tsp->tiles = al_fread16le(fp); for(i = 0; i < tsp->tiles; i++) { tsp->tile[i] = t3f_create_tile(); tsp->tile[i]->ap = t3f_load_animation_f(fp, fn); if(!tsp->tile[i]->ap) { printf("load animation failed\n"); return NULL; } tsp->tile[i]->flags = al_fread32le(fp); /* read user data */ if(tsp->tile[i]->flags & T3F_TILE_FLAG_USER_DATA) { for(j = 0; j < T3F_TILE_MAX_DATA; j++) { tsp->tile[i]->user_data[j] = al_fread32le(fp); } } /* read animation frames */ tsp->tile[i]->frame_list_total = al_fread32le(fp); for(j = 0; j < tsp->tile[i]->frame_list_total; j++) { tsp->tile[i]->frame_list[j] = al_fread16le(fp); } } /* read tileset data */ tsp->width = al_fread32le(fp); tsp->height = al_fread32le(fp); tsp->flags = al_fread32le(fp); break; } } return tsp; }
static dumb_ssize_t dfs_getnc(char *ptr, _al_dumb_size_t n, void *f) { return al_fread(f, ptr, n); }
DWORD CALLBACK read(void *buf, DWORD length, void *user) { ALLEGRO_FILE *f = (ALLEGRO_FILE *)user; DWORD r = al_fread(f, buf, length); return r; }
void mapReader::readData(ALLEGRO_FILE* fileStream){ //Read map dimensions al_fread(fileStream,&this->_mData->left,sizeof(int)); //Read start of line al_fread(fileStream,&this->_mData->top,sizeof(int)); //Read start of line al_fread(fileStream,&this->_mData->right,sizeof(int)); //Read start of line al_fread(fileStream,&this->_mData->bottom,sizeof(int)); //Read start of line int layerList; al_fread(fileStream,&layerList,sizeof(int)); //Read number of layers for (unsigned int i=0; i<layerList; i++) { tileLayer* newLayer = new tileLayer(); //Create the layer int layoutList; al_fread(fileStream,&layoutList,sizeof(int)); //Read number of layouts for (unsigned int j=0; j <layoutList; j++) { tileLayout* newLayout = new tileLayout(); short tileSetID; al_fread(fileStream,&tileSetID,sizeof(short)); //Read ID of tileset if (!(options & SKIPTILES)) { newLayout->tilesetData = tilesetManager::loadTileSet(tileSetID); //Load the tileset } int tileList; al_fread(fileStream,&tileList,sizeof(int)); //Read number of tiles for (unsigned int c=0; c<tileList; c++) { tile* newTile = new tile(); al_fread(fileStream,&newTile->mapX,sizeof(int)); al_fread(fileStream,&newTile->mapY,sizeof(int)); al_fread(fileStream,&newTile->tileSetX,sizeof(short)); al_fread(fileStream,&newTile->tileSetY,sizeof(short)); newLayout->tilePositions.push_back(newTile); } newLayer->layouts.push_back(newLayout); } if (!(options & SKIPTILES)) { _mData->tileLayers.push_back(newLayer); }else{ delete newLayer; } } int collisionLineList; al_fread(fileStream,&collisionLineList,sizeof(int)); for (unsigned int i=0; i<collisionLineList; i++) { collisionLine* newCLine = new collisionLine(); int buffer; al_fread(fileStream,&buffer,sizeof(int)); //Read start of line newCLine->p1.x = buffer; al_fread(fileStream,&buffer,sizeof(int)); //Read start of line newCLine->p1.y = buffer; al_fread(fileStream,&buffer,sizeof(int)); //Read end of line newCLine->p2.x = buffer; al_fread(fileStream,&buffer,sizeof(int)); //Read end of line newCLine->p2.y = buffer; al_fread(fileStream,&newCLine->jumpthrough,sizeof(bool)); //jumpthrough platform? _mData->addCollisionLine(newCLine); } //Backgrounds int backgroundLayers; al_fread(fileStream,&backgroundLayers,sizeof(int)); for (unsigned int i=0; i<backgroundLayers; i++) { mapBackground* newBg = new mapBackground; int BGID; al_fread(fileStream,&BGID,sizeof(int)); //Read end of line al_fread(fileStream,&newBg->xPos,sizeof(int)); //Read end of line al_fread(fileStream,&newBg->yPos,sizeof(int)); //Read end of line if (!(options & SKIPBACKGROUNDS)) { newBg->bgHandle = backgroundManager::loadBackground(BGID); } if (!(options & SKIPBACKGROUNDS)) { _mData->backgroundLayers.push_back(newBg); }else{ delete newBg; } } }
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; }
/** Reads raw bytes into a buffer. @param dst destination buffer; it must have enough space to read the given data. @param size number of bytes to read. @return the number of bytes actually read. */ size_t read(void *dst, size_t size) { return al_fread(get(), dst, size); }
/* read_data: * Custom read function to use Allegro packfile routines, * rather than C streams (so we can read from datafiles!) */ static void read_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_fread(f, data, length) != length) png_error(png_ptr, "read error (loadpng calling al_fs_entry_read)"); }
void mapData::loadMapData(ALLEGRO_FILE* fileStream) { //Load all the tiles, blocks, etc unsigned int offset_tile = 0; unsigned int offset_background = 0; al_fseek(fileStream,2*sizeof(int),ALLEGRO_SEEK_SET); al_fread(fileStream,&offset_tile,sizeof(int)); al_fread(fileStream,&offset_background,sizeof(int)); al_fseek(fileStream,offset_tile,ALLEGRO_SEEK_SET); int layerList; al_fread(fileStream,&layerList,sizeof(int)); //Read number of layers for (unsigned int i=0; i<layerList; i++) { tileLayer* newLayer = new tileLayer(); //Create the layer int layoutList; al_fread(fileStream,&layoutList,sizeof(int)); //Read number of layouts for (unsigned int j=0; j <layoutList; j++) { tileLayout* newLayout = new tileLayout(); short tileSetID; al_fread(fileStream,&tileSetID,sizeof(short)); //Read ID of tileset newLayout->tilesetData = tilesetManager::loadTileSet(tileSetID); //Load the tileset int tileList; al_fread(fileStream,&tileList,sizeof(int)); //Read number of tiles for (unsigned int c=0; c<tileList; c++) { tile* newTile = new tile(); al_fread(fileStream,&newTile->mapX,sizeof(int)); al_fread(fileStream,&newTile->mapY,sizeof(int)); al_fread(fileStream,&newTile->tileSetX,sizeof(short)); al_fread(fileStream,&newTile->tileSetY,sizeof(short)); newLayout->tilePositions.push_back(newTile); } newLayer->layouts.push_back(newLayout); } tileLayers.push_back(newLayer); } al_fseek(fileStream,offset_background,ALLEGRO_SEEK_SET); //Backgrounds int backgroundLayerCount = 0; al_fread(fileStream,&backgroundLayerCount,sizeof(int)); for (unsigned int i=0; i<backgroundLayerCount; i++) { mapBackground* newBg = new mapBackground; int BGID; al_fread(fileStream,&BGID,sizeof(int)); //Read end of line al_fread(fileStream,&newBg->xPos,sizeof(int)); //Read end of line al_fread(fileStream,&newBg->yPos,sizeof(int)); //Read end of line newBg->bgHandle = backgroundManager::loadBackground(BGID); backgroundLayers.push_back(newBg); } this->::mapCollisionData::loadMapData(fileStream); }