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. 39
      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){
@ -120,7 +128,7 @@ public class LwjglMouseInput implements MouseInput {
} }
if (btn != -1){ if (btn != -1){
MouseButtonEvent evt = new MouseButtonEvent(btn, MouseButtonEvent evt = new MouseButtonEvent(btn,
Mouse.getEventButtonState(), x, y); Mouse.getEventButtonState(), x, y);
evt.setTime(Mouse.getEventNanoseconds()); evt.setTime(Mouse.getEventNanoseconds());
listener.onMouseButtonEvent(evt); listener.onMouseButtonEvent(evt);
} }
@ -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,14 +170,20 @@ public class LwjglMouseInput implements MouseInput {
try { try {
Cursor newCursor = null; Cursor newCursor = null;
if (jmeCursor != null) { if (jmeCursor != null) {
newCursor = new Cursor( newCursor = cursorMap.get(jmeCursor);
jmeCursor.getWidth(), if (newCursor == null) {
jmeCursor.getHeight(), newCursor = new Cursor(
jmeCursor.getXHotSpot(), jmeCursor.getWidth(),
jmeCursor.getYHotSpot(), jmeCursor.getHeight(),
jmeCursor.getNumImages(), jmeCursor.getXHotSpot(),
jmeCursor.getImagesData(), jmeCursor.getYHotSpot(),
jmeCursor.getImagesDelay()); jmeCursor.getNumImages(),
jmeCursor.getImagesData(),
jmeCursor.getImagesDelay());
// Add to cache
cursorMap.put(jmeCursor, newCursor);
}
} }
Mouse.setNativeCursor(newCursor); Mouse.setNativeCursor(newCursor);
} catch (LWJGLException ex) { } catch (LWJGLException ex) {

Loading…
Cancel
Save