Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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  
  VSync  (Read 5253 times)
0 Members and 1 Guest are viewing this topic.
Offline ShannonSmith
« Posted 2012-11-09 21:42:49 »

I know this has been asked multiple times but does anyone know if there is a way yet to synchronize to the VSYNC pulse for Java2D. There is simply no way to get smooth motion without it and I can't find any information on how to do it.
I found some references to an API being added in 7 but nothing on the API itself. Is there anything in JavaFX?
Online matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2012-11-09 21:48:09 »

People on the internet, including me, recommend to just use a BufferStrategy.

If you want to have "smooth motion" in terms of an FPS cap, you need to search for game loops. There is awesome stuff around there.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ShannonSmith
« Reply #2 - Posted 2012-11-09 21:54:21 »

I am using a BufferStrategy, it doesn't help because the show() doesn't wait for the vsync, so it's very easy to get 1 frame displayed twice and then a frame skipped on the next cycle. Missing a single frame is actually quite noticeable.

The only way to get properly smooth motion is to have 1 frame per monitor refresh any other way looks glitchy.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2012-11-09 21:57:33 »

Since you also asked for JavaFX, googleing gave me this:
http://hg.openjdk.java.net/jdk7/2d/jdk/file/a389af17df10/src/share/classes/sun/java2d/pipe/hw/ExtendedBufferCapabilities.java

(Mentioned in one of our threads...)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ShannonSmith
« Reply #4 - Posted 2012-11-09 21:59:38 »

I saw that but note it's in the sun namespace so not officially part of the JDK. I assume it's something you can enable with vm args.

Online matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2012-11-09 22:03:26 »

Oh... okey...

No clue then  persecutioncomplex Undecided

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ShannonSmith
« Reply #6 - Posted 2012-11-09 22:22:49 »

I've been waiting for quite some time for the equivalent of javascript requestAnimationFrame() but nobody seems to care that much about it for Java2D. Hopefully JavaFX takes off and it becomes important enough to add. If anyone doesn't realize why it matters I suggest having a browse at: http://www.jankfree.com/



Offline ShannonSmith
« Reply #7 - Posted 2012-11-10 00:58:26 »

So I tried the ExtendedBufferCapabilities and it works like a charm, beautiful smooth movement, so I think I'll just wrap it in some exception handling and hope it makes it into Java 8.

Edit: Unfortunately it only works if my laptop is using an external monitor... In fact the createBufferStrategy with page flipping doesn't work either in this case (throws the same AWTException),  Sad

Online matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-11-10 22:03:04 »

I think you should just go on and create a capped game loop. In java2D there currently is no equivalent of the "requestAnimationFrame()"... You have to code the game loop yourself and you seem to be experienced enough.

If you want to get an idea, or just want a solution of a working game loop, have a look at my utilities.

Also, there should be no problem with the missing vsync, since you've got double buffering, which should eliminate all artifacts.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ShannonSmith
« Reply #9 - Posted 2012-11-11 00:01:26 »

Artifacts (like tearing) are not the problem. The problem is to achieve smooth motion VSYNC is essential.

Say you have a character moving across the screen at 1 pixel per frame. You have no idea when the frame will be actually be displayed it could be up to 16ms (at 60 Hz) from the point you calculate it. So even though you might create a series of frames with positions 1,2,3,4,5,6 they could be displayed like 1,2,2,4,5,5,6 depending on the relative timing of the show() and the vsync.

People don't tend realize how much of a difference it makes until they see the same thing done right. Done wrong it just looks a bit amateurish but you can't quite put your finger on the problem. The second you see it done properly it just looks so much better.

For a good explanation (and why vsync is the only real solution) of the problem I suggest watching:
<a href="http://www.youtube.com/v/hAzhayTnhEI?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/hAzhayTnhEI?version=3&amp;hl=en_US&amp;start=</a>

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #10 - Posted 2012-11-11 08:15:15 »

hmmm...if you are referring to the very jerky motion when you move an object slow in java2D that is because it does everything in ints. It can only ever move something by and int wich means you can't get really smooth movement slower then 1 pixel.

I have never had any issues using double buffering and not getting perfectly smooth motion that is not less then 1 pixel per frame.

Also, I don't think anyone really has the time to go and watch an hour long video hoping to find wth your are talking about. Maybe you have like amazing eyes or something because I really have never seen these "artifacts" you are talking about when properly double buffering.

Online matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #11 - Posted 2012-11-11 09:33:09 »

So it seems you are very experienced.

Then try out either LWJGL or JogAmp.
Both being a wrapper for opengl, and both giving you the ability to do VSync.

Which one is better? I don't want to start the thousandth flamewar, so here I try to be objective:

