Re: [AD] implementing seek

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Wed, Apr 7, 2010 at 1:01 AM, Peter Wang <novalazy@xxxxxxxxxx> wrote:
> On 2010-04-07, Matthew Leverton <meffer@xxxxxxxxxx> wrote:
>> ALLEGRO_FILE *f = fopen("images.blob", "rb");
>> ALLEGRO_BITMAP *bmp1 = al_load_bmp_f(f);
>> ALLEGRO_BITMAP *bmp2 = al_load_bmp_f(f);
>> ALLEGRO_BITMAP *bmp3 = al_load_bmp_f(f);
>
> No, I don't think so, because some formats are be arbitrarily long
> with no indication of the length until you hit the end.
>
I agree. But what about:

ALLEGRO_FILE *f = fopen("images.blob", "rb");
int pos = al_fread32le(f);
ALLEGRO_BITMAP *bmp1 = al_load_bmp_f(f);

al_fseek(f, pos, ALLEGRO_SEEK_SET);
pos += al_fread32le(f);
ALLEGRO_BITMAP *bmp2 = al_load_bmp_f(f);

al_fseek(f, pos, ALLEGRO_SEEK_SET);
pos += al_fread32le(f);
ALLEGRO_BITMAP *bmp3 = al_load_bmp_f(f);

where 'pos' is always pointing to the next position because the file
format stored the length?

The above still would not work with the GDI+ routines unless counter
measures are taken place. Again, this is because it uses a lot of
SEEK_SET calls. I suppose other loaders would have the same problems.

I can easily work around it, but I don't know if it's the best idea
for every loader to work around the same problems.

So one system-wide solution would be something like al_fopen_f or even
maybe more simply:

al_fseek(f, 1000);  // seeks to position 1000
al_set_fseek_origin(f, al_ftell(f));  // consider the current position
1000 to be 0.
al_fseek(f, 0);  // actually seeks to position 1000.

ALLEGRO_BITMAP *bmp2 = al_load_bmp_f(f); // guaranteed to work now

Basically it's just an offset that gets added to the offset value to
SEEK_SET. Now when loading functions want to seek from the "beginning"
of the file, the proper location is preserved.

With this, it's on the user to decide where the file stream begins, as
opposed to the loader code making assumptions.

>> Maybe I'm just over analyzing things, but it seems like people
>> wouldn't expect an image loader to fail due to it seeking backward on
>> a handle that only can seek forward.
>
> Probably.  Maybe we need a type of ALLEGRO_FILE that implements a memory
> buffer over the top of a seek-forward-only ALLEGRO_FILE.
>
Yes, I was thinking a memory buffer could be useful as well.

And again, I know I can solve the problems within the GDI+ loading
code, but it will be confusing if the behavior isn't consistent across
all image loaders and annoying if there's no simple way to work around
the problem.

--
Matthew Leverton




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/