From 8c22238f267ebf30057afa2ba9fa5773cf2ce197 Mon Sep 17 00:00:00 2001 From: Kirill Vilkov Date: Wed, 18 Nov 2015 12:52:54 +0300 Subject: [PATCH 1/2] double bufferization --- plugins/LocalTestRendererListener.java | 28 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/plugins/LocalTestRendererListener.java b/plugins/LocalTestRendererListener.java index 19bdb02..74af8bc 100644 --- a/plugins/LocalTestRendererListener.java +++ b/plugins/LocalTestRendererListener.java @@ -117,8 +117,7 @@ private final class ThreadListener extends Thread public static final String END_POST = "end post"; private ServerSocket socket; - private ArrayList messagesPre, messagesPost; - private boolean readyPre, readyPost; + private ArrayList messagesPre, messagesPost, lastMessagesPre, lastMessagesPost; private int queue; private Lock lock; public ThreadListener(int port) throws IOException @@ -126,7 +125,8 @@ public ThreadListener(int port) throws IOException socket = new ServerSocket(port); messagesPre = new ArrayList(); messagesPost = new ArrayList(); - readyPre = readyPost = false; + lastMessagesPre = new ArrayList(); + lastMessagesPost = new ArrayList(); queue = 0; lock = new ReentrantLock(); } @@ -151,24 +151,30 @@ public void run() lock.lock(); if (line.equals(BEGIN_PRE)) { - readyPre = false; + // swap lists + ArrayList 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 tmp = lastMessagesPost; + lastMessagesPost = messagesPost; + messagesPost = tmp; + messagesPost.clear(); queue = 1; } else if (line.equals(END_POST)) { - readyPost = true; queue = 0; } else if (queue != 0) @@ -206,13 +212,13 @@ public void draw(Graphics graphics, LocalTestRendererListener listner, boolean i { ArrayList messages; lock.lock(); - if (isPre && readyPre) + if (isPre) { - messages = messagesPre; + messages = lastMessagesPre; } - else if (!isPre && readyPost) + else if (!isPre) { - messages = messagesPost; + messages = lastMessagesPost; } else { From 81d0e73e90aea2be247266cdf5bc0e0b26ce3d53 Mon Sep 17 00:00:00 2001 From: Kirill Vilkov Date: Wed, 18 Nov 2015 13:04:19 +0300 Subject: [PATCH 2/2] csharp client --- clients/csharp/Debug.cs | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 clients/csharp/Debug.cs diff --git a/clients/csharp/Debug.cs b/clients/csharp/Debug.cs new file mode 100644 index 0000000..0712f51 --- /dev/null +++ b/clients/csharp/Debug.cs @@ -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))); + } + } + +} +