BARE2D
Window.cpp
Go to the documentation of this file.
1 #include "Window.hpp"
2 
3 #include <GL/glew.h>
4 
5 #include "Logger.hpp"
6 
7 namespace BARE2D {
8 
10  {
11  }
12 
14  {
15  SDL_GL_DeleteContext(m_GLContext);
16  SDL_DestroyWindow(m_SDLWindow);
17  SDL_Quit();
18  }
19 
20  void Window::create(unsigned int flags) {
21  // Since we are creating an OpenGL application, this is mandatory.
22  Uint32 windowFlags = SDL_WINDOW_OPENGL;
23 
24  windowFlags |= flags;
25 
26  // Set some SDL attributes, such as the minimum version required, if core profiles are enabled, etc.
27  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3.2); // Sets the OpenGL context attribute (minimum version) to 3.2
28  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4.5); // Sets the OpenGL context attribute (maximum version) to 4.5
29  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // Set the OpenGL context to be the core version
30 
31  // Actually create the window (or tell SDL to create it, anyways)
32  m_SDLWindow = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_width, m_height, windowFlags);
33 
34  // Check if creation was successful
35  if(m_SDLWindow == nullptr) {
37  }
38 
39  // This enables glew's experimental mode, where we can use core profiles, etc.
40  glewExperimental = GL_TRUE;
41 
42  // Now create the GL context
43  m_GLContext = SDL_GL_CreateContext(m_SDLWindow);
44 
45  // Check if creation was successful
46  if(m_GLContext == nullptr) {
48  }
49 
50  // Now that we actually have a GL context, we need to set it up - done with the help of GLEW!
51  GLenum err = glewInit();
52 
53  // Check for errors
54  if(err != GLEW_OK) {
56  }
57 
58  // Now log the OpenGL verion used, just a useful piece of info
59  std::printf("*** OpenGL Version: %s ***\n", glGetString(GL_VERSION));
60 
61  // Clear it, baby
62  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
63 
64  // Set VSync on
65  SDL_GL_SetSwapInterval(0);
66 
67  // Enable alpha blend
68  glEnable(GL_BLEND);
69  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
70  }
71 
72  void Window::setTitle(std::string newTitle) {
73  m_title = newTitle;
74  SDL_SetWindowTitle(m_SDLWindow, newTitle.c_str());
75  }
76 
77  void Window::setSize(unsigned int width, unsigned int height) {
78  m_width = width;
79  m_height = height;
80  if(m_SDLWindow) {
81  SDL_SetWindowSize(m_SDLWindow, width, height);
82 
83  // Reset the viewport to the full window
84  glViewport(0, 0, width, height);
85  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
86  }
87  }
88 
90  SDL_GL_SwapWindow(m_SDLWindow);
91  }
92 
93  unsigned int Window::getWidth() const {
94  return m_width;
95  }
96 
97  unsigned int Window::getHeight() const {
98  return m_height;
99  }
100 
101 }
102 
BARE2D::BAREError::GLEW_FAILURE
@ GLEW_FAILURE
BARE2D::Window::setSize
void setSize(unsigned int width, unsigned int height)
Sets the size of the window (defaults to 600x400)
Definition: Window.cpp:77
BARE2D
Definition: App.cpp:13
BARE2D::Window::m_title
std::string m_title
Definition: Window.hpp:79
BARE2D::Window::Window
Window()
Definition: Window.cpp:9
BARE2D::BAREError::SDL_FAILURE
@ SDL_FAILURE
BARE2D::Window::m_width
unsigned int m_width
Definition: Window.hpp:78
BARE2D::Window::setTitle
void setTitle(std::string newTitle)
Sets the title of the window.
Definition: Window.cpp:72
BARE2D::Window::create
void create(unsigned int flags)
This is a graphical window. It holds information about the GL context, as well as the SDL window cont...
Definition: Window.cpp:20
BARE2D::Window::m_SDLWindow
SDL_Window * m_SDLWindow
Definition: Window.hpp:75
BARE2D::Window::~Window
~Window()
Definition: Window.cpp:13
Window.hpp
BARE2D::Window::m_height
unsigned int m_height
Definition: Window.hpp:78
BARE2D::Window::swapBuffer
void swapBuffer()
Swaps the buffer of the SDL window - used to draw to one while the other displays,...
Definition: Window.cpp:89
BARE2D::Window::getWidth
unsigned int getWidth() const
Returns the width of the window.
Definition: Window.cpp:93
BARE2D::throwFatalError
void throwFatalError(BAREError err, std::string message)
Throws an error (fatal). Also calls displayErrors and exits the program.
Definition: BAREErrors.cpp:178
BARE2D::Window::getHeight
unsigned int getHeight() const
Returns the height of the window.
Definition: Window.cpp:97
Logger.hpp
BARE2D::Window::m_GLContext
SDL_GLContext m_GLContext
Definition: Window.hpp:76