voxspatium/src/Application.cpp

115 lines
2.0 KiB
C++

#include "Application.h"
#include "util/Log.h"
Application::Application()
{
}
Application::~Application()
{
}
void Application::Initialize()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return;
}
// Create our window centered at 1080x720 resolution
m_window = SDL_CreateWindow(
"Universium",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
1080,
720,
SDL_WINDOW_OPENGL
);
if (!m_window)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return;
}
// Set GL Attributes
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
m_glContext = SDL_GL_CreateContext(m_window);
if(m_glContext == NULL)
{
printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
return;
}
// Initialize GLEW
glewInit();
// Run the engine
Run();
}
void Application::Run()
{
m_run = true;
now_ = SDL_GetPerformanceCounter();
last_ = 0;
while(m_run)
{
last_ = now_;
now_ = SDL_GetPerformanceCounter();
while(SDL_PollEvent(&m_event) != 0)
{
// Close button is pressed
if(m_event.type == SDL_QUIT)
{
Exit();
}
}
deltaTime_ = ((now_ - last_) / (double)SDL_GetPerformanceFrequency());
Update(deltaTime_);
Render();
// Set background color as cornflower blue
glClearColor(0.39f, 0.58f, 0.93f, 1.f);
// Clear color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update window with OpenGL rendering
SDL_GL_SwapWindow(m_window);
}
// After loop exits
// Destroy window
SDL_DestroyWindow(m_window);
m_window = NULL;
// Destroy context
SDL_GL_DeleteContext(m_glContext);
// Quit SDL subsystems
SDL_Quit();
}
void Application::Update(GLfloat dtime)
{
log_info(std::to_string(dtime));
}
void Application::Render()
{
}