Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  [SOLVED]Saving and Loading  (Read 1325 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Reply #30 - Posted 2014-01-11 12:56:47 »

Stuff I would work on:

* You're loading all the images and then checking which one to use. You should make a class that contains all the images and loads them upon program start-up. When you want to check which image to use, don't load images. Check the name of the building and set it's image to one of the images that you loaded upon start-up.
* You shouldn't create buildings "by name". You should make a class for every building that extends building. For example,
a house would be:
1  
2  
3  
4  
5  
6  
7  
8  
public class House extends Building {
         
         public House(int x, int y) {
            super(x,y);
            this.renderedImage = Images.houseImage;
         }
         
      }

This what a tent would like like or something..
1  
2  
3  
4  
5  
6  
7  
8  
public class Tent extends Building {
         
         public House(int x, int y) {
            super(x,y);
            this.renderedImage = Images.tentImage;
         }
         
      }


If you want a quick performance boost, don't call Image() from draw() method. Call Image() from constructor.
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #31 - Posted 2014-01-11 13:02:18 »


1. You're reloading the image every single time draw() is called. Waste of CPU and memory. Just cache it and refer to the cached instance when you want to draw.

2. Why are you loading resources for EVERY building in the game in Image() ? You should only load the resources you NEED.

3. You can't compare strings with == , use .equals( .. )

4. Please use a better naming style, for example use aVariable instead of A_Variable.

5. What the hell is this?

1  
2  
3  
if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}


And if you want help with the Load/Save, you'll need to post your Load/Save code.

1. How could i cache it?
2. I need them all
3. I usually do
4. I usually do
5. they're temporary for when i decide to add some more buildings

I love making games...
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #32 - Posted 2014-01-11 13:36:31 »


5. What the hell is this?

1  
2  
3  
if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}


And if you want help with the Load/Save, you'll need to post your Load/Save code.





My friend you did it, you helped me the solve the issue! All it was was the "==" thing used on  Strings. Ive never had problems with ity in the pasyt but I just found out thats what was stopping the loading from working, so I thankyou!
Thanks to all those people who helped with the problem and f**k you to all the people who were here just tp give me shit!

I love making games...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StephenJSweeney

Junior Member


Medals: 1
Projects: 3



« Reply #33 - Posted 2014-01-11 13:48:14 »

This is what I do in my games:

1) Create a singleton called ImageManager (or whatever you want to call it)
2) Create a Hashmap to hold the filename of the image, and the actual image itself.
3) Provide an accessor method called getImage(String filename) which will look to see if the image is already loaded and returns it, or loads it, stores it in your hashmap and returns the image
4) Have all your entities, etc grab their images from the ImageManager.

So:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public class ImageManager
{
   public static ImageManager         INSTANCE   = new ImageManager();

   private final Map<String, Texture>   textures;

   private ImageManager()
   {
      textures = new HashMap<String, Texture>();
   }

   public Texture loadTexture(final String filename)
   {
      Texture texture = textures.get(filename);

      if (texture == null)
      {
         final String type = filename.endsWith("png") ? "PNG" : "JPG";
         texture = TextureLoader.getTexture(type, getClass().getResourceAsStream(filename));
         textures.put(filename, texture);
      }

      return texture;
   }
}


Now your textures and images are stored in a single accessible place.

And with your Entity:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class Entity
{
   private transient Texture texture;
   private String textureFilename;

   public void init()
   {
      texture = TextureManager.INSTANCE.loadTexture(textureFilename);
   }

   private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
   {
      in.defaultReadObject();

      init();
   }
}


When you create your entity, give it a textureFilename and call init(). This will load the texture (once) and return it for use. The advantage here is that when you deserialise the entity (after loading a game back up) the init() will be called and the texture will automatically be loaded and reapplied.

This code isn't 100% accurate, and something I've put together by quickly hacking up what I do myself, but it should give you the idea and point you in the right direction. You might want to add methods to the TextureManager to delete textures, etc.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #34 - Posted 2014-01-11 13:58:48 »

Haha I don't think that that's what Dxu1994 meant:

I think he wonders why you have exactly the same statement 3 times in a row. What's the reason for it?

This should work just the same:
1  
if (name.equals("")) return new Rectangle(x, y, 0, 0);


On other notes, please mind the last post from trollwarrior1 on this thread. He makes a good point. You could easily make this a better OOP designed code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public class Building extends Entity implements Serializable {

    private Image img;
    private Rectangle bounds;
    private int velX = 0;
    private int velY = 0;

    public Building(int x, int y, Image img, Rectangle bounds) {
        super(x, y);
        this.img = img;
        this.bounds = bounds;
    }

    public void update() {
        x += velX;
        y += velY;
    }

    public void draw(Graphics2D g2d) {
        g2d.drawImage(img, x, y, null);
        if (Game.debugMode)
             g2d.draw(getBounds());
    }
}
(Building.java)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class ImageLoader {
   
    private HashMap<String, Image> images = new Hashmap<>();

    public ImageLoader() {
        images.put("Base", loadImage("/backgrounds/NO TEXTURE.png"));
        images.put("ConcreteShack", loadImage("/buildings/CH1.png"));
        images.put("Tent", loadImage("/placable_Items/tent.png"));
        images.put("BarbedWire", loadImage("/placable_Items/Barbed_Wire.png"));
        // And so on ...
   }

    public Image get(String name) {
        return images.get(name);
    }

    public static Image loadImage(String path) {
        return new ImageIcon(this.getClass().getResource(path)).getImage();
    }
}
(ImageLoader.java)
1  
2  
3  
4  
5  
6  
// Now you can easily create a new Building type like this:
public class Base extends Building {
    public Base(int x, int y, ImageLoader loader) {
        super(x, y, loader.get("Base"), new Rectangle(x ,y , 0, 0));
    }
}
(Base.java)
or
1  
2  
3  
4  
5  
public class ConcreteShack extends Building {
    public ConcreteShack(int x, int y, ImageLoader loader) {
        super(x, y, loader.get("ConcreteShack"), new Rectangle(x ,y , 225, 205));
    }
}
(ConcreteShack.java)
And so on.

Finally, to make this work with ImageLoader, somewhere in your initialization code, use this:
1  
2  
3  
4  
5  
// in initialization, where the Buildings are created:
ImageLoader loader = new ImageLoader();
Building whateverbuilding = new Base(0, 0, loader);
Building anotherBuilding = new ConcreteShack(0, 0, loader);
// It's important to reuse the 'loader' once you have created one :)


Hope this helps improving code readability Smiley

So while I typed this monster, another reply also did something like that. I'll post mine anyways as some kind of addition and / or alternative to what StephenJSweeney posted.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: 1 [2]
  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 (20 views)
2014-08-19 09:29:53

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

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

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

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

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

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

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

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

BurntPizza (64 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!