• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

MouseMotionEvent Performance

 
Greenhorn
Posts: 21
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm writing a turn based game that involves scrolling a map made of squares which light up as you mouse over them. Everything is drawn on a frame with a 2 buffer strategy. The rendering is done in a separate thread that loops continuously. When I sleep the render thread for a few milliseconds after each frame to push the fps down to around 30, the squares light up very responsively but the scrolling map jitters pretty bad. If I don't sleep the thread, it sits at around 60 fps and the map scrolls really smoothly, but there is noticeable delay between the mouse passing over a square and it lighting up. The code for identifying which square to light is very simple and quick and the processor doesn't seem taxed, so I don't think that the render thread is slowing things down too much. I'm a little stumped as to what is causing the delay, maybe the mouseEvents aren't spawning fast enough? Any ideas would be greatly appreciated as I'm pretty stumped.
 
Saloon Keeper
Posts: 15712
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the sleep(), and try adding a yield() to the rendering thread. This is *not* a permanent solution, but it may give you an idea of what the problem might be. If yield() solves the problem, then it means your program might be suffering from starvation. The rendering thread is hogging the processor and it's not pausing enough to let the event dispatch thread do its work as well.

Best would be if you could post an SSCCE.
 
Daniel Croft
Greenhorn
Posts: 21
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried using Thread.yield(), but that seems to have no effect. I get pretty good results using a sleep of around 14 milliseconds; that seems to keep the fps at 60 and keeps the mouse responsive. It's kind of weird that completely removing the sleep doesn't result in any fps gains though. Also, I'm sorry but I don't know what an SSCCE is :/
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short, Self Contained, Correct (Compilable), Example

Click the link in Stephan's post

 
Daniel Croft
Greenhorn
Posts: 21
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh! I didn't realize that was a link. I'll try to trim it down somewhat but it might take a while, it's pretty involved already.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should do it anyways, not just for us. It will help you analyze non-trivial problems. Sometimes, with complicated problems, you cannot replicate the problem with the SSCCE. In this case, you start looking at the differrences between your code and SSCCE to figure out where it's going wrong. On the other hand, if the problem does happen with the SSCCE, atleast you have a simpler version of the actual code that you can do trial and error with. You can try different solutions to fix the SSCCE to find one that works, and then apply only the actual fix to the actual source.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic