Browse Source

readme

master
beta 5 years ago
parent
commit
aa6df20681
6 changed files with 193 additions and 62 deletions
  1. +17
    -0
      Readme.md
  2. +15
    -0
      includes/client.hpp
  3. +104
    -12
      includes/game.hpp
  4. +42
    -11
      includes/server.hpp
  5. +11
    -38
      source/game.cpp
  6. +4
    -1
      thanks.md

+ 17
- 0
Readme.md View File

@ -0,0 +1,17 @@
# Shape Game
## Setup
* use 3 linux computers connecting in a lan
* clone the repo `git clone <repo url>`
* 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 <server ip>`
* Bob `./bin/server.out 2 4444 <server ip>`
* Eve `./bin/server.out 3 4444 <server ip>`
## How it works

+ 15
- 0
includes/client.hpp View File

@ -56,14 +56,29 @@ private:
void stop(); void stop();
public: public:
/*
* Create an instance of client
*/
Client( Client(
MODE mode, MODE mode,
int port, int port,
std::string url, std::string url,
std::function<void(SHAPE &S, MODE &M)> &update); std::function<void(SHAPE &S, MODE &M)> &update);
~Client(); ~Client();
/*
* Kill the server
*/
void kill(); void kill();
/*
* Running
*/
bool running(); bool running();
/*
* Send the shape encrypted or raw
*/
void send(bool encrypt, SHAPE shape); void send(bool encrypt, SHAPE shape);
}; };
} // namespace betacore } // namespace betacore

+ 104
- 12
includes/game.hpp View File

@ -31,13 +31,13 @@ private:
const int SCREEN_WIDTH = 800; const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600; 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 KEEP_ALIVE = true;
bool SHOW_GRID = false; bool SHOW_GRID = false;
SHAPE USER_CURRENT = NONE; SHAPE USER_CURRENT = NONE;
SHAPE GUST_CURRENT = NONE;
SHAPE GUST_CURRENT = NONE;
MODE ME; MODE ME;
bool KEY_UP_ARROW_ACTIVE = false; bool KEY_UP_ARROW_ACTIVE = false;
bool KEY_DOWN_ARROW_ACTIVE = false; bool KEY_DOWN_ARROW_ACTIVE = false;
@ -46,46 +46,138 @@ private:
bool KEY_SPACE_ACTIVE = false; bool KEY_SPACE_ACTIVE = false;
bool KEY_RETURN_ACTIVE = false; bool KEY_RETURN_ACTIVE = false;
bool FULL_SCREEN = false; bool FULL_SCREEN = false;
Client* client = nullptr;
Client *client = nullptr;
public: public:
Game(MODE mode, Game(MODE mode,
int port,
std::string url);
int port,
std::string url);
~Game(); ~Game();
int init(); int init();
int initGL(); int initGL();
/**
* Draws box for use with guest \ current screens
*/
void box(); void box();
/**
* Close the application
*/
void close(); void close();
/*
* Draw a grid on the screen (G key)
*/
void grid(); void grid();
/*
* Paint the gl context
*/
void paint(); 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); void key_down(SDL_Keycode key_code);
/*
* On key up events
*/
void key_up(SDL_Keycode key_code); 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(); void triangle();
/*
* Draw a filled or wireframe triangle
*/
void triangle(bool filled); void triangle(bool filled);
/*
* Draw a filled or wireframe square
*/
void square(); void square();
/*
* Draw a filled or wireframe square
*/
void square(bool filled); void square(bool filled);
/*
* Draw a filled or wireframe circle
*/
void circle(); void circle();
/*
* Draw a filled or wireframe circle
*/
void circle(bool filled); void circle(bool filled);
/*
* Draw a filled or wireframe pentagon
*/
void pentagon(); void pentagon();
/*
* Draw a filled or wireframe pentagon
*/
void pentagon(bool filled); void pentagon(bool filled);
/*
* Draw a wireframe rectangle
*/
void rectangle(); void rectangle();
/*
* Draw a filled or wireframe cross
*/
void cross(bool filled); void cross(bool filled);
/*
* Draw the key pad compass
*/
void compass(); void compass();
/*
* Event call for up
*/
void on_key_up_arrow(); void on_key_up_arrow();
/*
* Event call for down
*/
void on_key_down_arrow(); void on_key_down_arrow();
/*
* Event call for left
*/
void on_key_left_arrow(); void on_key_left_arrow();
/*
* Event call for right
*/
void on_key_right_arrow(); void on_key_right_arrow();
/*
* Event call for space
*/
void on_key_space(); void on_key_space();
/*
* Event call for enter
*/
void on_key_enter(); void on_key_enter();
void top_screen(); void top_screen();
/*
* Draw user screen
*/
void user_screen(); void user_screen();
/*
* Draw guest screen
*/
void guest_screen(); void guest_screen();
void update(SHAPE &s,MODE &m);
/*
* On update from network
*/
void update(SHAPE &s, MODE &m);
}; };
} // namespace betacore } // namespace betacore
#endif #endif

+ 42
- 11
includes/server.hpp View File

@ -5,46 +5,77 @@
#include <iostream> #include <iostream>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <thread> #include <thread>
#include <utility> #include <utility>
#include <functional> #include <functional>
#include "common.hpp" #include "common.hpp"
// LINUX SYSTEM CALLS :)
#include <sys/socket.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <unistd.h>
namespace betacore namespace betacore
{ {
class Server class Server
{ {
private: private:
fd_set rset;
fd_set rset;
bool online = false; bool online = false;
bool server_running= false;
bool server_running = false;
int port; int port;
int server_socket; int server_socket;
std::vector<int> clients; std::vector<int> clients;
struct sockaddr_in server_address; struct sockaddr_in server_address;
/*
* Starts the server
*/
void start(); void start();
/*
* Shutdown Routine
*/
void shutdown(); void shutdown();
/*
* Listen for connection and spawn a new thread
*/
void listener(); void listener();
/*
* Read the socket, is blocking, but will block its own thread
*/
void read_socket(int client); 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: public:
/*
* Create a server on port
*/
Server(int port); Server(int port);
/*
* Turn it off
*/
void off(); void off();
bool running();
/*
* Return true if the server is running
*/
bool running();
}; };
} // namespace betacore } // namespace betacore

+ 11
- 38
source/game.cpp View File

@ -1,4 +1,6 @@
#include "game.hpp" #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) void gluPerspective(double fovy,double aspect, double zNear, double zFar)
{ {
// Start in projection mode. // Start in projection mode.
@ -31,8 +33,7 @@ Game::Game(MODE mode,
std::function<void(SHAPE & s, MODE & m)> fn = std::function<void(SHAPE & s, MODE & m)> fn =
std::bind(&Game::update, this, _1, _2); std::bind(&Game::update, this, _1, _2);
this->client = new Client(mode, port, url, fn); this->client = new Client(mode, port, url, fn);
// SDL_DisplayMode DM;
// SDL_GetCurrentDisplayMode(0, &DM);
this->MONITOR_HEIGHT = 1080; this->MONITOR_HEIGHT = 1080;
this->MONITOR_WIDTH = 1920; this->MONITOR_WIDTH = 1920;
SDL_Event event; SDL_Event event;
@ -70,11 +71,9 @@ int Game::init()
//Create window //Create window
int videoFlags = SDL_WINDOW_OPENGL; int videoFlags = SDL_WINDOW_OPENGL;
//videoFlags |= SDL_WINDOW_FULLSCREEN;
videoFlags |= SDL_WINDOW_SHOWN; videoFlags |= SDL_WINDOW_SHOWN;
videoFlags |= SDL_WINDOW_RESIZABLE; videoFlags |= SDL_WINDOW_RESIZABLE;
//videoFlags |= SDL_WINDOW_BORDERLESS;
// videoFlags |= SDL_WINDOW_INPUT_GRABBED;
this->WINDOW = SDL_CreateWindow( this->WINDOW = SDL_CreateWindow(
this->TITLE, this->TITLE,
@ -165,9 +164,7 @@ void Game::close()
void Game::grid() void Game::grid()
{ {
glPushMatrix(); 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); glTranslatef(0.0f, 0.0f, 0.0f);
glLineWidth(2.5); glLineWidth(2.5);
glColor3f(1.0, 1.0, 1.0); glColor3f(1.0, 1.0, 1.0);
@ -348,15 +345,6 @@ void Game::circle()
} }
void Game::circle(bool filled) 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; double radius = 1;
@ -387,8 +375,8 @@ void Game::pentagon(bool filled)
{ {
glRotatef(-54.0f, 0.0f, 0.0f, 1.0f); glRotatef(-54.0f, 0.0f, 0.0f, 1.0f);
glColor3f(1.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) if (filled)
glBegin(GL_TRIANGLE_FAN); glBegin(GL_TRIANGLE_FAN);
else else
@ -398,7 +386,7 @@ void Game::pentagon(bool filled)
for (int k = 0; k < 100; ++k) for (int k = 0; k < 100; ++k)
{ {
glVertex3f(radius * cos(angle), radius * sin(angle), 0.0f); glVertex3f(radius * cos(angle), radius * sin(angle), 0.0f);
angle += angleIncrement;
angle += angle_increment;
} }
glEnd(); glEnd();
} }
@ -408,10 +396,8 @@ void Game::user_screen()
glTranslatef(-1.5f, -1.0f, -6.0f); glTranslatef(-1.5f, -1.0f, -6.0f);
this->box(); 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); glTranslatef(-1.5f, -1.0f, -6.0f);
// draw a triangle (in smooth coloring mode)
switch (USER_CURRENT) switch (USER_CURRENT)
{ {
case TRIANGLE: case TRIANGLE:
@ -441,10 +427,7 @@ void Game::user_screen()
break; 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(); glPopMatrix();
} }
@ -454,7 +437,7 @@ void Game::guest_screen()
glTranslatef(1.5f, -1.0f, -6.0f); glTranslatef(1.5f, -1.0f, -6.0f);
this->box(); this->box();
glTranslatef(1.5f, -1.0f, -6.0f); glTranslatef(1.5f, -1.0f, -6.0f);
// draw a triangle (in smooth coloring mode)
switch (GUST_CURRENT) switch (GUST_CURRENT)
{ {
case TRIANGLE: case TRIANGLE:
@ -574,18 +557,13 @@ void Game::key_down(SDL_Keycode key_code)
{ {
SDL_SetWindowPosition(this->WINDOW, 100, 100); SDL_SetWindowPosition(this->WINDOW, 100, 100);
SDL_RestoreWindow(this->WINDOW); SDL_RestoreWindow(this->WINDOW);
//SDL_SetWindowResizable(this->WINDOW, SDL_TRUE);
//SDL_SetWindowBordered(this->WINDOW, 1);
SDL_SetWindowSize(this->WINDOW, SCREEN_WIDTH, SCREEN_HEIGHT); SDL_SetWindowSize(this->WINDOW, SCREEN_WIDTH, SCREEN_HEIGHT);
//SDL_SetWindowFullscreen(this->WINDOW,SDL_WINDOW_FULLSCREEN );
} }
else else
{ {
SDL_SetWindowPosition(this->WINDOW, 0, 0); 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_SetWindowSize(this->WINDOW, MONITOR_WIDTH, MONITOR_HEIGHT);
//SDL_SetWindowFullscreen(this->WINDOW,0 );
} }
this->FULL_SCREEN = !this->FULL_SCREEN; this->FULL_SCREEN = !this->FULL_SCREEN;
@ -629,11 +607,6 @@ void Game::key_up(SDL_Keycode key_code)
case SDLK_RETURN: case SDLK_RETURN:
this->KEY_RETURN_ACTIVE = false; this->KEY_RETURN_ACTIVE = false;
break; 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: default:
//Do nothing //Do nothing
break; break;

+ 4
- 1
thanks.md View File

@ -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/

Loading…
Cancel
Save