Sign in to follow this  
ishpeck

Sprites vs. Memory Limits

Recommended Posts

ishpeck    154
I'm building a sprite indexer to handle all the visual assets in my games. I'm pretty sure I won't come anywhere near the memory limits of the hardware I'm likely to run on but I still want to set a hard maximum of the total number of sprites my engine will load. How many sprites do you suppose a game like, say, Castlevania had in it? That ought to give me a good ballpark figure for workable memory limits.

Share this post


Link to post
Share on other sites
Captain P    1092
You may want to check memory consumption versus total available memory, and flush any unused images from your cache if you're running low on memory. Most games don't have all their sprites in use at the same time so that should be a workable strategy.

Setting a maximum number of sprites is likely not a smart thing to do, though, but using so-called 'memory budgets' for your levels would be a good thing. I would enforce it for the levels themselves, rather than in the engine, though: a tool that checks a levels sprite memory consumption shouldn't be hard to write, and it'll keep the engine itself a little more generic.

Share this post


Link to post
Share on other sites
freeworld    341
Quote:
Original post by ishpeck
Quote:
Original post by Captain P
Setting a maximum number of sprites is likely not a smart thing to do ...


Why not?


what happens when an asset requests a sprite resource that needs to be loaded, but because the limit has been reached the sprite is not loaded, what happens when the asset trys to use the sprite that wasn't loaded?

Share this post


Link to post
Share on other sites
implicit    504
Quote:
Original post by freeworld
Quote:
Original post by ishpeck
Quote:
Original post by Captain P
Setting a maximum number of sprites is likely not a smart thing to do ...


Why not?

what happens when an asset requests a sprite resource that needs to be loaded, but because the limit has been reached the sprite is not loaded, what happens when the asset trys to use the sprite that wasn't loaded?
Presumably the same way you'd deal with running out of memory if you hadn't set a fixed upper limit.

Share this post


Link to post
Share on other sites
theOcelot    498
Quote:
Original post by freeworld
Quote:
Original post by ishpeck
Quote:
Original post by Captain P
Setting a maximum number of sprites is likely not a smart thing to do ...


Why not?


what happens when an asset requests a sprite resource that needs to be loaded, but because the limit has been reached the sprite is not loaded, what happens when the asset trys to use the sprite that wasn't loaded?
Wouldn't you just raise an exception immediately on discovering that the sprite can't be loaded?

Share this post


Link to post
Share on other sites
ishpeck    154
Yeah, I'm not sure why an arbitrary upper limit set by me is any different than a memory limit. In the end, you've got to build your game in a way that will be able to work with either boundary.

In my current project, I can say with great confidence that I won't come anywhere near a hard limit of, say, 100 sprites so I'm not the least bit worried about bumping that limit.

I'm still curious how many images were used in other games. I figure if I take the number of sprites that were used in a heavily-animated game like one of the newer Castlevanias, I'll have more than enough room to operate in for my minimalist designs.

Share this post


Link to post
Share on other sites
Spodi    642
Probably the easiest approach to take while still being nice and flexible is just load all assets on-demand. If you need sprite X, and its not loaded, load it. Every time a sprite is used, set its last_used_time to the current time. Every so often (probably once every X seconds, along with every time an asset is loaded as long as the check is cheap enough), check to see if you need to free up any memory. If you do, just dispose of enough assets to put you back into the green, plus a bit more to give you a decent buffer. Piece of cake, assets are loaded on-demand (instead of all at once, giving you a longer initial load time), and you can use all of the memory you need.

Share this post


Link to post
Share on other sites
Captain P    1092
Quote:
Original post by ishpeck
Quote:
Original post by Captain P
Setting a maximum number of sprites is likely not a smart thing to do ...


Why not?

Sprites typically don't consume much memory: an image reference, a position, some other variables... say, 10-50 bytes per sprite. 1 thousand sprites would then occupy 10-50 KB. That's not really where you're going to save a lot of memory, so why bother capping that at the engine-level? It's an arbitrary limit that doesn't help much.

Enforce it in your levels, not in your engine. If you then decide to build a game that targets higher-end hardware, you don't have to remove or up the cap, you just put more content in your levels. And if you absolutely must hard-code such a limit, at least make it externally configurable.

Share this post


Link to post
Share on other sites
ishpeck    154
Quote:
Original post by Captain P
That's not really where you're going to save a lot of memory, so why bother capping that at the engine-level? It's an arbitrary limit that doesn't help much.


Right now, sprites are consuming the absolute most memory. This project is trying on many levels to enforce a spartan design philosophy.

Quote:
Original post by Captain P
Enforce it in your levels, not in your engine.


There really aren't "levels" per se. The Sprite Indexer loads all the sprites for the "level" anyway so enforcing it in the indexer is enforcing it for the "level."

Quote:
Original post by Captain P
If you then decide to build a game that targets higher-end hardware, you don't have to remove or up the cap,


I'm already targeting hardware that significantly out-classes my design. My limit will be way below the hardware's abilities.

Quote:
Original post by Captain P
And if you absolutely must hard-code such a limit, at least make it externally configurable.


Probably will be. Still needs a default value. And I still need to know generally where most games are likely to need it to be.

Share this post


Link to post
Share on other sites
Captain P    1092
I think we've been confusing each other with terminology, so to clarify, with 'sprite' I mean an image being displayed at a certain position, and any other properties it would need such as rotation, scale, etc. With 'image' I mean the actual bitmap or image data, as loaded from a file.

Image data take most memory, sprites are usually pretty light-weight. They do need to reference image data, of course, but multiple sprites can share the same image data.


Setting a limit to the number of images doesn't make much sense, as it has no correlation to memory consumption. Some images are larger than others. Some games use texture atlases, combining multiple smaller images into larger ones, so that's less images but a little more memory usage. One reason to keep the number of images down could be to reduce rendering state changes, depending on how you're rendering your sprites, but that's a performance issue, not really a memory issue.

Quote:
There really aren't "levels" per se. The Sprite Indexer loads all the sprites for the "level" anyway so enforcing it in the indexer is enforcing it for the "level."

What exactly is the purpose of this 'sprite indexer'? Does it check what images a level needs and does it load them into memory? Are you dealing with one big world/level, and loading all it's images up-front? Just asking for clarification, so we're not misunderstanding each other. :)

Quote:
Probably will be. Still needs a default value. And I still need to know generally where most games are likely to need it to be.

What do you need a default value for? Is there a technical reason for that?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this