charcoal/OpenGLEngine/Application.cpp
2018-09-05 02:49:02 -04:00

85 lines
2.0 KiB
C++

#include "Application.h"
#include <Windows.h>
#include <glew/glew.h>
#include <gl/GL.h>
#pragma comment(lib, "opengl32.lib")
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)
{
}
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()
{
if (!base_init()) return 1;
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()
{
}
bool Application::base_init()
{
if (!m_window.register_class()) return false;
if (!m_window.create_window("OpenGLEngine", -1, -1, m_client_size.x, m_client_size.y)) return false;
// 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);
m_window.set_h_glrc(wglCreateContext(m_window.get_h_dc()));
m_window.show(SW_NORMAL);
m_fps.prepare();
return init();
}
void Application::base_close()
{
close();
}