Skip to content

Refactor/client callbacks #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion include/client/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,72 @@
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/string_cast.hpp"

/**
* @brief Class representing a camera for rendering. Primarily used to compute the
* view-projection matrix and pass it to the Client for rendering objects from
* different viewing angles.
*/
class Camera {

public:
/**
* @brief Constructs a new Camera object.
*
*/
Camera();

/**
* @brief Destroys the Camera object.
*
*/
~Camera();

// Access functions
/**
* @brief Sets the aspect ratio of the camera.
*
* @param a The new aspect ratio.
*/
void setAspect(float a) { aspect = a; }

/**
* @brief Updates the the camera based on the input screen coordinates.
*
* @param xpos New x-coordinate for the cursor.
* @param ypos New y-coordinate for the cursor.
*/
void update(float xpos, float ypos);

/**
* @brief Moves the camera based on the axis, direction, and speed. Factors in the current
* direction of the camera to compute proper lateral and forward movement.
*
* @param is_x_axis A flag denoting whether the movement is along the x-axis or z-axis.
* @param dir The direction that the camera is moving in.
* @return glm::vec3 An output vector representing the movement of the camera in the specified
* axis and direction.
*/
glm::vec3 move(bool is_x_axis, float dir);

glm::mat4 getViewProj();
/**
* @brief Returns the view-projection matrix of the Camera, for use in rendering.
*
* @return glm::mat4 The view-projection matrix.
*/
glm::mat4 getViewProj() { return viewProjMat; }

/**
* @brief Returns a normalized vector representing the direction that the camera is facing.
*
* @return glm::vec3 A normalized vector representing the direction the camera is facing.
*/
glm::vec3 getFacing() { return cameraFront; }

/**
* @brief Updates the position of the camera.
*
* @param pos The new position of the camera.
*/
void updatePos(glm::vec3 pos);

glm::vec3 getPos();
Expand Down
168 changes: 132 additions & 36 deletions include/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "client/camera.hpp"
#include "client/audiomanager.hpp"

//#include "shared/game/gamestate.hpp"
#include "shared/game/sharedgamestate.hpp"
#include "shared/network/packet.hpp"
#include "shared/network/session.hpp"
Expand All @@ -37,87 +36,184 @@

using namespace boost::asio::ip;

/**
* @brief A clsas that represents the client for the game. Contains all local information,
* including the SharedGameState, as well as callbacks for rendering objects and initializing
* glfw, glew, shaders, and windows.
*/
class Client {

public:
/**
* @brief Constructs a new Client object.
*
* @param io_service
* @param config
*/
Client(boost::asio::io_service& io_service, GameConfig config);

/**
* @brief Destroys the Client object.
*/
~Client();

/**
* @brief Initializes glfw, glew, shaders, and the GLFWwindow.
*
* @return true if all libraries and objects were initialized correctly, false otherwise
*/
bool init();

/**
* @brief Frees all pointers, deletes the GLFWwindow, and terminates glfw.
*
* @return true if all objects were cleaned up correclty, false otherwise
*/
bool cleanup();

// Callbacks
/**
* @brief Display callback which handles the rendering of all local objects. Abstracts
* the logic of the main render loop. All logic relating to rendering and any glfw
* or OpenGL calls should go in here.
*/
void displayCallback();

/**
* @brief Callback which handles all updates to the local SharedGameState, and sends
* events to the server based on any local inputs. All logic relating to state updates
* shoud go in here.
*
* @param context
*/
void idleCallback(boost::asio::io_context& context);
void handleKeys(int eid, int keyType, bool keyHeld, bool *eventSent, glm::vec3 movement = glm::vec3(0.0f));

// Bound window callbacks
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
static void mouseCallback(GLFWwindow* window, double xposIn, double yposIn);
static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
static void charCallback(GLFWwindow* window, unsigned int codepoint);

/**
* @brief Callback which handles keyboard inputs to the GLFWwindow. Binds to the GLFWwindow.
*
* @param window The GLFWwindow being monitered.
* @param key The key that is being pressed or released.
* @param scancode A unique platform-specific code for each key.
* @param action One of GLFW_PRESS, GLFW_REPEAT, or GLFW_RELEASE representing the state of the key.
* @param mods Any modifiers to the key pressed (i.e. shift, ctrl, alt, etc.).
*/
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);

/**
* @brief Callback which handles mouse cursor movement. Does not include mouse button presses.
*
* @param window The GLFWwindow being monitered.
* @param xposIn The current x-coordinate of the cursor.
* @param yposIn The current y-coordinate of the cursor.
*/
void mouseCallback(GLFWwindow* window, double xposIn, double yposIn);

/**
* @brief Callback which handles mouse button presses.
*
* @param window The GLFWwindow being monitered.
* @param button The mouse button pressed.
* @param action One of GLFW_PRESS, GLFW_REPEAT, or GLFW_RELEASE representing the state of the button.
* @param mods Any modifiers to the key pressed (i.e. shift, ctrl, alt, etc.).
*/
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);

