Advertisement

What's the best way to store and manage my texture assets in code ?

Started by April 02, 2022 03:40 AM
2 comments, last by hplus0603 2 years, 7 months ago

Let's imagine I have three textures, Tank Bottom, Tank top, and Bullet

What's the best way to handle all of them ?

The best way I've thought of is creating an Array and IDs like this

namespace TextureID

{

static enum {

Tank = 0, Bullet = 2, // 0 is tank bottom, 1 is tank top

Last = Bullet // To keep up with the number of the Textures

};

}

static Texture GameTextures[TextureID::Last];

And when rendering

Draw(GameTextures[TextureID::Tank]);

Draw(GameTextures[TextureID::Tank + 1]);

Draw(GameTextures[TextureID::Bullet]);

It looks like a good way but to be honest I hardly have good ideas of how to handle things, Is it a good way ?

None

“best” is not a universally agreed concept. For example, the solution when you have a handful of textures is very different from when you have 50000 textures. There are also considerations like available tools that make certain assumptions, personal preferences, etc.

Last but not least, “best” is hard to find in the sea of possible solutions. Looking for 1 solution that is optimal in every possible way is near impossible. So instead, go with a solution that works for you, until it ceases to work satisfactory for some reason. Then find a different solution.

As for your suggested solution, if it works for you, use it.

I would make one change though. Avoid the hardcoded numbers and dependencies between numbers. In your current solution, if the tank offset changes, you now have to remember to change the bullet offset as well. You will forget to do that at some point or you will change it incorrectly, and it may cost a lot of time to find that mistake. So instead, let the computer do the counting rather than you, ie

 static enum {

TankTop,
TenkBottom,

Bullet,

NumberOfTextures // 1 more than your Last.
};

You didn't state the language you use, but most languages specify the number of elements that exist in an array, so your

static Texture GameTextures[TextureID::Last];

has then 2 entries (index 0 and index 1).

EDIT: “number of entries” (aka “length”) is always 1 more than the index of the last entry when you start counting from 0!

Advertisement

I've done something similar for code that compiles to WASM and runs in a browser. For very small things (Tank Wars, Space Invaders, etc) it works fine!

I would avoid having the caller do the indirect, though. If you need a texture, and there is a texture ID for each semantic texture in your game, just pass that in to the draw call. Let the engine figure out the indirection.

DrawSprite(TextureId::Tank, TankPosition, TankRotation);

As long as there's a small number of entities in your game, and you don't create a bunch of new levels with new entity kinds, this is fine.

Once you need to dynamically compose entities of pieces, you'd need to separate texture/material/geometry management into “get a reference to a thing by name, loading it if not already loaded” versus “draw this thing that is being referenced.” That's for bigger games, with more advanced art/editor pipelines, though!

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement