Bubba-3D  0.9.0
Awesome game engine!
Window.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include "Window.h"
18 #include <glutil/glutil.h>
19 #include <Globals.h>
20 #include "Logger.h"
21 #include <JoystickTranslator.h>
22 
23 Window::Window(int width, int height, std::string title) {
24 # if defined(__linux__)
25  linux_initialize_cwd();
26 # endif // ! __linux__
27 
28  this->width = width;
29  this->height = height;
30 
31  Globals::set(Globals::WINDOW_HEIGHT,height);
32  Globals::set(Globals::WINDOW_WIDTH,width);
33 
34  sf::ContextSettings settings = sf::ContextSettings(32, 8, 0, 3, 3);
35 
36  settings.majorVersion = 4;
37  settings.minorVersion = 2;
38  settings.attributeFlags = sf::ContextSettings::Debug | sf::ContextSettings::Core;
39  window = new sf::Window(sf::VideoMode(width, height), title, sf::Style::Default, settings);
40  glEnable(GL_FRAMEBUFFER_SRGB);
41  initGL();
42 }
43 
44 Window::~Window()
45 {
46 }
47 
48 void Window::initGL() {
49  glewExperimental = GL_TRUE;
50  glewInit();
51  glGetError();
52 
53  /* Print information about OpenGL and ensure that we've got at a context
54  * that supports least OpenGL 3.0. Then setup the OpenGL Debug message
55  * mechanism.
56  */
57  startupGLDiagnostics();
58  setupGLDebugMessages();
59 }
60 
61 void Window::resize(unsigned int width, unsigned int height) {
62  Globals::set(Globals::Key::WINDOW_HEIGHT, height);
63  Globals::set(Globals::Key::WINDOW_WIDTH, width);
64  resizeMethod(width, height);
65 }
66 
67 void Window::start(unsigned int maxFPS) {
68  window->setFramerateLimit(maxFPS);
69  resize(this->width, this->height);
70  bool running = true;
71  sf::Vector2i pos = sf::Mouse::getPosition(*window);
72  Globals::set(Globals::Key::MOUSE_WINDOW_X, pos.x);
73  Globals::set(Globals::Key::MOUSE_WINDOW_Y, pos.y);
74 
75  if (idleMethod == nullptr)
76  Logger::logWarning("Renderer: No idle method specified.");
77  if (displayMethod == nullptr) {
78  Logger::logError("Renderer: No display method specified.");
79  return;
80  }
81 
82  sf::Clock sinceStart, sinceLastIdleMethodCall;
83 
84  while (running)
85  {
86  // handle events
87  sf::Event event;
88  while (window->pollEvent(event)) {
89  if (event.type == sf::Event::Closed) {
90  // end the program
91  running = false;
92  }
93  else if (event.type == sf::Event::MouseMoved) {
94  Globals::set(Globals::Key::MOUSE_WINDOW_X, event.mouseMove.x);
95  Globals::set(Globals::Key::MOUSE_WINDOW_Y, event.mouseMove.y);
96  }
97  else if (event.type == sf::Event::JoystickDisconnected || event.type == sf::Event::JoystickConnected) {
98  JoystickTranslator::getInstance()->updateMapping();
99  }
100  else if (event.type == sf::Event::Resized)
101  {
102  resize(event.size.width, event.size.height);
103  }
104 
105  }
106 
107  float dt = sinceLastIdleMethodCall.restart().asSeconds();
108  float totTime = sinceStart.getElapsedTime().asSeconds();
109  if (idleMethod != nullptr)
110  idleMethod(totTime, dt);
111  displayMethod(totTime, dt);
112 
113  // end the current frame (internally swaps the front and back buffers)
114  window->display();
115  }
116 }
117 
118 void Window::setResizeMethod(void(*resize)(int, int)) {
119  resizeMethod = resize;
120 }
121 
122 void Window::setDisplayMethod(void(*display)(float, float)) {
123  displayMethod = display;
124 }
125 
126 void Window::setIdleMethod(void(*idle)(float, float)) {
127  idleMethod = idle;
128 }
129 
130 sf::Window* Window::getWindow() {
131  return window;
132 }
void setIdleMethod(void(*idle)(float sinceStart, float sinceLastMethodCall))
Definition: Window.cpp:126
void setDisplayMethod(void(*display)(float sinceStart, float sinceLastMethodCall))
Definition: Window.cpp:122