Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  "Stuttering" in windowed mode using Java2D  (Read 6001 times)
0 Members and 1 Guest are viewing this topic.
Offline Fid

Senior Newbie


Medals: 1



« Posted 2011-03-23 17:17:32 »

Hey all,

Just two quick questions that I'm a bit stuck with at the moment.

Firstly, after much researching and fiddling around with my game loop and drawing to the screen, I've managed to get rid of all the tearing and other problems. However one still remains... when windowed, my animation although most of the time smooth, when going across the screen can appear to "stutter".

I won't bother to post my game loop (I can if you think it's important though) but I think it is correct, runs at a solid 60fps and when fullscreen - all animation is as smooth as butter! Therefore it has lead me to believe it is not timing that is an issue, just Java2D itself? Would I be right in this?

Is there *anything* I could possibly do to stop this in Windowed mode?

Secondly, I called three methods with BufferCapabilities to see what was happening, this was the output:

PageFlipping:true
MultiBufferAvailable: false
FullScreenRequired:false

Surely MultiBufferAvailable should be true? I'm using BufferStrategy... set up as... createBufferStrategy(2); and I get the graphics from the buffer, write to buffer, then show buffer. So is that correct or am I missing something?

Any help would be appreciated! (Would be good to know if it is possible to achieve smooth animation in a window, or only full screen at the moment).

P.S. Although I know the solution would be to use LWJGL etc, for now I've stuck with doing it the hard way heh! Sad
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #1 - Posted 2011-03-23 17:24:47 »

My output:

Page flipping:true
Multi Buffer:false
Fullscreen required:false

I have never experienced any stuttering or something like that with Java2D. I'm also using it excactly the same way as you do.
What version of Java are you running and what os do you have?

Can you compile a test version so other people can see if the problem exists for them too?
Offline Fid

Senior Newbie


Medals: 1



« Reply #2 - Posted 2011-03-23 17:28:11 »

Ah so the BufferCapabilities are the same then, so no problem there.

I'm using the latest version of Java on Windows 7 x64. I shall also go and try on my laptop to see if I can reproduce it there - if I can, I'll compile a little test version for others to try.

It's hard to describe exactly the stuttering, it's more like when moving across a page every now and again for 0.5 a sec it appears it needs to "catch up"... something that doesn't happen at all when I go full screen.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Fid

Senior Newbie


Medals: 1



« Reply #3 - Posted 2011-03-23 22:33:01 »

Furthermore to my problem earlier, animation appears to run terribly on my laptop (which unfortunately has integrated graphics).

Can anything be done to improve running on integrated graphics to at least make it look smooth and not have lots of "stuttering"?

It runs perfectly full screen on my desktop (which has the graphics card).
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-03-24 00:49:41 »

If by running poorly you mean not fluid, then check your game loop. You should accommodate for slower computers by checking for time spent updating all components and subtracting that from the actual sleep time.

Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #5 - Posted 2011-03-24 07:45:16 »

Could be related to this this topic:
http://www.java-gaming.org/topics/player-sprite-just-doesnt-move-smooth/22762/view/topicseen.html

Dont't forget to vote Smiley

lots of sillystupid games: http://www.emaggame.com
Offline adon_y_coya

Senior Newbie





« Reply #6 - Posted 2011-03-24 10:16:15 »

... when windowed, my animation although most of the time smooth, when going across the screen can appear to "stutter".

What do you mean "going across the screen"? Dragging the window around with the mouse?
Offline Fid

Senior Newbie


Medals: 1



« Reply #7 - Posted 2011-03-24 12:58:22 »

ra4king: That could possibly be the problem, in the loop I do just have a sleep(10) and that remains the same throughout. And yep I mean not fluid, although it seems to run fluidly full screen on my Core 2 Quad machine, on my i3 laptop whether windowed or fullscreen it runs the same - so this does now suggest it isn't tearing or a Java2D bug but some timing error.

Can anyone recommend a good loop, or run through the basics on timing what should happen in a loop? (i.e. what things need to be timed and then subtracted etc)?

Hansdampf: Don't think it is tearing but I'm not sure, either way going to vote!

