jubilant-funicular
Window.cpp
1 #include <GL/glew.h>
2 #include <fstream>
3 
4 #ifdef NTA_USE_IMGUI
5  #include <imgui/imgui.h>
6  #include <imgui/imgui_impl_sdl.h>
7  #include <imgui/imgui_impl_opengl3.h>
8 #endif
9 
10 #include "nta/Window.h"
11 #include "nta/Logger.h"
12 #include "nta/IOManager.h"
13 #include "nta/utils.h"
14 
15 namespace nta {
16  WindowID Window::m_keyboard_focus(0);
17  std::mutex Window::m_keyboard_mutex;
18  WindowID Window::m_mouse_focus(0);
19  std::mutex Window::m_mouse_mutex;
21  }
22  Window::~Window() {
23  Logger::writeToLog("Destorying Window " + m_title + "...");
24  m_width = m_height = 0;
25  m_title = "";
26  SDL_DestroyWindow(m_window);
27  m_window = nullptr;
28  Logger::writeToLog("Destroyed Window");
29  }
30  SDL_Window* Window::getSDLWindow(GetSDLWindowKey key) const {
31  return m_window;
32  }
33  WindowID Window::getKeyboardFocus() {
34  std::lock_guard<std::mutex> g(m_keyboard_mutex);
35  return m_keyboard_focus;
36  }
37  void Window::setKeyboardFocus(const WindowID& win) {
38  std::lock_guard<std::mutex> g(m_keyboard_mutex);
39  m_keyboard_focus = win;
40  }
41  WindowID Window::getMouseFocus() {
42  std::lock_guard<std::mutex> g(m_mouse_mutex);
43  return m_mouse_focus;
44  }
45  void Window::setMouseFocus(const WindowID& win) {
46  std::lock_guard<std::mutex> g(m_mouse_mutex);
47  m_mouse_focus = win;
48  }
49  glm::vec2 Window::getDimensions() const {
50  return glm::vec2(m_width, m_height);
51  }
52  std::string Window::getTitle() const {
53  return m_title;
54  }
55  int Window::getWidth() const {
56  return m_width;
57  }
58  int Window::getHeight() const {
59  return m_height;
60  }
61  void Window::resize(int width, int height) {
62  SDL_SetWindowSize(m_window, width, height);
63  setDimensions(width, height);
64  }
65  void Window::setDimensions(int width, int height) {
66  m_width = width;
67  m_height = height;
68  }
69  void Window::createWindow(crstring title, int width, int height, int flags) {
70  Logger::writeToLog("Creating window with dimensions " + utils::to_string(width) +
71  " x " + utils::to_string(height) + ": " + title + "...");
73 
74  m_width = width;
75  m_height = height;
76  m_title = title;
77  int sdlFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
78  if (flags & BORDERLESS) {
79  sdlFlags |= SDL_WINDOW_BORDERLESS;
80  }
81  if (flags & FULLSCREEN) {
82  sdlFlags |= SDL_WINDOW_FULLSCREEN;
83  }
84  if (flags & INVISIBLE) {
85  sdlFlags |= SDL_WINDOW_HIDDEN;
86  }
87  if (flags & NOTRESIZEABLE) {
88  sdlFlags &= ~SDL_WINDOW_RESIZABLE;
89  }
90  if (flags & HIGHDPI) {
91  sdlFlags |= SDL_WINDOW_ALLOW_HIGHDPI;
92  }
93  m_window = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
94  m_width, m_height, sdlFlags);
95  check_error();
96  auto context = SDL_GL_CreateContext(m_window);
97  if (!context) {
98  Logger::writeErrorToLog("Failed to create an OpenGL context:\n\t"
99  + utils::to_string(SDL_GetError()),
100  GL_FAILURE);
101  }
102  glewExperimental = GL_TRUE; // I don't remember what this does or why it is needed
103 
104  const GLenum err = glewInit();
105  if (err != GLEW_OK) {
106  Logger::writeErrorToLog("Failed to initialize glew:\n\t"
107  + utils::to_string(glewGetErrorString(err)),
108  GL_FAILURE);
109  }
110  glBindFramebuffer(GL_FRAMEBUFFER, 0);
111  //SDL_GL_SetSwapInterval(1);
112  check_error();
113  #ifdef NTA_USE_IMGUI
114  ImGui_ImplOpenGL3_Init();
115  ImGui_ImplSDL2_InitForOpenGL(m_window, context);
116  #endif
117 
118  Logger::writeToLog("The window's ID is " + utils::to_string(getID()));
120  Logger::writeToLog("Created window using OpenGL version "
121  + utils::to_string(glGetString(GL_VERSION)));
122  }
123  void Window::swapBuffers() const {
124  #ifdef NTA_USE_IMGUI
125  ImGui::Render();
126  ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
127  #endif
128  SDL_GL_SwapWindow(m_window);
129  }
130 }
nta::Window::setDimensions
void setDimensions(int width, int height)
updates the window's stored dimensions
Definition: Window.cpp:65
nta::Window::m_title
std::string m_title
the title of the window
Definition: Window.h:42
nta::Window::getWidth
int getWidth() const
returns the width of the window
Definition: Window.cpp:55
nta::Logger::writeToLog
static void writeToLog(crstring entry)
writes an entry in the log
Definition: Logger.cpp:17
nta::Logger::unindent
static void unindent(size_t tab_size=TAB_SIZE)
unindents entries
Definition: Logger.cpp:46
nta::Window::resize
void resize(int width, int height)
resizes the window
Definition: Window.cpp:61
nta::GetSDLWindowKey
Key for unlocking the GetSDLWindow() "private" function of class Window.
Definition: Window.h:23
nta::Window::Window
Window()
constructor and destructor
Definition: Window.cpp:20
nta::Window::getDimensions
glm::vec2 getDimensions() const
returns the window's dimensions
Definition: Window.cpp:49
nta::Window::getID
WindowID getID() const
return the window's id
Definition: Window.h:56
nta::Window::swapBuffers
void swapBuffers() const
updates the screen
Definition: Window.cpp:123
nta::Window::getSDLWindow
SDL_Window * getSDLWindow(GetSDLWindowKey key) const
returns the underlying window
Definition: Window.cpp:30
nta
Definition: Animation2D.h:6
nta::Window::getTitle
std::string getTitle() const
returns the window's title
Definition: Window.cpp:52
nta::Window::m_window
SDL_Window * m_window
the window
Definition: Window.h:40
nta::Window::createWindow
void createWindow(crstring title, int width, int height, int flags=0)
creates a window
Definition: Window.cpp:69
nta::Window::m_width
int m_width
the dimensions of the window
Definition: Window.h:44
nta::utils::to_string
std::string to_string(const T &input, std::size_t precision=0)
converts input to a std::string
Definition: utils.h:36
nta::Logger::writeErrorToLog
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
Definition: Logger.cpp:31
nta::Window::getHeight
int getHeight() const
returns the height of the window
Definition: Window.cpp:58
nta::Logger::indent
static void indent(size_t tab_size=TAB_SIZE)
indents entries
Definition: Logger.cpp:43
nta::check_error
bool check_error()
Checks for and logs errors.
Definition: MyEngine.cpp:84