Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  (Slick2D) Managing Resources in game  (Read 668 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Junior Member


Projects: 1


Noob going through metamorphosis...


« Posted 2012-06-27 23:11:39 »

I am having a mental battle trying to decide the right course of action for managing resources in my game.  In building my game, I've only used the most abstract parts of Slick2D to do the rendering and left the rest of the game's design to myself.

Here was my first idea. For each level that is loaded, image and sound resources would be read and buffered.  While this is a convenient way of handling the process, I realize that it is terribly inefficient when considering that many levels reuse the exact same resources, background music in particular.  I figure that I can make a class that will handle the management of resources.  The manager will load resources on request and then cache them for later use.

Consider this:

Image lolCat = myGame.getResources().getImage("res/lolcat.png");

The next time I call getImage from the resource manager, the cached image will be returned rather than a new copy of the image.  While this is fine and dandy, I have my grievances with this method...  Each time that a resource is requested, the manager has to compare possibly hundreds of Strings to the one provided until a possible match is found.  If a match IS found, then we know, of course, that the resource is within the cache and can be reused.  If not, then the resource must be read from the file, cached, and returned.  The search could take a long time especially if the resource has not been loaded.

I also considered accessing particular resources through multiple getters within the manager.

For instance:

Image moonBackground = myGame.getResources().getBackground(Resources.BG_MOONWORLD);

would load a background specified by the manager itself.  This would prevent the manager from having to perform a time consuming search, as each time this particular method is called, it will only need to check the one resource for potential reuse.  This has the consequence of restricting my programming speed as each time I need to add a new element to the game, I must add it to the manager in order for it to be used efficiently.

Does anyone know of a more effective way to load images without having to create a large amount of String data and without restricting my programming speed?  Are either of these two methods I've suggested good programming practices?  I know there are tradeoffs, but I need to know the lesser of the two evils, or preferably, a third solution less evil than the first two...

Did you know that 90% of statistics are wrong?
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #1 - Posted 2012-06-27 23:19:40 »

I generally go:

1  
2  
3  
4  
5  
6  
7  
public class Resources {
      public static Image MOON_BACKGROUND;

      public static void init() throws IOException {
             MOON_BACKGROUND = new Image("moon_background.png");
      }
}


then just use Resources.MOON_BACKGROUND anywhere you feel like. No lookup no nothing. You can auto-generate the Resources class if you want to based on the contents of your res directory.

Cheers,

Kev

Offline DrewLols

Junior Member


Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2012-06-27 23:30:55 »

Say, thanks!  Seems that my second idea wasn't blasphemous after all.  The war is over...

Did you know that 90% of statistics are wrong?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (83 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.111 seconds with 21 queries.