initial commit
This commit is contained in:
26
src/glfw/context/context.cpp
Executable file
26
src/glfw/context/context.cpp
Executable file
@@ -0,0 +1,26 @@
|
||||
#include "context.hpp"
|
||||
|
||||
namespace VE
|
||||
{
|
||||
context::context(GLFWwindow* w) : m_win { w }
|
||||
{
|
||||
glfwMakeContextCurrent(m_win);
|
||||
int status = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
|
||||
hack::log(": ")("Glad loader status", status == 1 ? "Load" : "UNLOAD");
|
||||
hack::log(": ")("OpenGL Info");
|
||||
hack::log(": ")(" Vendor", glGetString(GL_VENDOR));
|
||||
hack::log(": ")(" Renderer", glGetString(GL_RENDERER));
|
||||
hack::log(": ")(" Version", glGetString(GL_VERSION));
|
||||
hack::log(": ")(" GLSL Version", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
||||
|
||||
glfwSwapInterval(1);
|
||||
|
||||
}
|
||||
|
||||
void context::swap_buffers()
|
||||
{
|
||||
glfwSwapBuffers(m_win);
|
||||
}
|
||||
}
|
||||
|
||||
19
src/glfw/context/context.hpp
Executable file
19
src/glfw/context/context.hpp
Executable file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/utils.hpp"
|
||||
|
||||
namespace VE
|
||||
{
|
||||
class context
|
||||
{
|
||||
public:
|
||||
context(GLFWwindow* w);
|
||||
|
||||
private:
|
||||
GLFWwindow* m_win;
|
||||
|
||||
public:
|
||||
void init();
|
||||
void swap_buffers();
|
||||
};
|
||||
}
|
||||
215
src/glfw/glfw.cpp
Executable file
215
src/glfw/glfw.cpp
Executable file
@@ -0,0 +1,215 @@
|
||||
#include "glfw.hpp"
|
||||
|
||||
// #include "renderer/renderer.hpp"
|
||||
// #include "event/system_event/category/key_event.hpp"
|
||||
// #include "event/system_event/category/window_event.hpp"
|
||||
|
||||
namespace VE
|
||||
{
|
||||
glfw::glfw()
|
||||
{
|
||||
if (!glfwInit())
|
||||
{
|
||||
hack::error()("Not glfw window init");
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
||||
glfw::~glfw()
|
||||
{
|
||||
hack::warn(": ")("Destroy glfw window", m_win_data.m_name);
|
||||
glfwDestroyWindow(m_win);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
void glfw::init(std::string win_name)
|
||||
{
|
||||
m_win_data.m_name = win_name;
|
||||
|
||||
set_hint();
|
||||
set_window();
|
||||
set_graphic_context();
|
||||
set_pointer();
|
||||
set_key_callback();
|
||||
set_mouse_callback();
|
||||
set_window_callback();
|
||||
|
||||
}
|
||||
|
||||
void glfw::set_hint()
|
||||
{
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
hack::log()("Set glfw window hint");
|
||||
}
|
||||
|
||||
void glfw::set_window()
|
||||
{
|
||||
// два полследних nullptr
|
||||
// первый - устанавливает монитор на ктором будетоткрытие(основной, дополнительнй)
|
||||
// второй - делиться ли ресурсами с кем-то
|
||||
m_win = glfwCreateWindow(
|
||||
glfwGetVideoMode(glfwGetPrimaryMonitor())->width,
|
||||
glfwGetVideoMode(glfwGetPrimaryMonitor())->height,
|
||||
m_win_data.m_name.c_str(),
|
||||
nullptr, nullptr
|
||||
);
|
||||
|
||||
if(m_win == NULL)
|
||||
{
|
||||
hack::error()("Failed to create GLFW window");
|
||||
glfwTerminate();
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
glfwGetWindowSize(m_win, &m_win_data.m_width, &m_win_data.m_height);
|
||||
|
||||
hack::log(": ")("Created glfw window", m_win_data.m_name);
|
||||
hack::log(" = ")(" width", m_win_data.m_width);
|
||||
hack::log(" = ")(" height", m_win_data.m_height);
|
||||
}
|
||||
|
||||
void glfw::set_graphic_context()
|
||||
{
|
||||
m_context = std::make_unique<context>(m_win);
|
||||
hack::log()("Set context");
|
||||
}
|
||||
|
||||
void glfw::set_pointer()
|
||||
{
|
||||
glfwSetWindowUserPointer(m_win, &m_win_data);
|
||||
}
|
||||
|
||||
GLFWwindow* glfw::get_win() const
|
||||
{
|
||||
return m_win;
|
||||
}
|
||||
|
||||
int glfw::width() const
|
||||
{
|
||||
return m_win_data.m_width;
|
||||
}
|
||||
|
||||
int glfw::height() const
|
||||
{
|
||||
return m_win_data.m_height;
|
||||
}
|
||||
|
||||
void glfw::update()
|
||||
{
|
||||
glfwPollEvents();
|
||||
m_context->swap_buffers();
|
||||
}
|
||||
|
||||
void glfw::clear() const
|
||||
{
|
||||
glClearColor(m_bgcolor.r, m_bgcolor.g, m_bgcolor.b, m_bgcolor.a);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void glfw::set_event_fn(const event_fn<event>& fn)
|
||||
{
|
||||
m_win_data.execute = fn;
|
||||
}
|
||||
|
||||
void glfw::set_key_callback()
|
||||
{
|
||||
glfwSetKeyCallback(m_win, [](GLFWwindow* w, int key, int scancode, int action, int mods)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
event e{ action, key };
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case GLFW_PRESS:
|
||||
{
|
||||
e.m_type = event_type::KEY_PRESSED;
|
||||
break;
|
||||
}
|
||||
case GLFW_RELEASE:
|
||||
{
|
||||
e.m_type = event_type::KEY_RELEASED;
|
||||
break;
|
||||
}
|
||||
case GLFW_REPEAT:
|
||||
{
|
||||
e.m_type = event_type::KEY_REPEATE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
d->execute(e);
|
||||
});
|
||||
}
|
||||
|
||||
void glfw::set_mouse_callback()
|
||||
{
|
||||
glfwSetMouseButtonCallback(m_win, [](GLFWwindow* w, int button, int action, int mods)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
event e{ action, button };
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case GLFW_PRESS:
|
||||
{
|
||||
e.m_type = event_type::MOUSE_BUTTON_PRESSED;
|
||||
break;
|
||||
}
|
||||
case GLFW_RELEASE:
|
||||
{
|
||||
e.m_type = event_type::MOUSE_BUTTON_RELEASED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
d->execute(e);
|
||||
});
|
||||
|
||||
glfwSetScrollCallback(m_win, [](GLFWwindow* w, double xOffset, double yOffset)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
event e{ event_type::MOUSE_SCROLL, std::pair<float, float>{ static_cast<float>(xOffset), static_cast<float>(yOffset) } };
|
||||
d->execute(e);
|
||||
});
|
||||
|
||||
glfwSetCursorPosCallback(m_win, [](GLFWwindow* w, double xPos, double yPos)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
event e{ event_type::MOUSE_CURSOR_POSITION, std::pair<float, float>{ static_cast<float>(xPos), static_cast<float>(yPos) } };
|
||||
d->execute(e);
|
||||
});
|
||||
}
|
||||
|
||||
void glfw::set_window_callback()
|
||||
{
|
||||
glfwSetWindowSizeLimits(m_win, m_win_data.m_width, m_win_data.m_height, GLFW_DONT_CARE, GLFW_DONT_CARE);
|
||||
|
||||
glfwSetWindowSizeCallback(m_win, [](GLFWwindow* w, int width, int height)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
d->m_width = width;
|
||||
d->m_height = height;
|
||||
|
||||
event e{ event_type::WINDOW_RESIZE, std::pair<float, float>{ static_cast<float>(width), static_cast<float>(height) } };
|
||||
d->execute(e);
|
||||
});
|
||||
|
||||
glfwSetWindowCloseCallback(m_win, [](GLFWwindow* w)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
event e{ event_type::WINDOW_CLOSE, nullptr };
|
||||
d->execute(e);
|
||||
});
|
||||
|
||||
glfwSetWindowFocusCallback(m_win, [](GLFWwindow* w, int focused)
|
||||
{
|
||||
auto d = static_cast<win_data*>(glfwGetWindowUserPointer(w));
|
||||
event e{ event_type::WINDOW_FOCUS, focused };
|
||||
d->execute(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
55
src/glfw/glfw.hpp
Executable file
55
src/glfw/glfw.hpp
Executable file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/utils.hpp"
|
||||
#include "context/context.hpp"
|
||||
#include "event/event.hpp"
|
||||
|
||||
namespace VE
|
||||
{
|
||||
class glfw
|
||||
{
|
||||
public:
|
||||
// HERE
|
||||
// реализовать остальные конструкторы
|
||||
glfw();
|
||||
~glfw();
|
||||
|
||||
|
||||
private:
|
||||
// ни каких unique_ptr тут не получится
|
||||
// т.к. glfwCreateWindow maloc-ом выделяет память
|
||||
// что не есть хорошо для умных указателей
|
||||
GLFWwindow* m_win;
|
||||
std::unique_ptr<context> m_context;
|
||||
mt::vec4 m_bgcolor = { 0.1f, 0.1f, 0.1f, 1.f };
|
||||
|
||||
struct win_data
|
||||
{
|
||||
std::string m_name;
|
||||
int m_width, m_height;
|
||||
event_fn<event&> execute; // в using.hpp
|
||||
} m_win_data;
|
||||
|
||||
public:
|
||||
void init(std::string win_name);
|
||||
|
||||
GLFWwindow* get_win() const;
|
||||
int width() const;
|
||||
int height() const;
|
||||
|
||||
void update();
|
||||
void clear() const;
|
||||
|
||||
void set_event_fn(const event_fn<event&>& fn);
|
||||
|
||||
private:
|
||||
void set_hint();
|
||||
void set_window();
|
||||
void set_graphic_context();
|
||||
void set_pointer();
|
||||
void set_key_callback();
|
||||
void set_mouse_callback();
|
||||
void set_window_callback();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user