]> git.localhorst.tv Git - tacos.git/blob - src/graphics/window.cpp
isolate some GL stuff
[tacos.git] / src / graphics / window.cpp
1 #include "window.hpp"
2
3 #include "../app/error.hpp"
4
5 #include <stdexcept>
6 #include <string>
7 #include <GL/glew.h>
8
9
10 namespace tacos {
11
12 Window::Window(int width, int height) {
13         window = SDL_CreateWindow(
14                 "tacos", // title
15                 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, // position
16                 width, height, // size
17                 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE // flags
18         );
19         if (!window) {
20                 throw SDLError("SDL_CreateWindow");
21         }
22         context = SDL_GL_CreateContext(window);
23         if (!context) {
24                 SDL_DestroyWindow(window);
25                 throw SDLError("SDL_GL_CreateContext");
26         }
27         glewExperimental = GL_TRUE;
28         GLenum err = glewInit();
29         if (err != GLEW_OK) {
30                 SDL_GL_DeleteContext(context);
31                 SDL_DestroyWindow(window);
32                 std::string msg("glewInit: ");
33                 msg += reinterpret_cast<const char *>(glewGetErrorString(err));
34                 throw std::runtime_error(msg);
35         }
36 }
37
38 Window::~Window() noexcept {
39         SDL_GL_DeleteContext(context);
40         SDL_DestroyWindow(window);
41 }
42
43 void Window::Flip() noexcept {
44         SDL_GL_SwapWindow(window);
45 }
46
47
48 }