/**
* @brief
*
* @param window
* @param codepoint
*/
void charCallback(GLFWwindow* window, unsigned int codepoint);

/**
* @brief Get a vec2 representing <width, height> of the window size.
*
* @return glm::vec2
*/
static glm::vec2 getWindowSize();

/**
* @brief Get the time of last keystroke.
*
* @return time_t
*/
static time_t getTimeOfLastKeystroke();

// Getter / Setters
/**
* @brief Returns a pointer to the GLFWwindow object.
*
* @return GLFWwindow* The GLFWwindow being used in the Client.
*/
GLFWwindow* getWindow() { return window; }

Client(boost::asio::io_service& io_service, GameConfig config);
~Client();

bool init();
bool cleanup();

void draw();
/**
* @brief Creates and connects to a Session at the specified IP address.
*
* @param ip_addr
*/
void connectAndListen(std::string ip_addr);

AudioManager* getAudioManager();

private:
void processClientInput();
/**
* @brief Processes all data received from the server and updates the SharedGameState.
*
* @param context
*/
void processServerInput(boost::asio::io_context& context);

/**
* @brief Draws all objects in the SharedGameState.
*/
void draw();

/* Current game state */
SharedGameState gameState;

/* Shader objects for various */
std::shared_ptr<Shader> cube_shader;
std::shared_ptr<Shader> model_shader;
std::shared_ptr<Shader> light_source_shader;

/* Character models and lighting objects, might need to move to different classes later */
std::unique_ptr<Model> player_model;
std::unique_ptr<Model> bear_model;
std::unique_ptr<LightSource> light_source;

float playerMovementDelta = 0.05f;

GLFWwindow *window;

/* GUI */
friend class gui::GUI;
gui::GUI gui;
gui::GUIState gui_state;
Camera *cam;

AudioManager* audioManager;

// Flags
static bool is_held_up;
static bool is_held_down;
static bool is_held_right;
static bool is_held_left;
static bool is_held_space;
static bool is_held_shift;

static bool cam_is_held_up;
static bool cam_is_held_down;
static bool cam_is_held_right;
static bool cam_is_held_left;

static bool is_left_mouse_down;
static bool is_click_available;
static float mouse_xpos;
static float mouse_ypos;
/* Camera object representing player's current position & orientation */
std::unique_ptr<Camera> cam;

/* Flags */
static int window_width;
static int window_height;

static time_t time_of_last_keystroke;

/* Key held flags */
bool is_held_up = false;
bool is_held_down = false;
bool is_held_right = false;
bool is_held_left = false;
bool is_held_space = false;

bool is_left_mouse_down = false;

/* Mouse position coordinates */
float mouse_xpos = 0.0f;
float mouse_ypos = 0.0f;

GameConfig config;
tcp::resolver resolver;
tcp::socket socket;
Expand Down
22 changes: 6 additions & 16 deletions src/client/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,15 @@ Camera::Camera() : cameraPos(glm::vec3(0.0f)), cameraFront(glm::vec3(0.0f, 0.0f,
sensitivity = 0.1f;
firstMouse = true;

// cameraPos = glm::vec3(0.0f);
// cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
// cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);

worldUp = cameraUp;

speed = 0.125f;

// viewProjMat = glm::mat4(1.0f);
}

Camera::~Camera() {

}

glm::mat4 Camera::getViewProj() {
return viewProjMat;
}

glm::vec3 Camera::getPos() {
return cameraPos;
}
Expand Down Expand Up @@ -72,6 +62,7 @@ void Camera::update(float xpos, float ypos) {
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);

glm::vec3 oldCamUp = cameraUp;
cameraRight = glm::normalize(glm::cross(cameraFront, worldUp));
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));

Expand All @@ -82,14 +73,13 @@ void Camera::update(float xpos, float ypos) {
}

glm::vec3 Camera::move(bool is_x_axis, float dir) {
glm::vec3 effCameraFront = glm::normalize(glm::vec3(cameraFront.x, 0.0f, cameraFront.z));

if (is_x_axis) {
//glm::vec3 effCameraFront = glm::normalize(cameraFront - cameraFront.y);
// cameraPos += dir * speed * effCameraFront;
//return dir * speed * effCameraFront;
return dir * speed * cameraRight;
return dir * glm::normalize(glm::cross(effCameraFront, cameraUp)) * speed;
} else {
// cameraPos += dir * glm::normalize(glm::cross(cameraFront, cameraUp)) * speed;
return dir * speed * cameraFront;
return dir * speed * effCameraFront;
// return dir * speed * cameraRight;
}
}

Expand Down
Loading
Loading