fixed some crap to work with stupid lwjgl display-mouse-keyboard combo

master
Ondřej Hruška 10 years ago
parent 476ca8ba61
commit d2fe36f745
  1. 12
      .classpath
  2. 129
      src/mightypork/gamecore/backends/lwjgl/LwjglInputModule.java
  3. 2
      src/mightypork/gamecore/backends/lwjgl/audio/SlickAudio.java
  4. 7
      src/mightypork/gamecore/backends/lwjgl/graphics/LwjglGraphicsModule.java

@ -6,8 +6,16 @@
<classpathentry combineaccessrules="false" kind="src" path="/MightyUtils"/> <classpathentry combineaccessrules="false" kind="src" path="/MightyUtils"/>
<classpathentry kind="lib" path="lib/jogg-0.0.7.jar" sourcepath="lib/lwjgl-source-2.8.4.zip"/> <classpathentry kind="lib" path="lib/jogg-0.0.7.jar" sourcepath="lib/lwjgl-source-2.8.4.zip"/>
<classpathentry kind="lib" path="lib/jorbis-0.0.15.jar" sourcepath="lib/lwjgl-source-2.8.4.zip"/> <classpathentry kind="lib" path="lib/jorbis-0.0.15.jar" sourcepath="lib/lwjgl-source-2.8.4.zip"/>
<classpathentry kind="lib" path="lib/lwjgl_util.jar" sourcepath="lib/lwjgl-source-2.8.4.zip"/> <classpathentry kind="lib" path="lib/lwjgl_util.jar" sourcepath="lib/lwjgl-source-2.8.4.zip">
<classpathentry kind="lib" path="lib/lwjgl.jar" sourcepath="lib/lwjgl-source-2.8.4.zip"/> <attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="GameCore-LWJGL/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/lwjgl.jar" sourcepath="lib/lwjgl-source-2.8.4.zip">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="GameCore-LWJGL/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/slick-util.jar" sourcepath="lib/slick-util-src.zip"/> <classpathentry kind="lib" path="lib/slick-util.jar" sourcepath="lib/slick-util-src.zip"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

