From 0a48131de4ad35d1e1b511e44349f5ac95249dfc Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 9 Jun 2013 17:06:32 +1000 Subject: [PATCH] Added common and test directories --- client/makefile | 2 +- common/defines.hpp | 4 ++ common/packet_type.hpp | 103 +++++++++++++++++++++++++++++++++++++ common/service_locator.hpp | 21 ++++++++ makefile | 1 + server/makefile | 2 +- test/foobar.cpp | 5 ++ test/foobar.hpp | 6 +++ test/main.cpp | 13 +++++ test/makefile | 38 ++++++++++++++ 10 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 common/defines.hpp create mode 100644 common/packet_type.hpp create mode 100644 common/service_locator.hpp create mode 100644 test/foobar.cpp create mode 100644 test/foobar.hpp create mode 100644 test/main.cpp create mode 100644 test/makefile diff --git a/client/makefile b/client/makefile index 7089e79..f40e00e 100644 --- a/client/makefile +++ b/client/makefile @@ -1,7 +1,7 @@ #config LIBDIR=../libs LIB=-lmingw32 -lSDLmain -lSDL $(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a -INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net +INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net ../common CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) #source diff --git a/common/defines.hpp b/common/defines.hpp new file mode 100644 index 0000000..0eb6b94 --- /dev/null +++ b/common/defines.hpp @@ -0,0 +1,4 @@ +#ifndef DEFINES_HPP_ +#define DEFINES_HPP + +#endif diff --git a/common/packet_type.hpp b/common/packet_type.hpp new file mode 100644 index 0000000..65e0693 --- /dev/null +++ b/common/packet_type.hpp @@ -0,0 +1,103 @@ +#ifndef PACKETTYPE_HPP_ +#define PACKETTYPE_HPP_ + +#include "vector2.hpp" + +#define PACKET_STRING_SIZE 100 + +#pragma pack(push, 0) + +enum class PacketType { + NONE = 0, + + PING = 1, + PONG = 2, + BROADCAST_REQUEST = 3, + BROADCAST_RESPONSE = 4, + JOIN_REQUEST = 5, + JOIN_RESPONSE = 6, + DISCONNECT = 7, + + SYNCHRONIZE = 8, + + PLAYER_NEW = 9, + PLAYER_DELETE = 10, + PLAYER_MOVE = 11, + +}; + +struct Ping { + PacketType type = PacketType::PING; +}; + +struct Pong { + PacketType type = PacketType::PONG; +}; + +struct BroadcastRequest { + PacketType type = PacketType::BROADCAST_REQUEST; +}; + +struct BroadcastResponse { + PacketType type = PacketType::BROADCAST_RESPONSE; + char name[PACKET_STRING_SIZE]; + //TODO: version +}; + +struct JoinRequest { + PacketType type = PacketType::JOIN_REQUEST; + //TODO: player data +}; + +struct JoinResponse { + PacketType type = PacketType::JOIN_RESPONSE; + int playerIndex; + //resource list +}; + +struct Disconnect { + PacketType type = PacketType::DISCONNECT; +}; + +struct Synchronize { + PacketType type = PacketType::SYNCHRONIZE; +}; + +struct PlayerNew { + PacketType type = PacketType::PLAYER_NEW; + int playerIndex; + //TODO Playerdata +}; + +struct PlayerDelete { + PacketType type = PacketType::PLAYER_DELETE; + int playerIndex; +}; + +struct PlayerMove { + PacketType type = PacketType::PLAYER_MOVE; + int playerIndex; + Vector2 position; + Vector2 motion; +}; + +union Packet { + Packet() {} + PacketType type = PacketType::NONE; + + Ping ping; + Pong pong; + BroadcastRequest broadcastRequest; + BroadcastResponse broadcastResponse; + JoinRequest joinRequest; + JoinResponse joinResponse; + Disconnect disconnect; + + PlayerNew playerNew; + PlayerDelete playerDelete; + PlayerMove playerMove; +}; + +#pragma pack(pop) + +#endif diff --git a/common/service_locator.hpp b/common/service_locator.hpp new file mode 100644 index 0000000..b9d9ad2 --- /dev/null +++ b/common/service_locator.hpp @@ -0,0 +1,21 @@ +#ifndef SERVICELOCATOR_HPP_ +#define SERVICELOCATOR_HPP_ + +template +class ServiceLocator { +public: + static T* Set(T* t) { + delete service; + return service = t; + } + static T* Get() { + return service; + } +private: + static T* service; +}; + +template +T* ServiceLocator::service = nullptr; + +#endif diff --git a/makefile b/makefile index cb89da0..407e817 100644 --- a/makefile +++ b/makefile @@ -4,6 +4,7 @@ all: $(OUTDIR) $(MAKE) -C libs $(MAKE) -C server $(MAKE) -C client + $(MAKE) -C test $(OUTDIR): mkdir $(OUTDIR) diff --git a/server/makefile b/server/makefile index fd86c59..7738991 100644 --- a/server/makefile +++ b/server/makefile @@ -1,7 +1,7 @@ #config LIBDIR=../libs LIB=-lmingw32 -lSDLmain -lSDL $(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a -INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net +INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net ../common CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) #source diff --git a/test/foobar.cpp b/test/foobar.cpp new file mode 100644 index 0000000..2f53595 --- /dev/null +++ b/test/foobar.cpp @@ -0,0 +1,5 @@ +#include "service_locator.hpp" + +int FooBar() { + return *ServiceLocator::Get(); +} \ No newline at end of file diff --git a/test/foobar.hpp b/test/foobar.hpp new file mode 100644 index 0000000..2a21514 --- /dev/null +++ b/test/foobar.hpp @@ -0,0 +1,6 @@ +#ifndef FOOBAR_HPP_ +#define FOOBAR_HPP_ + +int FooBar(); + +#endif diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..dba7af3 --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,13 @@ +//#include "packet_type.hpp" +#include "service_locator.hpp" +#include "foobar.hpp" + +#include +using namespace std; + +int main() { + ServiceLocator::Set(new int(42)); + cout << FooBar() << endl; + ServiceLocator::Set(nullptr); + return 0; +} \ No newline at end of file diff --git a/test/makefile b/test/makefile new file mode 100644 index 0000000..d9f79aa --- /dev/null +++ b/test/makefile @@ -0,0 +1,38 @@ +#config +LIBDIR=../libs +LIB=-lmingw32 -lSDLmain -lSDL $(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a +INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net ../common +CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) + +#source +SRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o)) + +#output +OUTDIR=../out +OUT=$(addprefix $(OUTDIR)/,test) + +#targets +all: $(OBJ) $(OUT) + $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIB) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all