1
\$\begingroup\$

How can I improve the performance of my rendering? Currently, I'm rendering with java.awt.Graphics, but it seems to be starting to slow down since I'm adding new stuff to render.

My rendering setup:

Loading a spritesheet: Spritesheet sheet = new Spritesheet(ImageLoader.loadImage("/textures/spritesheet.png"));

Loading sprites: BufferedImage player = sheet.crop(5, 0, Spritesheet.DEF_WIDTH, Spritesheet.DEF_HEIGHT);

And then rendering it with java.awt.Graphics in my gameloop: g.drawImage(player, x, y, Entity.DEF_WIDTH, Entity.DEF_HEIGHT);

Spritesheet.DEF_WIDTH and Spritesheet.DEF_HEIGHT refer to 32px for quick access since most of my sprites are 32x32px. Entity.DEF_WIDTH and Entity.DEF_HEIGHT refer to 64px to render the entity on the screen.

One way I know that could solve my problem is that I could use smaller sprites but scale them up while rendering, but let's see if it's possible without that first.

\$\endgroup\$
  • \$\begingroup\$ Maybe this page is helpful? zetcode.com/tutorials/javagamestutorial \$\endgroup\$ – mr_lou Feb 24 '18 at 18:44
  • \$\begingroup\$ Completely understand. I think I'm going to make my sprites smaller and scale them when drawing them to the screen. And, when doing a new project, I'll keep in mind to try out a third-party API. Thanks for your help! \$\endgroup\$ – Kerdo Feb 24 '18 at 18:50
  • \$\begingroup\$ You sure you're using java.awt.graphics correctly? Im not sure if scaling helps in this case. \$\endgroup\$ – Sidar Feb 24 '18 at 19:37
5
\$\begingroup\$

First of all, instead of relying on the ideas of people, who didn't see a single line of code or even a picture of your game, try profiling it, finding the bottleneck and removing it.

If that isn't option, here are some general tips:

  • Scaling won't help if your sprites are only 32x32, period. Maybe it would use less RAM, but that's not a problem.
  • Only render things if they are on the screen. If you render 900 bullets, but 99% of them aren't seen by the player, then you might as well just calculate the exact square root of 2, it would waste the same amount of processing power.
  • Make sure you aren't loading the sprites every frame. IO is usually a blocking call, which means nothing else will happen on that thread until it finishes.
  • Use more threads if you can. Make sure to tread carefully when doing that though, it can make your life much harder, than not using it.
  • Try using an engine or a framework (not an APi if you actually want to finish the game). The CPU is not designed to render stuff, but the GPU is. Use it
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.