Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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 manager in the SpaceInvaders game  (Read 1007 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Posted 2005-10-26 19:12:41 »

I've been going through this SpaceInvader tutorial, and it seems to be very badly designed.

For one thing, there is this SpriteCache class, that extends on ResourceCache (in case you have AudioCache manager also).

The thing is that each time I call spriteCache.getSprite("imagename.png");  it returns me a new object! This is a big overhead since I only need one instance of the image to exist.
So if I have the same 6 sprites on the screen, then the file is only loaded 1 time, but 6 instances of the image exist.
I want this to be so that if there were no matter how many sprites on the screen, then the sprite should only be loaded once, and only one instance exist .. just painted at different locations.


Any good SpriteCache (manager) out there that I can use? Or any ideas how to solve this?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline g666

Junior Member





« Reply #1 - Posted 2005-10-26 19:54:22 »

I think you are mistaken. (you are refering to this right: http://www.planetalia.com/cursos/index.jsp)
The image is only loaded if it doesnt already exist in the hashtable, otherwise it is just gotten out of the hashtable.

desperately seeking sanity
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #2 - Posted 2005-10-26 20:05:51 »

I think you are mistaken. (you are refering to this right: http://www.planetalia.com/cursos/index.jsp)
The image is only loaded if it doesnt already exist in the hashtable, otherwise it is just gotten out of the hashtable.

Oh yes, that's the game I'm referring to.

The problem with that game is that it creates a new image for each call to getSprite()  ... while it only loads the file once, it creates the image n times for n calls to getSprite()   ...   it in fact should cache the image, and not the file.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #3 - Posted 2005-10-26 20:22:10 »

I solved the problem.

I changed the getSprite method in SpriteCache class.

Added member var:
1  
private HashMap imageResources = new HashMap();


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   public BufferedImage getSprite(String name) {
      if(imageResources.containsKey(name)) return (BufferedImage)imageResources.get(name);
     
      BufferedImage loaded = (BufferedImage)getResource(name);
      BufferedImage compatible = createCompatible(loaded.getWidth(),loaded.getHeight(),Transparency.TRANSLUCENT);
      Graphics g = compatible.getGraphics();
      g.drawImage(loaded,0,0,this);
     
      imageResources.put(name,compatible);
      return compatible;
   }

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!