Browse code

load and init jinput correctly for gamepad api

bob authored on 28/05/2014 23:51:59
Showing 8 changed files
... ...
@@ -49,6 +49,27 @@
49 49
       <version>${lwjgl.version}</version>
50 50
       <classifier>natives-osx</classifier>
51 51
     </dependency>
52
+    
53
+    <dependency>
54
+        <groupId>net.java.jinput</groupId>
55
+        <artifactId>jinput-platform</artifactId>
56
+        <version>${jinput.version}</version>
57
+        <classifier>natives-linux</classifier>
58
+    </dependency>
59
+    
60
+    <dependency>
61
+        <groupId>net.java.jinput</groupId>
62
+        <artifactId>jinput-platform</artifactId>
63
+        <version>${jinput.version}</version>
64
+        <classifier>natives-windows</classifier>
65
+    </dependency>
66
+
67
+    <dependency>
68
+        <groupId>net.java.jinput</groupId>
69
+        <artifactId>jinput-platform</artifactId>
70
+        <version>${jinput.version}</version>
71
+        <classifier>natives-osx</classifier>
72
+    </dependency>
52 73
 
53 74
     <dependency>
54 75
       <groupId>jlayer</groupId>
... ...
@@ -10,9 +10,11 @@ import playn.core.Gamepad;
10 10
 class JavaGamepad implements Gamepad {
11 11
 
12 12
     private final Controller controller;
13
-
13
+    final boolean[] axisReady;
14
+    
14 15
     JavaGamepad(Controller controller) {
15 16
         this.controller = controller;
17
+        axisReady = new boolean[controller.getAxisCount()];
16 18
     }
17 19
 
18 20
     @Override
... ...
@@ -42,14 +44,14 @@ class JavaGamepad implements Gamepad {
42 44
 
43 45
     @Override
44 46
     public float axisValue(int index) {
45
-        return controller.getAxisValue(index);
47
+        return axisReady[index] ? controller.getAxisValue(index) : 0f;
46 48
     }
47 49
 
48 50
     @Override
49 51
     public boolean isAxisInDeadZone(int index) {
50 52
         float deadZone = controller.getDeadZone(index);
51 53
         float value = axisValue(index);
52
-        return value > deadZone || value < -deadZone;
54
+        return value < deadZone && value > -deadZone;
53 55
     }
54 56
 
55 57
 }
... ...
@@ -40,4 +40,14 @@ class JavaGamepads implements Gamepads {
40 40
         return gamepads;
41 41
     }
42 42
 
43
+    public void update() {
44
+        while(Controllers.next()) {
45
+            if(Controllers.isEventAxis()) {
46
+                int index = Controllers.getEventSource().getIndex();
47
+                JavaGamepad gamepad = (JavaGamepad) plugged().get(index);
48
+                gamepad.axisReady[Controllers.getEventControlIndex()] = true;
49
+            }
50
+        }
51
+    }
52
+
43 53
 }