LWJGL is pretty much based on static stuff. Princec says that this would make more sense, since OpenGL is written "static" too.
The windowing system is also static and very very easy. So you only have to say "Display.create()" and initialize some gl stuff (which you could leave out, but you better don't) and you're ready to draw stuff. Then call "Display.update()", and if you want you can use "Display.setVSyncEnabled(true);" so you have vsyncing when you call "Display.update()".
One big contra: You can't have two Displays in lwjgl, since its pretty much static stuff.
Summed up: LWJGL is very very very easy to use and learn, you could copy code directly from C to Java and it would almost work. It gives you only one window, but this shouldn't be the problem for a game.

JogAmp consists of "JOGL" (openGL stuff), "JOAL" (openAL stuff) "JOCL" (openCL stuff) and propably some more I don't know of, but these are the most important. (LWJGL gives OpenGL, OpenCL and OpenAL wrappes as well)
JogAmp is more Object.oriented, which means you can instantiate "GL" or "GL2" or whatever and use it for doing OpenGL stuff.
It allows you to have multiple windows and already gives you a pretty good GameLoop / Rendering loop. It as also allows you VSyncing.
You can use AWT or NEWT (their own windowing toolkit) to create windows and render with OpenGL.
And it gives you some little helpful helper-classes, like a wrapper around the Framebuffer api, called "Framebuffer"...
And more of the above example.

I hope I didn't miss a point.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ShannonSmith
« Reply #12 - Posted 2012-11-11 17:46:49 »

Quote
Also, I don't think anyone really has the time to go and watch an hour long video hoping to find wth your are talking about. Maybe you have like amazing eyes or something because I really have never seen these "artifacts" you are talking about when properly double buffering.
Most of the relevant information about the problem is is in the first 10 minutes or so. The rest about profiling in chrome.

Quote
hmmm...if you are referring to the very jerky motion when you move an object slow in java2D that is because it does everything in ints.
No Java2D uses doubles and you can turn on different interpolation modes to get sub-pixel image interpolation, however this doesn't really help unless you are updating at much higher rate than the refresh rate of the monitor. If you update at 300fps moving objects by very small amounts each time your positions will only be out by ~3ms which is probably not noticeable. The downside of this 4/5 of the frames you generate are never shown so it's a pretty massive waste of CPU. Also this doesn't work with up-scaled pixel art type graphics (the type I am doing at the moment) as you are typically moving objects ~ at 1/2 pixels per frame and can't use sub-pixels.

Quote
Then try out either LWJGL or JogAmp.
I would prefer not to do this because it does impose a number of barriers to people trying my game, but it may be the only option. One downside is that setVSyncEnabled is not guaranteed to work either so it may not be much help.

An option I was considering was using javascript to call requestAnimationFrame() and drive each new frame from the browser itself but I'm not sure how reliable this would be or how much overhead it would create

Online matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2012-11-11 20:41:38 »

Also, I don't think anyone really has the time to go and watch an hour long video hoping to find
An option I was considering was using javascript to call requestAnimationFrame() and drive each new frame from the browser itself but I'm not sure how reliable this would be or how much overhead it would create

I guess this would be VERY slow.

I really suggest to use an OpenGL wrapper, since this is lighting-fast and I don't see where there would be barriers for the users. There is almost no succsessful java game out there written with Java2D. Everyone uses OpenGL.

You could also go on and try out some engines, as for example Slick2D (remember not to download it from the site, but from bitbucket), libGDX or the JRabbit engine. We have guys here who could help you with each of them, for example:
Slick2D: davedes, kevglass (but he is somehow not active)
libGDX: Nate and badlogicgames
JRabbit: CodeBunny (who is inactive right now too Sad <edit>According to profile: Last active 3 days ago...</edit>)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline StumpyStrust
« Reply #14 - Posted 2012-11-12 06:04:25 »

Could you seriously post an example of what you are talking about? I know what vsycn does and the biggest thing is screen tearing.

And java2D can use double but when you want to draw anything, it takes ints. Even if it uses double under the hood, the fact is, you can't pass in doubles to the render methods. I also have played around with the different interpolation methods, most of which have to do with image filtering. I am talking about the ones you set in the graphics object. Java gives jerky movement when you move small amounts. I can give you an example of what I am talking about if you would like. 

Offline ShannonSmith
« Reply #15 - Posted 2012-11-12 06:44:26 »

All the Graphics2D methods take double precision values and there is a full set of double precision Shape objects for the draw(Shape) method. Even if you stuck to the regular Graphics calls that take ints you can use the Graphics2D double precision translate to draw at floating point coordinates.

I think I might try to mock up a quick example applet to demonstrate the problem (and test how often ExtendedBufferCapabilities is available).
Offline StumpyStrust
« Reply #16 - Posted 2012-11-12 19:09:50 »

the performance hit for translate is ridiculous.

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

Yes you can draw shapes with doubles but most people draw sprites...images which still take ints.

Offline ShannonSmith
« Reply #17 - Posted 2012-11-13 03:47:16 »

So I tried using JavaFX and AnimationTimer as it appeared a likely candidate (you don't specify the time delta) and it worked perfectly, vsync at last. Like requestAnimationFrame() you get one call per monitor refresh (verified by changing the refresh rate) and animation looks lovely and smooth!
Hopefully it's fairly cross platform and works the same way in a browser plugin.

I am still using Java2D to draw my scene to a buffer and copying it to a JavaFX WriteableImage every frame. Not particularly efficient but better than running at 300fps.
Offline CommanderKeith
« Reply #18 - Posted 2012-11-13 04:57:00 »

That's great! Thanks for the info shannon

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.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!