@ -1,15 +1,14 @@
# include <iostream>
# include <iostream>
# include <cmath>
# include <cmath>
# include <ctime>
# include <ctime>
# include "raylib-cpp/raylib-cpp.hpp"
# include "includes/raylib-cpp/raylib-cpp.hpp"
# include "paddle.hpp"
# include "includes/paddle.hpp"
# include "ball.hpp"
# include "includes/ball.hpp"
# include "easysock.hpp"
# include "includes/easysock.hpp"
# include "math-helpers.hpp"
# include "includes/math-helpers.hpp"
# include "connect-helpers.hpp"
# include "includes/connect-helpers.hpp"
# include "client.hpp"
# include "includes/client.hpp"
# include "server.hpp"
# include "includes/server.hpp"
/* Global variables used to instantiate structs */
/* Global variables used to instantiate structs */
const int WIDTH = 1500 ;
const int WIDTH = 1500 ;
const int HEIGHT = 600 ;
const int HEIGHT = 600 ;
@ -171,8 +170,12 @@ int main(int argc, char** argv) {
return - 6 ;
return - 6 ;
}
}
# ifdef DEBUG
type . mode = M_CLIENT ;
# endif
/* Initialize window and other variables */
/* Initialize window and other variables */
SetTraceLogLevel ( LOG_NONE ) ;
SetTraceLogLevel ( LOG_ I NF O) ;
raylib : : Window window = raylib : : Window ( WIDTH , HEIGHT , " Pong " ) ;
raylib : : Window window = raylib : : Window ( WIDTH , HEIGHT , " Pong " ) ;
window . ClearBackground ( BLACK ) ;
window . ClearBackground ( BLACK ) ;
SetTargetFPS ( 60 ) ;
SetTargetFPS ( 60 ) ;
@ -202,45 +205,45 @@ int main(int argc, char** argv) {
}
}
if ( game_started ) {
if ( game_started ) {
/* Receive response from the other machine, if the game is in multiplayer mode */
std : : string response = " " ;
if ( type . mode ! = M_SINGLE ) {
# ifdef DEBUG
response = " " ;
# else
std : : string response = type . netsock - > recvAll ( ) ;
# endif
}
std : : string to_send = " " ;
/* Update paddle velocity */
/* Update paddle velocity */
/* Left paddle - controlled by client */
/* Left paddle (controlled by client) */
/* Up motion */
/* Up motion */
if ( type . mode = = M_CLIENT ) {
if ( IsKeyPressed ( KEY_S ) | | response = = " D1 " ) {
if ( IsKeyPressed ( KEY_S ) ) {
pad1 . velocity . y = PADDLE_SPEED ; /* Set positive (downward) velocity, since (0,0) is top-left */
type . netsock - > sendAll ( std : : string ( " D " ) ) ;
if ( type . mode = = M_CLIENT ) { /* If this machine controls this paddle, Send the key press to the other machine */
pad1 . velocity . y = PADDLE_SPEED ; /* Set positive (downward) velocity, since (0,0) is top-left */
to_send = " D1 " ;
}
}
if ( type . mode = = M_SERVER ) {
if ( type . netsock - > recvAll ( ) = = " U " ) {
pad1 . velocity . y = PADDLE_SPEED ;
}
}
}
}
/* Down motion */
/* Down motion */
if ( type . mode = = M_CLIENT ) {
if ( IsKeyPressed ( KEY_W ) | | response = = " U1 " ) {
if ( IsKeyPressed ( KEY_W ) ) {
pad1 . velocity . y = ( - 1 ) * PADDLE_SPEED ; /* Set negative (upward) velocity */
type . netsock - > sendAll ( std : : string ( " U " ) ) ;
if ( type . mode = = M_CLIENT ) {
pad1 . velocity . y = ( - 1 ) * PADDLE_SPEED ; /* Set negative (upward) velocity */
to_send = " U1 " ;
}
}
if ( type . mode = = M_SERVER ) {
if ( type . netsock - > recvAll ( ) = = " D " ) {
pad1 . velocity . y = ( - 1 ) * PADDLE_SPEED ;
}
}
}
}
/* Stop */
if ( ( IsKeyReleased ( KEY_S ) | | IsKeyReleased ( KEY_W ) ) | | ( response = = " S1 " ) ) {
if ( IsKeyReleased ( KEY_S ) | | IsKeyReleased ( KEY_W ) ) {
if ( type . mode = = M_CLIENT ) {
type . netsock - > sendAll ( std : : string ( " S " ) ) ;
pad1 . velocity . y = 0 ;
pad1 . velocity . y = 0 ;
if ( type . mode = = M_CLIENT ) {
to_send = " S1 " ;
}
}
}
}
/* Right paddle */
/* Right paddle - controlled by server */
if ( IsKeyPressed ( KEY_DOWN ) ) {
if ( IsKeyPressed ( KEY_DOWN ) ) {
if ( type . mode = = M_SERVER ) {
if ( type . mode = = M_SERVER ) {
type . netsock - > sendAll ( std : : string ( " D " ) ) ;
type . netsock - > sendAll ( std : : string ( " D " ) ) ;