Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (784)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (858)
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 3230 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Devvie

Medals: 4
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?
Offline kevglass

« JGO Spiffy Duke »

Medals: 319
Projects: 25
Exp: 22 years

Coder, Trainee Pixel Artist, Game Reviewer

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

I generally go:

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.



Offline DrewLols

Senior Devvie

Medals: 4
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  

hadezbladez (1644 views)
2018-11-16 13:46:03

hadezbladez (662 views)
2018-11-16 13:41:33

hadezbladez (1641 views)
2018-11-16 13:35:35

hadezbladez (348 views)
2018-11-16 13:32:03

EgonOlsen (2698 views)
2018-06-10 19:43:48

EgonOlsen (2965 views)
2018-06-10 19:43:44

EgonOlsen (1654 views)
2018-06-10 19:43:20

DesertCoockie (2367 views)
2018-05-13 18:23:11

nelsongames (2281 views)
2018-04-24 18:15:36

nelsongames (2975 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 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‑
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!