diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e6271d7 --- /dev/null +++ b/Readme.md @@ -0,0 +1,17 @@ +# Shape Game +## Setup +* use 3 linux computers connecting in a lan +* clone the repo `git clone ` +* install `sh install.sh` +* compile `make` +* Start server `./bin/server.out 4444` +* get server ip `ifconfig` + * Take note of `eth0` or `enp2s0`; **note::** the numbers might be different + +* Start a client + * Alice `./bin/server.out 1 4444 ` + * Bob `./bin/server.out 2 4444 ` + * Eve `./bin/server.out 3 4444 ` + +## How it works + diff --git a/includes/client.hpp b/includes/client.hpp index 4fc5146..67b3782 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -56,14 +56,29 @@ private: void stop(); public: + + /* + * Create an instance of client + */ Client( MODE mode, int port, std::string url, std::function &update); ~Client(); + + /* + * Kill the server + */ void kill(); + /* + * Running + */ bool running(); + + /* + * Send the shape encrypted or raw + */ void send(bool encrypt, SHAPE shape); }; } // namespace betacore diff --git a/includes/game.hpp b/includes/game.hpp index ced64fb..3a64ccd 100644 --- a/includes/game.hpp +++ b/includes/game.hpp @@ -31,13 +31,13 @@ private: const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; - int MONITOR_WIDTH=1920; - int MONITOR_HEIGHT=1080; - const char *TITLE ; + int MONITOR_WIDTH = 1920; + int MONITOR_HEIGHT = 1080; + const char *TITLE; bool KEEP_ALIVE = true; bool SHOW_GRID = false; SHAPE USER_CURRENT = NONE; - SHAPE GUST_CURRENT = NONE; + SHAPE GUST_CURRENT = NONE; MODE ME; bool KEY_UP_ARROW_ACTIVE = false; bool KEY_DOWN_ARROW_ACTIVE = false; @@ -46,46 +46,138 @@ private: bool KEY_SPACE_ACTIVE = false; bool KEY_RETURN_ACTIVE = false; bool FULL_SCREEN = false; - Client* client = nullptr; + Client *client = nullptr; + public: Game(MODE mode, - int port, - std::string url); + int port, + std::string url); ~Game(); int init(); int initGL(); + + /** + * Draws box for use with guest \ current screens + */ void box(); + /** + * Close the application + */ void close(); + + /* + * Draw a grid on the screen (G key) + */ void grid(); + + /* + * Paint the gl context + */ void paint(); - void events( SDL_Event &event ); + + /* + * SDL Context + */ + void events(SDL_Event &event); + + /* + * On key down events + */ void key_down(SDL_Keycode key_code); + /* + * On key up events + */ void key_up(SDL_Keycode key_code); - void resize ( int width, int height ); + + /* + * Resize the window to width : height + */ + void resize(int width, int height); + + /** + * Draw a filled triange + */ void triangle(); + + /* + * Draw a filled or wireframe triangle + */ void triangle(bool filled); + /* + * Draw a filled or wireframe square + */ void square(); + /* + * Draw a filled or wireframe square + */ void square(bool filled); + /* + * Draw a filled or wireframe circle + */ void circle(); + /* + * Draw a filled or wireframe circle + */ void circle(bool filled); + /* + * Draw a filled or wireframe pentagon + */ void pentagon(); + /* + * Draw a filled or wireframe pentagon + */ void pentagon(bool filled); + /* + * Draw a wireframe rectangle + */ void rectangle(); + /* + * Draw a filled or wireframe cross + */ void cross(bool filled); + /* + * Draw the key pad compass + */ void compass(); - + + /* + * Event call for up + */ void on_key_up_arrow(); + /* + * Event call for down + */ void on_key_down_arrow(); + /* + * Event call for left + */ void on_key_left_arrow(); + /* + * Event call for right + */ void on_key_right_arrow(); + /* + * Event call for space + */ void on_key_space(); + /* + * Event call for enter + */ void on_key_enter(); void top_screen(); + /* + * Draw user screen + */ void user_screen(); + /* + * Draw guest screen + */ void guest_screen(); - - void update(SHAPE &s,MODE &m); + /* + * On update from network + */ + void update(SHAPE &s, MODE &m); }; } // namespace betacore #endif \ No newline at end of file diff --git a/includes/server.hpp b/includes/server.hpp index 1ba6c7d..21791ec 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -5,46 +5,77 @@ #include #include -#include -#include -#include - #include - #include #include #include -#include + #include #include #include #include "common.hpp" +// LINUX SYSTEM CALLS :) +#include +#include +#include +#include + + namespace betacore { - class Server { private: - fd_set rset; + fd_set rset; bool online = false; - bool server_running= false; + bool server_running = false; int port; int server_socket; std::vector clients; struct sockaddr_in server_address; + + /* + * Starts the server + */ void start(); + + /* + * Shutdown Routine + */ void shutdown(); + + /* + * Listen for connection and spawn a new thread + */ void listener(); + + /* + * Read the socket, is blocking, but will block its own thread + */ void read_socket(int client); - void forward_message(int client,char buffer[BUFFER_LENGTH] ); + + /* + * This will forward to all connected clients except the client fd given + */ + void forward_message(int client, char buffer[BUFFER_LENGTH]); + public: + /* + * Create a server on port + */ Server(int port); + /* + * Turn it off + */ void off(); - bool running(); + /* + * Return true if the server is running + */ + bool running(); }; } // namespace betacore diff --git a/source/game.cpp b/source/game.cpp index f2cd3cd..adf3116 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,4 +1,6 @@ #include "game.hpp" + +// Adding this a fix if gluPerspective is not defined, opengl 3+ I think? void gluPerspective(double fovy,double aspect, double zNear, double zFar) { // Start in projection mode. @@ -31,8 +33,7 @@ Game::Game(MODE mode, std::function fn = std::bind(&Game::update, this, _1, _2); this->client = new Client(mode, port, url, fn); - // SDL_DisplayMode DM; - // SDL_GetCurrentDisplayMode(0, &DM); + this->MONITOR_HEIGHT = 1080; this->MONITOR_WIDTH = 1920; SDL_Event event; @@ -70,11 +71,9 @@ int Game::init() //Create window int videoFlags = SDL_WINDOW_OPENGL; - //videoFlags |= SDL_WINDOW_FULLSCREEN; videoFlags |= SDL_WINDOW_SHOWN; videoFlags |= SDL_WINDOW_RESIZABLE; - //videoFlags |= SDL_WINDOW_BORDERLESS; - // videoFlags |= SDL_WINDOW_INPUT_GRABBED; + this->WINDOW = SDL_CreateWindow( this->TITLE, @@ -165,9 +164,7 @@ void Game::close() void Game::grid() { glPushMatrix(); - //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,gray); - //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); - //glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64); + glTranslatef(0.0f, 0.0f, 0.0f); glLineWidth(2.5); glColor3f(1.0, 1.0, 1.0); @@ -348,15 +345,6 @@ void Game::circle() } void Game::circle(bool filled) { - //glBegin(GL_LINE_LOOP); - // for (int i = 0; i <= 300; i++) - // { - // double angle = 2 * PI * i / 300; - // double x = cos(angle); - // double y = sin(angle); - // glVertex2d(x, y); - // } - // glEnd(); double radius = 1; @@ -387,8 +375,8 @@ void Game::pentagon(bool filled) { glRotatef(-54.0f, 0.0f, 0.0f, 1.0f); glColor3f(1.0f, 0.0f, 1.0f); - float angleIncrement = 360.0f / 5.0f; - angleIncrement *= PI / 180.0f; + float angle_increment = 360.0f / 5.0f; + angle_increment *= PI / 180.0f; if (filled) glBegin(GL_TRIANGLE_FAN); else @@ -398,7 +386,7 @@ void Game::pentagon(bool filled) for (int k = 0; k < 100; ++k) { glVertex3f(radius * cos(angle), radius * sin(angle), 0.0f); - angle += angleIncrement; + angle += angle_increment; } glEnd(); } @@ -408,10 +396,8 @@ void Game::user_screen() glTranslatef(-1.5f, -1.0f, -6.0f); this->box(); - //this->box(); - //glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0 glTranslatef(-1.5f, -1.0f, -6.0f); - // draw a triangle (in smooth coloring mode) + switch (USER_CURRENT) { case TRIANGLE: @@ -441,10 +427,7 @@ void Game::user_screen() break; } - // glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 Units - // // draw a square (quadrilateral) - // glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade. glPopMatrix(); } @@ -454,7 +437,7 @@ void Game::guest_screen() glTranslatef(1.5f, -1.0f, -6.0f); this->box(); glTranslatef(1.5f, -1.0f, -6.0f); - // draw a triangle (in smooth coloring mode) + switch (GUST_CURRENT) { case TRIANGLE: @@ -574,18 +557,13 @@ void Game::key_down(SDL_Keycode key_code) { SDL_SetWindowPosition(this->WINDOW, 100, 100); SDL_RestoreWindow(this->WINDOW); - //SDL_SetWindowResizable(this->WINDOW, SDL_TRUE); - //SDL_SetWindowBordered(this->WINDOW, 1); SDL_SetWindowSize(this->WINDOW, SCREEN_WIDTH, SCREEN_HEIGHT); - //SDL_SetWindowFullscreen(this->WINDOW,SDL_WINDOW_FULLSCREEN ); + } else { SDL_SetWindowPosition(this->WINDOW, 0, 0); - //SDL_SetWindowResizable(this->WINDOW, SDL_FALSE); - //SDL_SetWindowBordered(this->WINDOW, 0); SDL_SetWindowSize(this->WINDOW, MONITOR_WIDTH, MONITOR_HEIGHT); - //SDL_SetWindowFullscreen(this->WINDOW,0 ); } this->FULL_SCREEN = !this->FULL_SCREEN; @@ -629,11 +607,6 @@ void Game::key_up(SDL_Keycode key_code) case SDLK_RETURN: this->KEY_RETURN_ACTIVE = false; break; - case SDLK_F11: - // SDL_DisplayMode dm; - // SDL_RestoreWindow(this->WINDOW); //Incase it's maximized... - // SDL_SetWindowSize(this->WINDOW, dm.w, dm.h + 10); - // SDL_SetWindowPosition(this->WINDOW, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); default: //Do nothing break; diff --git a/thanks.md b/thanks.md index 2efb3c6..7e8e1c8 100644 --- a/thanks.md +++ b/thanks.md @@ -1 +1,4 @@ -https://stackoverflow.com/questions/10673585/start-thread-with-member-function +Links used to help make application: +* https://stackoverflow.com/questions/10673585/start-thread-with-member-function +* https://www.kernel.org/doc/man-pages/ +* http://www.cplusplus.com/ \ No newline at end of file