| 1 |
import arc.io.window, |
|---|
| 2 |
arc.io.input, |
|---|
| 3 |
arc.phy.util; |
|---|
| 4 |
|
|---|
| 5 |
import raknet.client, |
|---|
| 6 |
raknet.server, |
|---|
| 7 |
raknet.bitstream; |
|---|
| 8 |
|
|---|
| 9 |
import std.string; |
|---|
| 10 |
import std.stdio; |
|---|
| 11 |
|
|---|
| 12 |
import std.c.time; |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
import derelict.opengl.gl; |
|---|
| 16 |
import derelict.sdl.sdl; |
|---|
| 17 |
|
|---|
| 18 |
const ubyte PACKET_ID_LINE = 100; |
|---|
| 19 |
const ubyte PACKET_ID_NUM = 101; |
|---|
| 20 |
const ubyte PACKET_ID_NUM_REQUEST = 102; |
|---|
| 21 |
|
|---|
| 22 |
struct Line |
|---|
| 23 |
{ |
|---|
| 24 |
float x1, y1, x2, y2; |
|---|
| 25 |
int r, g, b; |
|---|
| 26 |
|
|---|
| 27 |
void generateColor() |
|---|
| 28 |
{ |
|---|
| 29 |
r = random_range(0, 255); |
|---|
| 30 |
g = random_range(0, 255); |
|---|
| 31 |
b = random_range(0, 255); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
void print() |
|---|
| 35 |
{ |
|---|
| 36 |
writefln(x1, " ", y1, " ", x2, " ", y2); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
void draw() |
|---|
| 40 |
{ |
|---|
| 41 |
glColor3ub(r,g,b); |
|---|
| 42 |
|
|---|
| 43 |
glBegin(GL_LINES); |
|---|
| 44 |
|
|---|
| 45 |
glVertex2f(x1, y1); |
|---|
| 46 |
glVertex2f(x2, y2); |
|---|
| 47 |
|
|---|
| 48 |
glEnd(); |
|---|
| 49 |
} |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
class ClientConnection |
|---|
| 53 |
{ |
|---|
| 54 |
private: |
|---|
| 55 |
Line[] lines; |
|---|
| 56 |
int playerNum = 0; |
|---|
| 57 |
|
|---|
| 58 |
this(char *serverIP, char *portString) |
|---|
| 59 |
{ |
|---|
| 60 |
// init client and connect to server |
|---|
| 61 |
raknet.client.initialize(); |
|---|
| 62 |
|
|---|
| 63 |
raknet.client.connect(serverIP, atoi(portString), 0, 0, 0); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
~this() |
|---|
| 67 |
{ |
|---|
| 68 |
// destroy client |
|---|
| 69 |
raknet.client.disconnect(300); |
|---|
| 70 |
raknet.client.destroy(); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
// client will keep track of all lines added by itself and the server |
|---|
| 74 |
void addLocalLine(float x1, float y1, float x2, float y2, int r, int g, int b) |
|---|
| 75 |
{ |
|---|
| 76 |
Line line; |
|---|
| 77 |
|
|---|
| 78 |
line.x1 = x1; |
|---|
| 79 |
line.x2 = x2; |
|---|
| 80 |
line.y1 = y1; |
|---|
| 81 |
line.y2 = y2; |
|---|
| 82 |
line.r = r; |
|---|
| 83 |
line.g = g; |
|---|
| 84 |
line.b = b; |
|---|
| 85 |
|
|---|
| 86 |
lines ~= line; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
// send player ID to server and wait to recieve player number |
|---|
| 90 |
void getPlayerNum() |
|---|
| 91 |
{ |
|---|
| 92 |
// get player ID |
|---|
| 93 |
PlayerID pid = raknet.client.playerID(); |
|---|
| 94 |
|
|---|
| 95 |
// request player num based on player ID from server |
|---|
| 96 |
raknet.bitstream.start(); |
|---|
| 97 |
|
|---|
| 98 |
raknet.bitstream.write(PACKET_ID_NUM_REQUEST); |
|---|
| 99 |
raknet.bitstream.write(pid.binaryAddress); |
|---|
| 100 |
raknet.bitstream.write(pid.port); |
|---|
| 101 |
|
|---|
| 102 |
raknet.client.sendBitstream(PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, 0); |
|---|
| 103 |
|
|---|
| 104 |
raknet.bitstream.end(); |
|---|
| 105 |
|
|---|
| 106 |
debug writefln("getPlayerNum: sent request"); |
|---|
| 107 |
|
|---|
| 108 |
// wait until recieve player number from server |
|---|
| 109 |
ubyte packetID = 0; |
|---|
| 110 |
|
|---|
| 111 |
do |
|---|
| 112 |
{ |
|---|
| 113 |
Packet * p = raknet.client.receivePacket(); |
|---|
| 114 |
|
|---|
| 115 |
if(p !is null) { |
|---|
| 116 |
|
|---|
| 117 |
// decode packet |
|---|
| 118 |
raknet.bitstream.start(cast(char*)p.data, p.length, false); |
|---|
| 119 |
|
|---|
| 120 |
// read packet ID |
|---|
| 121 |
raknet.bitstream.read(packetID); |
|---|
| 122 |
|
|---|
| 123 |
debug writefln("recieved packet ", packetID); |
|---|
| 124 |
|
|---|
| 125 |
// if we found it, read its data |
|---|
| 126 |
if (packetID == PACKET_ID_NUM) |
|---|
| 127 |
{ |
|---|
| 128 |
raknet.bitstream.read(playerNum); |
|---|
| 129 |
debug writefln("player ", playerNum, " connected"); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
// otherwise deallocate and wait some more |
|---|
| 133 |
raknet.client.deallocatePacket(p); |
|---|
| 134 |
|
|---|
| 135 |
} // p !is unll |
|---|
| 136 |
|
|---|
| 137 |
} while (packetID != PACKET_ID_NUM) |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
void sendLineToServer(float x1, float y1, float x2, float y2, int r, int g, int b) |
|---|
| 143 |
{ |
|---|
| 144 |
raknet.bitstream.start(); |
|---|
| 145 |
|
|---|
| 146 |
raknet.bitstream.write(PACKET_ID_LINE); |
|---|
| 147 |
raknet.bitstream.write(x1); |
|---|
| 148 |
raknet.bitstream.write(y1); |
|---|
| 149 |
raknet.bitstream.write(x2); |
|---|
| 150 |
raknet.bitstream.write(y2); |
|---|
| 151 |
raknet.bitstream.write(r); |
|---|
| 152 |
raknet.bitstream.write(g); |
|---|
| 153 |
raknet.bitstream.write(b); |
|---|
| 154 |
|
|---|
| 155 |
raknet.client.sendBitstream(PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, 0); |
|---|
| 156 |
|
|---|
| 157 |
raknet.bitstream.end(); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
// draw all lines to screen |
|---|
| 161 |
void drawLines() |
|---|
| 162 |
{ |
|---|
| 163 |
foreach(Line line; lines) |
|---|
| 164 |
{ |
|---|
| 165 |
line.draw(); |
|---|
| 166 |
} |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
// check to see if any packets coming from the server are waiting to be handled by the client |
|---|
| 170 |
void listenForPackets() |
|---|
| 171 |
{ |
|---|
| 172 |
Packet * p = raknet.client.receivePacket(); |
|---|
| 173 |
|
|---|
| 174 |
if(p !is null) { |
|---|
| 175 |
handlePacket(p); |
|---|
| 176 |
|
|---|
| 177 |
raknet.client.deallocatePacket(p); |
|---|
| 178 |
} |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
// will then decode the packet, which should be information about a line, and add a line to the list of lines to draw |
|---|
| 182 |
void handlePacket(Packet * p) |
|---|
| 183 |
{ |
|---|
| 184 |
raknet.bitstream.start(cast(char*)p.data, p.length, false); |
|---|
| 185 |
|
|---|
| 186 |
ubyte packetID; |
|---|
| 187 |
|
|---|
| 188 |
raknet.bitstream.read(packetID); |
|---|
| 189 |
|
|---|
| 190 |
switch(packetID) { |
|---|
| 191 |
case PACKET_ID_LINE: |
|---|
| 192 |
float x1, y1, x2, y2; |
|---|
| 193 |
int r, g, b; |
|---|
| 194 |
|
|---|
| 195 |
raknet.bitstream.read(x1); |
|---|
| 196 |
raknet.bitstream.read(y1); |
|---|
| 197 |
raknet.bitstream.read(x2); |
|---|
| 198 |
raknet.bitstream.read(y2); |
|---|
| 199 |
raknet.bitstream.read(r); |
|---|
| 200 |
raknet.bitstream.read(g); |
|---|
| 201 |
raknet.bitstream.read(b); |
|---|
| 202 |
|
|---|
| 203 |
addLocalLine(x1, y1, x2, y2, r, g, b); |
|---|
| 204 |
break; |
|---|
| 205 |
default: |
|---|
| 206 |
break; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
} |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
int main() |
|---|
| 214 |
{ |
|---|
| 215 |
ClientConnection clientConnect = new ClientConnection("127.0.0.1", "6881"); |
|---|
| 216 |
|
|---|
| 217 |
// send PID to |
|---|
| 218 |
clientConnect.getPlayerNum(); |
|---|
| 219 |
|
|---|
| 220 |
int lineCount = 0; |
|---|
| 221 |
|
|---|
| 222 |
arc.io.window.open("window.lua"); |
|---|
| 223 |
arc.io.input.open(); |
|---|
| 224 |
|
|---|
| 225 |
float lastX, lastY; |
|---|
| 226 |
|
|---|
| 227 |
while (!(arc.io.input.keyDown(SDL_QUIT) || arc.io.input.keyDown(SDLK_ESCAPE))) |
|---|
| 228 |
{ |
|---|
| 229 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 230 |
arc.io.input.process(); |
|---|
| 231 |
|
|---|
| 232 |
if (arc.io.input.mouseHit(LEFT)) |
|---|
| 233 |
{ |
|---|
| 234 |
lineCount++; |
|---|
| 235 |
|
|---|
| 236 |
// every other click is a new line |
|---|
| 237 |
if ((lineCount % 2) == 0) |
|---|
| 238 |
{ |
|---|
| 239 |
int r = random_range(50,255); |
|---|
| 240 |
int g = random_range(50,255); |
|---|
| 241 |
int b = random_range(50,255); |
|---|
| 242 |
|
|---|
| 243 |
clientConnect.addLocalLine(lastX, lastY, arc.io.input.mouseX, |
|---|
| 244 |
arc.io.input.mouseY,r,g,b); |
|---|
| 245 |
clientConnect.sendLineToServer(lastX, lastY, |
|---|
| 246 |
arc.io.input.mouseX, arc.io.input.mouseY,r,g,b); |
|---|
| 247 |
} |
|---|
| 248 |
else |
|---|
| 249 |
{ |
|---|
| 250 |
lastX = arc.io.input.mouseX; |
|---|
| 251 |
lastY = arc.io.input.mouseY; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
clientConnect.drawLines(); |
|---|
| 257 |
clientConnect.listenForPackets(); |
|---|
| 258 |
|
|---|
| 259 |
SDL_GL_SwapBuffers(); |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
arc.io.window.close(); |
|---|
| 263 |
|
|---|
| 264 |
return 0; |
|---|
| 265 |
} |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
extern(C): |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
char *gets( char *str ); |
|---|
| 273 |
int atoi( char *str ); |
|---|