Added a cursor cache to avoid cursor disappearing and app crashing when too many cursors are created

define_list_fix
Toni Helenius 9 years ago
parent 671be243b0
commit f2384c8063
  1. 21
      jme3-lwjgl/src/main/java/com/jme3/input/lwjgl/LwjglMouseInput.java

@ -39,6 +39,8 @@ import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent; import com.jme3.input.event.MouseMotionEvent;
import com.jme3.system.lwjgl.LwjglAbstractDisplay; import com.jme3.system.lwjgl.LwjglAbstractDisplay;
import com.jme3.system.lwjgl.LwjglTimer; import com.jme3.system.lwjgl.LwjglTimer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.lwjgl.LWJGLException; import org.lwjgl.LWJGLException;
@ -57,6 +59,12 @@ public class LwjglMouseInput implements MouseInput {
private boolean supportHardwareCursor = false; private boolean supportHardwareCursor = false;
private boolean cursorVisible = true; private boolean cursorVisible = true;
/**
* We need to cache the cursors
* (https://github.com/jMonkeyEngine/jmonkeyengine/issues/537)
*/
private Map<JmeCursor, Cursor> cursorMap = new HashMap<JmeCursor, Cursor>();
private int curX, curY, curWheel; private int curX, curY, curWheel;
public LwjglMouseInput(LwjglAbstractDisplay context){ public LwjglMouseInput(LwjglAbstractDisplay context){
@ -132,6 +140,13 @@ public class LwjglMouseInput implements MouseInput {
return; return;
Mouse.destroy(); Mouse.destroy();
// Destroy the cursor cache
for (Cursor cursor : cursorMap.values()) {
cursor.destroy();
}
cursorMap.clear();
logger.fine("Mouse destroyed."); logger.fine("Mouse destroyed.");
} }
@ -155,6 +170,8 @@ public class LwjglMouseInput implements MouseInput {
try { try {
Cursor newCursor = null; Cursor newCursor = null;
if (jmeCursor != null) { if (jmeCursor != null) {
newCursor = cursorMap.get(jmeCursor);
if (newCursor == null) {
newCursor = new Cursor( newCursor = new Cursor(
jmeCursor.getWidth(), jmeCursor.getWidth(),
jmeCursor.getHeight(), jmeCursor.getHeight(),
@ -163,6 +180,10 @@ public class LwjglMouseInput implements MouseInput {
jmeCursor.getNumImages(), jmeCursor.getNumImages(),
jmeCursor.getImagesData(), jmeCursor.getImagesData(),
jmeCursor.getImagesDelay()); jmeCursor.getImagesDelay());
// Add to cache
cursorMap.put(jmeCursor, newCursor);
}
} }
Mouse.setNativeCursor(newCursor); Mouse.setNativeCursor(newCursor);
} catch (LWJGLException ex) { } catch (LWJGLException ex) {

Loading…
Cancel
Save