@ -9,6 +9,7 @@ import mightypork.gamecore.input.events.KeyEvent;
import mightypork.gamecore.input.events.MouseButtonEvent; import mightypork.gamecore.input.events.MouseButtonEvent;
import mightypork.gamecore.input.events.MouseMotionEvent; import mightypork.gamecore.input.events.MouseMotionEvent;
import mightypork.utils.interfaces.Updateable; import mightypork.utils.interfaces.Updateable;
import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.vect.Vect; import mightypork.utils.math.constraints.vect.Vect;
import mightypork.utils.math.constraints.vect.var.VectVar; import mightypork.utils.math.constraints.vect.var.VectVar;
@ -24,19 +25,19 @@ import org.lwjgl.opengl.Display;
* @author Ondřej Hruška (MightyPork) * @author Ondřej Hruška (MightyPork)
*/ */
public class LwjglInputModule extends InputModule implements Updateable { public class LwjglInputModule extends InputModule implements Updateable {
/** Current mouse position */ /** Current mouse position */
private static final Vect mousePos = new Vect() { private static final Vect mousePos = new Vect() {
@Override @Override
public double x() public double x()
{ {
if (!Mouse.isInsideWindow()) return Integer.MIN_VALUE; if (!Mouse.isInsideWindow()) return Integer.MIN_VALUE;
return Mouse.getX(); return Mouse.getX();
} }
@Override @Override
public double y() public double y()
{ {
@ -45,26 +46,36 @@ public class LwjglInputModule extends InputModule implements Updateable {
return Display.getHeight() - Mouse.getY(); return Display.getHeight() - Mouse.getY();
} }
}; };
@Override @Override
protected void initDevices() protected void initDevices()
{ {
try { try {
Mouse.create(); tryCreate();
Keyboard.create();
Keyboard.enableRepeatEvents(false); Keyboard.enableRepeatEvents(false);
} catch (final LWJGLException e) { } catch (final LWJGLException e) {
throw new RuntimeException("Failed to initialize input devices.", e); throw new RuntimeException("Failed to initialize input devices.", e);
} }
} }
private void tryCreate() throws LWJGLException
{
if (Display.isCreated()) {
Mouse.create();
Keyboard.create();
}
}
@Override @Override
protected void initKeyCodes() protected void initKeyCodes()
{ {
Keys.NONE.setCode(Keyboard.KEY_NONE); Keys.NONE.setCode(Keyboard.KEY_NONE);
Keys.NUM_1.setCode(Keyboard.KEY_1); Keys.NUM_1.setCode(Keyboard.KEY_1);
Keys.NUM_2.setCode(Keyboard.KEY_2); Keys.NUM_2.setCode(Keyboard.KEY_2);
Keys.NUM_3.setCode(Keyboard.KEY_3); Keys.NUM_3.setCode(Keyboard.KEY_3);
@ -75,7 +86,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.NUM_8.setCode(Keyboard.KEY_8); Keys.NUM_8.setCode(Keyboard.KEY_8);
Keys.NUM_9.setCode(Keyboard.KEY_9); Keys.NUM_9.setCode(Keyboard.KEY_9);
Keys.NUM_0.setCode(Keyboard.KEY_0); Keys.NUM_0.setCode(Keyboard.KEY_0);
Keys.Q.setCode(Keyboard.KEY_Q); Keys.Q.setCode(Keyboard.KEY_Q);
Keys.W.setCode(Keyboard.KEY_W); Keys.W.setCode(Keyboard.KEY_W);
Keys.E.setCode(Keyboard.KEY_E); Keys.E.setCode(Keyboard.KEY_E);
@ -102,7 +113,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.B.setCode(Keyboard.KEY_B); Keys.B.setCode(Keyboard.KEY_B);
Keys.N.setCode(Keyboard.KEY_N); Keys.N.setCode(Keyboard.KEY_N);
Keys.M.setCode(Keyboard.KEY_M); Keys.M.setCode(Keyboard.KEY_M);
Keys.MINUS.setCode(Keyboard.KEY_MINUS); Keys.MINUS.setCode(Keyboard.KEY_MINUS);
Keys.EQUALS.setCode(Keyboard.KEY_EQUALS); Keys.EQUALS.setCode(Keyboard.KEY_EQUALS);
Keys.SLASH.setCode(Keyboard.KEY_SLASH); Keys.SLASH.setCode(Keyboard.KEY_SLASH);
@ -114,17 +125,17 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.GRAVE.setCode(Keyboard.KEY_GRAVE); Keys.GRAVE.setCode(Keyboard.KEY_GRAVE);
Keys.COMMA.setCode(Keyboard.KEY_COMMA); Keys.COMMA.setCode(Keyboard.KEY_COMMA);
Keys.PERIOD.setCode(Keyboard.KEY_PERIOD); Keys.PERIOD.setCode(Keyboard.KEY_PERIOD);
Keys.SPACE.setCode(Keyboard.KEY_SPACE); Keys.SPACE.setCode(Keyboard.KEY_SPACE);
Keys.BACKSPACE.setCode(Keyboard.KEY_BACK); Keys.BACKSPACE.setCode(Keyboard.KEY_BACK);
Keys.TAB.setCode(Keyboard.KEY_TAB); Keys.TAB.setCode(Keyboard.KEY_TAB);
Keys.ESCAPE.setCode(Keyboard.KEY_ESCAPE); Keys.ESCAPE.setCode(Keyboard.KEY_ESCAPE);
Keys.APPS.setCode(Keyboard.KEY_APPS); Keys.APPS.setCode(Keyboard.KEY_APPS);
Keys.POWER.setCode(Keyboard.KEY_POWER); Keys.POWER.setCode(Keyboard.KEY_POWER);
Keys.SLEEP.setCode(Keyboard.KEY_SLEEP); Keys.SLEEP.setCode(Keyboard.KEY_SLEEP);
//Keys.MENU.setCode(Keyboard.KEY_MENU); // not defined //Keys.MENU.setCode(Keyboard.KEY_MENU); // not defined
Keys.F1.setCode(Keyboard.KEY_F1); Keys.F1.setCode(Keyboard.KEY_F1);
Keys.F2.setCode(Keyboard.KEY_F2); Keys.F2.setCode(Keyboard.KEY_F2);
Keys.F3.setCode(Keyboard.KEY_F3); Keys.F3.setCode(Keyboard.KEY_F3);
@ -140,11 +151,11 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.F13.setCode(Keyboard.KEY_F13); Keys.F13.setCode(Keyboard.KEY_F13);
Keys.F14.setCode(Keyboard.KEY_F14); Keys.F14.setCode(Keyboard.KEY_F14);
Keys.F15.setCode(Keyboard.KEY_F15); Keys.F15.setCode(Keyboard.KEY_F15);
Keys.CAPS_LOCK.setCode(Keyboard.KEY_CAPITAL); Keys.CAPS_LOCK.setCode(Keyboard.KEY_CAPITAL);
Keys.SCROLL_LOCK.setCode(Keyboard.KEY_SCROLL); Keys.SCROLL_LOCK.setCode(Keyboard.KEY_SCROLL);
Keys.NUM_LOCK.setCode(Keyboard.KEY_NUMLOCK); Keys.NUM_LOCK.setCode(Keyboard.KEY_NUMLOCK);
Keys.NUMPAD_MINUS.setCode(Keyboard.KEY_SUBTRACT); Keys.NUMPAD_MINUS.setCode(Keyboard.KEY_SUBTRACT);
Keys.NUMPAD_PLUSS.setCode(Keyboard.KEY_ADD); Keys.NUMPAD_PLUSS.setCode(Keyboard.KEY_ADD);
Keys.NUMPAD_0.setCode(Keyboard.KEY_NUMPAD0); Keys.NUMPAD_0.setCode(Keyboard.KEY_NUMPAD0);
@ -161,7 +172,7 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.NUMPAD_ENTER.setCode(Keyboard.KEY_NUMPADENTER); Keys.NUMPAD_ENTER.setCode(Keyboard.KEY_NUMPADENTER);
Keys.NUMPAD_DIVIDE.setCode(Keyboard.KEY_DIVIDE); Keys.NUMPAD_DIVIDE.setCode(Keyboard.KEY_DIVIDE);
Keys.NUMPAD_MULTIPLY.setCode(Keyboard.KEY_MULTIPLY); Keys.NUMPAD_MULTIPLY.setCode(Keyboard.KEY_MULTIPLY);
Keys.CONTROL_LEFT.setCode(Keyboard.KEY_LCONTROL); Keys.CONTROL_LEFT.setCode(Keyboard.KEY_LCONTROL);
Keys.CONTROL_RIGHT.setCode(Keyboard.KEY_RCONTROL); Keys.CONTROL_RIGHT.setCode(Keyboard.KEY_RCONTROL);
Keys.ALT_LEFT.setCode(Keyboard.KEY_LMENU); Keys.ALT_LEFT.setCode(Keyboard.KEY_LMENU);
@ -170,47 +181,55 @@ public class LwjglInputModule extends InputModule implements Updateable {
Keys.SHIFT_RIGHT.setCode(Keyboard.KEY_RSHIFT); Keys.SHIFT_RIGHT.setCode(Keyboard.KEY_RSHIFT);
Keys.META_LEFT.setCode(Keyboard.KEY_LMETA); Keys.META_LEFT.setCode(Keyboard.KEY_LMETA);
Keys.META_RIGHT.setCode(Keyboard.KEY_RMETA); Keys.META_RIGHT.setCode(Keyboard.KEY_RMETA);
Keys.UP.setCode(Keyboard.KEY_UP); Keys.UP.setCode(Keyboard.KEY_UP);
Keys.DOWN.setCode(Keyboard.KEY_DOWN); Keys.DOWN.setCode(Keyboard.KEY_DOWN);
Keys.LEFT.setCode(Keyboard.KEY_LEFT); Keys.LEFT.setCode(Keyboard.KEY_LEFT);
Keys.RIGHT.setCode(Keyboard.KEY_RIGHT); Keys.RIGHT.setCode(Keyboard.KEY_RIGHT);
Keys.HOME.setCode(Keyboard.KEY_HOME); Keys.HOME.setCode(Keyboard.KEY_HOME);
Keys.END.setCode(Keyboard.KEY_END); Keys.END.setCode(Keyboard.KEY_END);
Keys.PAGE_UP.setCode(Keyboard.KEY_PRIOR); Keys.PAGE_UP.setCode(Keyboard.KEY_PRIOR);
Keys.PAGE_DOWN.setCode(Keyboard.KEY_NEXT); Keys.PAGE_DOWN.setCode(Keyboard.KEY_NEXT);
Keys.RETURN.setCode(Keyboard.KEY_RETURN); Keys.RETURN.setCode(Keyboard.KEY_RETURN);
Keys.PAUSE.setCode(Keyboard.KEY_PAUSE); Keys.PAUSE.setCode(Keyboard.KEY_PAUSE);
Keys.INSERT.setCode(Keyboard.KEY_INSERT); Keys.INSERT.setCode(Keyboard.KEY_INSERT);
Keys.DELETE.setCode(Keyboard.KEY_DELETE); Keys.DELETE.setCode(Keyboard.KEY_DELETE);
Keys.SYSRQ.setCode(Keyboard.KEY_SYSRQ); Keys.SYSRQ.setCode(Keyboard.KEY_SYSRQ);
} }
@Override @Override
public void destroy() public void destroy()
{ {
Mouse.destroy(); Mouse.destroy();
Keyboard.destroy(); Keyboard.destroy();
} }
private final VectVar mouseMove = Vect.makeVar(); private final VectVar mouseMove = Vect.makeVar();
private final VectVar mouseLastPos = Vect.makeVar(); private final VectVar mouseLastPos = Vect.makeVar();
@Override @Override
public synchronized void update(double delta) public synchronized void update(double delta)
{ {
// was destroyed or not initialized // was destroyed or not initialized
if (!Display.isCreated()) return; if (!Display.isCreated()) return;
if (!Mouse.isCreated()) return;
if (!Keyboard.isCreated()) return; if (!Mouse.isCreated() || !Keyboard.isCreated()) {
try {
tryCreate();
} catch (final LWJGLException e) {
Log.e(e);
}
}
if (!Mouse.isCreated() || !Keyboard.isCreated()) return;
Display.processMessages(); Display.processMessages();
// sum the moves // sum the moves
mouseMove.reset(); mouseMove.reset();
mouseLastPos.reset(); mouseLastPos.reset();
@ -219,81 +238,81 @@ public class LwjglInputModule extends InputModule implements Updateable {
onMouseEvent(mouseMove, mouseLastPos); onMouseEvent(mouseMove, mouseLastPos);
wasMouse = true; wasMouse = true;
} }
if (wasMouse && !mouseMove.isZero()) { if (wasMouse && !mouseMove.isZero()) {
App.bus().send(new MouseMotionEvent(mouseLastPos, mouseMove)); App.bus().send(new MouseMotionEvent(mouseLastPos, mouseMove));
} }
while (Keyboard.next()) { while (Keyboard.next()) {
onKeyEvent(); onKeyEvent();
} }
if (Display.isCloseRequested()) { if (Display.isCloseRequested()) {
App.shutdown(); App.shutdown();
} }
} }
private void onMouseEvent(VectVar moveSum, VectVar lastPos) private void onMouseEvent(VectVar moveSum, VectVar lastPos)
{ {
final int button = Mouse.getEventButton(); final int button = Mouse.getEventButton();
final boolean down = Mouse.getEventButtonState(); final boolean down = Mouse.getEventButtonState();
final VectVar pos = Vect.makeVar(Mouse.getEventX(), Mouse.getEventY()); final VectVar pos = Vect.makeVar(Mouse.getEventX(), Mouse.getEventY());
final VectVar move = Vect.makeVar(Mouse.getEventDX(), Mouse.getEventDY()); final VectVar move = Vect.makeVar(Mouse.getEventDX(), Mouse.getEventDY());
final int wheeld = Mouse.getEventDWheel(); final int wheeld = Mouse.getEventDWheel();
// flip Y axis // flip Y axis
pos.setY(Display.getHeight() - pos.y()); pos.setY(Display.getHeight() - pos.y());
if (button != -1 || wheeld != 0) { if (button != -1 || wheeld != 0) {
App.bus().send(new MouseButtonEvent(pos.freeze(), button, down, wheeld)); App.bus().send(new MouseButtonEvent(pos.freeze(), button, down, wheeld));
} }
moveSum.setTo(moveSum.add(move)); moveSum.setTo(moveSum.add(move));
lastPos.setTo(pos); lastPos.setTo(pos);
} }
private void onKeyEvent() private void onKeyEvent()
{ {
final int key = Keyboard.getEventKey(); final int key = Keyboard.getEventKey();
final boolean down = Keyboard.getEventKeyState(); final boolean down = Keyboard.getEventKeyState();
final char c = Keyboard.getEventCharacter(); final char c = Keyboard.getEventCharacter();
App.bus().send(new KeyEvent(key, c, down)); App.bus().send(new KeyEvent(key, c, down));
} }
@Override @Override
public Vect getMousePos() public Vect getMousePos()
{ {
return mousePos; return mousePos;
} }
@Override @Override
public boolean isMouseInside() public boolean isMouseInside()
{ {
return Mouse.isInsideWindow(); return Mouse.isInsideWindow();
} }
@Override @Override
public void grabMouse(boolean grab) public void grabMouse(boolean grab)
{ {
Mouse.setGrabbed(grab); Mouse.setGrabbed(grab);
} }
@Override @Override
public boolean isKeyDown(Key key) public boolean isKeyDown(Key key)
{ {
return key.isDefined() && Keyboard.isKeyDown(key.getCode()); return key.isDefined() && Keyboard.isKeyDown(key.getCode());
} }
@Override @Override
public boolean isMouseButtonDown(int button) public boolean isMouseButtonDown(int button)
{ {

@ -47,6 +47,8 @@ public class SlickAudio extends DeferredAudio {
final String ext = FileUtil.getExtension(resource); final String ext = FileUtil.getExtension(resource);
try(final InputStream stream = FileUtil.getResource(resource)) { try(final InputStream stream = FileUtil.getResource(resource)) {
if (stream == null) throw new IOException("Not found: " + resource);
if (ext.equalsIgnoreCase("ogg")) { if (ext.equalsIgnoreCase("ogg")) {
backingAudio = SoundStore.get().getOgg(resource, stream); backingAudio = SoundStore.get().getOgg(resource, stream);

@ -83,9 +83,16 @@ public class LwjglGraphicsModule extends GraphicsModule {
@Override @Override
public void init() public void init()
{
}
@Override
public void createDisplay()
{ {
try { try {
Display.create(); Display.create();
} catch (final Exception e) { } catch (final Exception e) {
throw new RuntimeException("Could not initialize display.", e); throw new RuntimeException("Could not initialize display.", e);
} }

Loading…
Cancel
Save