Changed Sock to Sock* in GameType struct to allow it to be set to null

master
Aadhavan Srinivasan 8 months ago
parent cb0fe1af6b
commit fc041539c5

@ -31,7 +31,7 @@ typedef enum {M_SINGLE, M_CLIENT, M_SERVER} Mode;
typedef struct {
Mode mode;
Sock netsock;
Sock* netsock;
} GameType;
raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
@ -79,7 +79,7 @@ GameType check_server_client(int argc, char** argv) {
connect_code = std::string(argv[2]); /* The connect code is a special string, that contains the server address and port. It is given by the server. */
try {
addr_port = connect_code::decode(connect_code);
Client client = Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
Client* client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1]));
type.mode = M_CLIENT;
type.netsock = client;
return type;
@ -118,7 +118,7 @@ GameType check_server_client(int argc, char** argv) {
std::string code = connect_code::encode(addr, std::to_string(port));
std::cout << "Your code is " << code << std::endl;
try {
Server server = Server(4, 'T', addr.data(), port);
Server* server = new Server(4, 'T', addr.data(), port);
type.mode = M_SERVER;
type.netsock = server;
return type;
@ -208,12 +208,12 @@ int main(int argc, char** argv) {
/* Up motion */
if (type.mode == M_CLIENT) {
if (IsKeyPressed(KEY_S)) {
client.sendAll(std::string("D"));
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 (server.recvAll() == "U") {
if (type.netsock->recvAll() == "U") {
pad1.velocity.y = PADDLE_SPEED;
}
}
@ -221,12 +221,12 @@ int main(int argc, char** argv) {
/* Down motion */
if (type.mode == M_CLIENT) {
if (IsKeyPressed(KEY_W)) {
client.sendAll(std::string("U"));
type.netsock->sendAll(std::string("U"));
pad1.velocity.y = (-1) * PADDLE_SPEED; /* Set negative (upward) velocity */
}
}
if (type.mode == M_SERVER) {
if (server.recvAll() == "D") {
if (type.netsock->recvAll() == "D") {
pad1.velocity.y = (-1) * PADDLE_SPEED;
}
}
@ -235,26 +235,27 @@ int main(int argc, char** argv) {
if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) {
if (type.mode == M_CLIENT) {
client.sendAll(std::string("S"));
pad1.velocity.y = 0;
type.netsock->sendAll(std::string("S"));
pad1.velocity.y = 0;
}
}
/* Right paddle */
if (IsKeyPressed(KEY_DOWN)) {
if (type.mode == M_SERVER) {
server.sendAll(std::string("D"));
type.netsock->sendAll(std::string("D"));
}
pad2.velocity.y = PADDLE_SPEED;
}
if (IsKeyPressed(KEY_UP)) {
if (type.mode == M_SERVER) {
server.sendAll(std::string("U"));
type.netsock->sendAll(std::string("U"));
}
pad2.velocity.y = (-1) * PADDLE_SPEED;
}
if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) {
if (type.mode == M_SERVER) {
server.sendAll(std::string("S"));
type.netsock->sendAll(std::string("S"));
}
pad2.velocity.y = 0;
}

Loading…
Cancel
Save