Static sprite batch?

两盒软妹~` 提交于 2019-12-23 15:33:09

问题


As my research goes everywhere it's said that we should only use one sprite batch per instance. But what if i set sprite batch to static in class which controls the game (calls different screens etc).

When i have tryed this its working like charm, but is that a good practice or it may result some problems in future. Then if it could be done can i also share stage and shape renderer?

note: the main reason why i am trying to go with that "static batch technique" is that java crashes when i try to dispose stage, sprite batch or shape renderer


回答1:


I do it that way as well and I didn't run into any problems so far.

You need to be careful with the SpriteBatch.begin() and SpriteBatch.end() calls, since you may never know where it was started already, and where it was stopped when it is shared.

Same applies for the Camera/ProjectionMatrix. And also for the currently used ShaderProgram in case you are working with shaders.

Make sure that you are always fully aware of where you are doing what with your SpriteBatch otherwise you might face weird bugs one day.

Stage should not be shared, since it will probably serve different purposes. For different UIs it doesn't make any sense to use only one Stage.

ShapeRenderer could be shared, since it kind of works the same like SpriteBatch, but probably you only want to use it in a single place.

dispose() should actually work without any problems. You have to make sure that you dispose everything in the correct order so nothing will be left over in the end and cause exceptions.




回答2:


As @noone allready said it should not cause any problems, if you take care. But i just wanted to point out another thing: In some Screens (MenuScreen, OptionScreen, ...) you may use Scene2dUi for some UI stuff. So you will use Stages, which belong to the Screen and are not shared. So in this case you should remember, that every Stage has its own SpriteBatch and Camera. So it could be, that you only need an additional SpriteBatch in your GameScreen. If this is the case i would not make it static and have it only in the GameScreen. As noone said dispose() should work, if you use the correct order, and so also going with more then 1 SpriteBatch should not cause any problems. So i think both ways will work and are good approaches. It depends on your design and how/where you want to use the SpriteBatches and Cameras.



来源:https://stackoverflow.com/questions/22121694/static-sprite-batch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!