charcoal/OpenGLEngine/Application.cpp

92 lines
2.3 KiB
C++

#include "Application.h"
#include "Exception.h"
Application::Application(const char* class_name, HINSTANCE h_instance)
: m_window(class_name, h_instance), m_input_manager(&m_window.get_input_manager()), m_client_size(1280, 720)
{
base_init();
}
Application::~Application()
{
HGLRC h_glrc = m_window.get_h_glrc();
if (h_glrc != nullptr) {
// Unset the current OpenGL Rendering Context and Device
wglMakeCurrent(NULL, NULL);
wglDeleteContext(h_glrc);
m_window.set_h_glrc(nullptr);
}
}
int Application::run()
{
init();
MessageResponse resp(UNSET);
while (resp != QUIT_MESSAGE) {
// Handle all messages
while ((resp = m_window.handle_message()) == HANDLED_MESSAGE) {}
float delta_time = m_fps.mark();
clock_t clock = m_fps.get_clock();
update(delta_time, clock);
render();
m_window.swap();
}
return 0;
}
void Application::close()
{
}
void Application::base_init()
{
if (!m_window.register_class()) throw EXCEPTION("Unable to register window class.");
if (!m_window.create_window("OpenGLEngine", -1, -1, m_client_size.x, m_client_size.y)) throw EXCEPTION("Unable to create window");
// Initialize window with OpenGL (May want to move this into MyApplication to allow application customization)
// https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)
HDC device_context = m_window.get_h_dc();
// PIXELFORMATDESCRIPTOR https://msdn.microsoft.com/en-us/library/cc231189.aspx
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int pixel_format = ChoosePixelFormat(device_context, &pfd);
SetPixelFormat(device_context, pixel_format, &pfd);
HGLRC glrc = wglCreateContext(m_window.get_h_dc());
wglMakeCurrent(device_context, glrc);
m_window.set_h_glrc(glrc);
GLenum glewErr = glewInit();
if (glewErr != GLEW_OK)
{
OutputDebugString("Glew Init Failed: ");
OutputDebugString((char*)glewGetErrorString(glewErr));
OutputDebugString("\n");
throw EXCEPTION("GLEW Init Failure");
}
m_window.show(SW_NORMAL);
m_fps.prepare();
}
void Application::base_close()
{
close();
}