- log area done, re-enabled the rcon connection

Sun, 14 Dec 2014 17:53:24 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 14 Dec 2014 17:53:24 +0200
changeset 15
33da84af4bba
parent 14
33b8f428bacb
child 16
33bac54867bf

- log area done, re-enabled the rcon connection

sources/basics.h file | annotate | diff | comparison | revisions
sources/format.h file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/main.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/basics.h	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/basics.h	Sun Dec 14 17:53:24 2014 +0200
@@ -31,7 +31,6 @@
 #pragma once
 #include <algorithm>
 #include <functional>
-#include <memory>
 
 #define FUNCTION auto
 #define STATIC
@@ -45,12 +44,6 @@
 using std::min;
 using std::max;
 
-template<typename T>
-using SharedPointer = std::shared_ptr<T>;
-
-template<typename T>
-using WeakPointer = std::weak_ptr<T>;
-
 enum Color
 {
 	BLACK,
--- a/sources/format.h	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/format.h	Sun Dec 14 17:53:24 2014 +0200
@@ -198,5 +198,5 @@
 template<typename... argtypes>
 void print (const String& fmtstr, const argtypes&... args)
 {
-	print_to_console (format (fmtstr, args...);
+	print_to_console (format (fmtstr, args...));
 }
--- a/sources/interface.cpp	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/interface.cpp	Sun Dec 14 17:53:24 2014 +0200
@@ -35,6 +35,12 @@
 static int g_cursor = 0;
 static int g_pan = 0;
 static bool g_needRefresh = false;
+static bool g_needStatusBarRender = false;
+static bool g_needInputRender = false;
+static String g_statusBarText;
+static struct { char ch; int x; } g_cursorChar;
+static Vector<String> g_output = {""};
+static int g_outputScroll = 0;
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -61,7 +67,17 @@
 static FUNCTION
 interface_render_log_area() -> void
 {
+	int height = LINES - 3;
+	int start = max (0, g_output.size() - height - 1);
+	int end = min (g_output.size(), start + height);
+	int y = 1;
+	assert (end - start <= height);
 
+	for (int i = start; i < end; ++i)
+	{
+		mvhline (y, 0, ' ', COLS);
+		mvprintw (y++, 0, "%s", g_output[i].chars());
+	}
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -101,7 +117,10 @@
 	mvprintw (y, 0, "%s", prompt);
 	mvprintw (y, strlen (prompt) + displayTextBegin.length(), "%s", displayTextEnd.chars());
 	mvprintw (y, strlen (prompt), "%s", displayTextBegin.chars());
+	g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0';
+	g_cursorChar.x = strlen (prompt) + displayTextBegin.length() - 1;
 	g_needRefresh = true;
+	g_needInputRender = false;
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -109,7 +128,20 @@
 static FUNCTION
 interface_render_statusbar() -> void
 {
+	int y = LINES - 1;
+	mvhline (y, 0, ' ', COLS);
+	mvprintw (y, 0, "%s", g_statusBarText.chars());
+	g_needRefresh = true;
+	g_needStatusBarRender = false;
+}
 
+// -------------------------------------------------------------------------------------------------
+//
+static FUNCTION
+set_statusbar_text (const String& a) -> void
+{
+	g_statusBarText = a;
+	g_needStatusBarRender = true;
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -121,7 +153,19 @@
 	interface_render_log_area();
 	interface_render_statusbar();
 	interface_render_input();
-	g_needRefresh = true;
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+static FUNCTION
+interface_position_cursor() -> void
+{
+	int y = LINES - 2;
+
+	if (g_cursorChar.ch != '\0')
+		mvprintw (y, g_cursorChar.x, "%c", g_cursorChar.ch);
+	else
+		mvprintw (y, 1, " ");
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -147,61 +191,75 @@
 Interface::handle_input() -> void
 {
 	int ch = ::getch();
+	set_statusbar_text (String::from_number (ch));
 
-	if (ch == KEY_F(1))
+	if (ch >= 0x20 and ch <= 0x7E)
 	{
+		g_input.insert (g_cursor++, char (ch));
+		g_needInputRender = true;
+	}
+	else switch (ch)
+	{
+	case KEY_F(1):
 		endwin();
 		exit (EXIT_SUCCESS);
-	}
-	else if (ch >= 0x20 and ch <= 0x7E)
-	{
-		g_input.insert (g_cursor++, char (ch));
-		interface_render_input();
-		refresh();
-	}
-	else if (ch == KEY_LEFT)
-	{
+		break;
+
+	case KEY_LEFT:
 		if (g_cursor > 0)
 		{
 			g_cursor--;
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_RIGHT)
-	{
+		break;
+
+	case KEY_RIGHT:
 		if (g_cursor < g_input.length())
 		{
 			g_cursor++;
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_HOME)
-	{
+		break;
+
+	case KEY_HOME:
 		if (g_cursor != 0)
 		{
 			g_cursor = 0;
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_END)
-	{
+		break;
+
+	case KEY_END:
 		if (g_cursor != g_input.length())
 		{
 			g_cursor = g_input.length();
-			interface_render_input();
+			g_needInputRender = true;
 		}
-	}
-	else if (ch == KEY_BACKSPACE)
-	{
-		if (not g_input.is_empty() and g_cursor > 0)
+		break;
+
+	case KEY_BACKSPACE:
+		if (g_cursor > 0)
 		{
 			g_input.remove_at (--g_cursor);
-			interface_render_input();
+			g_needInputRender = true;
 		}
+		break;
+
+	case KEY_DC:
+		if (g_cursor < g_input.length())
+		{
+			g_input.remove_at (g_cursor);
+			g_needInputRender = true;
+		}
+		break;
 	}
 
+	if (g_needStatusBarRender) interface_render_statusbar();
+	if (g_needInputRender) interface_render_input();
+
 	if (g_needRefresh)
 	{
+		interface_position_cursor();
 		refresh();
 		g_needRefresh = false;
 	}
@@ -209,5 +267,18 @@
 
 FUNCTION print_to_console (const String& a) -> void
 {
-	
+	for (char ch : a)
+	{
+		if (ch == '\n')
+		{
+			g_output << "";
+			continue;
+		}
+
+		g_output[g_output.size() - 1] += ch;
+	}
+
+	interface_render_log_area();
+	interface_position_cursor();
+	refresh();
 }
--- a/sources/interface.h	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/interface.h	Sun Dec 14 17:53:24 2014 +0200
@@ -31,6 +31,8 @@
 #pragma once
 #include "main.h"
 
+class RCONSession;
+
 namespace Interface
 {
 	void initialize();
--- a/sources/main.cpp	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/main.cpp	Sun Dec 14 17:53:24 2014 +0200
@@ -43,13 +43,9 @@
 	HUFFMAN_Construct();
 	Interface::initialize();
 
-	/*
-	Vector<RCONSession*> rconsessions;
-	RCONSession* sess = new RCONSession;
-	sess->set_password ("testpassword");
-	sess->connect (IPAddress (localhost, 10666));
-	rconsessions << sess;
-	*/
+	new RCONSession;
+	RCONSession::get_session()->set_password ("testpassword");
+	RCONSession::get_session()->connect (IPAddress (localhost, 10666));
 
 	for (;;)
 	{
@@ -60,15 +56,14 @@
 		timeout.tv_usec = 250000; // 0.25 seconds
 		FD_ZERO (&fdset);
 		FD_SET (0, &fdset);
+		RCONSession* session = RCONSession::get_session();
 
-		/*
-		for (RCONSession* session : rconsessions)
+		if (session)
 		{
 			int fd = session->socket()->file_descriptor();
 			highest = max (highest, fd);
 			FD_SET (fd, &fdset);
 		}
-		*/
 
 		select (highest + 1, &fdset, nullptr, nullptr, &timeout);
 
@@ -76,10 +71,8 @@
 			// stdin is ready, what's incoming?
 			Interface::handle_input();
 
-		/*
-		for (RCONSession* session : rconsessions)
+		if (session)
 			session->tick();
-		*/
 	}
 
 	return EXIT_SUCCESS;
--- a/sources/network/rconsession.cpp	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/network/rconsession.cpp	Sun Dec 14 17:53:24 2014 +0200
@@ -1,6 +1,6 @@
 #include "rconsession.h"
 
-static Vector<RCONSessionPointer> g_allSessions;
+RCONSession* g_rconSession = nullptr;
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -8,6 +8,14 @@
 	m_state (RCON_DISCONNECTED),
 	m_lastPing (0)
 {
+	if (g_rconSession != NULL)
+	{
+		g_rconSession->disconnect();
+		delete g_rconSession;
+	}
+
+	g_rconSession = this;
+
 	if (not m_socket.set_blocking (false))
 	{
 		// TODO: find a better way to deal with errors
@@ -18,7 +26,11 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-RCONSession::~RCONSession() {}
+RCONSession::~RCONSession()
+{
+	if (g_rconSession == this)
+		g_rconSession = nullptr;
+}
 
 // -------------------------------------------------------------------------------------------------
 //
@@ -242,7 +254,7 @@
 // -------------------------------------------------------------------------------------------------
 //
 STATIC METHOD
-RCONSession::all_sessions() -> const Vector<RCONSessionPointer>&
+RCONSession::get_session() -> RCONSession*
 {
-	return g_allSessions;
+	return g_rconSession;
 }
--- a/sources/network/rconsession.h	Sat Dec 13 07:36:00 2014 +0200
+++ b/sources/network/rconsession.h	Sun Dec 14 17:53:24 2014 +0200
@@ -103,7 +103,7 @@
 	METHOD tick() -> void;
 	METHOD bump_last_ping() -> void;
 
-	static METHOD all_sessions() -> const Vector<SharedPointer<RCONSession>>&;
+	static METHOD get_session() -> RCONSession*;
 
 private:
 	RCONSessionState m_state;
@@ -115,5 +115,3 @@
 	int m_serverProtocol;
 	String m_hostname;
 };
-
-using RCONSessionPointer = SharedPointer<RCONSession>;

mercurial