Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 1192 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Member


Medals: 2
Projects: 1


Noob going through metamorphosis...


« Posted 2012-06-27 21: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?
Online kevglass

JGO Kernel


Medals: 153
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2012-06-27 21: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

Senior Member


Medals: 2
Projects: 1


Noob going through metamorphosis...


« Reply #2 - Posted 2012-06-27 21: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.

 

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

The first screenshot will be displayed as a thumbnail.

atombrot (27 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!