So my question has 2 parts, here it goes. For the game I’m making I handle all textures and sounds in giant arrays and then access them with textures[index] and sounds[index]. Is this the best way to organize textures and sounds or is there another? Also I wanted to make a loading screen and was wondering how that would work since my resources take a couple seconds to load in (should the loading screen be it’s own game state? should loading occur on it’s own thread? how would the second thread work if I’m using OpenGL? etc…). These questions aren’t really covered in your standard java game development series on youtube, so I thought i’d ask here. Thanks!
For sounds and textures I would use something more flexible like an ArrayList or a HashMap so that I can vary the amount of textures and also (if I really need to) remove them and not ruin the whole thing.
For the first part of your question; Depends on how you apply the textures. I have a little loop I use when I want to load like-resources on the fly that may help you, it loads them into an arraylist and then assigns the one you’re using to a variable you can reference to.
for (int x = 0; x < SPRITE_ARRAY.size(); x++) {
if (SPRITE_ARRAY.get(x).getResourceReference().equals("res/sprites/"+file+".png")){
spriteId = x;
break;
}
if (x == SPRITE_ARRAY.size()-1){
SPRITE_ARRAY.add(new SpriteSheet("res/sprites/"+file+".png", 32, 32, 0));
spriteId = x;
}
}
Whats going on here:
It runs a forloop the size of the ArrayList, looking for a file that matches the file you’re looking for. If it exists, it assigns spriteId to whatever location it is in on the ArrayList, if it goes through the entire arraylist and fails to find any resources that match what you’re looking for, it loads the resource and then assigns spriteId to it. This allows on-the-fly file loading so you don’t have to load ALL the sprite sheets (or whatever resource you want) all at once, only the ones the game currently needs.
Later in your code, you can now use SPRITE_ARRAY[spriteId] to reference whatever resource you wanted to use.
Example of my pulling sprite 0,0 out of my spritesheet:
SPRITE_ARRAY.get(spriteId).getSprite(0,0)
I use this code for my sprite sheets (if the variable names weren’t obvious lol), basically all my NPCs and the player use multiple sprite sheets overlapping each other to build the character, the sprite sheets they use are sometimes shared. (like if 2 characters are wearing the same hat, for example) So this method is used so if the exact same sheet is needed on 2 different NPCs that are loaded, it won’t load it twice.
Hey thanks guys. I’ll definitely try the “on the fly” loading technique for my next game. However, I’ve kind of already started with the whole load everything at once technique, and really need guidance for the loading screen… I’m using LWJGL and OpenGL and I don’t know if I should do loading on another thread or just render at the same time that loading is taking place or what. When you guys do loading screens (if you do) how do you do them?
just display a splash screen, load all the resources, then cut to main menu. that should be fine if its only taking a couple of seconds to load.
Yeah, I would say this is the best option (and the one I plan to do in my game as well) if load times are under 5-10~ seconds.
You really only need completion bars, animations and extra info if your load time is long enough a player might start thinking the game locked up.
can be a different story if you have a lot of/massive textures and need to dynamically load/unload to avoid running out of memory. I cant imagine myself getting to that point in any hurry… my sprite sheets are still under 150kb
My entire /res/ folder is only 4mb :
Although I’m sure that will change when I change gears from coding my engine to making tons and tons of content. Right now all my images are pretty much a cross between “Bare minimum needed to code with” and “Stuff I made when I was bored”.
The Spritesheets from my example though will probably eventually be huge, it only has to worry about 13 of them (at only 128kb) right now, but in the future there will literally be hundreds.
But semi-back on topic; If your games entire resource usage is really tiny, honestly I would load the entire game all at once and never have to worry about it again. I’d only worry about using my method if you plan to have tons and tons of resources that will not all be needed at the same time. That’s why I use that system for my sprite sheets, I plan on having hundreds of pieces the characters can wear, but I can’t imagine more than 50~ being used all at once at any one given time.
Ok so… the whole loading screen thing still isn’t working out for me. As I’m using OpenGL, I run into “No OpenGL context found on this thread” exceptions. This is because I’m loading textures on a different thread than the one I’m rendering on. But if I don’t use a separate thread, how does loading occur while rendering is happening simultaneously? I just need a way to render at the same time loading is done in the background without running into this error. Sorry for it taking me this long to word it like that…
Maybe I should repost this question on the OpenGL discussion page?
You can consider doing texture streaming. Theagentd did a great job of explaining it in this article. Basically, you load up all textures with really low resolution. Then, if you are going to render the texture, you load the full resolution photo. You also limit the number of texture loads per frame so that you don’t stutter too bad. Anyways, you should really check it out because it’s a great explanation.
It may of changed due to the new rendering attributes in OpenGL 3.x on, but while I was using immediate rendering mode I didn’t have to much of a problem with loading textures at run time. ( I don’t mind taking a small take on these questions, though there is a huge chance I may be repeating what others have said… :clue: )
There is no “best” way to organize textures. I do mine in a simple array[] just because I feel that is the best way. However, the true answer is if you can find it within your code, it is decently done. An array does the job for me because having everything in a list makes life easier…
[quote]Also I wanted to make a loading screen and was wondering how that would work since my resources take a couple seconds to load in (should the loading screen be it’s own game state? should loading occur on it’s own thread? how would the second thread work if I’m using OpenGL? etc…).
[/quote]
Actually, unless you are loading a ridiculous amount of textures there is really no need to use more than one thread at a time to do it. The GPU is running on a separate thread already using your graphics card, so why would you need 3 threads.
I recommend that loading be done at the beginning. It can be done in its own state, though it is usually what is easier. Running it on a separate thread creates some boilerplate that should just be avoided. Really, you just want to keep it as simple as you can. Creating multiple threads makes things harder to keep track of and more difficult to keep at a decent speed.
If you run into problems using one thread, then look into other methods. Splitting things into states always makes things easier, but there is a time where you can go overboard in that too. As long as you know where things are and it is working, that is a good rule that you are doing something right.
[quote]When you guys do loading screens (if you do) how do you do them?
[/quote]
I do loading directly at the beginning of a game. Usually, people don’t like loading screens in the middle of a game unless the game is running custom maps alongside high end graphics. If you have a simple game, loading screens are discouraged unless you do one big load at the beginning. It is all about the size of the production, the smaller the production, the less loading screens you should show. Man, do I miss the days where systems didn’t have any loading screens at all…
Final Thoughts
KISS (Keep It Simple, Stupid), seriously. I mean, the only time you want to go into creating loading screens is if your assets are taking too long to load. Other than that, people should be striving to not make users wait as much as they can, instead of trying to load things in crazy ways just to make the time take longer. If you can’t avoid it, then you can’t avoid it. But the easier you keep loading assets, the better off you and your user base will be in the long run.