GameKit  0.0.1a
C++ gamedev tools
Window.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Window.cpp
5  *
6  * Description:
7  *
8  * Created: 20/12/2014 00:17:10
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/core/Window.hpp"
15 #include "gk/gl/GLCheck.hpp"
16 #include "gk/gl/OpenGL.hpp"
17 #include "gk/core/Exception.hpp"
18 
19 namespace gk {
20 
21 void Window::open(const std::string &caption, u16 width, u16 height) {
22  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
23  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
24 
25  m_window.reset(SDL_CreateWindow(caption.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN));
26  if(!m_window) {
27  throw EXCEPTION("Window initialization failed:", SDL_GetError());
28  }
29 
30  m_context.reset(SDL_GL_CreateContext(m_window.get()));
31  if(!m_context) {
32  throw EXCEPTION("OpenGL context creation failed:", SDL_GetError());
33  }
34 
35  m_size.x = width;
36  m_size.y = height;
37 
38  m_defaultView.reset(gk::FloatRect{0, 0, (float)width, (float)height});
40 
41  m_isOpen = true;
42 
43 #ifdef __MINGW32__
44 #ifdef USE_GLAD
45  if(!gladLoadGL()) {
46  throw EXCEPTION("glad init failed");
47  }
48 #else
49  if(glewInit() != GLEW_OK) {
50  throw EXCEPTION("glew init failed");
51  }
52 #endif
53 #endif
54 
55  glCheck(glEnable(GL_BLEND));
56  glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
57 
58  glCheck(glEnable(GL_TEXTURE_2D));
59 }
60 
61 void Window::clear() {
62  glCheck(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
63 }
64 
66  SDL_GL_SwapWindow(m_window.get());
67 }
68 
69 void Window::setVerticalSyncEnabled(bool enabled) {
70  if(SDL_GL_SetSwapInterval(enabled) < 0) {
71  DEBUG("Warning: Can't enable VSync");
72  }
73 }
74 
75 } // namespace gk
76 
SDL_GLContextPtr m_context
Definition: Window.hpp:49
#define DEBUG(args...)
Definition: Debug.hpp:31
void setVerticalSyncEnabled(bool enabled)
Definition: Window.cpp:69
unsigned short u16
Definition: IntTypes.hpp:22
View m_defaultView
Definition: Window.hpp:55
void setView(const View &view)
#define EXCEPTION(args...)
Definition: Exception.hpp:22
void display()
Definition: Window.cpp:65
void open(const std::string &caption, u16 width, u16 height)
Definition: Window.cpp:21
void reset(const FloatRect &rectangle)
Definition: View.cpp:59
SDL_WindowPtr m_window
Definition: Window.hpp:48
#define glCheck(expr)
Definition: GLCheck.hpp:18
bool m_isOpen
Definition: Window.hpp:53
Vector2u m_size
Definition: Window.hpp:51
void clear()
Definition: Window.cpp:61