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.system.lwjgl.LwjglAbstractDisplay;
import com.jme3.system.lwjgl.LwjglTimer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
@ -57,6 +59,12 @@ public class LwjglMouseInput implements MouseInput {
private boolean supportHardwareCursor = false;
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;
public LwjglMouseInput(LwjglAbstractDisplay context){
@ -132,6 +140,13 @@ public class LwjglMouseInput implements MouseInput {
return;
Mouse.destroy();
// Destroy the cursor cache
for (Cursor cursor : cursorMap.values()) {
cursor.destroy();
}
cursorMap.clear();
logger.fine("Mouse destroyed.");
}
@ -155,6 +170,8 @@ public class LwjglMouseInput implements MouseInput {
try {
Cursor newCursor = null;
if (jmeCursor != null) {
newCursor = cursorMap.get(jmeCursor);
if (newCursor == null) {
newCursor = new Cursor(
jmeCursor.getWidth(),
jmeCursor.getHeight(),
@ -163,6 +180,10 @@ public class LwjglMouseInput implements MouseInput {
jmeCursor.getNumImages(),
jmeCursor.getImagesData(),
jmeCursor.getImagesDelay());
// Add to cache
cursorMap.put(jmeCursor, newCursor);
}
}
Mouse.setNativeCursor(newCursor);
} catch (LWJGLException ex) {

Loading…
Cancel
Save