adon_y_coya: Sorry that's my poor explaining, I mean't say we have a square 150x150 image that is supposed to glide (move) from one side of the screen to the other. That is where the stuttering occurs, on that sprite/image.
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #8 - Posted 2011-03-24 13:43:45 »

Hansdampf: Don't think it is tearing but I'm not sure, either way going to vote!
Beside the tearing (not my main complaint), sometimes I have occational stuttering for about 0.3 to 1 second. As if the screen gets only 10 updates per second. It is not the GC. Like when I run an Applet in a browser with OS X and Java 1.6: the frame counter says 60fps but obviously the visible area is updated with 5 fps.

lots of sillystupid games: http://www.emaggame.com
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-03-24 18:54:09 »

Can anyone recommend a good loop, or run through the basics on timing what should happen in a loop? (i.e. what things need to be timed and then subtracted etc)?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
long lastTime = System.nanoTime();
int FPS = 60;

while(true) {
   long timeDiff = System.nanoTime()-lastTime;
   lastTime = System.nanoTime();
   
   //Game code
 
   try{
      long sleepTime = (1000000000/FPS) - (System.nanoTime()-lastTime);
      if(sleepTime < 0)
         continue;
      Thread.sleep((int)Math.round(sleepTime/1000000.0));
   }
   catch(Exception exc) {}
}


Hansdampf: Don't think it is tearing but I'm not sure, either way going to vote!
Beside the tearing (not my main complaint), sometimes I have occational stuttering for about 0.3 to 1 second. As if the screen gets only 10 updates per second. It is not the GC. Like when I run an Applet in a browser with OS X and Java 1.6: the frame counter says 60fps but obviously the visible area is updated with 5 fps.
Are you using separate threads for the game loop and redraw? If you are then the frame counter only checks the game loop when your redraw thread could be stuttering.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #10 - Posted 2011-03-24 20:08:18 »

Quote
Are you using separate threads for the game loop and redraw? If you are then the frame counter only checks the game loop when your redraw thread could be stuttering.
No, I don't. Of course not. And there is no performance problem, yielding 70% of the time.

lots of sillystupid games: http://www.emaggame.com
Offline Fid

Senior Newbie


Medals: 1



« Reply #11 - Posted 2011-03-24 20:48:48 »

ra4King:

Tried the example loop and got the stuttering in full screen that I didn't have before... so I'm doing something wrong somewhere and I think it *must* be the timing?

Probably the most confusing bit ever with building a game engine heh!

I'm thinking that it however must definitely be the game loop that is the problem... one loops makes it even worse on my laptop, the other works but still stutters, and the same for my desktop. Just need to get that right, don't think Java2D is the problem here (as I haven't seen any tearing etc).
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-03-25 03:23:17 »

I made an error where I casted (System.nanoTime()-lastTime) to an int. See if that works now.
I use that loop for all my games without any problems Huh

EDIT: What FPS are you using?

Offline Fid

Senior Newbie


Medals: 1



« Reply #13 - Posted 2011-03-25 15:17:26 »

Using 60 FPS.

And still getting a few stuttering when full screen on my desktop, and quite a bit of stuttering on my laptop. Assuming it is still down to timing, or something else, going to have a look around.
Offline whome

Junior Member




Carte Noir Java


« Reply #14 - Posted 2011-03-29 17:26:26 »

How can I make this simple game loop better? topic has few different game loops to compare. This message in a link is what I use nowdays. Main loop may look complex...well it is...but a good update+render logic is not the easy task.
Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #15 - Posted 2011-03-29 19:32:10 »

Probably not related, but it's worth asking. Which datatype do you use for position and motion? int, long, float, double? It's been a while, but I distinctly remember in some cases where objects moved across the screen, that changing from floats to doubles would help motion appear smoother. But that could have been some weird freaky artifact.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-03-29 19:42:34 »

How can I make this simple game loop better? topic has few different game loops to compare. This message in a link is what I use nowdays. Main loop may look complex...well it is...but a good update+render logic is not the easy task.
It is a very bad design to have the game loop in the constructor. It is advisable to start a new thread for the loop.

@Fid
What kind of stuttering do you see? The FPS not being stable or the character not moving smoothly?

Offline Fid

Senior Newbie


Medals: 1



« Reply #17 - Posted 2011-03-31 16:44:16 »

How can I make this simple game loop better? topic has few different game loops to compare. This message in a link is what I use nowdays. Main loop may look complex...well it is...but a good update+render logic is not the easy task.
It is a very bad design to have the game loop in the constructor. It is advisable to start a new thread for the loop.

@Fid
What kind of stuttering do you see? The FPS not being stable or the character not moving smoothly?

The FPS is continually stable, the character is just not moving smoothly. That could be because I have something like, if left pressed, x+2.5 and then it would draw the sprite 2.5 pixels to the left - could that be the problem?

I think I'm using doubles (I'll have to check that, I can't remember now if I used float or double and I'm not at my computer to see the code right now heh).

