/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 if (show == HUD) {\n hud.draw();\n } else {\n dlg.draw();\n }\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 ui.clear();\n Vector<Actor> widges = hud;\n if (what == DLG) {\n widges = dlg;\n }\n Iterator iter = widges.iterator();\n while (iter.hasNext()) {\n ui.addActor(iter.next());\n }\n}\n\nvoid render() {\n 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>"}}