diff --git a/Assets/Woods.png b/Assets/Woods.png new file mode 100644 index 00000000..8cf6c786 Binary files /dev/null and b/Assets/Woods.png differ diff --git a/RoboCat/Chapter3.vcxproj b/RoboCat/Chapter3.vcxproj index c2d193be..b43bd365 100644 --- a/RoboCat/Chapter3.vcxproj +++ b/RoboCat/Chapter3.vcxproj @@ -92,6 +92,10 @@ true true Bin\$(Configuration)\ + true + true + true + true true @@ -128,7 +132,7 @@ ProgramDatabase EnableFastChecks ..\SDL\include;Inc;..\ - Use + NotUsing RoboCatPCH.h @@ -371,6 +375,9 @@ + + + @@ -383,14 +390,23 @@ + + + + + + + + + diff --git a/RoboCat/Inc/Colour.h b/RoboCat/Inc/Colour.h new file mode 100644 index 00000000..b7b59539 --- /dev/null +++ b/RoboCat/Inc/Colour.h @@ -0,0 +1,45 @@ +#pragma once + +/* +Allegro Wrapper Functions +Written by Adel Talhouk in FA21 +Colour.h + + File information: + This file contains data used for colours. +*/ + + + +class Colour +{ + //-------------------------Private data------------------------- + + //Red channel + unsigned __int8 mR; + + //Green channel + unsigned __int8 mG; + + //Blue channel + unsigned __int8 mB; + + //Alpha channel + unsigned __int8 mA; + + //-------------------------Public data------------------------- +public: + + //Constructor(s) + Colour(unsigned __int8 r, unsigned __int8 g, unsigned __int8 b); + Colour(unsigned __int8 r, unsigned __int8 g, unsigned __int8 b, unsigned __int8 a); + + //Destructor + ~Colour(); + + //Accessor(s) + unsigned __int8 getR() { return mR; }; + unsigned __int8 getG() { return mG; }; + unsigned __int8 getB() { return mB; }; + unsigned __int8 getA() { return mA; }; +}; diff --git a/RoboCat/Inc/GraphicsLibrary.h b/RoboCat/Inc/GraphicsLibrary.h new file mode 100644 index 00000000..2906975e --- /dev/null +++ b/RoboCat/Inc/GraphicsLibrary.h @@ -0,0 +1,68 @@ +#pragma once + +/* +Allegro Wrapper Functions +Written by Adel Talhouk in FA21 +GraphicsLibrary.h + + File information: + This file contains function abstractions from Allegro 5, wrapped up in my Graphics Library. This will + be used to render images and text to the screen. + + Source I am consulting: Allegro 5.0.10 Manual - http://cdn.allegro.cc/file/library/allegro/5.0.10/allegro-5.0.10-manual.pdf +*/ + +#include +#include + +#include "Colour.h" + + +//https://github.com/liballeg/allegro_wiki/wiki/Allegro-in-Visual-Studio#using-nuget-within-visual-studio +#include +#include +#include +#include +#include +#include + +class GraphicsLibrary +{ + //-------------------------Private data------------------------- + + //Screen data + float mScreenSizeX; + float mScreenSizeY; + + //Allegro display + ALLEGRO_DISPLAY* mpDisplay; + + //Other images to draw + std::vector> mBitmapPointersVector; + + friend class InputSystem; + + //-------------------------Public data------------------------- +public: + + //Constructor(s) + GraphicsLibrary(float screenSizeX, float screenSizeY); + + //Destructor + ~GraphicsLibrary(); + + //Accessor(s) + float getScreenSizeX() { return mScreenSizeX; }; + float getScreenSizeY() { return mScreenSizeY; }; + + //Mutator(s) + + //Functions + bool init(std::string backgroundFilePath); + void render(); + void loadImage(std::string imageFilePath, std::string imageIdentifier); + + //Drawing functions + void drawImage(std::string imageIdentifier, float posX, float posY); + void drawTintedImage(std::string imageIdentifier, float posX, float posY, Colour col); +}; \ No newline at end of file diff --git a/RoboCat/Inc/InputSystem.h b/RoboCat/Inc/InputSystem.h new file mode 100644 index 00000000..4ac594de --- /dev/null +++ b/RoboCat/Inc/InputSystem.h @@ -0,0 +1,72 @@ +#pragma once + +/* +Allegro Wrapper Functions +Written by Adel Talhouk in FA21 +InputSystem.h + + File information: + This file contains the keycodes for input, which can be used in any way desired by other classes + and files. +*/ + +#include "GraphicsLibrary.h" + + +//Include allegro libraries for input +#include +#include + +enum KeyCode +{ + Esc = ALLEGRO_KEY_ESCAPE, + R = ALLEGRO_KEY_R, + S = ALLEGRO_KEY_S, + D = ALLEGRO_KEY_D +}; + +enum MouseButton +{ + LeftMouse = 0, + RightMouse = 1, + MiddleMouse = 2 +}; + +enum InputMode +{ + NONE = -1, + KeyPressed = ALLEGRO_EVENT_KEY_DOWN, + KeyReleased = ALLEGRO_EVENT_KEY_UP, + MouseDown = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN, + MouseUp = ALLEGRO_EVENT_MOUSE_BUTTON_UP +}; + +class InputSystem +{ + //-------------------------Private data------------------------- + + //Event queue + ALLEGRO_EVENT_QUEUE* mpEventQueue; + + //Event + ALLEGRO_EVENT mEvent; + + //-------------------------Public data------------------------- +public: + + //Constructor(s) + InputSystem(); + + //Destructor + ~InputSystem(); + + //Accessor(s) + float getMouseX(); + float getMouseY(); + std::pair getMousePosition(); + + //Functions + bool init(GraphicsLibrary* pGraphicsLib); + MouseButton getMouseInput(); + KeyCode getKeyboardInput(); +}; \ No newline at end of file diff --git a/RoboCat/Src/Colour.cpp b/RoboCat/Src/Colour.cpp new file mode 100644 index 00000000..7a5ef3d1 --- /dev/null +++ b/RoboCat/Src/Colour.cpp @@ -0,0 +1,32 @@ +/* +Allegro Wrapper Functions +Written by Adel Talhouk in FA21 +Colour.cpp +*/ + +#include "Colour.h" +#include "RoboCatPCH.h" + +//Constructor - without alpha +Colour::Colour(unsigned __int8 r, unsigned __int8 g, unsigned __int8 b) +{ + mR = r; + mG = g; + mB = b; + mA = 255; +} + +//Constructor - with alpha +Colour::Colour(unsigned __int8 r, unsigned __int8 g, unsigned __int8 b, unsigned __int8 a) +{ + mR = r; + mG = g; + mB = b; + mA = a; +} + +//Destructor +Colour::~Colour() +{ + +} \ No newline at end of file diff --git a/RoboCat/Src/GameObject.cpp b/RoboCat/Src/GameObject.cpp new file mode 100644 index 00000000..185b4b8f --- /dev/null +++ b/RoboCat/Src/GameObject.cpp @@ -0,0 +1,119 @@ +#include "GameObject.h" + +GameObject::GameObject() +{ + mColor = Colour(0, 255, 0); +} + +GameObject::~GameObject() +{ + +} + +void GameObject::CreateObject() +{ + int val; + val = rand() % 3; + + if (val == 0) + { + al_draw_filled_circle(400, 400, 10, al_map_rgb(0, 255, 0)); + objectType = 'g'; + positionX = 400; + positionY = 400; + } + else if (val == 1) + { + al_draw_filled_circle(400, 400, 10, al_map_rgb(255, 0, 0)); + objectType = 'r'; + positionX = 400; + positionY = 400; + + //al_draw_triangle(); + } + else if (val == 2) + { + al_draw_filled_circle(400, 400, 10, al_map_rgb(0, 0, 255)); + objectType = 'b'; + positionX = 400; + positionY = 400; + + //al_draw_rectangle(); + } + + //probably no need for a final else statement +} + +void GameObject::DeleteObject() +{ + //idk if there is any deletion i need to do necessarily + //possibly just undrawing things + +} + +void GameObject::UpdatePosition() +{ + int val; + val = rand() % 4; + + if (val == 0) + { + //have it move up and to the right + positionX += 20; + positionY += 20; + } + + if (val == 1) + { + //move down and to the right + positionX += 20; + positionY -= 20; + } + + if (val == 2) + { + //down and to the left + positionX -= 20; + positionY -= 20; + } + + if (val == 3) + { + //up and to the left + positionX -= 20; + positionY += 20; + } +} + +void GameObject::DrawObjects() +{ + if (objectType == 'r') + { + al_draw_filled_circle(positionX, positionY, 10, al_map_rgb(255, 0, 0)); + } + + if (objectType == 'g') + { + al_draw_filled_circle(positionX, positionY, 10, al_map_rgb(0, 255, 0)); + } + + if (objectType == 'b') + { + al_draw_filled_circle(positionX, positionY, 10, al_map_rgb(0, 0, 255)); + } +} + +char GameObject::GetObjectType() +{ + return objectType; +} + +float GameObject::GetPositionX() +{ + return positionX; +} + +float GameObject::GetPositionY() +{ + return positionY; +} diff --git a/RoboCat/Src/GameObject.h b/RoboCat/Src/GameObject.h new file mode 100644 index 00000000..f496f20e --- /dev/null +++ b/RoboCat/Src/GameObject.h @@ -0,0 +1,30 @@ +#pragma once + +#include "Colour.h" +#include "InputSystem.h" +#include "GraphicsLibrary.h" +#include + +//I think I will have this be an individual object + //have most object management be done in main + +class GameObject +{ + private: + Colour mColor = Colour(0, 255, 0); + float positionX; + float positionY; + char objectType; + public: + GameObject(); + ~GameObject(); + + void CreateObject(); + void DeleteObject(); + void UpdatePosition(); + void DrawObjects(); + + char GetObjectType(); + float GetPositionX(); + float GetPositionY(); +}; \ No newline at end of file diff --git a/RoboCat/Src/GraphicsLibrary.cpp b/RoboCat/Src/GraphicsLibrary.cpp new file mode 100644 index 00000000..59a7db13 --- /dev/null +++ b/RoboCat/Src/GraphicsLibrary.cpp @@ -0,0 +1,126 @@ +/* +Allegro Wrapper Functions +Written by Adel Talhouk in FA21 +GraphicsLibrary.cpp +*/ + +#include "GraphicsLibrary.h" +#include "RoboCatPCH.h" + +#include +#include + +//Constructor +GraphicsLibrary::GraphicsLibrary(float screenSizeX, float screenSizeY) +{ + //Setup data - screen size + mScreenSizeX = screenSizeX; + mScreenSizeY = screenSizeY; + + //Allegro display + mpDisplay = nullptr; +} + +//Destructor +GraphicsLibrary::~GraphicsLibrary() +{ + //Delete bitmaps + std::vector>::iterator iterator; + for (iterator = mBitmapPointersVector.begin(); iterator != mBitmapPointersVector.end(); ++iterator) + { + al_destroy_bitmap(iterator->second); + } + mBitmapPointersVector.clear(); + + //Clean up display + al_destroy_display(mpDisplay); + mpDisplay = nullptr; +} + +bool GraphicsLibrary::init(std::string backgroundFilePath) +{ + //Init allegro + if (!al_init()) + { + std::cout << "error initting Allegro\n"; + system("pause"); + return false; + } + + //Init image addon + if (!al_init_image_addon()) + { + std::cout << "error initting image add-on\n"; + system("pause"); + return false; + } + + //Init font add on + if (!al_init_font_addon()) + { + std::cout << "error initting font add-on\n"; + system("pause"); + return false; + } + + //Init ttf add on + if (!al_init_ttf_addon()) + { + std::cout << "error initting ttf add-on\n"; + system("pause"); + return false; + } + + //Setup display + mpDisplay = al_create_display(mScreenSizeX, mScreenSizeY); + + if (mpDisplay == nullptr) + { + return false; + } + + return true; +} + +void GraphicsLibrary::render() +{ + //Flip display buffers + al_flip_display(); +} + +void GraphicsLibrary::loadImage(std::string imageFilePath, std::string imageIdentifier) +{ + //Add the name of the image and the loaded bitmap to the vector of pairs + mBitmapPointersVector.push_back(std::make_pair(imageIdentifier, al_load_bitmap(imageFilePath.c_str()))); +} + +void GraphicsLibrary::drawImage(std::string imageIdentifier, float posX, float posY) +{ + //Find the image and draw if it exists + std::vector>::iterator iterator; + + for (iterator = mBitmapPointersVector.begin(); iterator != mBitmapPointersVector.end(); ++iterator) + { + if (iterator->first == imageIdentifier) + { + al_draw_bitmap(iterator->second, posX, posY, 0); + } + } +} + +void GraphicsLibrary::drawTintedImage(std::string imageIdentifier, float posX, float posY, Colour col) +{ + //Find the image and draw if it exists + std::vector>::iterator iterator; + + //Set colour + ALLEGRO_COLOR colour = al_map_rgba(col.getR(), col.getG(), col.getB(), col.getA()); + + for (iterator = mBitmapPointersVector.begin(); iterator != mBitmapPointersVector.end(); ++iterator) + { + if (iterator->first == imageIdentifier) + { + al_draw_tinted_bitmap(iterator->second, colour, posX, posY, 0); + } + } +} \ No newline at end of file diff --git a/RoboCat/Src/InputSystem.cpp b/RoboCat/Src/InputSystem.cpp new file mode 100644 index 00000000..f2f99fa2 --- /dev/null +++ b/RoboCat/Src/InputSystem.cpp @@ -0,0 +1,136 @@ +/* +Allegro Wrapper Functions +Written by Adel Talhouk in FA21 +InputSystem.cpp +*/ + +#include "InputSystem.h" +#include "RoboCatPCH.h" + +#include + +//Constructor +InputSystem::InputSystem() +{ + //Create an event queue + mpEventQueue = al_create_event_queue(); +} + +//Destructor +InputSystem::~InputSystem() +{ + //Cleanup event queue + al_destroy_event_queue(mpEventQueue); + mpEventQueue = nullptr; +} + +float InputSystem::getMouseX() +{ + //Update mouse state + ALLEGRO_MOUSE_STATE mouseState; + al_get_mouse_state(&mouseState); + + return mouseState.x; +} + +float InputSystem::getMouseY() +{ + //Update mouse state + ALLEGRO_MOUSE_STATE mouseState; + al_get_mouse_state(&mouseState); + + return mouseState.y; +} + +std::pair InputSystem::getMousePosition() +{ + //Update mouse state + ALLEGRO_MOUSE_STATE mouseState; + al_get_mouse_state(&mouseState); + + return std::make_pair(mouseState.x, mouseState.y); +} + +//Init +bool InputSystem::init(GraphicsLibrary* pGraphicsLib) +{ + //Init keyboard + if (!al_install_keyboard()) + { + std::cout << "error installing Allegro keyboard plugin\n"; + system("pause"); + return false; + } + + //Init mouse + if (!al_install_mouse()) + { + std::cout << "error installing Allegro mouse plugin\n"; + system("pause"); + return false; + } + + //Register screen event source + al_register_event_source(mpEventQueue, al_get_display_event_source(pGraphicsLib->mpDisplay)); + + //Register keyboard event source + al_register_event_source(mpEventQueue, al_get_keyboard_event_source()); + + //Register mouse event source + al_register_event_source(mpEventQueue, al_get_mouse_event_source()); + + return true; +} + +MouseButton InputSystem::getMouseInput() +{ + //If there is an event + al_wait_for_event(mpEventQueue, &mEvent); + + if (mEvent.type == InputMode::MouseDown) + { + //Update mouse state + ALLEGRO_MOUSE_STATE mouseState; + al_get_mouse_state(&mouseState); + + //Check the button pressed + if (mouseState.buttons & 1) //Left mouse held + { + return MouseButton::LeftMouse; + } + else if (mouseState.buttons & 2) //Right mouse held + { + return MouseButton::RightMouse; + } + else if (mouseState.buttons & 4) //Middle mouse held + { + return MouseButton::MiddleMouse; + } + } +} + +KeyCode InputSystem::getKeyboardInput() +{ + //If there is an event + al_wait_for_event(mpEventQueue, &mEvent); + + if (mEvent.type == InputMode::KeyPressed) + { + //Check the type + switch (mEvent.keyboard.keycode) + { + case KeyCode::Esc: + return KeyCode::Esc; + break; + + case KeyCode::R: + return KeyCode::R; + break; + + default: + /*return KeyCode::NONE*/; + } + } + + //return KeyCode::NONE; +} \ No newline at end of file diff --git a/RoboCat/Src/Main.cpp b/RoboCat/Src/Main.cpp index 3b0ac9de..11aea85d 100644 --- a/RoboCat/Src/Main.cpp +++ b/RoboCat/Src/Main.cpp @@ -1,9 +1,16 @@ +//#include "RoboCatPCH.h" +#include "InputSystem.h" +#include "GraphicsLibrary.h" +#include "Colour.h" +#include "GameObject.h" +#include "NetworkManager.h" +#include "NetworkManagerServer.h" -#include "RoboCatPCH.h" +const int MAX_OBJECT_COUNT = 100; //i think this might be needed i am unsure +std::string BACKGROUND_PATH = "..\\..\\Assets\\Woods.png"; //I think #if _WIN32 - int main(int argc, const char** argv) { UNREFERENCED_PARAMETER(argc); @@ -19,7 +26,115 @@ int main(int argc, const char** argv) SocketUtil::StaticInit(); + al_init(); + + GameObject mGameObjects[MAX_OBJECT_COUNT]; //possibly make this an array with MAX_OBJECT_COUNT + GraphicsLibrary* mpGraphicsLibrary = new GraphicsLibrary(1600, 1200); + InputSystem* mpInputSystem = new InputSystem(); + NetworkManager mNetworkManager; + NetworkManagerServer mNetworkManagerServer; + + bool activeConnection = true; + bool isServer; + int unitCount = 0; //use this to track changes for deleting/making units + int pastUnitCount = 0; + + mpGraphicsLibrary->init(BACKGROUND_PATH); + mpInputSystem->init(mpGraphicsLibrary); + + mpGraphicsLibrary->loadImage(BACKGROUND_PATH, "Woods.png"); + + isServer = StringUtils::GetCommandLineArg(1) == "server"; + + if (isServer) + { + SocketAddress servAddress(INADDR_LOOPBACK, 8081); //8081? + + mNetworkManagerServer.init(servAddress, "server"); + } + else + { + SocketAddress servAddress(INADDR_LOOPBACK, 8080); + + mNetworkManager.init(servAddress, "client"); + } + + mpGraphicsLibrary->init(BACKGROUND_PATH); + + while (activeConnection == true) + { + pastUnitCount = unitCount; + + if (mpInputSystem->getKeyboardInput() == KeyCode::S) + { + //spawn an object + mGameObjects[unitCount].CreateObject(); + + unitCount++; + } + + if (mpInputSystem->getKeyboardInput() == KeyCode::D) + { + //despawn an object + int tmpRand; + GameObject tmpObjects[MAX_OBJECT_COUNT]; + + tmpRand = rand() % MAX_OBJECT_COUNT; + + mGameObjects[tmpRand].DeleteObject(); //could be unneeded + + for (int i = 0; i < unitCount; i++) + { + if (i != tmpRand) + { + tmpObjects[i] = mGameObjects[i]; + } + } + + unitCount--; + + for (int i = 0; i < unitCount; i++) + { + mGameObjects[i] = tmpObjects[i]; + } + } + + if (mpInputSystem->getKeyboardInput() == KeyCode::Esc) + { + //basically exit the game and shut everything down + activeConnection = false; + } + + for (int i = 0; i < unitCount; i++) + { + mGameObjects[i].UpdatePosition(); + mGameObjects[i].DrawObjects(); + } + + //now i need to move and send this info + if (isServer) + { + mNetworkManagerServer.update(mGameObjects); + } + else + { + mNetworkManager.update(mGameObjects); + } + + //al_create_bitmap(1600, 1200); + //mpGraphicsLibrary->drawImage("Woods.png", 0, 0); //i need to make this work + mpGraphicsLibrary->render(); //render at the very end + } + + delete mpGraphicsLibrary; + mpGraphicsLibrary = NULL; //is this needed? + delete mpInputSystem; + mpInputSystem = NULL; + + //delete mNetworkManagerServer + //delete mNetworkManager + SocketUtil::CleanUp(); return 0; -} +} \ No newline at end of file diff --git a/RoboCat/Src/NetworkManager.cpp b/RoboCat/Src/NetworkManager.cpp new file mode 100644 index 00000000..40d769d4 --- /dev/null +++ b/RoboCat/Src/NetworkManager.cpp @@ -0,0 +1,105 @@ +#include "NetworkManager.h" + +NetworkManager::NetworkManager() +{ + mNetworkID = 0; + //mAddress = + mSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + mLastHello = 0.0f; + mLastInputPacket = 0.0f; + mCurrentState = NetworkStates::New; + mName = "client"; +} + +NetworkManager::~NetworkManager() +{ + +} + +void NetworkManager::init(SocketAddress address, std::string name) +{ + mAddress = address; + mSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + mLastHello = 0.0f; + mLastInputPacket = 0.0f; + mCurrentState = NetworkStates::New; + mName = name; + + mSocket->Bind(address); +} + +void NetworkManager::update(GameObject objects[]) +{ + mLastHello += 0.00167; //trying to do something with sdl timer for this + mLastInputPacket += 0.00167; + + if (mLastInputPacket > TIME_BETWEEN_PACKETS) + { + SendPackets(objects); + } +} + +void NetworkManager::SendHello() +{ + if (mCurrentState == NetworkStates::New && mLastHello >= 0.5f) + { + //send out the hello packet + OutputMemoryBitStream sendHello; + + //sendHello.Write(); + sendHello.Write(mName); + + //SendPacket(sendHello); + + mSocket->Send(&sendHello, sizeof(sendHello)); //this might be totally wrong? + } +} + +void NetworkManager::SendPackets(GameObject objects[]) +{ + if (mCurrentState == Hello && mLastHello > TIME_BETWEEN_PACKETS) + { + SendHello(); + } + + if (mCurrentState == Packet) + { + OutputMemoryBitStream output; + + //i assume this is basically gathering all info for a state packet + output.Write(mGameObjectCount); + + for (int i = 0; i < mGameObjectCount; i++) + { + output.Write(mObjectTypes[i]); + output.Write(mXPositions[i]); + output.Write(mYPositions[i]); + } + + mSocket->Send(&output, sizeof(output)); + } +} + +void NetworkManager::ReceivePackets(InputMemoryBitStream& inputStream) +{ + mSocket->Receive(&inputStream, sizeof(inputStream)); + + if (mCurrentState == NetworkStates::Hello) + { + int tmpID; + inputStream.Read(tmpID); + + mNetworkID = tmpID; + mCurrentState = NetworkStates::Packet; + } +} + +int NetworkManager::getID() +{ + return mNetworkID; +} + +string NetworkManager::getName() +{ + return mName; +} \ No newline at end of file diff --git a/RoboCat/Src/NetworkManager.h b/RoboCat/Src/NetworkManager.h new file mode 100644 index 00000000..ce35b61b --- /dev/null +++ b/RoboCat/Src/NetworkManager.h @@ -0,0 +1,47 @@ +#pragma once + +#include "GameObject.h" +#include "RoboCatPCH.h" +#include + +const float TIME_BETWEEN_PACKETS = 0.25f; + +enum NetworkStates +{ + New, + Waiting, + Hello, + Packet +}; + +class NetworkManager +{ + private: + int mNetworkID; + float mLastHello; + float mLastInputPacket; + std::string mName; + + int mGameObjectCount; + char mObjectTypes[100]; + int mXPositions[100]; //maybe just have array parameter instead of this + int mYPositions[100]; + + SocketAddress mAddress; + TCPSocketPtr mSocket; + NetworkStates mCurrentState; + public: + NetworkManager(); + ~NetworkManager(); + + void init(SocketAddress address, std::string name); + void update(GameObject objects[]); + + //maybe pass in a socket to use here, or have one in network manager + void SendHello(); + void SendPackets(GameObject objects[]); //GameObject objects[] + void ReceivePackets(InputMemoryBitStream& inputStream); + + int getID(); + string getName(); +}; \ No newline at end of file diff --git a/RoboCat/Src/NetworkManagerServer.cpp b/RoboCat/Src/NetworkManagerServer.cpp new file mode 100644 index 00000000..dacd25ca --- /dev/null +++ b/RoboCat/Src/NetworkManagerServer.cpp @@ -0,0 +1,100 @@ +#include "NetworkManagerServer.h" + +NetworkManagerServer::NetworkManagerServer() +{ + mName = "server"; + mLastPacket = 0.0f; + clientWelcomeNumber = 0; + mSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + //mAddress = +} + +NetworkManagerServer::~NetworkManagerServer() +{ + +} + +void NetworkManagerServer::init(SocketAddress address, std::string name) +{ + mServerAddress = address; + mName = name; + mLastPacket = 0.0f; + mSocket = SocketUtil::CreateTCPSocket(SocketAddressFamily::INET); + clientWelcomeNumber = 0; + + //bind socket? + mSocket->Bind(address); +} + +void NetworkManagerServer::update(GameObject objects[]) +{ + mLastPacket += 0.00167; + + if (mLastPacket > TIME_BETWEEN_PACKETS) + { + mLastPacket = 0.0f; + sendUpdatePacket(objects); + } +} + +void NetworkManagerServer::sendWelcomePacket() +{ + OutputMemoryBitStream output; + + std::string welcome = "welcome"; + + output.Write(welcome); + output.Write(clientWelcomeNumber); + + mSocket->Send(&output, sizeof(output)); + + clientWelcomeNumber++; +} + +void NetworkManagerServer::sendUpdatePacket(GameObject objects[]) +{ + OutputMemoryBitStream output; + + output.Write(mObjectCount); + + for (int i = 0; i < mObjectCount; i++) + { + output.Write(mObjectTypes[i]); + output.Write(mXPositions[i]); + output.Write(mYPositions[i]); + } + + mSocket->Send(&output, sizeof(output)); +} + +void NetworkManagerServer::receiveHelloPackets(InputMemoryBitStream& inputStream) +{ + mSocket->Receive(&inputStream, sizeof(inputStream)); + + sendWelcomePacket(); +} + +void NetworkManagerServer::ReceiveUpdatePackets(InputMemoryBitStream& inputStream) +{ + mSocket->Receive(&inputStream, sizeof(inputStream)); + + int gameObjectCount; + int posX; + int posY; + char objectType; + + inputStream.Read(gameObjectCount); + + mObjectCount = gameObjectCount; + + for (int i = 0; i < gameObjectCount; i++) + { + inputStream.Read(objectType); + inputStream.Read(posX); + inputStream.Read(posY); + + mObjectTypes[i] = objectType; + mXPositions[i] = posX; + mYPositions[i] = posY; + } +} \ No newline at end of file diff --git a/RoboCat/Src/NetworkManagerServer.h b/RoboCat/Src/NetworkManagerServer.h new file mode 100644 index 00000000..200448e0 --- /dev/null +++ b/RoboCat/Src/NetworkManagerServer.h @@ -0,0 +1,36 @@ +#pragma once + +#include "GameObject.h" +//#include "RoboCatPCH.h" +#include "NetworkManager.h" + +//const float TIME_BETWEEN_PACKETS = 0.25f; + +class NetworkManagerServer +{ + private: + float mLastPacket; + std::string mName; + int clientWelcomeNumber; //client network id + + int mObjectCount; + char mObjectTypes[100]; + int mXPositions[100]; + int mYPositions[100]; + //std::string mClientNames[100]; //in case i need to track client names i am not sure + + SocketAddress mServerAddress; + TCPSocketPtr mSocket; + public: + NetworkManagerServer(); + ~NetworkManagerServer(); + + void init(SocketAddress address, std::string name); + void update(GameObject objects[]); + + void sendWelcomePacket(); + void sendUpdatePacket(GameObject objects[]); + + void receiveHelloPackets(InputMemoryBitStream& inputStream); //InputMemoryBitStream& inputStream + void ReceiveUpdatePackets(InputMemoryBitStream& inputStream); //InputMemoryBitStream& inputStream +}; \ No newline at end of file