2016-06-22 12:29:39 +02:00
|
|
|
#include "GameWindow.hpp"
|
2018-12-09 22:43:42 +01:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
#include <core/Logger.hpp>
|
2018-12-09 22:43:42 +01:00
|
|
|
#include <render/GameRenderer.hpp>
|
|
|
|
|
|
|
|
#include <SDL_mouse.h>
|
2016-06-22 12:29:39 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void GameWindow::create(const std::string& title, size_t w, size_t h,
|
|
|
|
bool fullscreen) {
|
|
|
|
Uint32 style = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
|
|
|
|
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);
|
|
|
|
|
|
|
|
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED,
|
2018-08-30 02:02:02 +02:00
|
|
|
SDL_WINDOWPOS_CENTERED, static_cast<int>(w), static_cast<int>(h), style);
|
2016-09-09 22:13:20 +02:00
|
|
|
if (window == nullptr) {
|
|
|
|
// Window creation failure is fatal
|
|
|
|
std::string sdlErrorStr = SDL_GetError();
|
|
|
|
throw std::runtime_error("SDL_CreateWindow failed: " + sdlErrorStr);
|
|
|
|
}
|
|
|
|
glcontext = SDL_GL_CreateContext(window);
|
|
|
|
if (glcontext == nullptr) {
|
|
|
|
// context creation failure is fatal
|
|
|
|
std::string sdlErrorStr = SDL_GetError();
|
|
|
|
throw std::runtime_error("SDL_GL_CreateContext failed: " + sdlErrorStr);
|
|
|
|
}
|
|
|
|
|
2018-06-29 23:15:23 +02:00
|
|
|
// This part sets an embedded icon to the window
|
|
|
|
// The source "image" is a 32-bit RGBA buffer exported from GIMP
|
|
|
|
// The full name of the format is "GIMP RGBA C-Source image dump"
|
|
|
|
#include "WindowIcon.hpp"
|
|
|
|
Uint32 rmask, gmask, bmask, amask;
|
|
|
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
|
|
|
// Big Endian
|
|
|
|
rmask = 0xff000000;
|
|
|
|
gmask = 0x00ff0000;
|
|
|
|
bmask = 0x0000ff00;
|
|
|
|
amask = 0x000000ff;
|
|
|
|
#else
|
|
|
|
// Little Endian
|
|
|
|
rmask = 0x000000ff;
|
|
|
|
gmask = 0x0000ff00;
|
|
|
|
bmask = 0x00ff0000;
|
|
|
|
amask = 0xff000000;
|
|
|
|
#endif
|
2018-07-27 22:53:35 +02:00
|
|
|
icon = SDL_CreateRGBSurfaceFrom(
|
|
|
|
static_cast<void*>(const_cast<unsigned char*>(windowIconData)),
|
|
|
|
windowIconWidth, windowIconHeight, 32, windowIconWidth * (32 / 8),
|
|
|
|
rmask, gmask, bmask, amask);
|
2018-06-29 23:15:23 +02:00
|
|
|
SDL_SetWindowIcon(window, icon);
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
SDL_ShowWindow(window);
|
2016-06-22 12:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void GameWindow::close() {
|
|
|
|
SDL_GL_DeleteContext(glcontext);
|
2018-06-29 23:15:23 +02:00
|
|
|
SDL_FreeSurface(icon);
|
2016-09-09 22:13:20 +02:00
|
|
|
SDL_DestroyWindow(window);
|
2016-06-22 12:29:39 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
window = nullptr;
|
2016-06-22 12:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void GameWindow::showCursor() {
|
|
|
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
2016-06-22 12:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void GameWindow::hideCursor() {
|
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
2016-06-22 12:29:39 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
glm::ivec2 GameWindow::getSize() const {
|
|
|
|
int x, y;
|
|
|
|
SDL_GL_GetDrawableSize(window, &x, &y);
|
2016-06-22 12:29:39 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
return glm::ivec2(x, y);
|
2016-06-22 12:29:39 +02:00
|
|
|
}
|