Skip to content

Commit f3d9090

Browse files
committed
Merge branch 'develop'
2 parents fba3752 + c424a86 commit f3d9090

23 files changed

+94
-91
lines changed

android/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.rolandoislas.drcsimclient"
4-
android:versionCode="2"
5-
android:versionName="1.1" >
4+
android:versionCode="3"
5+
android:versionName="1.2" >
66

77
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="25" />
88

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ allprojects {
1515
apply plugin: "eclipse"
1616
apply plugin: "idea"
1717

18-
version = '1.1'
18+
version = '1.2'
1919
ext {
2020
appName = "drc-sim-client"
2121
gdxVersion = '1.9.4'

core/src/com/rolandoislas/drcsimclient/Client.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ public static boolean connect(String ip, boolean setStageOnFailure) {
9292
@Override
9393
public void resize(int width, int height) {
9494
super.resize(width, height);
95-
try {
96-
setStage(Client.stage.getClass().newInstance());
97-
} catch (InstantiationException e) {
98-
e.printStackTrace();
99-
} catch (IllegalAccessException e) {
100-
e.printStackTrace();
101-
}
95+
Client.stage.resize(width, height);
10296
}
10397
}

core/src/com/rolandoislas/drcsimclient/audio/AudioThread.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
*/
1111
public class AudioThread extends Thread {
1212
private final AudioUtil audioUtil;
13+
private final NetUtil netUtil;
1314
private boolean running = true;
1415

1516
public AudioThread() {
1617
this.audioUtil = new AudioUtil();
1718
this.setName("Network Thread: Audio");
19+
netUtil = new NetUtil();
1820
}
1921

2022
@Override
2123
public void run() {
2224
while (running) {
2325
try {
24-
byte[] packet = NetUtil.recv(sockets.socketAud, "audio");
26+
byte[] packet = netUtil.recv(sockets.socketAud);
2527
audioUtil.addData(packet);
2628
} catch (NetUtil.ReadTimeoutException ignore) {
2729
try {
@@ -40,4 +42,8 @@ public void dispose() {
4042
running = false;
4143
audioUtil.dispose();
4244
}
45+
46+
public void resetTimeout() {
47+
netUtil.resetTimeout();
48+
}
4349
}

core/src/com/rolandoislas/drcsimclient/config/Config.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class Config {
2525
public static final String JOYSTICK_LEFT_Y = "JOYSTICK_LEFT_Y";
2626
public static final String JOYSTICK_RIGHT_X = "JOYSTICK_RIGHT_X";
2727
public static final String JOYSTICK_RIGHT_Y = "JOYSTICK_RIGHT_Y";
28+
public static final String MIC_BLOW = "MIC_BLOW";
2829
public int buttonA;
2930
public int buttonB;
3031
public int buttonX;
@@ -42,6 +43,7 @@ public class Config {
4243
public int buttonMinus;
4344
public int buttonPlus;
4445
public int buttonHome;
46+
public int micBlow;
4547

4648
public void set(String item, int input) {}
4749

core/src/com/rolandoislas/drcsimclient/config/ConfigControllerConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ConfigControllerConfig extends Config {
1111
public int joystickRightY;
1212

1313
ConfigControllerConfig(String name) {
14-
config = Gdx.app.getPreferences("com.rolandoislas.controller." + name.replaceAll(" ", ""));
14+
config = Gdx.app.getPreferences("com.rolandoislas.drcsimclient.controller." + name.replaceAll(" ", ""));
1515
}
1616

1717
public void load() {
@@ -36,6 +36,7 @@ public void load() {
3636
joystickLeftY = config.getInteger(JOYSTICK_LEFT_Y, -1);
3737
joystickRightX = config.getInteger(JOYSTICK_RIGHT_X, -1);
3838
joystickRightY = config.getInteger(JOYSTICK_RIGHT_Y, -1);
39+
micBlow = config.getInteger(MIC_BLOW, -1);
3940
config.flush();
4041
}
4142

core/src/com/rolandoislas/drcsimclient/config/ConfigGeneral.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ConfigGeneral extends Config {
1212
public int touchScreen;
1313

1414
public ConfigGeneral() {
15-
config = Gdx.app.getPreferences("com.rolandoislas.drcsimclient.config.touch");
15+
config = Gdx.app.getPreferences("com.rolandoislas.drcsimclient.config.general");
1616
}
1717

1818
@Override

core/src/com/rolandoislas/drcsimclient/config/ConfigKeyboard.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void load() {
3232
buttonMinus = config.getInteger(BUTTON_MINUS, Input.Keys.X);
3333
buttonPlus = config.getInteger(BUTTON_PLUS, Input.Keys.C);
3434
buttonHome = config.getInteger(BUTTON_HOME, Input.Keys.Z);
35+
micBlow = config.getInteger(MIC_BLOW, Input.Keys.B);
3536
config.putInteger(BUTTON_A, buttonA);
3637
config.putInteger(BUTTON_B, buttonB);
3738
config.putInteger(BUTTON_X, buttonX);
@@ -49,6 +50,7 @@ public void load() {
4950
config.putInteger(BUTTON_MINUS, buttonMinus);
5051
config.putInteger(BUTTON_PLUS, buttonPlus);
5152
config.putInteger(BUTTON_HOME, buttonHome);
53+
config.putInteger(MIC_BLOW, micBlow);
5254
config.flush();
5355
}
5456

core/src/com/rolandoislas/drcsimclient/control/ControlController.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.rolandoislas.drcsimclient.config.ConfigController;
77
import com.rolandoislas.drcsimclient.config.ConfigControllerConfig;
88
import com.rolandoislas.drcsimclient.data.Constants;
9+
import com.rolandoislas.drcsimclient.net.Codec;
910
import com.rolandoislas.drcsimclient.stage.StageControl;
1011

1112
import static com.rolandoislas.drcsimclient.Client.sockets;
@@ -62,6 +63,9 @@ public void update() {
6263
buttonBits |= Constants.BUTTON_PLUS;
6364
if (controller.getButton(config.buttonHome))
6465
buttonBits |= Constants.BUTTON_HOME;
66+
// Microphone
67+
if (controller.getButton(config.micBlow))
68+
sockets.sendCommand(Constants.COMMAND_INPUT_MIC_BLOW, Codec.encodeInput(true));
6569
// Check joystick
6670
axes[0] = controller.getAxis(config.joystickLeftX);
6771
axes[1] = controller.getAxis(config.joystickLeftY);

core/src/com/rolandoislas/drcsimclient/control/ControlKeyboard.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.badlogic.gdx.Gdx;
44
import com.rolandoislas.drcsimclient.config.ConfigKeyboard;
55
import com.rolandoislas.drcsimclient.data.Constants;
6+
import com.rolandoislas.drcsimclient.net.Codec;
67
import com.rolandoislas.drcsimclient.stage.StageControl;
78

89
import static com.rolandoislas.drcsimclient.Client.sockets;
@@ -54,13 +55,16 @@ public void update() {
5455
if (Gdx.input.isKeyPressed(config.buttonHome))
5556
buttonbits |= Constants.BUTTON_HOME;
5657
sockets.sendButtonInput(buttonbits);
57-
// L3R3
58+
// Extra
5859
int extraButtonBits = 0;
5960
if (Gdx.input.isKeyPressed(config.buttonL3))
6061
extraButtonBits |= Constants.BUTTON_L3;
6162
if (Gdx.input.isKeyPressed(config.buttonR3))
6263
extraButtonBits |= Constants.BUTTON_R3;
6364
sockets.sendExtraButtonInput(extraButtonBits);
65+
// Mic
66+
if (Gdx.input.isKeyPressed(config.micBlow))
67+
sockets.sendCommand(Constants.COMMAND_INPUT_MIC_BLOW, Codec.encodeInput(true));
6468
// Joystick
6569
// TODO get joystick input based on mouse capture
6670
}

core/src/com/rolandoislas/drcsimclient/data/Constants.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class Constants {
99
// Info
1010
public static final String NAME = "DRC Sim Client";
11-
public static final String VERSION = "1.1.1";
11+
public static final String VERSION = "1.2";
1212
// Ports
1313
public static final int PORT_SERVER_VID = 50000;
1414
public static final int PORT_SERVER_AUD = 50001;
@@ -19,6 +19,7 @@ public class Constants {
1919
public static final String COMMAND_INPUT_BUTTON_EXTRA = "INPUT_L3R3"; // TODO rename on server
2020
public static final String COMMAND_INPUT_TOUCH = "INPUT_TOUCH";
2121
public static final String COMMAND_INPUT_JOYSTICK = "INPUT_JOYSTICK";
22+
public static final String COMMAND_INPUT_MIC_BLOW = "INPUT_MIC_BLOW";
2223
public static final String COMMAND_VIBRATE = "VIBRATE";
2324
public static final String COMMAND_PING = "PING";
2425
public static final String COMMAND_PONG = "PONG";

core/src/com/rolandoislas/drcsimclient/graphics/VideoThread.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package com.rolandoislas.drcsimclient.graphics;
22

33
import com.rolandoislas.drcsimclient.net.NetUtil;
4-
import com.rolandoislas.drcsimclient.stage.StageConnect;
54
import com.rolandoislas.drcsimclient.util.logging.Logger;
65

7-
import static com.rolandoislas.drcsimclient.Client.setStage;
86
import static com.rolandoislas.drcsimclient.Client.sockets;
97

108
/**
119
* Created by rolando on 4/6/17.
1210
*/
1311
public class VideoThread extends Thread {
12+
private final NetUtil netUtil;
1413
private boolean running = true;
1514
private byte[] imageData = new byte[0];
1615

1716
public VideoThread() {
1817
this.setName("Network Thread: Video");
18+
netUtil = new NetUtil();
1919
}
2020

2121
@Override
2222
public void run() {
2323
while (running) {
2424
try {
25-
byte[] data = NetUtil.recv(sockets.socketVid, "video");
25+
byte[] data = netUtil.recv(sockets.socketVid);
2626
synchronized (this) {
2727
imageData = data;
2828
}
@@ -52,4 +52,8 @@ public byte[] getImageData() {
5252
}
5353
return data;
5454
}
55+
56+
public void resetTimeout() {
57+
netUtil.resetTimeout();
58+
}
5559
}

core/src/com/rolandoislas/drcsimclient/net/NetUtil.java

+22-26
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,46 @@
1717
* Created by Rolando on 12/21/2016.
1818
*/
1919
public class NetUtil {
20-
private static HashMap<String, byte[]> buffers = new HashMap<String, byte[]>();
21-
private static HashMap<String, Long> timestamps = new HashMap<String, Long>();
22-
private static boolean pingSent = false;
20+
private byte[] buffer = new byte[0];
21+
private long timestamp = 0;
22+
private boolean pingSent = false;
2323

24-
public static byte[] recv(Socket socket, String bufferId) throws DisconnectedException, ReadTimeoutException {
24+
public byte[] recv(Socket socket) throws DisconnectedException, ReadTimeoutException {
2525
try {
2626
BufferedInputStream inStream = new BufferedInputStream(socket.getInputStream());
27-
if (!buffers.containsKey(bufferId))
28-
buffers.put(bufferId, new byte[0]);
29-
if (!timestamps.containsKey(bufferId))
30-
timestamps.put(bufferId, System.currentTimeMillis());
31-
while (!new String(buffers.get(bufferId)).contains(Codec.endDelimiter)) {
27+
if (timestamp == 0)
28+
timestamp = System.currentTimeMillis();
29+
while (!new String(buffer).contains(Codec.endDelimiter)) {
3230
// Disconnected
33-
long time = System.currentTimeMillis() - timestamps.get(bufferId);
34-
if (time >= 10000 && !Client.connect(sockets.getIp(), false)) {
31+
long time = System.currentTimeMillis() - timestamp;
32+
if (time >= 10000) {
3533
Logger.debug("Disconnected from server");
3634
clear();
3735
throw new DisconnectedException();
3836
}
3937
if (time >= 5000 && !pingSent) {
4038
Logger.debug("Sending PING command to server");
4139
sockets.sendCommand(Constants.COMMAND_PING);
42-
Client.connect(sockets.getIp(), false);
4340
pingSent = true;
4441
}
4542
// Timeout
4643
if (inStream.available() < 2)
4744
throw new ReadTimeoutException();
48-
timestamps.put(bufferId, System.currentTimeMillis());
45+
timestamp = System.currentTimeMillis();
4946
// Read
5047
byte[] read = new byte[100000];
5148
int numRead = inStream.read(read);
5249
read = Arrays.copyOfRange(read, 0, numRead);
5350
// Combine saved and new bytes
54-
byte[] newBytes = new byte[buffers.get(bufferId).length + read.length];
55-
System.arraycopy(buffers.get(bufferId), 0, newBytes, 0, buffers.get(bufferId).length);
56-
System.arraycopy(read, 0, newBytes, buffers.get(bufferId).length, read.length);
51+
byte[] newBytes = new byte[buffer.length + read.length];
52+
System.arraycopy(buffer, 0, newBytes, 0, buffer.length);
53+
System.arraycopy(read, 0, newBytes, buffer.length, read.length);
5754
// Save
58-
buffers.put(bufferId, newBytes);
55+
buffer = newBytes;
5956
}
60-
int index = Bytes.indexOf(buffers.get(bufferId), Codec.endDelimiter.getBytes());
61-
byte[] packet = Arrays.copyOfRange(buffers.get(bufferId), 0, index);
62-
buffers.put(bufferId, Arrays.copyOfRange(buffers.get(bufferId), index + Codec.endDelimiter.length(),
63-
buffers.get(bufferId).length));
57+
int index = Bytes.indexOf(buffer, Codec.endDelimiter.getBytes());
58+
byte[] packet = Arrays.copyOfRange(buffer, 0, index);
59+
buffer = Arrays.copyOfRange(buffer, index + Codec.endDelimiter.length(), buffer.length);
6460
return Codec.decode(packet);
6561
}
6662
catch (IOException e) {
@@ -70,14 +66,14 @@ public static byte[] recv(Socket socket, String bufferId) throws DisconnectedExc
7066
}
7167
}
7268

73-
static void clear() {
74-
timestamps.clear();
75-
buffers.clear();
69+
private void clear() {
70+
timestamp = 0;
71+
buffer = new byte[0];
7672
pingSent = false;
7773
}
7874

79-
public static void resetTimeout() {
80-
timestamps.clear();
75+
public void resetTimeout() {
76+
timestamp = 0;
8177
pingSent = false;
8278
}
8379

core/src/com/rolandoislas/drcsimclient/net/Sockets.java

-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public void dispose() {
118118
e.printStackTrace();
119119
}
120120
}
121-
NetUtil.clear();
122121
}
123122

124123
public String getIp() {

core/src/com/rolandoislas/drcsimclient/stage/Stage.java

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.badlogic.gdx.Gdx;
44
import com.badlogic.gdx.Graphics;
55
import com.badlogic.gdx.Input;
6+
import com.rolandoislas.drcsimclient.Client;
67

78
/**
89
* Created by Rolando on 2/6/2017.
@@ -36,4 +37,14 @@ private void toggleFullscreen() {
3637
}
3738

3839
public void onBackButtonPressed() {}
40+
41+
public void resize(int width, int height) {
42+
try {
43+
Client.setStage(this.getClass().newInstance());
44+
} catch (InstantiationException e) {
45+
e.printStackTrace();
46+
} catch (IllegalAccessException e) {
47+
e.printStackTrace();
48+
}
49+
}
3950
}

core/src/com/rolandoislas/drcsimclient/stage/StageConfigController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class StageConfigController extends StageList {
3737
new String[]{"R3", ConfigControllerConfig.BUTTON_R3},
3838
new String[]{"Minus", ConfigControllerConfig.BUTTON_MINUS},
3939
new String[]{"Plus", ConfigControllerConfig.BUTTON_PLUS},
40-
new String[]{"Home", ConfigControllerConfig.BUTTON_HOME}
40+
new String[]{"Home", ConfigControllerConfig.BUTTON_HOME},
41+
new String[]{"Mic Blow", ConfigControllerConfig.MIC_BLOW}
4142
};
4243

4344
public StageConfigController(boolean enableDropdown) {

core/src/com/rolandoislas/drcsimclient/stage/StageControl.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
import com.badlogic.gdx.utils.GdxRuntimeException;
99
import com.rolandoislas.drcsimclient.audio.AudioThread;
1010
import com.rolandoislas.drcsimclient.config.ConfigGeneral;
11-
import com.rolandoislas.drcsimclient.config.ConfigTouch;
1211
import com.rolandoislas.drcsimclient.control.Control;
1312
import com.rolandoislas.drcsimclient.data.Constants;
1413
import com.rolandoislas.drcsimclient.graphics.VideoThread;
1514
import com.rolandoislas.drcsimclient.net.CommandThread;
16-
import com.rolandoislas.drcsimclient.net.NetUtil;
1715
import com.rolandoislas.drcsimclient.util.logging.Logger;
1816

1917
import static com.rolandoislas.drcsimclient.Client.*;
@@ -56,6 +54,13 @@ public StageControl() {
5654
commandThread.start();
5755
}
5856

57+
@Override
58+
public void resize(int width, int height) {
59+
spritebatch = new SpriteBatch();
60+
wiiScreen.setBounds(0, 0, width, height);
61+
this.getViewport().update(width, height);
62+
}
63+
5964
@Override
6065
public void onBackButtonPressed() {
6166
setStage(new StageConnect());
@@ -88,8 +93,10 @@ public void act() {
8893
private void checkNetworkCommands() {
8994
CommandThread.Command command = commandThread.getCommand();
9095
// Handle command
91-
if (command.isCommand(Constants.COMMAND_PONG))
92-
NetUtil.resetTimeout();
96+
if (command.isCommand(Constants.COMMAND_PONG)) {
97+
audioThread.resetTimeout();
98+
videoThread.resetTimeout();
99+
}
93100
else if (command.isCommand(Constants.COMMAND_VIBRATE))
94101
for (Control control : controls)
95102
control.vibrate(1000);

0 commit comments

Comments
 (0)