Skip to content
Open
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
48 changes: 47 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,52 @@
"iterator": "cpp",
"utility": "cpp",
"string": "cpp",
"map": "cpp"
"map": "cpp",
"memory": "cpp",
"iostream": "cpp",
"array": "cpp",
"bitset": "cpp",
"functional": "cpp",
"__bit_reference": "cpp",
"__debug": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"atomic": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"string_view": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"mutex": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"unordered_map": "cpp"
}
}
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ SOURCE_FILES=$(wildcard $(SRC)/app/*.cpp) \
$(wildcard $(SRC)/devices/opengl/*.cpp) \
$(wildcard $(SRC)/graphic/api/*.cpp) \
$(wildcard $(SRC)/graphic/*.cpp) \
$(wildcard $(SRC)/ecs/components/*.cpp) \
$(wildcard $(SRC)/ecs/systems/*.cpp) \
$(wildcard $(SRC)/ecs/*.cpp) \
$(wildcard $(SRC)/utils/*.cpp) \
$(wildcard $(SRC)/events/*.cpp) \
$(wildcard $(SRC)/events/types/*.cpp) \
$(wildcard $(SRC)/*.cpp)

OBJECT_FILES=$(SOURCE_FILES:%.cpp=$(OBJ)/%.o)
Expand Down
66 changes: 63 additions & 3 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,77 @@
#include "application.h"
#include "../utils/logger.h"
#include "../events/dispatcher.h"

Application::Application(std::string title): m_Window(title) {}
Application::Application(std::string title): m_Window(title), m_Scene(nullptr), m_Input(nullptr) {
Eng::Utils::Logger::Init();
}

Application::~Application() {
delete m_Renderer;
delete m_Scene;
}

void Application::Init() {
void Application::Init(void(*inintScene)(Eng::Scene*)) {
Graphic::RenderingAPI api = Graphic::RenderingAPI::OpenGL;

m_Window.Init(api);
Graphic::CreateContext(api);
m_Renderer = Graphic::Renderer::Create(api);
m_Scene = new Eng::Scene();
m_Input = Eng::Input(m_Window.getGlfwWindow());

inintScene(m_Scene);

m_Scene->SetProjection(m_Window.getWidth(), m_Window.getHeight());
}

void Application::Run() {
float time = glfwGetTime();
float lastUpdatedTime = time;

float lastTime = 0.0f;
float deltaTime = 0.0f;
int framesCnt = 0;
int updatesCnt = 0;

while(m_Window.isOpen()) {
float currentTime = glfwGetTime();
float updateTick = 1.0f / 60.0f;

deltaTime = currentTime - lastTime;
lastTime = currentTime;

if (currentTime - lastUpdatedTime > updateTick) {
update(deltaTime);
updatesCnt++;
lastUpdatedTime = glfwGetTime();
}

render();
framesCnt++;

if (currentTime - time > 1.0f) {
LOG_INFO("frames: {}, updates: {}", framesCnt, updatesCnt);

framesCnt = 0;
updatesCnt = 0;
time = currentTime;
}
}
}

void Application::Run() {}
void Application::render() {
m_Renderer->Clear();

m_Scene->Render(m_Renderer);

m_Window.swapBuffers();
m_Window.pollEvents();
}

void Application::update(float deltaTime) {
m_Input.Process(deltaTime);
m_Scene->Update(&m_Input);

Eng::Events::Dispatcher::ClearEvents();
}
10 changes: 9 additions & 1 deletion src/app/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
#include <string>

#include "../graphic/renderer.h"
#include "../scene.h"
#include "../input.h"

class Application {
private:
Window m_Window;
Graphic::Renderer *m_Renderer;
Eng::Scene* m_Scene;
Eng::Input m_Input;

public:
Application(std::string title);
~Application();
void Init();
void Init(void(*inintScene)(Eng::Scene*));
void Run();

inline const Window GetWindow() const { return m_Window; }
inline Graphic::Renderer* GetRenderer() const { return m_Renderer; }

private:
void render();
void update(float deltaTime);
};
4 changes: 1 addition & 3 deletions src/app/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class Window {
return (float)m_Height;
}

inline GLFWwindow* getGlfwWindow() const {
return m_Window;
}
inline GLFWwindow* getGlfwWindow() const { return m_Window; }

inline bool isInitialized() const { return m_InitSuccess; }
};
72 changes: 48 additions & 24 deletions src/camera.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
#include <glm/gtc/matrix_transform.hpp>

#include "camera.h"
#include "events/dispatcher.h"

using namespace Eng;

Camera::Camera(): m_Position(0.0f, 0.0f, 3.0f),
m_ViewDirection(0.0f, 0.0f, -1.0f),
m_Up(0.0f, 1.0f, 0.0f),
m_Yaw(-90.0f),
m_Pitch(0.0f),
m_MouseSensitivity(0.05f){}
m_Pitch(0.0f)
{
m_MovementFnPtrs["forward"] = std::bind(&Camera::forward, this, std::placeholders::_1);
m_MovementFnPtrs["backward"] = std::bind(&Camera::backward, this, std::placeholders::_1);
m_MovementFnPtrs["left"] = std::bind(&Camera::left, this, std::placeholders::_1);
m_MovementFnPtrs["right"] = std::bind(&Camera::right, this, std::placeholders::_1);

Events::Dispatcher::AddEventListener("moveCameraMouse", std::bind(&Camera::OnCameraMouseMove, this, std::placeholders::_1));
Events::Dispatcher::AddEventListener("moveCameraKeyboard", std::bind(&Camera::OnCameraKeyboardMove, this, std::placeholders::_1));
}

Camera::~Camera() {}
Camera::~Camera() {
Events::Dispatcher::RemoveEventListener("moveCameraMouse", std::bind(&Camera::OnCameraMouseMove, this, std::placeholders::_1));
Events::Dispatcher::RemoveEventListener("moveCameraKeyboard", std::bind(&Camera::OnCameraKeyboardMove, this, std::placeholders::_1));
}

glm::mat4 Camera::getViewMatrix() const {
glm::mat4 Camera::ViewMatrix() const {
return glm::lookAt(m_Position, m_Position + m_ViewDirection, m_Up);
}

void Camera::OnCameraMouseMove(SharedEvent e) {
auto event = std::static_pointer_cast<Eng::Events::MoveCameraEvent>(e);
processMouseMovement(event->xOffset, event->yOffset, event->sensetivity);
updateCameraVectors();
}

void Camera::OnCameraKeyboardMove(SharedEvent e) {
auto event = std::static_pointer_cast<Eng::Events::MoveCameraEvent>(e);
m_MovementFnPtrs[event->direction](event->deltaTime * event->sensetivity);
}

void Camera::updateCameraVectors() {
glm::vec3 direction;

direction.x = cos(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));
direction.y = sin(glm::radians(m_Pitch));
direction.z = sin(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));

m_ViewDirection = glm::normalize(direction);
}

void Camera::forward(float offset) {
m_Position += offset * m_ViewDirection;
}
Expand All @@ -31,28 +65,18 @@ void Camera::right(float offset) {
m_Position += glm::normalize(glm::cross(m_ViewDirection, m_Up)) * offset;
}

void Camera::processMouseMovement(float xOffset, float yOffset) {
xOffset *= m_MouseSensitivity;
yOffset *= m_MouseSensitivity;
void Camera::processMouseMovement(float xOffset, float yOffset, float mouseSensitivity) {
xOffset *= mouseSensitivity;
yOffset *= mouseSensitivity;

m_Yaw += xOffset;
m_Pitch += yOffset;

if (m_Pitch > 89.0f)
m_Pitch = 89.0f;
if (m_Pitch < -89.0f)
m_Pitch = -89.0f;
if (m_Pitch > 89.0f) {
m_Pitch = 89.0f;
}

updateCameraVectors();
if (m_Pitch < -89.0f) {
m_Pitch = -89.0f;
}
}

void Camera::updateCameraVectors() {
glm::vec3 direction;

direction.x = cos(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));
direction.y = sin(glm::radians(m_Pitch));
direction.z = sin(glm::radians(m_Yaw)) * cos(glm::radians(m_Pitch));

m_ViewDirection = glm::normalize(direction);
}

61 changes: 33 additions & 28 deletions src/camera.h
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
#pragma once

#include <glm/glm.hpp>
#include "events/types/move_camera_event.h"
#include <map>

class Camera {
private:
glm::vec3 m_Position;
glm::vec3 m_ViewDirection;
glm::vec3 m_Up;
float m_Yaw;
float m_Pitch;
float m_MouseSensitivity;

public:
Camera();
~Camera();
glm::mat4 getViewMatrix() const;

void forward(float offset);
void backward(float offset);
void left(float offset);
void right(float offset);

void processMouseMovement(float xOffset, float yOffset);

inline glm::vec3 GetPosition() const {
return m_Position;
}

private:
void updateCameraVectors();
};
namespace Eng {
class Camera {
private:
glm::vec3 m_Position;
glm::vec3 m_ViewDirection;
glm::vec3 m_Up;
float m_Yaw;
float m_Pitch;

std::map<std::string, std::function<void(float)>> m_MovementFnPtrs;

public:
Camera();
~Camera();
glm::mat4 ViewMatrix() const;

inline glm::vec3 GetPosition() const { return m_Position; }

void OnCameraMouseMove(SharedEvent event);
void OnCameraKeyboardMove(SharedEvent event);

private:
void updateCameraVectors();

void forward(float offset);
void backward(float offset);
void left(float offset);
void right(float offset);
void processMouseMovement(float xOffset, float yOffset, float mouseSensitivity);
};
}
11 changes: 6 additions & 5 deletions src/devices/opengl/context.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <stdexcept>
#include "common.h"
#include "context.h"
#include "../../utils/logger.h"

using namespace Devices;

Expand All @@ -9,10 +10,10 @@ GLContext::GLContext() {
throw std::runtime_error("Cannot initialize glew for OpenGL");
}

std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;
LOG_DEBUG("OpenGL version: {0}", glGetString(GL_VERSION));
LOG_DEBUG("GLSL version: {0}", glGetString(GL_SHADING_LANGUAGE_VERSION));
LOG_DEBUG("Vendor: {0}", glGetString(GL_VENDOR));
LOG_DEBUG("Renderer: {0}", glGetString(GL_RENDERER));

GLCall(glEnable(GL_CULL_FACE));
GLCall(glCullFace(GL_BACK));
Expand Down
Loading