I made a GLFW window abstraction, as I find it very useful for me to make such abstractions while learning an API, and It actually pretty much eases my work while coding.
Anyways, share your thoughts. Is the code practice good? Is it bad If I am very explicit in my naming conventions? For example, every method is Window...something. Like DestroyWindow(). Should it be only Destroy();?
Window.h
#pragma once
#include "GLFW\glfw3.h"
enum class WindowMode
{
FULLSCREEN = 0,
WINDOWED = 1
};
struct WindowProps
{
unsigned int WindowWidth;
unsigned int WindowHeight;
const char* WindowTitle;
WindowMode WM;
WindowProps(unsigned int windowWidth = 1280,
unsigned int windowHeight = 720,
const char* windowTitle = "OpenGL",
WindowMode wm = WindowMode::WINDOWED)
: WindowWidth(windowWidth), WindowHeight(windowHeight), WindowTitle(windowTitle), WM(wm)
{
}
};
class Window
{
private:
GLFWwindow* m_Window;
unsigned int m_WindowMinimumWidth;
unsigned int m_WindowMaximumWidth;
unsigned int m_WindowMinimumHeight;
unsigned int m_WindowMaximumHeight;
unsigned int m_MonitorWidth;
unsigned int m_MonitorHeight;
struct WindowData
{
unsigned int WindowWidth;
unsigned int WindowHeight;
const char* WindowTitle;
WindowMode WM;
} m_WindowData;
private:
void InitWindow(const WindowProps& props = WindowProps());
public:
Window(const unsigned int& windowWidth, const unsigned int& windowHeight, const char* windowTitle, WindowMode windowMode);
Window();
void MakeWindowContextCurrent();
void DestroyWindow();
void WindowOnFocus();
void MaximizeWindow();
void MinimizeWindow();
void RestoreWindow();
void CloseWindow();
void SetWindowWidth(const unsigned int& windowWidth);
void SetWindowHeight(const unsigned int& windowHeight);
void SetWindowSizeLimit(const unsigned int& windowMinWidth, const unsigned int& windowMinHeight, const unsigned int& windowMaxWidth, const unsigned int& windowMaxHeight);
void SetWindowTitle(const char* windowTitle);
unsigned int GetMonitorWidth();
unsigned int GetMonitorHeight();
inline unsigned int GetWindowWidth() const { return m_WindowData.WindowWidth; }
inline unsigned int GetWindowHeight() const { return m_WindowData.WindowHeight; }
inline unsigned int GetWindowMinimumWidth() const { return m_WindowMinimumWidth; }
inline unsigned int GetWindowMaximumWidth() const { return m_WindowMaximumWidth; }
inline unsigned int GetWindowMinimumHeight() const { return m_WindowMinimumHeight; }
inline unsigned int GetWindowMaximumHeight() const { return m_WindowMaximumHeight; }
inline const char* GetWindowTitle() const { return m_WindowData.WindowTitle; }
inline GLFWwindow* GetWindowInstance() const { return m_Window; }
};
Window.cpp
#include "Window.h"
Window::Window(const unsigned int& windowWidth, const unsigned int& windowHeight, const char* windowTitle, WindowMode windowMode)
{
WindowProps windowProperties;
windowProperties.WindowWidth = windowWidth;
windowProperties.WindowHeight = windowHeight;
windowProperties.WindowTitle = windowTitle;
windowProperties.WM = windowMode;
InitWindow(windowProperties);
}
Window::Window()
{
InitWindow();
}
void Window::InitWindow(const WindowProps& windowProperties)
{
m_WindowData.WindowWidth = windowProperties.WindowWidth;
m_WindowData.WindowHeight = windowProperties.WindowHeight;
m_WindowData.WindowTitle = windowProperties.WindowTitle;
m_Window = glfwCreateWindow(
windowProperties.WM == WindowMode::FULLSCREEN ? GetMonitorWidth() : windowProperties.WindowWidth,
windowProperties.WM == WindowMode::FULLSCREEN ? GetMonitorHeight() : windowProperties.WindowHeight,
windowProperties.WindowTitle,
windowProperties.WM == WindowMode::FULLSCREEN ? glfwGetPrimaryMonitor() : nullptr, nullptr
);
}
void Window::MakeWindowContextCurrent()
{
glfwMakeContextCurrent(m_Window);
}
void Window::DestroyWindow()
{
glfwDestroyWindow(m_Window);
}
void Window::WindowOnFocus()
{
glfwFocusWindow(m_Window);
}
void Window::MaximizeWindow()
{
glfwMaximizeWindow(m_Window);
}
void Window::MinimizeWindow()
{
glfwIconifyWindow(m_Window);
}
void Window::RestoreWindow()
{
glfwRestoreWindow(m_Window);
}
void Window::CloseWindow()
{
glfwSetWindowShouldClose(m_Window, GL_TRUE);
}
void Window::SetWindowWidth(const unsigned int& windowWidth)
{
m_WindowData.WindowWidth = windowWidth;
glfwSetWindowSize(m_Window, m_WindowData.WindowWidth, m_WindowData.WindowHeight);
}
void Window::SetWindowHeight(const unsigned int& windowHeight)
{
m_WindowData.WindowHeight = windowHeight;
glfwSetWindowSize(m_Window, m_WindowData.WindowWidth, m_WindowData.WindowHeight);
}
void Window::SetWindowSizeLimit(const unsigned int& windowMinWidth, const unsigned int& windowMinHeight, const unsigned int& windowMaxWidth,
const unsigned int& windowMaxHeight)
{
m_WindowMinimumWidth = windowMinWidth;
m_WindowMinimumHeight = windowMinHeight;
m_WindowMaximumWidth = windowMaxWidth;
m_WindowMaximumHeight = windowMaxHeight;
glfwSetWindowSizeLimits(m_Window, m_WindowMinimumWidth, m_WindowMinimumHeight, m_WindowMaximumWidth, m_WindowMaximumHeight);
}
void Window::SetWindowTitle(const char* windowTitle)
{
m_WindowData.WindowTitle = windowTitle;
glfwSetWindowTitle(m_Window, m_WindowData.WindowTitle);
}
unsigned int Window::GetMonitorWidth()
{
const GLFWvidmode* VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
m_MonitorWidth = VidMode->width;
return m_MonitorWidth;
}
unsigned int Window::GetMonitorHeight()
{
const GLFWvidmode* VidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
m_MonitorHeight = VidMode->height;
return m_MonitorHeight;
}
Usage Example:
Window myWindow(960, 540, "OpenGL", WindowMode::WINDOWED);
myWindow.MakeWindowContextCurrent();
std::cout << myWindow.GetWindowTitle() << "\n";
std::cout << myWindow.GetWindowWidth() << "\n";
std::cout << myWindow.GetWindowHeight() << "\n";
std::cout << myWindow.GetMonitorWidth() << "\n";
std::cout << myWindow.GetMonitorHeight() << "\n";
myWindow.DestroyWindow();
Window
is already known, having it as substring of member function names e.g. inInitWindow()
,MaximizeWindow()
is indeed redundant \$\endgroup\$