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