Approaches to active rendering with no libraries

Are there any effective/efficient ways to render 2d objects besides using BufferStrategy and Graphics? Or are there maybe some tips/tricks to using them effectively? I render my game objects with them (which seems to be the norm), but it seems to be taking up a lot of time. I looked up alternative ways to render but all I found was either use a library or a “pixel renderer” (Is this what it’s really called?). At the moment, I do not want to use a library because I want to know the (practical) limits of what I can do with out one. Eventually, I will use a library, but for now, I’m probably going to end up using the “pixel renderer”, unless I can find better alternatives. My problem with it is it doesn’t handle partial transparency nor render strings, and honestly, it feels almost hacky to me.

My current rendering code can be found here in case anyone wants to see it.

If you haven’t already, make sure you use the OpenGL flags to allow hardware acceleration, which you are trying to do. You can enable the flags with VM arguments or programmically by using:

System.setProperty("sun.java2d.opengl","True");

Make sure to put this before any rendering code, preferably before you enter the Swing EDT.

I haven’t read all of your code, but I noticed a few things whilst scanning through, in RenderingManager.java your getImage method does not dispose of the graphics object here:

image.getGraphics().drawImage(bimg, 0, 0, null);

Without disposing the graphics object the image will not be hardware accelerated, because for images to be accelerated their rasters must be encapsulated to prevent modification when the pipeline converts it into a VolatileImage. Simply create a local variable of the image graphics, do your drawing, and dispose.

Also, you will get better performance if you edit your code to allow you to specify the image transparency, you currently have it on Bit-mask only, if your background is opaque, it is worthwhile to use the OPAQUE setting instead. Incase it makes it any easier, the constant values are: 1 = OPAQUE, 2 = BITMASK, 3 = TRANSLUCENT.

Lastly some things that you might consider doing is using the RenderingHints for Graphics2D, here is an article by Oracle explaining the process. https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html

I don’t have time to look over all of your code right now, but if you are still having issues with Java2D performance after trying my suggestions then please don’t hesitate to PM me.

Good Luck.

EDIT: I would take a look at your game loop, Thread.sleep() isn’t accurate or reliable, I find Thread.yield() gives smoother result; additionally you might experience non-smooth game play since you aren’t capping your FPS, VSync would require Fullscreen Exclusive mode, however creating a manual cap to the current GraphicDevice Refresh Rate makes it smoother in my experience, you will have to see what works for you.

Keep in mind that Java2D/AWT/Swing are now deprecated and from Java8 onwards you probably want to be looking at JavaFX8 instead, which is designed for this sort of thing and somewhat better at it.

Cas :slight_smile:

@Drae
Thanks for the tips! I’ll be sure to look into those. If you do decide to look through all the code, I’d seriously appreciate it. In case you do, you might wanna take a look at this thread I made about the project just so you know what my goal with this it is.

Regarding the game loop, I actually plan on changing it to allow control over fps. I’ve actually made another thread about it here.

@princec
Huh. I didn’t know that. I’m still on Java7 actually, but when I do move to 8, I’ll be sure to look it up. Thanks!

As Java7 is already in EOL it’s probably a good idea to start on Java8 right now :slight_smile:

Cas :slight_smile:

Well, since I’m plan on overhauling the rendering code, I guess now’s as good a time as any to move up to 8. Will do :slight_smile:

There is this thread with a sample FX game loop, just noticed it, haven’t tried it myself yet:

Bwah? Do you have an “official” source for this? I know JavaFX is the supposedly preferred UI library, but deprecated is a strong word.

Surprised me too.

Closest I could find was this (in the JavaFX faq):

[quote]6. Is JavaFX replacing Swing as the new client UI library for Java SE?
Yes. However, Swing will remain part of the Java SE specification for the foreseeable future, and therefore included in the JRE. While we recommend developers to leverage JavaFX APIs as much as possible when building new applications, it is possible to extend a Swing application with JavaFX, allowing for a smoother transition.
[/quote]

I also doubt that Swing is ever going to be replaced (i.e. going to be removed from the JRE), as it is still Java’s major windowing toolkit used by many companies’ in-house and distributed projects.
And Oracle is sure to be aware of this. Oracle can provide alternatives with JavaFX, though, but complete adaptation can take decades.
So porting (i.e. rewriting) existing Swing apps is far beyond anything imaginable.

It’s kind of the same with legacy OpenGL (OpenGL 2.1 and below). I don’t think that graphics hardware and driver vendors will stop supporting legacy OpenGL completely, since the existing codebase using it and the cost of porting is quite too large.
But they do provide alternatives, such as OpenGL 3/4 with Core profile.

EDIT:

But from Oracle’s (economical) standpoint deprecating and removing AWT/Swing would make sense, yes, if JavaFX is to be pushed since the two reasons why Oracle would then want to do that in my opinion are:

  • reducing maintenance costs for bugs in AWT/Swing
  • reducing costs for porting to coming versions of OS’es supporting the Java Platform
    Since JavaFX basically faces those same two problems, it would indeed make sense to reduce the costs by deprecating and removing Swing support.
    Let’s just hope that this is not happening too soon. :slight_smile:

One thing to keep in mind is that going forward the preferred way to distribute Java application is going to be (already is) by embedding a JRE (since you can’t really rely on there being a system JRE anymore).

Java 9 is introducing modularisation (project jigsaw), so AWT/Swing will probably be put into its own module and JavaFX into its own. Therefore it’ll be up to the developer to include the modules or other libraries (SWT, LWJGL, LibGDX, etc) one needs in addition to the basic JRE. So not really any point targeting “no libraries” since you’ll have to include a module library or 3rd party library anyway so best to just pick the one best for the job.

AWT/Swing module will probably be around for a while though but eventually dropped once usage drops below a certain level.

Well, “no libraries” isn’t really the end goal. As I said, I’ll eventually move on to using a library, but for now I want to know what I can do with what Java already provides. I’m pretty sure in the long run it’d be handy to know/experience even considering what you mentioned about Java 9. At the very least, it would be educational :stuck_out_tongue:

Looks interesting. Thanks! I might actually go with this instead of the pixel renderer thing… hmm. I’m still curious though about my current rendering code and how I can improve it. Still haven’t had the time to really look into Drae’s suggestions