/2011/12/21/libgdx-multiple-stages/

{"item":{"status":"visible","url":"\/2011\/12\/21\/libgdx-multiple-stages\/","trash":"","name":"libgdx-multiple-stages","parent":"","title":"Libgdx: Creating multiple Stages degrades performace","date":"2011-12-21 11:16:07","filepath":"posts\/2000961_libgdx-multiple-stages.xml.json","type":"post","content":"While working on a game using the ever wonderful <a href=\"http:\/\/libgdx.badlogicgames.com\/\">libgdx<\/a>, I noticed slow performance when I was using two different <a href=\"http:\/\/libgdx.l33tlabs.org\/docs\/api\/com\/badlogic\/gdx\/scenes\/scene2d\/Stage.html\">Stages<\/a>. \u00a0One stage was the game HUD and the other served as a poorman's dialog.\n\nI didn't notice the choppy\u00a0performance\u00a0hiccups until after I called draw on the second stage then returned back to the original HUD stage.\n\nI had something like the following (<strong>WARNING:<\/strong> pseudocode):\n<p style=\"padding-left: 30px;\"><code>\nStage hud = new Stage();\nStage dlg = new Stage();\n...\nhud.addActor(widget1);\nhud.addActor(widget2);\ndlg.addActor(widget3);\n...\nvoid render() {\n&nbsp;&nbsp;if (show == HUD) {\n&nbsp;&nbsp;&nbsp;&nbsp;hud.draw();\n&nbsp;&nbsp;} else {\n&nbsp;&nbsp;&nbsp;&nbsp;dlg.draw();\n&nbsp;&nbsp;}\n}\n<\/code><\/p>\nAfter seeing the problem, I had a suspicion the performance degrades when swapping between Stages and I still am not sure why it does so. \u00a0At any rate, I fixed the problem by using only <strong>one<\/strong> Stage then adding and removing <a href=\"http:\/\/libgdx.l33tlabs.org\/docs\/api\/com\/badlogic\/gdx\/scenes\/scene2d\/Actor.html\">Actors<\/a> when needed.\n\nSo, the code ended up something like this:\n\n<code>\nStage ui = new Stage();\nVector<Actor> hud = new Vector<Actor>();\nhud.add(widget1);\nhud.add(widget2);\nVector<Actor> dlg = new Vector<Actor>();\ndlg.add(widget3);\n...\n\n\/\/ Call show() when you want to switch context\nvoid show(what) {\n&nbsp;&nbsp;ui.clear();\n&nbsp;&nbsp;Vector<Actor> widges = hud;\n&nbsp;&nbsp;if (what == DLG) {\n&nbsp;&nbsp;&nbsp;&nbsp;widges = dlg;\n&nbsp;&nbsp;}\n&nbsp;&nbsp;Iterator iter = widges.iterator();\n&nbsp;&nbsp;while (iter.hasNext()) {\n&nbsp;&nbsp;&nbsp;&nbsp;ui.addActor(iter.next());\n&nbsp;&nbsp;}\n}\n\nvoid render() {\n&nbsp;&nbsp;ui.draw();\n}\n<\/code>\n\nThat cleared up my performance trouble.\n\n<em>Find out more about libgdx at <a href=\"http:\/\/www.badlogicgames.com\">Badlogic Games<\/a><\/em>"}}
comments powered by Disqus
 

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

 
This page is best viewed with Firefox 3.5+, Chrome 5+, Safari 5+, Opera 10.6+, IE 9+
Copyright © 2014 8bitrobot.com