Performance loading images game class

I am trying to work out several ways to draw images for objects in a game.

I want objects to have their own method draw()

, but I'm afraid that I might cause unnecessary copies of the image or some other performance degradation, noticeable or not.

The first method loads BufferedImages

when the level starts.

public class levelone{

    BufferedImage imageguy;

    public void draw(Graphics2d g){
        g.drawImage(imageguy, null, guy.getx(), guy.gety());
    }

}

      

The second way loads the image into guy.class and retrieves it from there only with a call guy.draw(g);

to levelone.class.

The third way loads the image into levelone.class and fetches it from guy.class with only one guy.draw(imageguy, g);

in levelone.class.

Help me figure out the best way to do this. If it's not clear what I'm asking for, please tell me and I'll try to make it clearer.

+3


source to share


1 answer


It's more of a design issue. To start:

... some decrease in efficiency, noticeable or not.

... not a very healthy or productive mindset. If it is not noticeable, it is not worth worrying about. Otherwise, you could write your game in assembler if you are going to loop over each clock cycle.

However, there may be greater scalability at the design level.

It's worth noting that chaining / drawing logic with a high-level class can become a maintenance burden. It depends on the scope of your project, but if it's a fairly large project, you will have a lot more breathing room if you separate your drawing / rendering logic from your game logic. You can then focus your attention on what the "monster" or "item" or something should be doing in terms of game design, without worrying about how it will be drawn. The drawing is done elsewhere.

But if we assume that you want to have drawing logic inside these objects, then it seems that your question ultimately comes down to where you want to store some image buffers for graphics associated with objects in your game.

And the short answer is, "It depends." What assumptions are you sure of what you are doing? For example, in your posted code:

public void draw(Graphics2d g){
    g.drawImage(imageguy, null, guy.getx(), guy.gety());
}

      

... it seems ridiculous if all you ever do is the closeness of the object to the graphic. If all you're ever going to do is blend the image of the item with the graphics, then you can eliminate the method entirely draw

, expose a public accessor to the image, and let one central place handle the responsibility of wandering those images around at the right time and place. ...



And at this point, if all you are doing is storing images and giving them access to objects in your objects, you can also let the outside world take care of loading / storing those images and flickering them, leaving your objects clean and drawing-free. It will also give you maximum flexibility to optimize when and how drawings are made without intrusively touching all of your objects.

But are you always going to just flip through images from an object? Will there ever be a case where an object in your game can draw additional things, shapes, text, etc.? If so, you might want to keep the method draw

.

So the final question for me is: do you really want to draw the drawing inside your objects or not?

  • If not, then you have maximum flexibility to optimize your heart content, as you can be responsible for the most efficient loading, storage and drawing of graphics into a central renderer instead of spreading that responsibility to every object in your game (and, you may need to change all of them if you ever want to do something differently).

  • if you want, then you can completely encapsulate all the details involved in rendering the object within the object, including saving any resources it needs so the outside world doesn't need to worry about it and can just call "draw" and pass the drawing target (for example Graphics2D).

So I would suggest that perhaps none of your suggestions can be perfect. But if there is one option that might be bad, I would say that this is your third. I.e:

The third way loads the image into levelone.class and fetches it from guy.class with just guy.draw (imageguy, g); in levelone.class.

The problem is, you kind of get the worst of both worlds. You are leaking the drawing details of this "guy" into the outside world, but the outside world still has to depend on the "guy" to make the drawing, passing those drawing details. If nothing else, you really want to give either the guy or the outside world primary responsibility here, with the latter being more flexible, but the former being more encapsulated. When the outside world has to pass the guy back to the guy for the guy to paint, you don't get any of these benefits. If you really want a guy to "paint himself," then you usually want to do it without undue help from outside. Give one entity the responsibility of the firm instead of extending it to several.

About unnecessary copies of the same image that are easily resolved no matter what you do with the central image cache. The easy way is to just use the map. When uploading the image, find the map for the path. If it already exists, return the link to the already uploaded image. Otherwise download the image and insert the path / image as a key / value pair into the map.

0


source







All Articles