charcoal/OpenGLEngine/Application.cpp

83 lines
1.7 KiB
C++
Raw Normal View History

2018-09-04 19:25:54 +00:00
#include "Application.h"
#include "Exception.h"
2018-09-04 19:25:54 +00:00
#include "Util.h"
2018-09-12 21:03:46 +00:00
namespace charcoal
2018-09-04 19:25:54 +00:00
{
2018-09-12 21:03:46 +00:00
Application::Application(int width, int height)
: m_screen_size(width, height)
{
if (width < 0 || height < 0)
throw EXCEPTION("Invalid screen dimensions");
2018-09-12 21:03:46 +00:00
if (!glfwInit())
throw EXCEPTION("Unable to Initialize GLFW");
2018-09-12 21:03:46 +00:00
m_p_window = glfwCreateWindow(width, height, "OpenGLEngine", NULL, NULL);
if (!m_p_window)
{
glfwTerminate();
throw EXCEPTION("Unable to Create GLFW Window");
}
2018-09-12 21:03:46 +00:00
glfwMakeContextCurrent(m_p_window);
glfwSwapInterval(1);
2018-09-12 21:03:46 +00:00
glewExperimental = GL_TRUE;
GLenum glew_err = glewInit();
if (glew_err != GLEW_OK)
{
glfwTerminate();
throw EXCEPTION("Unable to Initialize GLEW: " + std::string((char*)glewGetErrorString(glew_err)));
}
2018-09-12 21:03:46 +00:00
// Output Version Information
OutputDebugString("\nOpenGL Version Information:\nRenderer: ");
OutputDebugString((char*)glGetString(GL_RENDERER));
OutputDebugString("\nOpenGL Version: ");
OutputDebugString((char*)glGetString(GL_VERSION));
OutputDebugString("\n\n");
2018-09-12 21:03:46 +00:00
// TODO: Mouse Movement, Mouse Scroll
m_glfw_input_manager.init(m_p_window);
glfwSetKeyCallback(m_p_window, &GLFWInputManager::key_callback);
2018-09-12 21:03:46 +00:00
m_fps.prepare();
}
2018-09-04 19:25:54 +00:00
2018-09-12 21:03:46 +00:00
int Application::run()
{
2018-09-12 21:03:46 +00:00
try
{
2018-09-12 21:03:46 +00:00
init();
while (!glfwWindowShouldClose(m_p_window))
{
// Handle all messages
m_glfw_input_manager.mark();
glfwPollEvents();
float delta_time = m_fps.mark();
clock_t clock = m_fps.get_clock();
update(delta_time, clock);
prerender();
render();
CHECK_GL_ERR();
glfwSwapBuffers(m_p_window);
}
2018-09-12 21:03:46 +00:00
return 0;
}
catch (Exception& e)
{
glfwTerminate();
throw e;
}
}
2018-09-12 21:03:46 +00:00
void Application::base_close()
{
2018-09-12 21:03:46 +00:00
close();
2018-09-04 19:25:54 +00:00
}
2018-09-12 21:03:46 +00:00
}