java - Android - active rendering in game loop? -
best way render in game loop on android?
when i'm rendering in java know how this. create buffer strategy, graphics, draw, dispose , flip buffer. there andorid equivalent of that?
looked view, doesn't work correctly when try draw lot. looked surfaceview than, can't understand how can refresh drawing on it. invalidate breaks loop, postinvalidate doesn't work. lock canvas , draw on it, , unlock , post in "create" method surface view (locking canvas in loop doesn't work, white screen in app appears). don't it.
what's efficient way render heavily in android?
the common approach render within surfaceview through surfaceholder.
usually you'll canvas through holder , draw on it:
surfaceholder surfaceholder = surfaceview.getholder(); canvas canvas = surfaceholder.getsurface().lockcanvas(); //draw in canvas canvas.drawpoint(...); surfaceholder.unlockcanvasandpost(canvas);
the correct approach loop code between lockcanvas()
, unlockcanvasandpost()
(inclusive) in separate thread (the render thread) , control framerate thread.sleep()
inside loop.
edit:
there many ways control fps of render thread, basic one, set wanted fps on constant:
public class renderthread extends thread { private boolean running; private final static int max_fps = 30; private final static int frame_period = 1000 / max_fps; @override public void run() { long begintime; long timediff; int sleeptime; sleeptime = 0; while (running) { begintime = system.currenttimemillis(); //render // how long did render take timediff = system.currenttimemillis() - begintime; // calculate sleep time sleeptime = (int)(frame_period - timediff); if (sleeptime > 0) { try { thread.sleep(sleeptime); } catch (interruptedexception e) {} } } } }
there lots of references if search game loops.
Comments
Post a Comment