I am new to Rust and I'm working on a game with the Piston engine to wet my feet.
I want to render a bunch of entities using sprite sheets, but a lot of entities might share a sprite sheet so I would like to only load and store one copy of each file.
In pseudo code, my approach is basically like this:
fn get_spritesheet(hashmap_cache, file):
if file not in hashmap_cache:
hashmap_cache[file] = load_image_file(file)
return hashmap_cache[file]
Then maybe something like:
//These 'sprite' fileds point to the same image in memory
let player1 = Entity { sprite: get_spritesheet(cache, "player.png") };
let player2 = Entity { sprite: get_spritesheet(cache, "player.png") };
However, I am running in to a lot of barriers with Rust's ownership system (probably because I just don't understand it).
As far as I can tell, I want the cahe/hashmap to "own" the image resources. Specifically, then, returning references (as in the get_spritesheet
function) seems to be weird. Also, is it possible for a struct to not own all its members? I think it is but I was confused about how to do that.