Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
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  
  SpriteCache class  (Read 2291 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Member





« Posted 2008-01-22 18:24:03 »

Hi,

I got a design issue with my SpriteCache class.
It has a map to store Sprites. Sprites is the top object where all my sprites are extended from.

  private HashMap<String, Sprite> spriteCache;

and my method to retrieve or when none there add the sprite to the spriteCache:

  public Sprite getSprite(String name) {
    if (spriteCache.containsKey(name)) {
       return spriteCache.get(name);
    } else {
      logger.fine(name + " " + ERR_NOT_FOUND + " in spritesCache Making new one");
      Sprite sprite = new Sprite(resources.getAnim(name), new Pt(0,0));
      spriteCache.put(name, sprite);
      return sprite;
    }
  }

When I call this Function to create a Cursor Sprite for example I get a Sprite and a classCast exception
    Sprite curs = spriteCache.getSprite("Cursor");
    Cursor cursor = (Cursor) curs;

What is the best way to work around this problem?

Should I add Specific Sprites like Cursor to the cache before calling spriteCache.get
Should I insert a parameter of the sprite to be created?
something like spriteCache.getSprite("Cursor",curs.class)

This is my design:
http://www.kwbbz.be/Downloads/Main.jpg
Offline cylab

JGO Knight


Medals: 34



« Reply #1 - Posted 2008-01-22 23:25:10 »

Depends on what you want, but I would suggest to populate the cache with correctly typed instances at the game start. If you are really keen on using lazy initialization you can implement your spriteCache.getSprite("Cursor",Cursor.class) strategy using reflection, but keep in mind that you might get into a quandary when trying something like
1  
2  
3  
spriteCache.getSprite("Leo",RifleMan.class) // This "Leo" is created as a RifleMan, which is not a Tank
// (some other code)
spriteCache.getSprite("Leo",Tank.class) // This call will return an already created RifleMan, but not a new Tank


This can be considered as coding error, but you will at least have to check the type of the already cached Sprite and handle the type mismatch in some way.

Also I think you are overdesigning your Sprite hierarchie. A Sprite is a Sprite not much more, but it seems you are mixing sprites with entities. A Sprite is something graphics related and can easily be retrieved from a cache, an Entity is something in your game world that has different attributes like armor, damage status, behaviour, sound and visual appearance (the Sprite). Management and creation of Entities is quite different than the same for Sprites.

Mathias - I Know What [you] Did Last Summer!
Offline stef569

Junior Member





« Reply #2 - Posted 2008-01-23 02:37:01 »

1  
an Entity is something in your game world that has different attributes

Thanks that realy helped!
Your absolutely right Grin Every Entity has a Sprite. So I create Sprites inside the Entity
wich then can be updated(entity) and drawen(sprite) and there can be entitys with different code but the same Sprite that's why we cache them afterall!
I understand it a bit better now Smiley
Also removed the staticSprite everything on the screen is a sprite now.

Now i'm having another design problem lol.

I need the resources from the ResourceManager, and the spriteCache into the Entity class...
and I cannot make it a static class function because it containts another function
So I pass it from game, to playerManager, to Player(as on the uml).
But this realy looks bad. and directly is also not an option I think, it's not the way my game is setup... How would you get resources into the entity class?

I create these objects in my top class, and I can't create a new one because I only want to have 1 object.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Knight


Medals: 34



« Reply #3 - Posted 2008-01-23 10:37:10 »

Now i'm having another design problem lol.

I need the resources from the ResourceManager, and the spriteCache into the Entity class...
and I cannot make it a static class function because it containts another function
So I pass it from game, to playerManager, to Player(as on the uml).
But this realy looks bad. and directly is also not an option I think, it's not the way my game is setup... How would you get resources into the entity class?

I create these objects in my top class, and I can't create a new one because I only want to have 1 object.

This is a typical situation for a Singleton http://en.wikipedia.org/wiki/Singleton_pattern. Some people consider Singletons to be a lazy man's excuse for bad OO design, but I think otherwise (at least with things like caches, resource loaders etc.).

Mathias - I Know What [you] Did Last Summer!
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #4 - Posted 2008-01-23 11:29:48 »

This is a typical situation for a Singleton http://en.wikipedia.org/wiki/Singleton_pattern. Some people consider Singletons to be a lazy man's excuse for bad OO design, but I think otherwise (at least with things like caches, resource loaders etc.).
Bullcrap. A singleton would be a horrible solution to this - theres absolutely no reason why you should enforce a single cache or provide global access to it, other than introduce awkward bugs and behaviour for yourself in the long run.

stef569: Passing the resources and cache to each entity is perfectly reasonable and sensible. Your entities depend on them, so all you're doing is making the dependancy explicit instead of hiding it behind a global or similar.

Alternatively, have your game state (or whatever hold the entities) hold onto the resources and cache, and instead pass itself to the entity. Then provide methods in the game state which just delagate straight through as needed. Or provider a higher level interface (eg. GameState.makeSprite(String imageName)) which deals with the actual initialisation of sprites and their images.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cylab

JGO Knight


Medals: 34



« Reply #5 - Posted 2008-01-23 12:38:44 »

Bullcrap. A singleton would be a horrible solution to this - theres absolutely no reason why you should enforce a single cache or provide global access to it, other than introduce awkward bugs and behaviour for yourself in the long run.

As I said, there are strong opinions on this matter. I for myself find it passable to use a Singleton for resource management, others don't. I respect this opinion, even if I find a "Bullcrap" response slightly offensive Tongue.Some arguments agains Singletons can be found in the "Just Create One" article that can be found http://butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne.

Mathias - I Know What [you] Did Last Summer!
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #6 - Posted 2008-01-23 15:50:00 »

As I said, there are strong opinions on this matter. I for myself find it passable to use a Singleton for resource management, others don't.

Suggesting being lazy and using a singleton when someone already has a working and superiour solution already implemented is just plain silly. Tongue

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
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.

xsi3rr4x (41 views)
2014-04-15 18:08:23

BurntPizza (37 views)
2014-04-15 03:46:01

UprightPath (53 views)
2014-04-14 17:39:50

UprightPath (35 views)
2014-04-14 17:35:47

Porlus (52 views)
2014-04-14 15:48:38

tom_mai78101 (74 views)
2014-04-10 04:04:31

BurntPizza (133 views)
2014-04-08 23:06:04

tom_mai78101 (233 views)
2014-04-05 13:34:39

trollwarrior1 (195 views)
2014-04-04 12:06:45

CJLetsGame (203 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!