Page 1 of 2

Dynamic calculation of resource IDs

PostPosted: Jan 27, 2005 @ 7:09pm
by fzammetti
Hello all...

I frequently run into the situation where I have a series of resources, cells of animation for instance, that I need to load into an array. The resources might be named:

GFX_PLAYER_CELL_1
GFX_PLAYER_CELL_2

...and so on. I usually wind up writing code like this (this is for GapiDraw, but ignore that fact, it's still a generic question)...

pPlayer[0]->CreateSurface(NULL, hInst, GFX_PLAYER_CELL_1, TEXT("GRAPHICS"));
pPlayer[1]->CreateSurface(NULL, hInst, GFX_PLAYER_CELL_2, TEXT("GRAPHICS"));

...and so on for all the cells of animation. Here's my question... is there any way to dynamically get the resourceID (GFX_PLAYER_CELL_x) instead of hardcoding it?

I know I could find the resourceID of the first cell and just add one for each subsequent cell, but that will only work if the resources are numbered sequentially, which they might usually be, but I can't assume VC++ will do that, and I don't want to be renumbering them myself each time I add or change something.

Any ideas? Sure, they could be external files, then I could construct the filename dynamically no problem, and I may in fact do that later on, but for development it's usually easier if they are resources, then there's just one file to toss from desktop to PPC to test.

Thanks all!

PostPosted: Jan 27, 2005 @ 10:14pm
by dan.p
NOTE: I love discussing game design (especially if it's something within the realm of my understanding), and I ended up rambling on a little bit. Sorry. :)

I don't think there's a dynamic way of doing it. Maybe if you could somehow get access to all of the resources available, then go through them one by one (must like you would if they were files stored in a folder). However, you'd still have to know which cells go with which object. How else would you determine which cell goes with which object if you don't have a hardcoded ID? I personally prefer the hardcoded ID because I have a GraphicManager which keeps track of which graphics are being referenced, which ones need to be created, and which ones need to be destroyed. All graphics I need are retrieved via GraphicManager::Reference(Resource ID);

In any case.. the only "dynamic" solution I can think of would be to try and store the resource.h file as a resource itself, then parse it for #define GFX_* values.

It sounds like you have all of your animation frames (or cells as you call them) as seperate graphics. If this is the case, perhaps you could store the entire animation as a single graphic, then dynamically create the cells when it's loaded. This would at the very least dramatically cut down on your code that needs to be hardcoded. I have a Graphic and Animation class that does just that. Now instead of having to load 32 individual cells for a single animation, you only have to create a single graphic, then have an animation class handle all of the animation stuff for you.

Personally, I have all of the animations for an object stored in a single graphic image (walking, running, jumping, etc.). I then have a Graphic class which breaks it down into an array of cells. After that, I have individual Animation objects which encapsulate Graphic objects and control animations (there are several Animation objects per Graphic object). All of my Graphics are kept track of in a GraphicManager, and Animations are created via AnimationFactory.

PostPosted: Jan 27, 2005 @ 10:19pm
by fzammetti
Yep, that's one decent approach, the single "sheet" of images for all the cells.

I havne't done that in the past, at least not early on in development, just because it makes it a little harder to make changes and tweak the animation sequences, and that tends to happen a lot early on. That being said, I think I might go that route anyway, it's the best solution I can see.

Don't worry about rambling on by the way... I'm not exactly known for my brevity around here ;)

PostPosted: Jan 27, 2005 @ 10:39pm
by Andy

PostPosted: Jan 27, 2005 @ 10:47pm
by fzammetti

PostPosted: Jan 28, 2005 @ 10:02pm
by j.edwards

PostPosted: Jan 28, 2005 @ 10:12pm
by fzammetti

PostPosted: Jan 28, 2005 @ 10:33pm
by rcp

PostPosted: Jan 28, 2005 @ 10:59pm
by fzammetti

PostPosted: Jan 28, 2005 @ 11:12pm
by dan.p

PostPosted: Jan 28, 2005 @ 11:24pm
by fzammetti

PostPosted: Jan 29, 2005 @ 2:00am
by Dan East
You should make your compound image a tall, single-frame width image, which makes each frame a contiguous piece of memory. If the images are side by side in columns then there is a cache miss each scanline.

Dan East

PostPosted: Jan 29, 2005 @ 4:45am
by fzammetti
Thanks Dan, I wouldn't have realized that. When I change the code tomorrow to do the "blit with rects" I'll do that too.

PostPosted: Jan 30, 2005 @ 11:52pm
by dan.p

PostPosted: Jan 31, 2005 @ 5:25am
by fzammetti
I looked at it for a little while, but I decided against it, for much the same reason I originally had all the animation cells independant: easier to tweak things, especially during development. With something like VFS, I'd need to "rebuild" it when I made a change, and that's just extra work in my eyes. Especially since I have no plans to develop for anything but Win32 and PocketPC. I'm not saying it's a bad suggestion by any means, just pointing ut that I had considered it and ultimately rejected it.

In the interest of not having anyone expending more cycles on this debate than they have to, I want to say that I am actually rather happy with how things are being done now. I'm not by any stretch claiming it's the best way or anything, but it is working for me, and in fact better than before as a result of this discussion. I just didn't want anyone thinking I still had a problem here to be addressed, I no longer do :)

(Of course, feel free to continue to debate in a general kind of way all you like! Discussion is rarely a bad thing!)