... ...
@@ -363,6 +363,7 @@ public class JavaPlatform extends AbstractPlatform {
363 363
     mouse.update();
364 364
     keyboard.update();
365 365
     pointer.update();
366
+    gamepads.update();
366 367
 
367 368
     // Execute any pending runnables.
368 369
     runQueue.execute();
... ...
@@ -383,13 +384,38 @@ public class JavaPlatform extends AbstractPlatform {
383 384
       return;
384 385
 
385 386
     SharedLibraryExtractor extractor = new SharedLibraryExtractor();
386
-    File nativesDir = null;
387 387
     try {
388
-      nativesDir = extractor.extractLibrary("lwjgl", null).getParentFile();
388
+      File nativesDir = nativesDir = extractor.extractLibrary("lwjgl", null).getParentFile();
389
+      System.setProperty("org.lwjgl.librarypath", nativesDir.getAbsolutePath());
389 390
     } catch (Throwable ex) {
390 391
       throw new RuntimeException("Unable to extract LWJGL native libraries.", ex);
391 392
     }
392
-    System.setProperty("org.lwjgl.librarypath", nativesDir.getAbsolutePath());
393
+    
394
+    try {
395
+        String[] jinputLibs;
396
+        if (extractor.isWindows) {
397
+              if(extractor.is64Bit) {
398
+                  jinputLibs = new String[] {"jinput-dx8_64.dll", "jinput-raw_64.dll"};
399
+              }else {
400
+                  jinputLibs =  new String[] {"jinput-dx8.dll", "jinput-raw.dll", "jinput-wintab.dll"};
401
+              }
402
+          } else if (extractor.isLinux) {
403
+            jinputLibs =  new String[] { "libjinput-linux" + (extractor.is64Bit ? "64.so" : ".so") };
404
+        } else if (extractor.isMac) {
405
+            jinputLibs =  new String[] { "libjinput" + "-osx.jnilib"};
406
+        } else {
407
+          jinputLibs =  new String[] { "jinput" };
408
+        }
409
+      File nativesDir = null;
410
+      for(String jinputLib : jinputLibs) {
411
+        nativesDir = extractor.extractLibrary(jinputLib, "jinput").getParentFile();
412
+      }
413
+      if(null != nativesDir) {
414
+        System.setProperty("net.java.games.input.librarypath", nativesDir.getAbsolutePath());
415
+      }
416
+    } catch (Throwable ex) {
417
+      throw new RuntimeException("Unable to extract jinput native libraries.", ex);
418
+    }
393 419
   }
394 420
 
395 421
   protected boolean isInJavaWebStart() {
... ...
@@ -28,10 +28,10 @@ import java.util.zip.CRC32;
28 28
  */
29 29
 public class SharedLibraryExtractor {
30 30
 
31
-  private final boolean isWindows = System.getProperty("os.name").contains("Windows");
32
-  private final boolean isLinux = System.getProperty("os.name").contains("Linux");
33
-  private final boolean isMac = System.getProperty("os.name").contains("Mac");
34
-  private final boolean is64Bit = System.getProperty("os.arch").equals("amd64");
31
+  final boolean isWindows = System.getProperty("os.name").contains("Windows");
32
+  final boolean isLinux = System.getProperty("os.name").contains("Linux");
33
+  final boolean isMac = System.getProperty("os.name").contains("Mac");
34
+  final boolean is64Bit = System.getProperty("os.arch").equals("amd64");
35 35
 
36 36
   /**
37 37
    * Extracts the specified library into the specified temp directory (if it has not already been
... ...
@@ -91,14 +91,27 @@ public class SharedLibraryExtractor {
91 91
     }
92 92
     throw new FileNotFoundException("Unable to find shared lib for '" + libraryName + "'");
93 93
   }
94
+  
95
+  private String[] platformSpecificSuffix = new String[]{".dll", ".so", ".jnilib", ".dylib"};
94 96
 
97
+  private boolean isPlatformDependentName(String libraryName) {
98
+      for(String suffix : platformSpecificSuffix) {
99
+          if(libraryName.endsWith(suffix)) {
100
+              return true;
101
+          }
102
+      }
103
+      return false;
104
+  }
105
+  
95 106
   /** Maps a platform independent library name to one or more platform dependent names. */
96 107
   private String[] platformNames(String libraryName) {
97
-    if (isWindows) return new String[] { libraryName + (is64Bit ? "64.dll" : ".dll") };
98
-    if (isLinux) return new String[] { "lib" + libraryName + (is64Bit ? "64.so" : ".so") };
99
-    if (isMac) return new String[] { "lib" + libraryName + ".jnilib",
100
-                                     "lib" + libraryName + ".dylib" };
101
-    return new String[] { libraryName };
108
+      if(!isPlatformDependentName(libraryName)) {
109
+        if (isWindows) return new String[] { libraryName + (is64Bit ? "64.dll" : ".dll") };
110
+        if (isLinux) return new String[] { "lib" + libraryName + (is64Bit ? "64.so" : ".so") };
111
+        if (isMac) return new String[] { "lib" + libraryName + ".jnilib",
112
+                                         "lib" + libraryName + ".dylib" };
113
+      }
114
+      return new String[] { libraryName };
102 115
   }
103 116
 
104 117
   /** Returns a CRC of the remaining bytes in the stream. */
... ...
@@ -49,6 +49,7 @@
49 49
     <gwt.version>2.6.0</gwt.version>
50 50
     <jbox2d.version>2.2.1.1</jbox2d.version>
51 51
     <lwjgl.version>2.9.0</lwjgl.version>
52
+    <jinput.version>2.0.5</jinput.version>
52 53
     <!--
53 54
       GWT log levels:
54 55
       ERROR, WARN, INFO, TRACE, DEBUG, SPAM, or ALL
55 56
new file mode 100644
... ...
@@ -0,0 +1,63 @@
1
+package playn.tests.core;
2
+
3
+import playn.core.CanvasImage;
4
+import playn.core.Color;
5
+import playn.core.Gamepad;
6
+import playn.core.ImageLayer;
7
+import static playn.core.PlayN.graphics;
8
+import static playn.core.PlayN.gamepads;
9
+
10
+/**
11
+ *
12
+ * @author devnewton <devnewton@bci.im>
13
+ */
14
+class GamepadTest extends Test {
15
+
16
+    private ImageLayer movable;
17
+
18
+    @Override
19
+    public String getName() {
20
+        return "Gamepad";
21
+    }
22
+
23
+    @Override
24
+    public String getDescription() {
25
+        return "Tests support for gamepads";
26
+    }
27
+
28
+    @Override
29
+    public void init() {
30
+        CanvasImage image = graphics().createImage(32, 32);
31
+        image.canvas().setFillColor(Color.rgb(100,100, 100));
32
+        image.canvas().fillRect(0, 0, image.width(), image.height());
33
+        movable = graphics().createImageLayer(image);
34
+        movable.setTranslation(graphics().width() / 2.0f, graphics().height() / 2.0f);
35
+        graphics().rootLayer().add(movable);
36
+    }
37
+
38
+    @Override
39
+    public void update(int delta) {
40
+        if (gamepads().hasGamepads()) {
41
+            for (Gamepad gamepad : gamepads().plugged()) {
42
+                if (gamepad.axisCount() > 0) {
43
+                    if (!gamepad.isAxisInDeadZone(0)) {
44
+                        float value = gamepad.axisValue(0);
45
+                        movable.transform().translateX(value);
46
+                    }
47
+                }
48
+                if (gamepad.axisCount() > 1) {
49
+                    if (!gamepad.isAxisInDeadZone(1)) {
50
+                        movable.transform().translateY(gamepad.axisValue(1));
51
+                    }
52
+                }
53
+                if(gamepad.buttonCount() > 0) {
54
+                    if(!gamepad.isButtonInDeadZone(0)) {
55
+                        movable.setTint(Color.rgb(255, 0, 0));
56
+                    } else {
57
+                        movable.setTint(Color.rgb(255, 255, 255));
58
+                    }
59
+                }
60
+            }
61
+        }
62
+    }
63
+}
... ...
@@ -113,6 +113,7 @@ public class TestsGame extends Game.Default {
113 113
     new SoundTest(),
114 114
     new NetTest(),
115 115
     new FullscreenTest(),
116
+    new GamepadTest(),
116 117
     /*new YourTest(),*/
117 118
   };
118 119
   private Test currentTest;