drawing - efficient android rendering -
i've read quite few tutorials on game programming on android, , of them provide same solution drawing game, having dedicated thread spinning this:
public void run() { while(true) { if(!surfaceholder.getsurface().isvalid()) continue; canvas canvas = surfaceholder.lockcanvas(); drawgame(canvas); /* actual drawing here */ surfaceholder.unlockcanvasandpost(canvas); } }
now i'm wondering, isn't wasteful? suppose i've game simple graphics, actual time in drawgame little; i'm going draw same things on , on, stealing cpu other threads; possibility skipping drawing , sleeping bit if game state hasn't changed, check having state update thread mantaining suitable status flag. maybe there other options. example, couldn'it possible synchronize rendering, don't post updates often? or missing , precisely lockcanvas does, blocks , burns no cpu until proper time?
thanks in advance
l.
i tutorials have seen wrong, want wait in main loop. 16 milliseconds target frame time in example below
public void run() { while(true) { long start = system.currenttimemillis(); if(!surfaceholder.getsurface().isvalid()) continue; canvas canvas = surfaceholder.lockcanvas(); drawgame(canvas); /* actual drawing here */ surfaceholder.unlockcanvasandpost(canvas); long frametime = system.currenttimemillis() - start; try { thread.sleep(math.max(0, 16 - ( frametime ))); } catch (interruptedexception e) { e.printstacktrace(); } } }
Comments
Post a Comment