Could someone please help me understand how I can properly dispose a Box2D World and Debug Renderer?
I have a playscreen that has a world and renderer and I would like to dispose of these when I change to another screen as I do not need them anymore. I have included in my playscreen dispose with the following, and called this manually when an event has been triggered to change screen. At the moment by calling these dispose() methods my game is crashing. Must a game have to have a Box2D world and renderer at all times? What would 'EXCEPTION_ACCESS_VIOLATION' mean?
#Override
public void dispose() {
System.out.println("PlayScreen disposed.");
world.dispose();
b2dr.dispose();
...
}
From my experience there are (at least) two situations where you can get an EXCEPTION_ACCESS_VIOLATION in libGDX-box2d:
world.dispose is called during world.step
world.dispose is called from a different thread
Related
When I press the home key and return to my game, a black screen occurs.
I read online that is because textures are lost.
So, I want to know how to keep the texture valid when I resume the game. Are there any reasons for the problem? Any advice will be appreciated.
Generally,There are two reasons for this problem.
Firstly,The resource of Texture have been disposed.
Secondly,You have load repeated resources.
My problem is the second problem.
Because I always create new world in Screen's show method.
My solution,I just need to judge whether the world is null.
I only need create the world when it is null.
Based on your reply (as an answer), you can check if the world is null by checking if it's null.
public void resume() {
If(world == null) {
System.out.println("world is null");
// create world
} else {
// do stuff when it's not null.
}
}
Of course you can reference the world by making it public static when you declare it in play state so you can use it in your core code that extends the ApplicationAdapter.
Edit: Running some tests, I guess you need to set world = null; after calling world.dispose; in the dispose() method.
Say I have a method that loads all my assets for a Screen, this method is called in that Screens constructor:
public void load(){
manager.load(pd_bg, Texture.class, textureParams);
}
Now when I exit that Screen, I have e method that unloads all these assets:
public void unLoad(){
manager.unload(pd_bg);
}
Inside my Screen I might use this asset for a Sprite, like so:
Sprite bg = new Sprite(GdxAssetManager.manager.get(GdxAssetManager.pd_bg, Texture.class));
Finally, do I need to dispose of the texture used in this sprite even though I call the unLoad() method? i.e:
public void dispose(){
GdxAssetManager.unLoad();
bg.getTexture.dispose(); //Is this line needed?
}
I am also wondering, if I load all resources when I start the app, should I then unload resources when I exit a Screen? How will they be loaded up next time then (since I only load the on launch)?.
I am using a Sprite as an example, but I guess the answer will be true for any asset.
No, you don't have to (and must not) dispose it when you unload it. In general, the rule of thumb goes: if you create it, you destroy it. So if you create a Texture (by using the new keyword) then you own that texture and are responsible for destroying it (calling its dispose method). In the case of AssetManager, it is the AssetManager who owns the resource and is responsible for destroying it.
To keep track of which resources needs to be created and destroyed, AssetManager uses reference counting. So it is important that you eventually call unload for everytime you call load.
And because you created the AssetManager using the new keyword, you own it and you are responsible for calling its dispose method:
public class MyGame extends ApplicationAdapter {
public AssetManager assetManager;
#Override public void create() {
assetManager = new AssetManager();
...
}
...
#Override public void create() {
assetManager.dispose();
assetManager = null;
...
}
}
Have a look at the documentation: https://github.com/libgdx/libgdx/wiki/Managing-your-assets
Btw, in your code it looks like you are using a static AssetManager. Although not related to your question, be aware that will lead to issues. So I'd advice you to implement a proper object oriented design instead of making things static.
As for your second question, it is unclear what you mean. If you mean when you should call AssetManager#unload, then the answer is whenever the class that called the corresponding AssetManager#load method no longer needs it.
For example, if you have an asset named "image.png" and you call the assetManager.load("image.png", Texture.class) in both your MyGame and MyScreen classes then you should call assetManager.unload("image.png") also in both your MyGame and MyScreen class.
To answer your last two questions:
In your launcher class, you should load every texture you need from all screens.
Then in your screens you aceess the images you need and you dont unload them and you also don't dispose them nor do you dispose the assetmanager.
Then in your launcher class in the
dispose(){}
method you first unload evereything and then call
assetmanager.dispose();
I'm dealing with a design issue.
Actually, i have a class GameManager, instantiated and updated by a Screen class. The class GameManager, as its name suggests, holds the game logic (transitions between levels, updates other managers, keeps data as score, money ecc...) and the game state (paused, running, game over, transition).
Developing the mechanism of transition between levels, while the transition phase is running, the game should not being updated, but i can't do that from the GameManager because the stages are updated in the Screen class.
I thought about 2 solutions:
1) I move all the game logic in the screen class, so by doing that, in the render method i can easily stop updating stages if i'm in the transition state.
2) Learn how to use finite-state machines in Libgdx
What could i do? Thank you for your help.
Update
I solved (but not avoided bad design) creating this class:
public class GameStateManager {
public static enum GameState {
RUNNING, PAUSE, GAME_OVER, LEVEL_TRANSITION;
}
private static GameState currentGameState;
public static GameState getCurrentGameState() {
return currentGameState;
}
public static void setCurrentGameState(GameState gameState) {
currentGameState = gameState;
}
}
In this way, in the act method of the Actors i can check if the current state of the game is RUNNING. If not, they don't act.
As much as i understand your situation, what you are trying to do is enabling and disabling stage update from a GameManager Class whose object is instantiated in a Screen. And stage is in Screen instead of GameManager. If i got you right then the easiest solution is create a public static boolean TRANSITION = false in Screen.
And enable/disable this from your game manager class by Screen.TRANSITION = true/false. So that stage.act() or whatever code that you want to get stopped when levels are loading can be haulted. Add this in screen class for eg.
if(!TRANSITION)
{ //code
stage.act();
}
I think you have an invalid relation between your classes. Currently your GameManager is owned by Screen. But I tend to think they should be on the same level in your class design table.
I can propose the following:
Create a new class Coordinator. It will own both Screen and GameManager.
Make simple event handling for your classes, so that Coordinator could subscribe to the events, fired by Screen and GameManager
Now, whenever GameManager thinks game situation changed, it can fire some event. Coordinator will decide what to do - to call Screen or not to. And vice versa.
Also, I think your GameManager now handles too much and should be converted into a module because you are heading straight towards God Object antipattern. And that means the whole class design could be changed.
I have developed an app in AS3 and Starling to be ported to IOS. I have updated the Default.png image and this works great however my app takes a while to load and a black screen is shown for about 3-4 seconds.
I have looked everywhere for a solution but couldn't find any that work. Does someone have a working solution?
Many thanks
I'm not sure if there is a neater solution at the moment but what I do is add a bitmap of the default screen to the native flash stage. Then when Starling is ready I remove the bitmap.
So before you instantiate Starling, add the bitmap image to the stage (this will be the Flash stage)
public static var _splash:Bitmap;
//load or embed your bitmap//
addChild(_splash);
Then instantiate and start Starling. e.g.
myStarling = new Starling(Main, stage, null, null, Context3DRenderMode.AUTO, Context3DProfile.BASELINE);
myStarling.stage3D.addEventListener(starling.events.Event.CONTEXT3D_CREATE, function(e:flash.events.Event):void {
// Starling is ready!
myStarling.start();
});
In your root Starling class (in this example it's Main), use an ADDED_TO_STAGE listener and when this is triggered, remove the bitmap.
public function Main() {
addEventListener(starling.events.Event.ADDED_TO_STAGE, onAdded);
}
private function onAdded ( e:starling.events.Event ):void {
StartUp._splash.parent.removeChild(StartUp._splash);
StartUp._splash = null;
}
In the example above the root document class is called 'StartUp'.
As described by docs there is Default.png used as splash screen in iOS.
Are there any opensource project to build reusable GUI components in QT4 with extra connection informaition saved in a JSON file? Qt designer lets you build a dialog and connect signals and slots together and save the results as a UI file. I am looking for a project that would extend this to build components that you could easily plugin into a c++ or pyside application. An example would be to build a playcontrol for a movie player with all the start, stop, rewind, fastforward buttons. Then in the application you just load the UI file or perhaps a JSON file with extra input and outputs for callbacks.
leeg, what you are talking about doing with JSON is fairly extensive. I have to agree with Stefan's comment that modeling your custom widgets as subclasses of QWidget is the correct approach.
You can easily drop in the classes, so to speak, into any Qt GUI application. I use this technique frequently when creating new GUI elements not already present in the standard Qt GUI library. You can set layouts (vertical and horizontal) and then nest widgets in the widgets to design complexly behaving widgets. From there, you can create custom slots and signals for handling different events.
For example, you mentioned a player control for a movie player with a start, stop, rewind, and fast forward button. You would need to create a containing widget and then add a horizontal layout to your containing widget. Then you can add buttons for start, stop, rewind, and fast forward. If you want images for your buttons, you can set the button images on the buttons themselves. Then you could emit signals for start, stop, rewind, and fast forward. In the calling object, you would then connect slots to each respective signal.
This skeleton should help you get started:
#include <QtCore>
#include <QtGui>
class PlayerControl : public QWidget
{
Q_OBJECT
signals:
void start();
void stop();
void rewind();
void fastForward();
private slots:
void startClicked();
void stopClicked();
void rewindClicked();
void fastForwardClicked();
public:
PlayerControl(QWidget *parent);
~PlayerControl();
private:
QPushButton startButton;
QPushButton stopButton;
QPushButton rewindButton;
QPushButton fastForwardButton;
}
void PlayerControl::PlayerControl(QWidget *parent)
{
// do setup here
connect(startButton, SIGNAL(clicked()), this, SLOT(startClicked()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stopClicked()));
connect(rewindButton, SIGNAL(clicked()), this, SLOT(rewindClicked()));
connect(fastForwardButton, SIGNAL(clicked()), this, SLOT(fastForwardClicked()));
}
void PlayerControl::~PlayerControl()
{
// clean up
}
void PlayerControl::startClicked()
{
emit start();
}
void PlayerControl::stopClicked()
{
emit stop();
}
void PlayerControl::rewindClicked()
{
emit rewind();
}
void PlayerControl::fastForwardClicked()
{
emit fastForward();
}