diff --git a/common/defines.hpp b/common/defines.hpp index 0eb6b94..321bd39 100644 --- a/common/defines.hpp +++ b/common/defines.hpp @@ -1,4 +1,8 @@ #ifndef DEFINES_HPP_ #define DEFINES_HPP +#include + +typedef std::chrono::high_resolution_clock Clock; + #endif diff --git a/common/packet_type.hpp b/common/packet_type.hpp index 65e0693..54845bd 100644 --- a/common/packet_type.hpp +++ b/common/packet_type.hpp @@ -23,66 +23,65 @@ enum class PacketType { PLAYER_NEW = 9, PLAYER_DELETE = 10, PLAYER_MOVE = 11, - }; struct Ping { - PacketType type = PacketType::PING; + PacketType type; }; struct Pong { - PacketType type = PacketType::PONG; + PacketType type; }; struct BroadcastRequest { - PacketType type = PacketType::BROADCAST_REQUEST; + PacketType type; }; struct BroadcastResponse { - PacketType type = PacketType::BROADCAST_RESPONSE; + PacketType type; char name[PACKET_STRING_SIZE]; //TODO: version }; struct JoinRequest { - PacketType type = PacketType::JOIN_REQUEST; + PacketType type; //TODO: player data }; struct JoinResponse { - PacketType type = PacketType::JOIN_RESPONSE; + PacketType type; int playerIndex; //resource list }; struct Disconnect { - PacketType type = PacketType::DISCONNECT; + PacketType type; }; struct Synchronize { - PacketType type = PacketType::SYNCHRONIZE; + PacketType type; }; struct PlayerNew { - PacketType type = PacketType::PLAYER_NEW; + PacketType type; int playerIndex; //TODO Playerdata }; struct PlayerDelete { - PacketType type = PacketType::PLAYER_DELETE; + PacketType type; int playerIndex; }; struct PlayerMove { - PacketType type = PacketType::PLAYER_MOVE; + PacketType type; int playerIndex; Vector2 position; Vector2 motion; }; union Packet { - Packet() {} + Packet() {}; PacketType type = PacketType::NONE; Ping ping; diff --git a/rsc/config.cfg b/rsc/config.cfg new file mode 100644 index 0000000..a7e8805 --- /dev/null +++ b/rsc/config.cfg @@ -0,0 +1 @@ +serverport=1991 \ No newline at end of file diff --git a/server/makefile b/server/makefile index 7738991..3df7ea7 100644 --- a/server/makefile +++ b/server/makefile @@ -1,6 +1,7 @@ #config LIBDIR=../libs -LIB=-lmingw32 -lSDLmain -lSDL $(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a +LOCALLIBS=$(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libSDL_net.a +LIB=$(LOCALLIBS) -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/SDL_net ../common CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES)) diff --git a/server/server_application.cpp b/server/server_application.cpp index 242f5af..8901f16 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -1,5 +1,14 @@ #include "server_application.hpp" +#include +#include + +using namespace std; + +//------------------------- +//Public access members +//------------------------- + ServerApplication::ServerApplication() { // } @@ -9,13 +18,87 @@ ServerApplication::~ServerApplication() { } void ServerApplication::Init() { - // + if (SDLNet_Init()) { + throw(runtime_error("Failed to initialide SDL_net")); + } + + configUtil.Load("rsc/config.cfg"); + netUtil.Open(configUtil.Int("serverport"), sizeof(Packet)); + running = true; } void ServerApplication::Proc() { - // + while(running) { + HandleNetwork(); + UpdateWorld(); + SDL_Delay(10); + } } void ServerApplication::Quit() { - // + netUtil.Close(); + SDLNet_Quit(); } + +//------------------------- +//Game loop +//------------------------- + +void ServerApplication::HandleNetwork() { + Packet p; + while(netUtil.Receive()) { + memcpy(&p, netUtil.GetInData(), sizeof(Packet)); + switch(p.type) { + case PacketType::PING: + // + break; + case PacketType::PONG: + // + break; + case PacketType::BROADCAST_REQUEST: + // + break; + case PacketType::BROADCAST_RESPONSE: + // + break; + case PacketType::JOIN_REQUEST: + // + break; + case PacketType::JOIN_RESPONSE: + // + break; + case PacketType::DISCONNECT: + // + break; + case PacketType::SYNCHRONIZE: + // + break; + case PacketType::PLAYER_NEW: + // + break; + case PacketType::PLAYER_DELETE: + // + break; + case PacketType::PLAYER_MOVE: + // + break; + default: + throw(runtime_error("Failed to recognize the packet type")); + } + } +} + +void ServerApplication::UpdateWorld() { + Clock::duration delta = Clock::now() - lastTick; + lastTick = Clock::now(); + double d = double(delta.count()) / Clock::duration::period::den; + for (auto it : players) { + it.second.Update(d); + } +} + +//------------------------- +//Network loop +//------------------------- + +//... \ No newline at end of file diff --git a/server/server_application.hpp b/server/server_application.hpp index 254dbb5..80aecba 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -1,6 +1,16 @@ #ifndef SERVERAPPLICATION_HPP_ #define SERVERAPPLICATION_HPP_ +#include "defines.hpp" +#include "packet_type.hpp" + +#include "config_Utility.hpp" +#include "udp_network_utility.hpp" +#include "vector2.hpp" + +#include +#include + class ServerApplication { public: ServerApplication(); @@ -8,8 +18,40 @@ public: void Init(); void Proc(); void Quit(); + + ServerApplication(ServerApplication const&) = delete; private: - bool running = true; + struct ClientData { + int index; + int channel; + int playerIndex; + }; + struct PlayerData { + int index; + int clientIndex; + Vector2 position; + Vector2 motion; + + void Update(double delta) { + position += motion * delta; + } + }; + + //game loop + void HandleNetwork(); + void UpdateWorld(); + + //network loop + //... + + Clock::time_point lastTick = Clock::now(); + + std::map clients; + std::map players; + + ConfigUtility configUtil; + UDPNetworkUtility netUtil; + bool running = false; }; #endif diff --git a/test/main.cpp b/test/main.cpp index dba7af3..cf90d1c 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,13 +1,19 @@ -//#include "packet_type.hpp" +#include "packet_type.hpp" #include "service_locator.hpp" #include "foobar.hpp" +#include "vector2.hpp" + #include using namespace std; int main() { ServiceLocator::Set(new int(42)); + ServiceLocator::Set(new Packet()); + cout << FooBar() << endl; + ServiceLocator::Set(nullptr); + ServiceLocator::Set(nullptr); return 0; } \ No newline at end of file