Skip to content

Commit

Permalink
Added pauseWhenMinimized and pauseWhenLostFocus to Lwjgl3 backend (#7287
Browse files Browse the repository at this point in the history
)
  • Loading branch information
obigu committed Apr 21, 2024
1 parent 2849926 commit c8918bb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- iOS: The iOS backend now implements AudioDevice. It can be configured through IOSApplicationConfiguration with audioDeviceBufferSize/audioDeviceBufferCount
- Fixed GlyphLayout for fixed width glyph offsets at the start and end of lines.
- Fixed scene2d.ui layout for fractional positions and sizes.
- LWJGL3: Added pauseWhenMinimized and pauseWhenLostFocus flags to Lwjgl3ApplicationConfiguration.
- libGDX is now built using Java 17 due to Gradle 8 requirements.
- New GDX Setup projects now use Gradle 8.4 and AGP Plugin 8.1.2 which require at least Java 17.
- Fixed Timer#stop, remember time spent stopped and delay tasks when started again. #7281
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public Lwjgl3Window newWindow (ApplicationListener listener, Lwjgl3WindowConfigu

private Lwjgl3Window createWindow (final Lwjgl3ApplicationConfiguration config, ApplicationListener listener,
final long sharedContext) {
final Lwjgl3Window window = new Lwjgl3Window(listener, config, this);
final Lwjgl3Window window = new Lwjgl3Window(listener, lifecycleListeners, config, this);
if (sharedContext == 0) {
// the main window is created immediately
createWindow(window, config, sharedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.PrintStream;
import java.nio.IntBuffer;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.LifecycleListener;
import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.GLFW;
Expand Down Expand Up @@ -66,6 +68,9 @@ public enum GLEmulation {
int idleFPS = 60;
int foregroundFPS = 0;

boolean pauseWhenMinimized = true;
boolean pauseWhenLostFocus = false;

String preferencesDirectory = ".prefs/";
Files.FileType preferencesFileType = FileType.External;

Expand Down Expand Up @@ -99,6 +104,8 @@ void set (Lwjgl3ApplicationConfiguration config) {
transparentFramebuffer = config.transparentFramebuffer;
idleFPS = config.idleFPS;
foregroundFPS = config.foregroundFPS;
pauseWhenMinimized = config.pauseWhenMinimized;
pauseWhenLostFocus = config.pauseWhenLostFocus;
preferencesDirectory = config.preferencesDirectory;
preferencesFileType = config.preferencesFileType;
hdpiMode = config.hdpiMode;
Expand Down Expand Up @@ -185,6 +192,18 @@ public void setForegroundFPS (int fps) {
this.foregroundFPS = fps;
}

/** Sets whether to pause the application {@link ApplicationListener#pause()} and fire
* {@link LifecycleListener#pause()}/{@link LifecycleListener#resume()} events on when window is minimized/restored. **/
public void setPauseWhenMinimized (boolean pauseWhenMinimized) {
this.pauseWhenMinimized = pauseWhenMinimized;
}

/** Sets whether to pause the application {@link ApplicationListener#pause()} and fire
* {@link LifecycleListener#pause()}/{@link LifecycleListener#resume()} events on when window loses/gains focus. **/
public void setPauseWhenLostFocus (boolean pauseWhenLostFocus) {
this.pauseWhenLostFocus = pauseWhenLostFocus;
}

/** Sets the directory where {@link Preferences} will be stored, as well as the file type to be used to store them. Defaults to
* "$USER_HOME/.prefs/" and {@link FileType#External}. */
public void setPreferencesConfig (String preferencesDirectory, Files.FileType preferencesFileType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.IntBuffer;

import com.badlogic.gdx.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWDropCallback;
Expand All @@ -28,10 +29,6 @@
import org.lwjgl.glfw.GLFWWindowMaximizeCallback;
import org.lwjgl.glfw.GLFWWindowRefreshCallback;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
Expand All @@ -40,6 +37,7 @@
public class Lwjgl3Window implements Disposable {
private long windowHandle;
final ApplicationListener listener;
private final Array<LifecycleListener> lifecycleListeners;
final Lwjgl3ApplicationBase application;
private boolean listenerInitialized = false;
Lwjgl3WindowListener windowListener;
Expand All @@ -62,9 +60,24 @@ public void invoke (long windowHandle, final boolean focused) {
public void run () {
if (windowListener != null) {
if (focused) {
if (config.pauseWhenLostFocus) {
synchronized (lifecycleListeners) {
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.resume();
}
}
}
windowListener.focusGained();
} else {
windowListener.focusLost();
if (config.pauseWhenLostFocus) {
synchronized (lifecycleListeners) {
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.pause();
}
}
listener.pause();
}
}
Lwjgl3Window.this.focused = focused;
}
Expand All @@ -84,9 +97,23 @@ public void run () {
}
Lwjgl3Window.this.iconified = iconified;
if (iconified) {
listener.pause();
if (config.pauseWhenMinimized) {
synchronized (lifecycleListeners) {
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.pause();
}
}
listener.pause();
}
} else {
listener.resume();
if (config.pauseWhenMinimized) {
synchronized (lifecycleListeners) {
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.resume();
}
}
listener.resume();
}
}
}
});
Expand Down Expand Up @@ -156,8 +183,10 @@ public void run () {
}
};

Lwjgl3Window (ApplicationListener listener, Lwjgl3ApplicationConfiguration config, Lwjgl3ApplicationBase application) {
Lwjgl3Window (ApplicationListener listener, Array<LifecycleListener> lifecycleListeners, Lwjgl3ApplicationConfiguration config,
Lwjgl3ApplicationBase application) {
this.listener = listener;
this.lifecycleListeners = lifecycleListeners;
this.windowListener = config.windowListener;
this.config = config;
this.application = application;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.badlogic.gdx.Graphics.DisplayMode;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.LifecycleListener;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics;
Expand All @@ -48,6 +49,22 @@ public static void main (String[] argv) throws NoSuchFieldException, SecurityExc

@Override
public void create () {
Gdx.app.addLifecycleListener(new LifecycleListener() {
@Override
public void pause () {
Gdx.app.log("LifecycleListener", "Application pause()");
}

@Override
public void resume () {
Gdx.app.log("LifecycleListener", "Application resume()");
}

@Override
public void dispose () {
Gdx.app.log("LifecycleListener", "Application dispose()");
}
});
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_4BYTE_ABGR);
texture = new Texture("data/badlogic.jpg");
batch = new SpriteBatch();
Expand Down

0 comments on commit c8918bb

Please sign in to comment.