2016-06-22 12:29:39 +02:00
|
|
|
#include <core/Logger.hpp>
|
|
|
|
#include "GameWindow.hpp"
|
|
|
|
|
|
|
|
GameWindow::GameWindow() :
|
|
|
|
window(nullptr), glcontext(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-03 00:00:11 +02:00
|
|
|
void GameWindow::create(const std::string& title, size_t w, size_t h, bool fullscreen)
|
2016-06-22 12:29:39 +02:00
|
|
|
{
|
2016-08-24 23:06:12 +02:00
|
|
|
Uint32 style = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
|
2016-06-22 12:29:39 +02:00
|
|
|
if (fullscreen)
|
|
|
|
style |= SDL_WINDOW_FULLSCREEN;
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
|
|
|
|
2016-07-03 00:00:11 +02:00
|
|
|
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, style);
|
2016-08-24 22:33:31 +02:00
|
|
|
if (window == nullptr) {
|
|
|
|
// Window creation failure is fatal
|
|
|
|
std::string sdlErrorStr = SDL_GetError();
|
|
|
|
throw std::runtime_error("SDL_CreateWindow failed: " + sdlErrorStr);
|
|
|
|
}
|
2016-06-22 12:29:39 +02:00
|
|
|
glcontext = SDL_GL_CreateContext(window);
|
2016-08-24 22:33:31 +02:00
|
|
|
if (glcontext == nullptr) {
|
|
|
|
// context creation failure is fatal
|
|
|
|
std::string sdlErrorStr = SDL_GetError();
|
|
|
|
throw std::runtime_error("SDL_GL_CreateContext failed: " + sdlErrorStr);
|
|
|
|
}
|
2016-08-24 23:06:12 +02:00
|
|
|
|
|
|
|
SDL_ShowWindow(window);
|
2016-06-22 12:29:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameWindow::close()
|
|
|
|
{
|
|
|
|
SDL_GL_DeleteContext(glcontext);
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
|
|
|
|
window = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameWindow::showCursor()
|
|
|
|
{
|
|
|
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GameWindow::hideCursor()
|
|
|
|
{
|
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glm::ivec2 GameWindow::getSize() const
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
SDL_GL_GetDrawableSize(window, &x, &y);
|
|
|
|
|
|
|
|
return glm::ivec2(x, y);
|
|
|
|
}
|