Restructered project and better building
@@ -55,7 +55,8 @@ MESSAGE( STATUS "Box2D lib: " ${BOX2D_LIBRARIES} )
|
|||||||
|
|
||||||
file(GLOB_RECURSE DarkRP2D_SOURCE_FILES ${DarkRP2D_SOURCE_DIR}/src/*.cpp)
|
file(GLOB_RECURSE DarkRP2D_SOURCE_FILES ${DarkRP2D_SOURCE_DIR}/src/*.cpp)
|
||||||
|
|
||||||
if (${CMAKE_BUILD_TYPE} MATCHES "DEBUG")
|
if (${CMAKE_BUILD_TYPE} MATCHES "DEBUG")
|
||||||
|
# Include imgui to build
|
||||||
file(GLOB IMGUI_SOURCE_FILES ${IMGUI_DIR}/*.cpp)
|
file(GLOB IMGUI_SOURCE_FILES ${IMGUI_DIR}/*.cpp)
|
||||||
include_directories(${IMGUI_DIR}/include)
|
include_directories(${IMGUI_DIR}/include)
|
||||||
set(DarkRP2D_SOURCE_FILES ${DarkRP2D_SOURCE_FILES} ${IMGUI_SOURCE_FILES})
|
set(DarkRP2D_SOURCE_FILES ${DarkRP2D_SOURCE_FILES} ${IMGUI_SOURCE_FILES})
|
||||||
@@ -71,13 +72,22 @@ target_link_libraries(DarkRP2D ${SDL2_LIBRARIES}
|
|||||||
${BOX2D_LIBRARIES}
|
${BOX2D_LIBRARIES}
|
||||||
${ENTITYX_LIBRARIES})
|
${ENTITYX_LIBRARIES})
|
||||||
|
|
||||||
add_custom_target(CopyBinaries
|
add_custom_command(
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_DLL} ${CMAKE_BINARY_DIR}
|
TARGET ${PROJECT_NAME} POST_BUILD
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_IMAGE_DLL} ${CMAKE_BINARY_DIR}
|
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy ${FREETYPE_DLL} ${CMAKE_BINARY_DIR}
|
COMMAND ${CMAKE_COMMAND} -E copy ${FREETYPE_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy ${GLEW_DLL} ${CMAKE_BINARY_DIR}
|
COMMAND ${CMAKE_COMMAND} -E copy ${GLEW_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy ${BOX2D_DLL} ${CMAKE_BINARY_DIR}
|
COMMAND ${CMAKE_COMMAND} -E copy ${BOX2D_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy ${ENTITYX_DLL} ${CMAKE_BINARY_DIR}
|
COMMAND ${CMAKE_COMMAND} -E copy ${ENTITYX_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_IMAGE_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_PNG_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_ZLIB_DLL} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
COMMENT "copys binaries from to ${CMAKE_CURRENT_BINARY_DIR}"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_dependencies(DarkRP2D CopyBinaries)
|
set (RESOURCE_DIR "${DarkRP2D_SOURCE_DIR}/resources")
|
||||||
|
add_custom_command(
|
||||||
|
TARGET ${PROJECT_NAME} POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_directory ${RESOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
COMMENT "copys resources from ${RESOURCE_DIR} => ${CMAKE_CURRENT_BINARY_DIR}"
|
||||||
|
)
|
||||||
9
dedicated_server/src/Constants.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace constants {
|
||||||
|
|
||||||
|
const int CLIENT_PORT = 9999;
|
||||||
|
const int MAX_CLIENTS = 128;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
22
dedicated_server/src/Protocoll.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define CONNECTION_REQUEST 0x01
|
||||||
|
#define CONNECTION_ACCEPTED 0x02
|
||||||
|
#define CONNECTION_DENIED 0x03
|
||||||
|
|
||||||
|
struct ConnectionRequest {
|
||||||
|
ConnectionRequest() : type(CONNECTION_REQUEST) {}
|
||||||
|
char type = CONNECTION_REQUEST;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ConnectionAccepted {
|
||||||
|
ConnectionAccepted() : type(CONNECTION_ACCEPTED) {}
|
||||||
|
char type;
|
||||||
|
int index;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ConnectionDenied {
|
||||||
|
ConnectionDenied() : type(CONNECTION_DENIED) {}
|
||||||
|
char type;
|
||||||
|
};
|
||||||
|
|
||||||
164
dedicated_server/src/Server.cpp
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
#include "Server.h"
|
||||||
|
#include "Protocoll.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
Server::Server(int port) : m_maxClients(constants::MAX_CLIENTS), m_clientConnected{0}
|
||||||
|
{
|
||||||
|
udpSocket = SDLNet_UDP_Open(port);
|
||||||
|
|
||||||
|
if (udpSocket == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_UDP_Open failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "Server was started and is listening on port: " << port << std::endl;
|
||||||
|
|
||||||
|
//Creating ConnectionAccepted packet!
|
||||||
|
// Allocate udp packet with 512 byte size
|
||||||
|
connectionAccepted = SDLNet_AllocPacket(512);
|
||||||
|
|
||||||
|
if (connectionAccepted == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
ConnectionAccepted conAcc = ConnectionAccepted();
|
||||||
|
memcpy(connectionAccepted->data, &conAcc, sizeof(ConnectionAccepted));
|
||||||
|
connectionAccepted->len = sizeof(ConnectionAccepted);
|
||||||
|
|
||||||
|
connectionDenied = SDLNet_AllocPacket(512);
|
||||||
|
|
||||||
|
if (connectionDenied == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
ConnectionDenied conDen = ConnectionDenied();
|
||||||
|
memcpy(connectionDenied->data, &conDen, sizeof(ConnectionDenied));
|
||||||
|
connectionDenied->len = sizeof(ConnectionDenied);
|
||||||
|
|
||||||
|
//Creating receiver packet!
|
||||||
|
receivedPacket = SDLNet_AllocPacket(512);
|
||||||
|
|
||||||
|
if (receivedPacket == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Server::FindFreeClientIndex() const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < m_maxClients; ++i)
|
||||||
|
{
|
||||||
|
if (!m_clientConnected[i])
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Server::FindExistingClientIndex(const IPaddress &ip) const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < m_maxClients; ++i)
|
||||||
|
{
|
||||||
|
if (m_clientConnected[i] && m_clients[i].host == ip.host && m_clients[i].port == ip.port)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::IsClientConnected(int clientIndex) const
|
||||||
|
{
|
||||||
|
return m_clientConnected[clientIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
const IPaddress &Server::GetClientAddress(int clientIndex) const
|
||||||
|
{
|
||||||
|
return m_clients[clientIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::update(unsigned int delta)
|
||||||
|
{
|
||||||
|
//Receive packages
|
||||||
|
if (SDLNet_UDP_Recv(udpSocket, receivedPacket))
|
||||||
|
{
|
||||||
|
switch (*(receivedPacket->data))
|
||||||
|
{
|
||||||
|
case CONNECTION_REQUEST:
|
||||||
|
std::cout << "Connection request package received\n";
|
||||||
|
int freeIndex;
|
||||||
|
if ((freeIndex = this->FindFreeClientIndex()) == -1)
|
||||||
|
{
|
||||||
|
std::cout << "Connection denied because server full" << std::endl;
|
||||||
|
//Send Connection denied
|
||||||
|
this->sendConnectionDenied(receivedPacket->address);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Free index found: " << freeIndex << std::endl;
|
||||||
|
int indexOfExistingClient;
|
||||||
|
if ((indexOfExistingClient = FindExistingClientIndex(receivedPacket->address)) == -1)
|
||||||
|
{
|
||||||
|
std::cout << "Client doesent exist create new" << std::endl;
|
||||||
|
m_clients[freeIndex] = receivedPacket->address;
|
||||||
|
m_clientConnected[freeIndex] = true;
|
||||||
|
//Send connection accepted with index
|
||||||
|
this->sendConnectionAccepted(receivedPacket->address, freeIndex);
|
||||||
|
m_numConnectedClients++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Client exist sending accepted" << std::endl;
|
||||||
|
//Send connection accepted with index
|
||||||
|
this->sendConnectionAccepted(receivedPacket->address, indexOfExistingClient);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Wait 10 sec
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
std::this_thread::sleep_for(1s);
|
||||||
|
|
||||||
|
//Send data
|
||||||
|
/*for(auto &value: clients) {
|
||||||
|
value.send(this->udpSocket,*helloWorldPacket);
|
||||||
|
}
|
||||||
|
std::cout << "Data was send!!" << std::endl;*/
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::sendPacket(UDPpacket &packet)
|
||||||
|
{
|
||||||
|
// Send
|
||||||
|
// SDLNet_UDP_Send returns number of packets sent. 0 means error
|
||||||
|
if (SDLNet_UDP_Send(this->udpSocket, -1, &packet) == 0)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_UDP_Send failed : " << SDLNet_GetError() << "\n"
|
||||||
|
<< "==========================================================================================================\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::sendConnectionDenied(IPaddress dest)
|
||||||
|
{
|
||||||
|
this->connectionDenied->address.host = dest.host;
|
||||||
|
this->connectionDenied->address.port = dest.port;
|
||||||
|
return this->sendPacket(*connectionDenied);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::sendConnectionAccepted(IPaddress dest, int index)
|
||||||
|
{
|
||||||
|
this->connectionAccepted->address.host = dest.host;
|
||||||
|
this->connectionAccepted->address.port = dest.port;
|
||||||
|
((ConnectionAccepted *) connectionAccepted->data)->index = index;
|
||||||
|
return this->sendPacket(*connectionAccepted);
|
||||||
|
}
|
||||||
|
|
||||||
|
Server::~Server()
|
||||||
|
{
|
||||||
|
SDLNet_FreePacket(receivedPacket);
|
||||||
|
SDLNet_FreePacket(connectionAccepted);
|
||||||
|
SDLNet_UDP_Close(udpSocket);
|
||||||
|
}
|
||||||
36
dedicated_server/src/Server.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL_net.h>
|
||||||
|
#include "Constants.h"
|
||||||
|
|
||||||
|
class Server {
|
||||||
|
public:
|
||||||
|
Server(int port);
|
||||||
|
~Server();
|
||||||
|
|
||||||
|
int FindFreeClientIndex() const;
|
||||||
|
int FindExistingClientIndex( const IPaddress &ip ) const;
|
||||||
|
bool IsClientConnected( int clientIndex ) const;
|
||||||
|
const IPaddress & GetClientAddress( int clientIndex ) const;
|
||||||
|
|
||||||
|
void update(unsigned int delta);
|
||||||
|
|
||||||
|
void gameTick();
|
||||||
|
|
||||||
|
private:
|
||||||
|
UDPsocket udpSocket;
|
||||||
|
|
||||||
|
UDPpacket *connectionDenied;
|
||||||
|
UDPpacket *connectionAccepted;
|
||||||
|
UDPpacket *receivedPacket;
|
||||||
|
|
||||||
|
int m_maxClients;
|
||||||
|
int m_numConnectedClients;
|
||||||
|
bool m_clientConnected[constants::MAX_CLIENTS];
|
||||||
|
IPaddress m_clients[constants::MAX_CLIENTS];
|
||||||
|
|
||||||
|
bool sendPacket(UDPpacket &packet);
|
||||||
|
|
||||||
|
bool sendConnectionDenied(IPaddress dest);
|
||||||
|
bool sendConnectionAccepted(IPaddress dest, int index);
|
||||||
|
};
|
||||||
@@ -4,9 +4,13 @@ set(SDL2_IMAGE_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
|
|||||||
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
|
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
|
||||||
set(SDL2_IMAGE_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_image.lib")
|
set(SDL2_IMAGE_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_image.lib")
|
||||||
set(SDL2_IMAGE_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_image.dll")
|
set(SDL2_IMAGE_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2_image.dll")
|
||||||
|
set(SDL2_PNG_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x64/libpng16-16.dll")
|
||||||
|
set(SDL2_ZLIB_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x64/zlib1.dll")
|
||||||
else ()
|
else ()
|
||||||
set(SDL2_IMAGE_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_image.lib")
|
set(SDL2_IMAGE_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_image.lib")
|
||||||
set(SDL2_IMAGE_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_image.dll")
|
set(SDL2_IMAGE_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2_image.dll")
|
||||||
|
set(SDL2_PNG_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x86/libpng16-16.dll")
|
||||||
|
set(SDL2_ZLIB_DLL "${CMAKE_CURRENT_LIST_DIR}/lib/x86/zlib1.dll")
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
string(STRIP "${SDL2_IMAGE_LIBRARIES}" SDL2_IMAGE_LIBRARIES)
|
string(STRIP "${SDL2_IMAGE_LIBRARIES}" SDL2_IMAGE_LIBRARIES)
|
||||||
BIN
resources/assets/1x1black.png
Normal file
|
After Width: | Height: | Size: 119 B |
|
Before Width: | Height: | Size: 372 B After Width: | Height: | Size: 372 B |
|
Before Width: | Height: | Size: 696 B After Width: | Height: | Size: 696 B |
|
Before Width: | Height: | Size: 147 B After Width: | Height: | Size: 147 B |
|
Before Width: | Height: | Size: 671 B After Width: | Height: | Size: 671 B |
|
Before Width: | Height: | Size: 594 B After Width: | Height: | Size: 594 B |
|
Before Width: | Height: | Size: 319 B After Width: | Height: | Size: 319 B |
|
Before Width: | Height: | Size: 407 B After Width: | Height: | Size: 407 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 982 B After Width: | Height: | Size: 982 B |
|
Before Width: | Height: | Size: 607 B After Width: | Height: | Size: 607 B |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
|
Before Width: | Height: | Size: 747 B After Width: | Height: | Size: 747 B |
|
Before Width: | Height: | Size: 952 B After Width: | Height: | Size: 952 B |
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 803 B After Width: | Height: | Size: 803 B |
|
Before Width: | Height: | Size: 812 B After Width: | Height: | Size: 812 B |
BIN
resources/assets/edge.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 149 B After Width: | Height: | Size: 149 B |
|
Before Width: | Height: | Size: 167 B After Width: | Height: | Size: 167 B |
|
Before Width: | Height: | Size: 147 B After Width: | Height: | Size: 147 B |
|
Before Width: | Height: | Size: 167 B After Width: | Height: | Size: 167 B |
|
Before Width: | Height: | Size: 149 B After Width: | Height: | Size: 149 B |
|
Before Width: | Height: | Size: 196 B After Width: | Height: | Size: 196 B |
|
Before Width: | Height: | Size: 811 B After Width: | Height: | Size: 811 B |
|
Before Width: | Height: | Size: 167 B After Width: | Height: | Size: 167 B |
|
Before Width: | Height: | Size: 183 B After Width: | Height: | Size: 183 B |
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 642 B After Width: | Height: | Size: 642 B |
@@ -26,13 +26,13 @@ void DarkRP2D::create()
|
|||||||
{
|
{
|
||||||
std::cout << "Game got created!" << std::endl;
|
std::cout << "Game got created!" << std::endl;
|
||||||
assets = new Assets();
|
assets = new Assets();
|
||||||
assets->loadTexture("police_officer.png", true, "police_officer");
|
assets->loadTexture("assets/police_officer.png", true, "police_officer");
|
||||||
assets->loadShader("vert.shader", "frag.shader", nullptr, "defaultShader");
|
assets->loadShader("shaders/vert.shader", "shaders/frag.shader", nullptr, "defaultShader");
|
||||||
assets->loadShader("bitmapvert.shader", "bitmapfrag.shader", nullptr, "bitmapShader");
|
assets->loadShader("shaders/bitmapvert.shader", "shaders/bitmapfrag.shader", nullptr, "bitmapShader");
|
||||||
|
|
||||||
renderer = new Renderer();
|
renderer = new Renderer();
|
||||||
|
|
||||||
font = new BitmapFont("arial.ttf");
|
font = new BitmapFont("fonts/arial.ttf");
|
||||||
|
|
||||||
gameScreen = new GameScreen(renderer, assets);
|
gameScreen = new GameScreen(renderer, assets);
|
||||||
gameScreen->show();
|
gameScreen->show();
|
||||||
|
|||||||
41
testClient/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.0)
|
||||||
|
project(DarkRP2D_TEST_CLIENT)
|
||||||
|
set (CMAKE_CXX_STANDARD 11)
|
||||||
|
include(ExternalProject)
|
||||||
|
|
||||||
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE RELEASE)
|
||||||
|
add_definitions(-DNDEBUG)
|
||||||
|
endif(NOT CMAKE_BUILD_TYPE)
|
||||||
|
|
||||||
|
MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )
|
||||||
|
MESSAGE( STATUS "DarkRP2D_TEST_CLIENT_SOURCE_DIR: " ${DarkRP2D_TEST_CLIENT_SOURCE_DIR} )
|
||||||
|
|
||||||
|
set(DarkRP2D_EXTERNAL_DIR ${DarkRP2D_TEST_CLIENT_SOURCE_DIR}/../external)
|
||||||
|
set(SDL2_net_DIR ${DarkRP2D_EXTERNAL_DIR}/SDL2_net-2.0.1)
|
||||||
|
set(SDL2_DIR ${DarkRP2D_EXTERNAL_DIR}/SDL2-2.0.8)
|
||||||
|
|
||||||
|
#Libs
|
||||||
|
|
||||||
|
find_package(SDL2 REQUIRED CONFIG)
|
||||||
|
include_directories(${SDL2_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
find_package(SDL2_net REQUIRED CONFIG)
|
||||||
|
include_directories(${SDL2_NET_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
MESSAGE( STATUS "SDL2 Lib: " ${SDL2_LIBRARIES} )
|
||||||
|
MESSAGE( STATUS "SDL2_NET Lib: " ${SDL2_NET_LIBRARIES} )
|
||||||
|
|
||||||
|
file(GLOB_RECURSE DarkRP2D_TEST_CLIENT_SOURCE_FILES ${DarkRP2D_TEST_CLIENT_SOURCE_DIR}/src/*.cpp)
|
||||||
|
|
||||||
|
add_executable(DarkRP2DTestClient ${DarkRP2D_TEST_CLIENT_SOURCE_FILES})
|
||||||
|
|
||||||
|
target_link_libraries(DarkRP2DTestClient ${SDL2_LIBRARIES}
|
||||||
|
${SDL2_NET_LIBRARIES} )
|
||||||
|
|
||||||
|
add_custom_target(CopyBinaries
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_DLL} ${CMAKE_BINARY_DIR}
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy ${SDL2_NET_DLL} ${CMAKE_BINARY_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
add_dependencies(DarkRP2DTestClient CopyBinaries)
|
||||||
87
testClient/src/Client.cpp
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#include "Client.h"
|
||||||
|
#include "Constants.h"
|
||||||
|
#include "Protocoll.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
Client::Client(std::string _serverIP, int port) : connected(new Connected(this)),
|
||||||
|
connecting(new Connecting(this)),
|
||||||
|
disconnected(new Disconnected(this))
|
||||||
|
{
|
||||||
|
|
||||||
|
udpSocket = SDLNet_UDP_Open(constants::CLIENT_PORT);
|
||||||
|
|
||||||
|
if (udpSocket == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_UDP_Open failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set IP and port number with correct endianess
|
||||||
|
if (SDLNet_ResolveHost(&serverIP, _serverIP.c_str(), port) == -1)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_ResolveHost failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
connectionRequest = SDLNet_AllocPacket(512);
|
||||||
|
|
||||||
|
if (connectionRequest == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
ConnectionRequest conReq = ConnectionRequest();
|
||||||
|
memcpy(connectionRequest->data, &conReq, sizeof(ConnectionRequest));
|
||||||
|
connectionRequest->len = sizeof(ConnectionRequest);
|
||||||
|
|
||||||
|
receivedPacket = SDLNet_AllocPacket(512);
|
||||||
|
|
||||||
|
if (receivedPacket == nullptr)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_AllocPacket failed : " << SDLNet_GetError() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->setClientState(connecting);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::setClientState(ClientState *state)
|
||||||
|
{
|
||||||
|
std::cout << "State changed!" << std::endl;
|
||||||
|
currentState = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::update(unsigned int delta)
|
||||||
|
{
|
||||||
|
currentState->update(delta);
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
std::this_thread::sleep_for(2s);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Client::sendConnectionRequest(IPaddress dest)
|
||||||
|
{
|
||||||
|
connectionRequest->address.host = dest.host;
|
||||||
|
connectionRequest->address.port = dest.port;
|
||||||
|
return this->sendPacket(*connectionRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Client::sendPacket(UDPpacket &packet)
|
||||||
|
{
|
||||||
|
// Send
|
||||||
|
// SDLNet_UDP_Send returns number of packets sent. 0 means error
|
||||||
|
if (SDLNet_UDP_Send(this->udpSocket, -1, &packet) == 0)
|
||||||
|
{
|
||||||
|
std::cout << "\tSDLNet_UDP_Send failed : " << SDLNet_GetError() << "\n"
|
||||||
|
<< "==========================================================================================================\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Client::~Client()
|
||||||
|
{
|
||||||
|
delete connected;
|
||||||
|
delete connecting;
|
||||||
|
delete disconnected;
|
||||||
|
SDLNet_FreePacket(connectionRequest);
|
||||||
|
SDLNet_FreePacket(receivedPacket);
|
||||||
|
SDLNet_UDP_Close(udpSocket);
|
||||||
|
}
|
||||||
37
testClient/src/Client.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL_net.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Client;
|
||||||
|
|
||||||
|
#include "ClientStates.h"
|
||||||
|
|
||||||
|
|
||||||
|
class Client {
|
||||||
|
public:
|
||||||
|
Client(std::string serverIP, int port);
|
||||||
|
~Client();
|
||||||
|
|
||||||
|
Client(const Client&) = delete;
|
||||||
|
|
||||||
|
void setClientState(ClientState *state);
|
||||||
|
|
||||||
|
void update(unsigned int delta);
|
||||||
|
|
||||||
|
|
||||||
|
bool sendConnectionRequest(IPaddress dest);
|
||||||
|
bool sendPacket(UDPpacket &packet);
|
||||||
|
|
||||||
|
UDPsocket udpSocket;
|
||||||
|
IPaddress serverIP;
|
||||||
|
|
||||||
|
UDPpacket *connectionRequest;
|
||||||
|
UDPpacket *receivedPacket;
|
||||||
|
|
||||||
|
ClientState *currentState;
|
||||||
|
|
||||||
|
Connected *connected;
|
||||||
|
Connecting *connecting;
|
||||||
|
Disconnected *disconnected;
|
||||||
|
};
|
||||||
40
testClient/src/ClientStates.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#include "ClientStates.h"
|
||||||
|
|
||||||
|
#include "Protocoll.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void Connecting::update(unsigned int delta)
|
||||||
|
{
|
||||||
|
std::cout << "Connecting.. (Sending Request)" << std::endl;
|
||||||
|
m_client->sendConnectionRequest(m_client->serverIP);
|
||||||
|
if (SDLNet_UDP_Recv(m_client->udpSocket, m_client->receivedPacket))
|
||||||
|
{
|
||||||
|
std::cout << "Something received.." << std::endl;
|
||||||
|
switch (*(m_client->receivedPacket->data))
|
||||||
|
{
|
||||||
|
case CONNECTION_ACCEPTED:
|
||||||
|
{
|
||||||
|
int index = ((ConnectionAccepted *)(m_client->receivedPacket->data))->index;
|
||||||
|
std::cout << "Server accepted Connection! My Index ist: " << index << std::endl;
|
||||||
|
m_client->setClientState(m_client->connected);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CONNECTION_DENIED:
|
||||||
|
std::cout << "Server denied Connection!" << std::endl;
|
||||||
|
m_client->setClientState(m_client->disconnected);
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Connected::update(unsigned int delta)
|
||||||
|
{
|
||||||
|
std::cout << "I AM IN CONNECTED STATE!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Disconnected::update(unsigned int delta)
|
||||||
|
{
|
||||||
|
std::cout << "I AM IN DISCONNECTED STATE!" << std::endl;
|
||||||
|
}
|
||||||
47
testClient/src/ClientStates.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class ClientState;
|
||||||
|
|
||||||
|
class Connecting;
|
||||||
|
class Connected;
|
||||||
|
class Disconnected;
|
||||||
|
|
||||||
|
#include "Client.h"
|
||||||
|
|
||||||
|
class ClientState
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void update(unsigned int delta) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Connecting : public ClientState {
|
||||||
|
public:
|
||||||
|
Connecting(Client *client) : m_client(client) {}
|
||||||
|
|
||||||
|
virtual void update(unsigned int delta) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client *m_client;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Connected : public ClientState
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Connected(Client *client) : m_client(client) {}
|
||||||
|
|
||||||
|
virtual void update(unsigned int delta) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client *m_client;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Disconnected : public ClientState
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Disconnected(Client *client) : m_client(client) {}
|
||||||
|
|
||||||
|
virtual void update(unsigned int delta) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Client *m_client;
|
||||||
|
};
|
||||||
8
testClient/src/Constants.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace constants {
|
||||||
|
|
||||||
|
const int CLIENT_PORT = 9999;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
22
testClient/src/Protocoll.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define CONNECTION_REQUEST 0x01
|
||||||
|
#define CONNECTION_ACCEPTED 0x02
|
||||||
|
#define CONNECTION_DENIED 0x03
|
||||||
|
|
||||||
|
struct ConnectionRequest {
|
||||||
|
ConnectionRequest() : type(CONNECTION_REQUEST) {}
|
||||||
|
char type = CONNECTION_REQUEST;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ConnectionAccepted {
|
||||||
|
ConnectionAccepted() : type(CONNECTION_ACCEPTED) {}
|
||||||
|
char type;
|
||||||
|
int index;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ConnectionDenied {
|
||||||
|
ConnectionDenied() : type(CONNECTION_DENIED) {}
|
||||||
|
char type;
|
||||||
|
};
|
||||||
|
|
||||||
33
testClient/src/TestClient.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <SDL_net.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Client.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
std::cout << "Starting DarkRP2D Client..." << std::endl;
|
||||||
|
|
||||||
|
if (SDLNet_Init() < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool clientQuit = false;
|
||||||
|
{
|
||||||
|
Client client("127.0.0.1",12000);
|
||||||
|
unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime;
|
||||||
|
while (!clientQuit)
|
||||||
|
{
|
||||||
|
currentTime = SDL_GetTicks();
|
||||||
|
elapsedTime = currentTime - lastTime;
|
||||||
|
lastTime = currentTime;
|
||||||
|
client.update(elapsedTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDLNet_Quit();
|
||||||
|
std::cout << "Client closed!" << std::endl;
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||