awt - Java weird render on images -
allow me begin mentioning description may not understandable. have trouble finding words describe problem, compiled code executable jar may run try understand saying.
here link: http://www.mediafire.com/download/r1n0ox8ioyzwb2n/work.jar
as image moved (via arrow keys) direction image have weird effect. can describe effect image being drawn top left bottom right slow enough visibly noticeable. if had guess believe may have sleeping being done in game loop. here game loop , render method.
public void gameloop() { final int fps = 60; int frames = 0; lasttime = system.nanotime(); long lastfps; lasttime = lastfps = system.nanotime(); init(); while (running) { long deltatime = system.nanotime() - lasttime; lasttime += deltatime; if (!frame.isfocusowner()||!isfocusowner()) update(deltatime); else{ updatefocus(deltatime); } // must setup these do-while loops according // bufferstrategy's javadoc render(); // calculate fps frames++; if (system.nanotime() - lastfps >= 1e9) { fpsvalue = frames; frames = 0; lastfps += 1e9; } // gets remaining number of milliseconds left in frame long sleeptime = math .round((1e9 / fps - (system.nanotime() - lasttime)) / 1e6); if (sleeptime <= 0) continue; // sleeping method uses thread.sleep(1) first 4/5 of // sleep loop, , thread.yield() rest. gives me // accuracy of 3-4 microseconds long prev = system.nanotime(), diff; while ((diff = system.nanotime() - prev) < sleeptime) { if (diff < sleeptime * 0.8) try { thread.sleep(1); } catch (exception exc) { } else thread.yield(); } sync(60); } stop(); } public void sync(int sync) { if (sync!=-1) { long diff = 1000000000l / sync + lasttime; long = system.nanotime(); try { while (diff > now) { thread.sleep((diff-now) / 2000000l); = system.nanotime(); } } catch (exception e) {} lasttime = now; } } public void render() { bufferstrategy strategy = getbufferstrategy(); if (strategy == null) { createbufferstrategy(3); return; } { { graphics2d g = (graphics2d) strategy.getdrawgraphics(); // draw game g.drawimage(image, 0, 0, getwidth(), getheight(), null); render.setgraphics(g); // clear screen render.clear(); if (sbg != null) { try { sbg.renderstate(render, g); // if not focused draw focus nagger if (!frame.isfocusowner() && !isfocusowner() && sbg.getnagstate() != null) { sbg.getnagstate().render(render, g); } } catch (vulpusexception e) { e.printstacktrace(); } } if (drawfps) { g.setcolor(color.white); g.drawstring("fps: " + fpsvalue, 15, 20); } g.dispose(); toolkit.getdefaulttoolkit().sync(); } while (strategy.contentsrestored()); strategy.show(); } while (strategy.contentslost()); }
the sync method allows run @ 60fps, otherwise game running 1000+ fps. if have idea's / suggestions / need me clarify please ask. again in advance.
Comments
Post a Comment