Skip to content

Commit

Permalink
Merge pull request #3 from WideWord/master
Browse files Browse the repository at this point in the history
Двойная буферизация и второй C# клиент
  • Loading branch information
JustAMan committed Nov 18, 2015
2 parents 40df71d + 81d0e73 commit 9dc90fe
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 11 deletions.
83 changes: 83 additions & 0 deletions clients/csharp/Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace Com.CodeGame.CodeRacing2015.DevKit.CSharpCgdk {

// цвет нужно задавать hex-числом, например 0xABCDEF, AB - red, CD - green, EF - blue, каждый цвет - число из двух hex-цифр в диапазоне от 00 до FF

public struct Debug {

private static TcpClient client;
private static StreamWriter writer;

public static void connect(string host, int port) {
client = new TcpClient(host, port);
}

public static void disconnect() {
client.Close();
}

private static void sendCommand(string command) {
if (client != null) {
if (writer == null) {
writer = new StreamWriter(client.GetStream (), Encoding.ASCII);
}
writer.WriteLine(command);
}
System.Console.WriteLine(command);
}

public static void beginPre() {
sendCommand("begin pre");
}

public static void beginPost() {
sendCommand("begin post");
}

public static void endPre() {
sendCommand("end pre");
}

public static void endPost() {
sendCommand("end post");
}

private static string encodeColor(int color) {
int red = (color & 0xFF0000) >> 16;
int green = (color & 0x00FF00) >> 8;
int blue = color & 0x0000FF;

return String.Format("{0} {1} {2}", (double)red / 256.0, (double)green / 256.0, (double)blue / 256.0);
}

public static void circle(double x, double y, double radius, int color) {
sendCommand(String.Format("circle {0} {1} {2} {3}", x, y, radius, encodeColor(color)));
}

public static void circle(double x, double y, double radius, int color) {
sendCommand(String.Format("fill_circle {0} {1} {2} {3}", x, y, radius, encodeColor(color)));
}

public static void rect(double x1, double y1, double x2, double y2, int color) {
sendCommand(String.Format("rect {0} {1} {2} {3} {4}", x1, y1, x2, y2, encodeColor(color)));
}

public static void fillRect(double x1, double y1, double x2, double y2, int color) {
sendCommand(String.Format("fill_rect {0} {1} {2} {3} {4}", x1, y1, x2, y2, encodeColor(color)));
}

public static void line(double x1, double y1, double x2, double y2, int color) {
sendCommand(String.Format("line {0} {1} {2} {3} {4}", x1, y1, x2, y2, encodeColor(color)));
}

public static void print(double x, double y, string msg, int color = 0) {
sendCommand(String.Format("text {0} {1} {2} {3}", x, y, msg, encodeColor(color)));
}
}

}

28 changes: 17 additions & 11 deletions plugins/LocalTestRendererListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ private final class ThreadListener extends Thread
public static final String END_POST = "end post";

private ServerSocket socket;
private ArrayList<Message> messagesPre, messagesPost;
private boolean readyPre, readyPost;
private ArrayList<Message> messagesPre, messagesPost, lastMessagesPre, lastMessagesPost;
private int queue;
private Lock lock;
public ThreadListener(int port) throws IOException
{
socket = new ServerSocket(port);
messagesPre = new ArrayList<Message>();
messagesPost = new ArrayList<Message>();
readyPre = readyPost = false;
lastMessagesPre = new ArrayList<Message>();
lastMessagesPost = new ArrayList<Message>();
queue = 0;
lock = new ReentrantLock();
}
Expand All @@ -151,24 +151,30 @@ public void run()
lock.lock();
if (line.equals(BEGIN_PRE))
{
readyPre = false;
// swap lists
ArrayList<Message> tmp = lastMessagesPre;
lastMessagesPre = messagesPre;
messagesPre = tmp;

messagesPre.clear();
queue = -1;
}
else if (line.equals(END_PRE))
{
readyPre = true;
queue = 0;
}
else if (line.equals(BEGIN_POST))
{
readyPost = false;
// swap lists
ArrayList<Message> tmp = lastMessagesPost;
lastMessagesPost = messagesPost;
messagesPost = tmp;

messagesPost.clear();
queue = 1;
}
else if (line.equals(END_POST))
{
readyPost = true;
queue = 0;
}
else if (queue != 0)
Expand Down Expand Up @@ -206,13 +212,13 @@ public void draw(Graphics graphics, LocalTestRendererListener listner, boolean i
{
ArrayList<Message> messages;
lock.lock();
if (isPre && readyPre)
if (isPre)
{
messages = messagesPre;
messages = lastMessagesPre;
}
else if (!isPre && readyPost)
else if (!isPre)
{
messages = messagesPost;
messages = lastMessagesPost;
}
else
{
Expand Down

0 comments on commit 9dc90fe

Please sign in to comment.