move proto/p2 -> src/proto2
authorterom
Mon, 03 Nov 2008 22:59:50 +0000
changeset 5 617813994ab1
parent 4 e28b28b8817c
child 6 faa4e777cc6e
move proto/p2 -> src/proto2
CMakeLists.txt
proto/p2/Dimension.hh
proto/p2/Drawer.cc
proto/p2/GameState.hh
src/proto2/Dimension.hh
src/proto2/Drawer.cc
src/proto2/GameState.hh
src/proto2/Network.hh
src/proto2/NetworkClient.hh
src/proto2/NetworkServer.cc
src/proto2/NetworkServer.hh
--- a/CMakeLists.txt	Mon Nov 03 22:20:21 2008 +0000
+++ b/CMakeLists.txt	Mon Nov 03 22:59:50 2008 +0000
@@ -1,5 +1,5 @@
-set(PROJECT_SHORT_NAME "exampleproject")
-set(PROJECT_LONG_NAME "Example Project")
+set(PROJECT_SHORT_NAME "kg")
+set(PROJECT_LONG_NAME "Kisna Glista")
 set(PROJECT_VERSION_MAJOR 0)
 set(PROJECT_VERSION_MINOR 1)
 
--- a/proto/p2/Dimension.hh	Mon Nov 03 22:20:21 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-#ifndef DIMENSION_HH
-#define DIMENSION_HH
-
-class Dimension {
-	public:
-		uint32_t width;
-		uint32_t height;
-
-		Dimension (uint32_t width, uint32_t height) : width(width), height(height) { }
-};
-
-class Coordinate {
-	public:
-		uint32_t x;
-		uint32_t y;
-
-		Coordinate (uint32_t x, uint32_t y) : x(x), y(y) { }
-
-		Coordinate &operator+= (const PositionDelta &d) {
-			this->x += d.dx;
-			this->y += d.dy;
-		}
-
-		uint32_t scaledX() {
-			// Scale the coordinate so that it 
-			// matches the pixel resolution
-			return this->x/1;
-		}
-
-		uint32_t scaledY() {
-			return this->y/1;
-		}
-};
-
-class PositionDelta {
-	public:
-		uint32_t dx;
-		uint32_t dy;
-
-		PositionDelta (uint32_t dx, uint32_t dy) : dx(dx), dy(dy) { }
-};
-
-#endif
--- a/proto/p2/Drawer.cc	Mon Nov 03 22:20:21 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-#include <ClanLib/core.h>
-#include <ClanLib/gl.h> //do I need this in linux?
-#include <ClanLib/display.h>
-#include <ClanLib/application.h>
-#include "GameState.hh"
-#include "Dimension.hh"
-
-class CL_Game : public CL_ClanApplication {
-private:
-	bool keep_going;
-	void check_input() {
-		if(CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
-			keep_going = false;
-			
-        LocalPlayer& lp = gs.getLocalPlayer();
-
-		if(CL_Keyboard::get_keycode(CL_KEY_UP)) {
-			lp.doMovement(PositionDelta(0,3));
-		}
-		if(CL_Keyboard::get_keycode(CL_KEY_DOWN)) {
-			lp.doMovement(PositionDelta(0,-3));
-		}
-		if(CL_Keyboard::get_keycode(CL_KEY_LEFT)) {
-			lp.doMovement(PositionDelta(-3,0));
-		}
-		if(CL_Keyboard::get_keycode(CL_KEY_RIGHT)) {
-			lp.doMovement(PositionDelta(3,0));
-		}
-	}
-
-public:
-	GameState gs;
-
-	CL_Game() : keep_going(true) {}
-	virtual int main(int argc, char **argv) {
-		CL_SetupCore setup_init;
-		CL_SetupDisplay setup_disp;
-		CL_SetupGL setup_gl;
-		
-		//gs.player_list.push(Player(Coordinate()));
-
-		CL_DisplayWindow win("ikkuna", 640, 480);
-//		CL_Surface bg(CL_PNGProvider("image.png");
-
-		unsigned int last_draw = CL_System::get_time();
-		int r = 100, g = 100, b = 100;
-		unsigned int frame_count = 0;
-		bool R = false, G = false, B = false;
-
-		while(keep_going) { //not good idea to put infinite loop
-			CL_Display::clear(CL_Color(r, g, b));
-            
-			int colorIdx = 0;
-			for(std::list<Player>::iterator it = gs.player_list.begin(); it != gs.player_list.end(); ++it) {
-
-				CL_Display::fill_rect(CL_Rect(it->getPosition().scaledX()-10, it->getPosition().scaledY()-10, 
-					it->getPosition().scaledX()+10, it->getPosition().scaledY()+10), CL_Color((colorIdx*30)%255, (colorIdx*30)%255, (colorIdx*30)%255));
-
-				colorIdx++;
-			}
- 			
-			CL_Display::flip(1);
-			frame_count++;
-			unsigned int cur_draw = CL_System::get_time();
-		
-			check_input();
-
-			last_draw = cur_draw;
-			CL_System::keep_alive();
-//			sleep(10); //flip already wait a short amount of time
-		}
-		return 0;
-	}
-};
-
-CL_Game inst;
--- a/proto/p2/GameState.hh	Mon Nov 03 22:20:21 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-#ifndef GAMESTATE_HH
-#define GAMESTATE_HH
-
-#include "Dimension.hh"
-
-class GameState {
-	public:
-		Dimension map_dimensions;
-		std::list<Player> player_list;
-
-		LocalPlayer &getLocalPlayer (void) {
-			// XXX: jotain
-		}
-};
-
-enum PlayerType {
-	PLAYER_LOCAL = 0x01;
-	PLAYER_REMOTE = 0x02;
-};
-
-class Player {
-	private:
-		Coordinate position;
-
-	public:
-
-		Player(Coordinate c) : position(c) {}
-
-		PlayerType type;
-		Dimensions dimensions;
-
-		Coordinate getPosition (void) const {
-			return this->position;
-		}
-};
-
-class LocalPlayer : public Player {
-	public:
-		void doMovement (PositionDelta d) {
-			this->position += d;
-
-			// XXX: notify server
-		}
-};
-
-class RemotePlayer : public Player {
-	public:
-		void updatePosition (Position p) {
-			this->position = p;
-		}
-}
-
-#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/Dimension.hh	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,43 @@
+#ifndef DIMENSION_HH
+#define DIMENSION_HH
+
+class Dimension {
+	public:
+		uint32_t width;
+		uint32_t height;
+
+		Dimension (uint32_t width, uint32_t height) : width(width), height(height) { }
+};
+
+class Coordinate {
+	public:
+		uint32_t x;
+		uint32_t y;
+
+		Coordinate (uint32_t x, uint32_t y) : x(x), y(y) { }
+
+		Coordinate &operator+= (const PositionDelta &d) {
+			this->x += d.dx;
+			this->y += d.dy;
+		}
+
+		uint32_t scaledX() {
+			// Scale the coordinate so that it 
+			// matches the pixel resolution
+			return this->x/1;
+		}
+
+		uint32_t scaledY() {
+			return this->y/1;
+		}
+};
+
+class PositionDelta {
+	public:
+		uint32_t dx;
+		uint32_t dy;
+
+		PositionDelta (uint32_t dx, uint32_t dy) : dx(dx), dy(dy) { }
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/Drawer.cc	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,76 @@
+#include <ClanLib/core.h>
+#include <ClanLib/gl.h> //do I need this in linux?
+#include <ClanLib/display.h>
+#include <ClanLib/application.h>
+#include "GameState.hh"
+#include "Dimension.hh"
+
+class CL_Game : public CL_ClanApplication {
+private:
+	bool keep_going;
+	void check_input() {
+		if(CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
+			keep_going = false;
+			
+        LocalPlayer& lp = gs.getLocalPlayer();
+
+		if(CL_Keyboard::get_keycode(CL_KEY_UP)) {
+			lp.doMovement(PositionDelta(0,3));
+		}
+		if(CL_Keyboard::get_keycode(CL_KEY_DOWN)) {
+			lp.doMovement(PositionDelta(0,-3));
+		}
+		if(CL_Keyboard::get_keycode(CL_KEY_LEFT)) {
+			lp.doMovement(PositionDelta(-3,0));
+		}
+		if(CL_Keyboard::get_keycode(CL_KEY_RIGHT)) {
+			lp.doMovement(PositionDelta(3,0));
+		}
+	}
+
+public:
+	GameState gs;
+
+	CL_Game() : keep_going(true) {}
+	virtual int main(int argc, char **argv) {
+		CL_SetupCore setup_init;
+		CL_SetupDisplay setup_disp;
+		CL_SetupGL setup_gl;
+		
+		//gs.player_list.push(Player(Coordinate()));
+
+		CL_DisplayWindow win("ikkuna", 640, 480);
+//		CL_Surface bg(CL_PNGProvider("image.png");
+
+		unsigned int last_draw = CL_System::get_time();
+		int r = 100, g = 100, b = 100;
+		unsigned int frame_count = 0;
+		bool R = false, G = false, B = false;
+
+		while(keep_going) { //not good idea to put infinite loop
+			CL_Display::clear(CL_Color(r, g, b));
+            
+			int colorIdx = 0;
+			for(std::list<Player>::iterator it = gs.player_list.begin(); it != gs.player_list.end(); ++it) {
+
+				CL_Display::fill_rect(CL_Rect(it->getPosition().scaledX()-10, it->getPosition().scaledY()-10, 
+					it->getPosition().scaledX()+10, it->getPosition().scaledY()+10), CL_Color((colorIdx*30)%255, (colorIdx*30)%255, (colorIdx*30)%255));
+
+				colorIdx++;
+			}
+ 			
+			CL_Display::flip(1);
+			frame_count++;
+			unsigned int cur_draw = CL_System::get_time();
+		
+			check_input();
+
+			last_draw = cur_draw;
+			CL_System::keep_alive();
+//			sleep(10); //flip already wait a short amount of time
+		}
+		return 0;
+	}
+};
+
+CL_Game inst;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/GameState.hh	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,55 @@
+#ifndef GAMESTATE_HH
+#define GAMESTATE_HH
+
+#include "Dimension.hh"
+
+#include <list>
+
+class GameState {
+	public:
+		Dimension map_dimensions;
+		std::list<Player> player_list;
+
+		LocalPlayer &getLocalPlayer (void) {
+			// XXX: jotain
+		}
+};
+
+enum PlayerType {
+	PLAYER_LOCAL = 0x01;
+	PLAYER_REMOTE = 0x02;
+};
+
+class Player {
+	private:
+		Coordinate position;
+
+	public:
+
+		Player(Coordinate c) : position(c) {}
+
+		PlayerType type;
+		Dimensions dimensions;
+
+		Coordinate getPosition (void) const {
+			return this->position;
+		}
+};
+
+class LocalPlayer : public Player {
+	public:
+		void doMovement (PositionDelta d) {
+			this->position += d;
+
+			// XXX: notify server
+		}
+};
+
+class RemotePlayer : public Player {
+	public:
+		void updatePosition (Position p) {
+			this->position = p;
+		}
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/Network.hh	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,26 @@
+#ifndef NETWORK_HH
+#define NETWORK_HH
+
+#include <ClanLib/Network/socket.h>
+
+#define NETWORK_PORT_STR "9338"
+#define NETWORK_PACKET_MAX 1280
+
+class NetworkBase {
+	protected:
+		CL_Socket socket;
+
+		NetworkBase () : 
+				socket(CL_Socket::udp)
+		{
+				socket.set_nonblocking(true);
+		}
+
+		NetworkBase (CL_Socket socket) :
+				socket(socket)
+		{
+				socket.set_nonblocking(true);
+		}
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/NetworkClient.hh	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,12 @@
+#ifndef NETWORKCLIENT_HH
+#define NETWORKCLIENT_HH
+
+class NetworkClient {
+	private:
+		GameState *state;
+	
+	public:
+		NetworkClient (GameState *state);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/NetworkServer.cc	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,63 @@
+#include "NetworkServer.hh"
+
+#include <iostream>
+
+NetworkServer::NetworkServer (GameState &state, const CL_IPAddress &listen_ip) : NetworkBase(), state(state) {
+	socket.bind(listen_ip);
+	socket.listen(NETWORK_SERVER_BACKLOG);
+	
+	// add socket to listener
+	listener.add_trigger(socket.get_read_trigger());
+
+	// bind slot for recv
+	slot_on_recv = socket.sig_read_triggered.connect(this, &NetworkServer::_onRecv);
+}
+
+static NetworkServer *NetworkServer::newServer (void) {
+	GameState *state = new GameState(/* XXX */);
+	CL_IPAddress listen_ip(NETWORK_PORT_STR);
+	
+	return new NetworkServer(state, listen_ip);
+}		
+
+void run (void) {
+	bool isRunning = true;
+
+	while (isRunning) {
+		/* XXX: all I need to do? */
+		listener.wait();
+		listener.reset_all();
+	}
+}
+
+void NetworkServer::_onRecv (void) {
+	char buf[NETWORK_PACKET_MAX];
+	CL_Socket src;
+	int ret;
+
+	ret = socket.recv(buf, NETWORK_PACKET_MAX, src);
+	
+	std::cout << src.get_address() << ":" << src.get_port() << " <- " << std::string(buf, NETWORK_PACKET_MAX) << std::endl;
+}
+		
+NetworkServerClient::NetworkServerClient (NetworkServer &server) : server(server) {
+	
+}
+		
+void NetworkServerClient::_onRecv (void) {
+	
+}
+
+void runNetworkServer (void) {
+	NetworkServer *server = NULL;
+
+	server = NetworkServer::newServer();
+	server->run();
+}
+
+int main (int argc, char **argv) {
+	runNetworkServer();
+
+	return 0;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/proto2/NetworkServer.hh	Mon Nov 03 22:59:50 2008 +0000
@@ -0,0 +1,39 @@
+#ifndef NETWORKSERVER_HH
+#define NETWORKSERVER_HH
+
+#include <list>
+
+#define NETWORK_SERVER_BACKLOG 8
+
+class NetworkServer : public NetworkBase {
+	private:
+		GameState *state;
+
+		std::map<CL_IPAddress, NetworkServerClient> client_sockets;
+
+		CL_EventListener listener;
+		
+		CL_Slot slot_on_accept;
+
+		NetworkServer (GameState *state, const CL_IPAddress &listen_ip);
+
+	public:
+		static NetworkServer *newServer (void);
+
+		void run (void);
+
+	private:
+		void _onAccept (void);
+};
+
+class NetworkServerClient {
+	private:
+		NetworkServer &server;
+		
+	private:
+		NetworkServerClient (NetworkServer &server);
+};
+
+void runNetworkServer (void);
+
+#endif