Hello I am working out a game and I wonder how to dispose resources as I am experiencing memory problems.
I have something like this:
public SplashScreen implements Screen {
#Override
public void render(float delta) {
}
#Override
public void dispose() {
if (batch != null)
batch.dispose();
batch = null;
}
}
public MapScreen implements Screen {
#Override
public void render(float delta) {
}
#Override
public void show() {
splashScreenInstance.dispose();
}
#Override
public void dispose() {
if (mesh != null)
mesh.dispose();
mesh = null;
}
}
And I am disposing the splash screen as soon as the show method of MapScreen is called. Previously I'd settled the screen to the MapScree. Still the render method of the splashScreenInstance is called and I'd received null pointer exceptions. Why this is so?
I'd expect that once I set another screen, the previous one is no longer rendered. This is not seemingly so. I'd tried disposing right after setting the screen using the game instance, right after the hide method is called on the screen I want to dispose and finally on the show method of the next screen. All of these cases still renders the previous screen a few times before rendering the current one.
I really need to recover memory and also I don't want to test each time (On the render method) for null pointers as this has performance penalties.
Any suggestions?
Thanks
This is how I usually handle this problem:
public SplashScreen implements Screen {
#Override
public void render(float delta) {
// I assume that you have a reference to the game somewhere to switch the screen
game.setScreen(new MapScreen());
dispose();
return;
}
}
I first set the new Screen, then dispose() the current one and then immediately stop any further execution of the current screen via return. This way the current render cycle should be stopped and in the next cycle the render() of your next screen will be called.
Another approach might be to call dispose() in your hide() method of the Screens, because that is going to be the last method being called before the Game will use the next screen. This is especially useful when there could be several different next screens. In that case there will still be only a single place of dispose() and that will be in the hide() method.
Where are you calling setScreen? Since everything should be happening in the rendering thread (even InputListeners) you should be able to call setScreen in your first Screen and then return from the render method. The Game instance will automatically call hide on your first Screen which is where you can call dispose.
Related
I have a big problem.I wrote the whole game in same(one) class MyGdxGame extends from the ApplicationAdapter. How do I make the main menu? So that when opening the game, the main menu opens, in which 1 button, when pressed, opens the game itself (MyGdxGame)? Just tell me how to do it (what to create, what to change)
It's better to achieve high cohesion but when you code inside one class, cohesiveness become very low.
Use Screen and Game class to implement more than one screen as in your case like MainScreen, GameScreen, LevelScreen...
But if you don't want to implement that use flags for different screen in your MyGdxGame class.
public enum GameScreen{
MENU_SCREEN, GAME_SCREEN, LEVELSCREEN;
}
In render method of your MyGdxGame
public GameScreen currentScreen=GameScreen.MENU_SCREEN;
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(currentScreen==GameScreen.MENU_SCREEN){
//render objects for Menu Screen
}else if(currentScreen==GameScreen.GAME_SCREEN){
//render objects for Game Screen
}
}
Change value of currentScreen when you want to move on other screen.
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(currentScreen==GameScreen.MENU_SCREEN){
//detect button of MenuScreen by bounds or any other way
if(check in bound){
currentScreen==GameScreen.GAME_SCREEN;
}
}else if(currentScreen==GameScreen.GAME_SCREEN){
// for Game Screen
}
return false;
}
EDIT
Pelocho suggested to use abstract method inside enum.
public enum GameScreen{
MENU_SCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
}, GAME_SCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
}, LEVELSCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
};
public abstract void render(MyGdxGame gdxGame);
}
And inside render method of MyGdxGame
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
currentScreen.render(this);
}
You should do it properly instead of hacking a menu in. If you look at the Screen class it has the about the same methods as the ApplicationAdapter (show, render, resize, dispose, hide, pause, resume). If you would create a new class and have it implement Screen you can start to cut/paste from your main class to this screen class. If you have something in the Create method then either put it in the Show method or in the constructor. You then create another Screen for your main menu, options, credits, etc.
Have you main class MyGdxGame extend from Game instead of ApplicationAdapter delete all methods except the create method. In the create method you can load your assets and call your first screen like this setScreen(new MenuScreen()). For changing screens you have several options.
Pass the Game object allong with the screens so you can do game.setScreen(..)
Have some sort of screen manager with public static methods to change screens.
Gdx.app.getApplicationListener can be casted into game. This way you can change screen from anywhere in your LibGDX app. ((Game)Gdx.app.getApplicationListener).setScreen(myScreen)
If you are feeling scared you will destroy functionality, make a backup of you game in its current state. Abstracting code from big classes to new smaller classes is a important task for each programmer so it will be good practice. If you feel at any point that a certain piece of code should belong to another/new class feel free to do so. There are many ways to do things and keeping your code readable and understandable is your number 1 priority.
I want my button to appear whenever an object reaches a certain position, that object is a sprite that is generated every second:
public void create() {
if(spritePosition>700) {
buttonObj.createButton();
}
}
public void render() {
if (condition==true) {
stage.draw();
}
}
The problem is that when the games starts no Sprite is generated yet, so the result is an error. I'm also thinking of calling the createButton() method on the render method but it would generate a new button every frame because it's called constantly.
A simple way to let your button "disappear" is to just set its position to some position outside of the visible screen area.
For example something like:
buttonObj.setPosition(-1000, -1000);
To make it visible again you can just set the real coordinates again!
How about:
public void create() {
buttonObj.createButton();
buttonObj.setVisible(false);
}
public void render() {
if (condition==true) {
buttonObj.setVisible(true);
}
}
All actors in Scene2d have the setVisible method. simply try :
yourButton.setVisible(true)
or
yourButton.setVisible(false);
I have a splash screen and a menu screen class that loads all my texture atlases and skins for the menu and processes lots of stuff. If I put the constructor for the menu screen in the SplashScreen constructor or in the create() method of my main game class (MyGame class) then it would pass a lot of time with no splash screen render image on the phone while the whole stuff loads so I have delayed the loading of the menu class to the second frame render (gets called in the SplashScreen render method in the second frame); I simply check if I passed the first frame then I call a method on the main game class that loads the menus.
It all works fine , as expected , but only if the SplashScreen load process is not interrupted. If I get a call or I simple press the HOME button of the mobile and then I re-enter the game by clicking the icon on the home screen I get an AndroidGraphics error : "waiting for pause synchronization took too long: assuming dead lock and killing"; this is just after I can see the splash screen on the screen for about a second;
I tried to dispose the MyGame in the hide() and pause() methods of both the main game class and the splash screen class so it's terminated if I press the HOME button but they never get called.
How can I fix this? It would be annoying to get a call while loading the game and after you finish the call and try to re-enter the game it crashes.
public class MyGame extends Game{
...
public MainMenu menu;
...
#Override
public void create(){
this.screen_type == SCREEN_TYPE.SPLASH;
splashScreen = new SplashScreen();
setScreen(splashScreen);
}
...
#Override
public void pause(){
//never gets called if I press the HOME button in middle of splash screen
if(this.screen_type == SCREEN_TYPE.SPLASH)
{
this.dispose();
}
}
...
public void LoadMenuTimeConsumingConstructor(){
//load all menus and process data
main_menu = new MainMenu();
loaded_menu = true;
}
}
public class SplashScreen implements InputProcessor, Screen{
public MyGame main_game;
...
public SplashScreen(MyGame game){
this.main_game = game;
}
#Override
public void pause(){
//never gets called if I press the HOME button in middle of splash screen
if(main_game.screen_type == SCREEN_TYPE.SPLASH)
{
main_game.dispose();
}
}
#Override
public void hide(){
//never gets called if I press the HOME button in middle of splash screen
if(main_game.screen_type == SCREEN_TYPE.SPLASH)
{
main_game.dispose();
}
}
#Override
public void render(delta float){
...
//wait 1.5 sec
if(TimeUtils.millis() - startTime > 1500){
{
if(main_game.loaded_menu = true){
main_game.setScreen(main_game.main_menu);
}
}
...
if(is_this_second_frame){ // we start loading menus in the second frame so we already have the splash onscreen
main_game.LoadMenuTimeConsumingConstructor();
}
...
}
}
I do not understand your question very well, but if you are not using AssetManger Class, I think this read can help, I hope so.
If this response is not valid or not useful to you, tell me, and delete
wiki LibGDX
https://github.com/libgdx/libgdx/wiki/Managing-your-assets
video on YouTUBE
https://www.youtube.com/watch?v=JXThbQir2gU
other example in GitHub
https://github.com/Matsemann/libgdx-loading-screen
NEW look this:
Proper way to dispose screens in Libgdx
What's the right place to dispose a libgdx Screen
It can help you decide if you need to call dipose, and how, I hope it will help.
Before you read, this will be informative: Java JFrame won't show up after using .setVisible(true) after being invisible
Hello I am working on a library API that let's you capture an area of the screen, and it returns you a class that contains the ByteArrayInputStream and utility methods like createBufferedImage, createFile, etc.
Basically you create a Bootstrap instance, and pass the capturer type you want as a dependency (ScreenshotCapturer or GifCapturer):
Bootstrap b = new Bootstrap(new ScreenshotCapturer());
And the beginCapture method receives an object that implements ScreenCaptureCallback which is the callback event that the captured result will be passed to.
This is a short background.
Now when you use the beginCapture method, basically what it does is creates new instance of SelectionCamera, this is basically the component that paints the selection area you're selecting when dragging the mouse, and updates the listeners.
once created instance, it calls super.setVisible(true);
After that method gets called, the frame will show up, and also show the old painted screen for like 600-500miliseconds, I am not exactly sure, but it disappears so quickly.
Take a look at this live example:
Note use the video option, otherwise you will not see what I'm seeing as gif is too slow to show it!
http://gyazo.com/d2f0432ada37842966b42dfd87be4240
You can see after I click Screenshot again, it shows the old selected area and disappears quickly. (by the way the frame you see in the gif is not part of the app, just dummy hello world example usage).
The process of image capture.
Step 1
beginCapture gets called:
public void beginCapture(final ScreenCaptureCallback c) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
capturer.setCallback(c);
capturer.beginSelection();
}
});
}
Step 2
beginSelection gets called in the Capturer class (ScreenshotCapturer extends Capturer (abstract)
#Override
public void beginSelection() {
super.init();
this.setHotkeys();
super.getCamera().startSelection();
}
Step 3
CaptureCamera#startSelection() gets called
public void startSelection() {
super.getContentPane().removeAll();
super.getContentPane().repaint();
super.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
this.selector = new SelectionCamera();
this.selectionMosueAdapter.updateCamera(this.selector);
this.selectionMouseMotion.updateCamera(this.selector);
super.add(this.selector);
super.setVisible(true);
super.repaint();
super.getContentPane().repaint();
}
Step 4
The user selects an area, and both mouse listener and mouse motion listens to it(Take a look at mouse motion):
#Override
public void mouseDragged(MouseEvent e) {
Point dragPoint = e.getPoint();
Point startPoint = this.selector.getStartPoint();
int x = Math.min(startPoint.x, dragPoint.x);
int y = Math.min(startPoint.y, dragPoint.y);
int width = Math.max(startPoint.x - dragPoint.x, dragPoint.x - startPoint.x);
int height = Math.max(startPoint.y - dragPoint.y, dragPoint.y - startPoint.y);
this.selector.setCameraDimension(width, height);
this.selector.setCoordinates(x, y);
this.camera.repaint(); // important
}
by the way this.selector is SelectorCamera which is the component that paints the selection area.
Step 5
CaptureCamera#endSelection() gets called, this method gets the x,y, width, height from the selection camera and passes it to the capturer class which uses Robot to get screenshot with that rectangle, and before that it removes ALL components from the content pane, and repaints everything and then sets visibility to false.
public void endSelection() {
super.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
int x = this.selector.getCameraX();
int y = this.selector.getCameraY();
int w = this.selector.getCameraWidth();
int h = this.selector.getCameraHeight();
super.getContentPane().removeAll();
super.getContentPane().repaint();
//super.repaint();
super.setVisible(false);
this.c.startCapturing(x, y, w, h);
}
Basically this is the last step, rest steps are unnecessary for the debugging as it only sends back the callback.
I really tried my best explaining the process of my application, I've tried figuring it out for 5 and half hours now, and no luck at all. Tried different ways, by creating new SelectionCamera object as you see, doesn't work.
Why is it doing this? Is it something to do with the swing core?
SelectionCamera code: https://github.com/BenBeri/WiseCapturer/blob/master/src/il/ben/wise/SelectionCamera.java
Thanks in advance.
Based on this example...
try {
final Bootstrap b = new Bootstrap(new ScreenshotCapturer());
b.beginCapture(new ScreenCaptureCallback() {
#Override
public void captureEnded(CapturedImage img) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
b.beginCapture(new ScreenCaptureCallback() {
#Override
public void captureEnded(CapturedImage img) {
System.out.println("...");
JFrame frame = new JFrame();
frame.add(new JLabel(new ImageIcon(img.getBufferedImage())));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
});
}
});
System.out.println("Hello");
} catch (AWTException exp) {
exp.printStackTrace();
}
I won't focus on the initialise stage of the first round, I will focus on the initialisation of the second round as this is where the problem is...
b.beginCapture call's this.capturer.beginSelection();, which calls super.getCamera().startSelection(); which calls setVisible(true) (CaptureCamera been a JFrame).
This will immediately show what ever was previously displayed on the CaptureCamera. It's important to note here, that no new instances of objects were created through the process...
Now, I made a lot of changes to the base testing this, but it appears that the problem is with the restoration of the frame when it's made visible for the second time. This seems to be an issue with the transparency support of the Window as it seems to restore the last "known" state instead of repainting the window immediately...
Now, I tried clearing the selector before making the CaptureCamera invisible to no eval, as the window seems to be made invisible before the selector is painted.
The final solution I came up with was to call dispose on the CaptureCamera, which releases it's native peer and therefore destroys the old graphics context, forcing the frame to rebuild itself when it is made visible again.
"A" problem with this could be the fact that when all the windows are disposed (and the only running threads are daemon threads), the JVM will exit...
This was an issue during my testing as I was using a javax.swing.Timer to put a delay between the first and second capture process so I could see where the problem was occurring and this caused my JVM to exit (as the timer uses a daemon thread and I had no other windows open).
I got around this by creating a tiny (1x1) transparent window in the Capturer class, keep this in mind if the JVM exists gracefully for no reason ;)
Side Notes...
Now, there is an issue with SelectionCamera (which extends JPanel), it is opaque, but is using a transparent background, this is incredibly dangerous as Swing only knows how to deal with opaque or fully transparent components.
public SelectionCamera() {
super.setBackground(new Color(0, 0, 0, 0));
super.setVisible(false);
}
Should be updated to something like...
public SelectionCamera() {
setOpaque(false);
//super.setBackground(new Color(0, 0, 0, 0));
super.setVisible(false);
}
I'm also confused over the use of super.xxx, the only reason you would do this is if you had overrriden those methods and didn't want to call them at this time...In my testing, I removed all the calls to super where a method wasn't overridden in the current class (and I wasn't already in the overriden method)
Also, the paintComponent method should be calling super.paintComponent
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(0, 0, 0, 0.5f));
g.fillRect(this.x, this.y, this.width, this.height);
}
Make Frame left to be -10,000 then set visible true, add a timer 2 seconds (try lower to 25-100 milliseconds, just to give it sligth pause to invalidate content) , on timer :left to 0 . I think it works due to caching & double buffereing. Frame shows what it had in buffer, buffer points to old image due to caching/ lazy repaint.
Alternative :
Maybe a repaint or invalidate before your show would work too, and don't need to do the left -10,000. I dont work much with ui-swing, just a but years back and remember some strange things like this.
I have a list, each item of which has several things in it, including a JProgressBar which can be updated a lot. Each time one of the items updates its JProgressBar, the ListDataListener on the list tries to scroll it to the visible range using
/*
* This makes the updating content item automatically scroll
* into view if it is off the viewport.
*/
public void contentsChanged(final ListDataEvent evt) {
if (!EventQueue.isDispatchThread()) {
/**
* Make sure the scrolling happens in the graphics "dispatch" thread.
*/
EventQueue.invokeLater(new Runnable() {
public void run() {
contentsChanged(evt);
}
});
}
if (playbackInProgress) {
int index = evt.getIndex0();
currentContentList.ensureIndexIsVisible(index);
}
}
Note that I'm trying to make sure the scrolling is done in the dispatch thread, since I thought maybe the problem was it being scrolled while it was repainting. And yet, I still have a problem where if things are really active, some of the list items paint outside of the viewport, overwriting what's outside the JScrollPane. Forcing an exposure event will repaint those things, but it's annoying.
Is there anything else I need to look out for to stop these things painting outside of their clipping area?
Have you tried explicitly enabling double-buffering on the JList and/or the components that it is drawing over? (with:setDoubleBuffered(boolean aFlag))
Another thought is that you might need to exit the function immediately after delegating to the EDT. The way your code is written, it looks like the update will happen in both threads if ContentChanged is invoked from a non-EDT thread. Logging in the first if (or set a breakpoint in the if -- but not in the runnable -- should help determine if that is your problem.
eg:
public void contentsChanged(final ListDataEvent evt)
{
if (!EventQueue.isDispatchThread())
{
log.debug("Delegating contentsChanged(...) to EDT");
EventQueue.invokeLater(new Runnable()
{
public void run()
{
contentsChanged(evt);
}
});
// don't run ensureIndexIsVisible twice:
return;
}
if (playbackInProgress)
{
int index = evt.getIndex0();
currentContentList.ensureIndexIsVisible(index);
}
}