Changed include paths, added a cmdline argument ('server') to indicate if the game is networkded or not
This commit is contained in:
		
							
								
								
									
										40
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								main.cpp
									
									
									
									
									
								
							@@ -1,11 +1,11 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <ctime>
 | 
			
		||||
#include <raylib-cpp.hpp>
 | 
			
		||||
#include "includes/paddle.hpp"
 | 
			
		||||
#include "includes/ball.hpp"
 | 
			
		||||
#include "includes/math-helpers.hpp"
 | 
			
		||||
#include "includes/client.hpp"
 | 
			
		||||
#include "raylib-cpp/raylib-cpp.hpp"
 | 
			
		||||
#include "paddle.hpp"
 | 
			
		||||
#include "ball.hpp"
 | 
			
		||||
#include "math-helpers.hpp"
 | 
			
		||||
#include "client.hpp"
 | 
			
		||||
 | 
			
		||||
/* Global variables used to instantiate structs */
 | 
			
		||||
const int WIDTH = 1500;
 | 
			
		||||
@@ -39,18 +39,28 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
 | 
			
		||||
	return raylib::Vector2(new_x_vel, new_y_vel);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
int main(int argc, char** argv) {
 | 
			
		||||
	/* Initialize window and other variables */
 | 
			
		||||
	SetTraceLogLevel(LOG_NONE);
 | 
			
		||||
	raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong");
 | 
			
		||||
	SetTraceLogLevel(LOG_INFO);
 | 
			
		||||
	window.ClearBackground(BLACK);
 | 
			
		||||
	SetTargetFPS(60);
 | 
			
		||||
	SetExitKey(KEY_Q);
 | 
			
		||||
	std::string points_str = std::string("0\t\t0");
 | 
			
		||||
	bool game_started = false;
 | 
			
		||||
	srand(std::time(NULL));
 | 
			
		||||
	Client client = Client(4,'T',"127.0.0.1",6500);
 | 
			
		||||
 | 
			
		||||
	bool in_server_mode = false;
 | 
			
		||||
	Client client;
 | 
			
		||||
	if (argc > 1 && strcmp(argv[1],"server") == 0) {
 | 
			
		||||
		try {
 | 
			
		||||
			client = Client(4,'T',"127.0.0.1",6500);
 | 
			
		||||
			in_server_mode = true;
 | 
			
		||||
		}
 | 
			
		||||
		catch (int e) {
 | 
			
		||||
			std::cout << "Unable to connect to the given address." << std::endl;
 | 
			
		||||
			return -1;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	/* Instantiate Paddle and Ball objects */
 | 
			
		||||
	Paddle pad1 = Paddle(10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H);
 | 
			
		||||
	Paddle pad2 = Paddle(window.GetWidth() - RECT_W - 10, (HEIGHT / 2) - (RECT_H / 2), RECT_W, RECT_H);
 | 
			
		||||
@@ -85,15 +95,21 @@ int main() {
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (IsKeyPressed(KEY_UP)) {
 | 
			
		||||
				client.sendAll(std::string("U"));
 | 
			
		||||
				if(in_server_mode) {
 | 
			
		||||
					client.sendAll(std::string("U"));
 | 
			
		||||
				}
 | 
			
		||||
				pad2.velocity.y = (-1) * PADDLE_SPEED;
 | 
			
		||||
			}
 | 
			
		||||
			if (IsKeyPressed(KEY_DOWN)) {
 | 
			
		||||
				client.sendAll(std::string("D"));
 | 
			
		||||
				if (in_server_mode) {
 | 
			
		||||
					client.sendAll(std::string("D"));
 | 
			
		||||
				}
 | 
			
		||||
				pad2.velocity.y = PADDLE_SPEED;
 | 
			
		||||
			}
 | 
			
		||||
			if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) {
 | 
			
		||||
				client.sendAll(std::string("S"));
 | 
			
		||||
				if (in_server_mode) {
 | 
			
		||||
					client.sendAll(std::string("S"));
 | 
			
		||||
				}
 | 
			
		||||
				pad2.velocity.y = 0;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user