Thanks for all your help guys, I really appreciate it!
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-03-31 19:36:57 »

I marvel at how you say the FPS is stable yet you blame the game loop Tongue
If so, then you probably don't use floating points to store x and y, which cause the character to stutter when moving decimal in values.

Offline Fid

Senior Newbie


Medals: 1



« Reply #19 - Posted 2011-03-31 23:04:10 »

Yep I was using Double - moving to Float now see if that works, which I'm guessing it should based on your reply Tongue.

And you know probably when I started it probably was down to the game loop (it was incredibly basic and sleep was just a set variable, therefore performed badly on my laptop when compared to desktop. Now that is resolved and it attempts to stick to a certain FPS, it obviously has to be something else (well, I've elimated everything else heh!)
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2011-03-31 23:18:30 »

Double and Float are floating point primitive types. Float is a shorter Double, by exactly half.
Double is more recommended here.

Offline Fid

Senior Newbie


Medals: 1



« Reply #21 - Posted 2011-04-01 00:22:23 »

Ah, sorry I completely misread your post there. I'm definitely now using doubles haha.

Although I'm confused now ra4king - when I implemented a FPS counter, (tried it in several different ways) it always gave the correct FPS (to what I had set it to) - yet there would still be stuttering now and again.

If it isn't the gameloop then (as the FPS is reported as being 60)... what else could it be? (Or was your comment on the fact that the FPS could have possibly be being reported incorrectly).

Using an external timer seems to be working better really at the moment (unless my eyes have just stopped being fussy and are starting to ignore the slight stutter lol).
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2011-04-01 03:45:31 »

Are you using the same thread for updating and drawing or are you relying on repaint() (EDT)?

Offline Fid

Senior Newbie


Medals: 1



« Reply #23 - Posted 2011-04-01 12:03:56 »

Using the same thread for drawing and with a buffer (BufferStrategy... then drawing to buffer... then showing buffer etc).
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2011-04-01 17:51:18 »

Haha accidentally clicked "Appreciate". Well free medal for you Tongue

Well if the FPS stays constant and the drawing is done in the game loop, then I don't know how it would stutter other than being a bug in your game code.

Offline dishmoth
« Reply #25 - Posted 2011-04-01 19:29:53 »

when I implemented a FPS counter, (tried it in several different ways) it always gave the correct FPS (to what I had set it to) - yet there would still be stuttering now and again.
I don't know if it will help, but you could try looking more closely at exactly how constant your frame-rate is.  (To be honest, I'm a little suspicious when you say the counter always gives you the "correct FPS".  You mean it always says 60, never 59.9 or 60.1?  What period are you averaging the FPS over?)

For 60 frames-per-second, the value of nanoTime() at the start of the current loop minus the value at the start of the previous loop would ideally be around 1e9/60 (=16,666,666).  Try printing out the value if it is significantly different from that (for instance, less than 5,000,000 or more than 30,000,000).  Do you see these numbers do anything strange when the game stutters?  What are the most extreme numbers that are printed?

Simon

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #26 - Posted 2011-04-23 17:27:41 »

Are you using delta-timing? If not, then an FPS change can make the game stutter a bit. Pointing

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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (24 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!