BARE2D
App.cpp
Go to the documentation of this file.
1 #include "App.hpp"
2 
3 #include "BARECEGUI.hpp"
4 
5 #include <SDL2/SDL.h>
6 #include <GL/gl.h>
7 
8 #include "BARE2DEngine.hpp"
9 #include "InputManager.hpp"
10 #include "Screen.hpp"
11 #include "Window.hpp"
12 
13 namespace BARE2D {
14 
16  init();
17  }
18 
20  if(m_isGameRunning) {
21  exitApp();
22  }
23  delete m_window;
24  delete m_inputManager;
25  delete m_timer;
26  m_screenList.release();
27  }
28 
29  void App::run() {
30  // Init if we haven't already
31  if(!m_isInited) {
32  init();
33  }
34 
35  // Start the gameloop!
36  m_isGameRunning = true;
37 
38  // Make sure we actually have an entry point
39  if(!m_screenList->getCurrentScreen())
40  return;
41 
42 // If we're running the debug build, make sure that we always log errors.
43 #ifdef DEBUG
45 #endif
46 
47  // Make sure to actually "enter" the entry screen
48  m_screenList->getCurrentScreen()->onEntry();
49 
50  // Gameloop
51  while(m_isGameRunning) {
53  while(m_timer->integrateFrame()) {
55  }
56  draw();
58  m_timer->endTimer();
59  }
60 
61  // Exit the app!
62  if(m_screenList) {
63  m_screenList->getCurrentScreen()->onExit();
64  m_screenList.reset(); // Destroy the screenList
65  }
66  }
67 
69  return m_screenList.get();
70  }
71 
73  return m_window;
74  }
75 
77  return m_inputManager;
78  }
79 
80  void App::init() {
81  // Make sure not to init twice
82  if(m_isInited)
84 
85  // Init the screen list
86  m_screenList = std::make_unique<ScreenList>();
87 
88  // Init the engine
89  BARE2D::init();
90 
91  // Uncomment this to require hardware acceleration: (defaults to allow accelerated OR accept non-accelerated)
92  // SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
93 
94  // Init the input manager
96 
97  // Init the window
98  m_window = new Window();
99  m_window->create(0);
100 
101  // Init the timer
102  m_timer = new Timer();
103  m_timer->setDeltaTimeLimit(1.0 / 60.0);
104 
105  // Make sure we don't double init by setting this var to true
106  m_isInited = true;
107  }
108 
109  void App::update(double dt) {
110  // Update everything
111  updateInput();
112 
113  switch(m_screenList->getCurrentScreen()->getState()) {
115  m_screenList->getCurrentScreen()->update(dt);
116  break;
118  m_screenList->moveToNextScreen();
119  break;
121  m_screenList->moveToPreviousScreen();
123  exitApp();
124  break;
125  default:
126  break;
127  }
128  }
129 
130  void App::draw() {
131  // Reset the viewport to the full window, just in case the window changed sizes or the viewport had been changed by, for example, an FBO
132  glViewport(0, 0, m_window->getWidth(), m_window->getHeight());
133 
134  // If the current screen is running, draw according to its protocols
135  if(m_screenList->getCurrentScreen()->getState() == ScreenState::RUNNING)
136  m_screenList->getCurrentScreen()->draw();
137  }
138 
141 
142  pollSDLInput();
143  }
144 
147 
148  SDL_Event event;
149  while(SDL_PollEvent(&event)) {
150  // Just check if the gui is initialized and there is a context that will be handling these events
151  if(gui->getContext())
152  gui->handleSDLEvent(event); // if there is, use it!
153  switch(event.type) {
154  case SDL_QUIT:
155  exitApp();
156  break;
157  case SDL_MOUSEMOTION:
158  m_inputManager->setMousePosition((float)event.motion.x, (float)event.motion.y);
159  break;
160  case SDL_KEYDOWN:
161  m_inputManager->pressKey(event.key.keysym.sym);
162  break;
163  case SDL_KEYUP:
164  m_inputManager->releaseKey(event.key.keysym.sym);
165  break;
166  case SDL_MOUSEBUTTONDOWN:
167  m_inputManager->pressKey(event.button.button);
168  break;
169  case SDL_MOUSEBUTTONUP:
170  m_inputManager->releaseKey(event.button.button);
171  break;
172  case SDL_MOUSEWHEEL:
174  break;
175  }
176  }
177  }
178 
179  void App::exitApp() {
180  m_isGameRunning = false;
181  }
182 
183 } // namespace BARE2D
BARE2D::App::m_isInited
bool m_isInited
Definition: App.hpp:47
BARE2D::App::m_screenList
std::unique_ptr< ScreenList > m_screenList
Definition: App.hpp:49
BARE2D
Definition: App.cpp:13
BARE2D::App::getScreenList
ScreenList * getScreenList()
Returns the screen list, where the program can add more screens, set entry points,...
Definition: App.cpp:68
BARE2D::App::run
void run()
Runs the program! Starts the update-draw loop.
Definition: App.cpp:29
BARE2D::InputManager::update
void update()
Updates the inputmanager. Should be called every frame.
Definition: InputManager.cpp:13
BARE2D::App::update
void update(double dt)
Updates input, output, etc. Only for internal use.
Definition: App.cpp:109
BARE2D::ScreenState::RUNNING
@ RUNNING
BARE2D::App::updateInput
void updateInput()
Updates any input, and throws it down the pipeline!
Definition: App.cpp:139
BARE2D::App::init
void init()
Initializes the applet and engine. Creates a window, held within this App. Only for internal use.
Definition: App.cpp:80
BARE2D::BARECEGUI::getContext
CEGUI::GUIContext * getContext()
Definition: BARECEGUI.cpp:277
BARE2D::Timer::endTimer
void endTimer()
To be called at the end of a frame.
Definition: Timer.cpp:34
Screen.hpp
BARECEGUI.hpp
BARE2D::App::m_timer
Timer * m_timer
Definition: App.hpp:53
BARE2D::BARECEGUI::handleSDLEvent
void handleSDLEvent(SDL_Event &evnt)
Handles and propagates input.
Definition: BARECEGUI.cpp:193
BARE2D::App::draw
void draw()
Draws the current screen, based on that screen's drawing protocol.
Definition: App.cpp:130
BARE2D::ScreenState::CHANGE_NEXT
@ CHANGE_NEXT
BARE2DEngine.hpp
BARE2D::Timer::getDeltaTime
double getDeltaTime()
Definition: Timer.cpp:24
BARE2D::App::m_isGameRunning
bool m_isGameRunning
Definition: App.hpp:46
BARE2D::BAREError::DOUBLE_INIT
@ DOUBLE_INIT
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::App::~App
~App()
Definition: App.cpp:19
Window.hpp
BARE2D::Timer::setDeltaTimeLimit
void setDeltaTimeLimit(double minDt)
Sets the maximum value of dt. Naturally, there is no minimum. This doesn't take away from accuracy....
Definition: Timer.cpp:44
BARE2D::BARECEGUI::getInstance
static BARECEGUI * getInstance()
Definition: BARECEGUI.cpp:14
BARE2D::Timer
Not only does the timer keep track of the current time and delta-times, it can do all the "fancy" cal...
Definition: Timer.hpp:11
BARE2D::App::pollSDLInput
void pollSDLInput()
Polls inputs from SDL and updates the inputmanager as appropriate.
Definition: App.cpp:145
BARE2D::App::getInputManager
InputManager * getInputManager()
Definition: App.cpp:76
BARE2D::InputManager::pressKey
void pressKey(unsigned int keyID)
Sets a key as 'pressed'.
Definition: InputManager.cpp:25
BARE2D::Timer::startTimer
void startTimer()
To be called at the start of a frame.
Definition: Timer.cpp:29
BARE2D::Window
It's a graphical window, holds the contexts for GL and SDL.
Definition: Window.hpp:13
BARE2D::App::App
App()
Definition: App.cpp:15
BARE2D::ScreenState::CHANGE_PREV
@ CHANGE_PREV
BARE2D::App::getWindow
Window * getWindow()
Definition: App.cpp:72
BARE2D::Timer::integrateFrame
bool integrateFrame()
IntegrateFrame gives an indication of whether we should be updatign again with dt or if we should exi...
Definition: Timer.cpp:14
InputManager.hpp
BARE2D::init
void init()
Inits the required systems used by BARE2D.
Definition: BARE2DEngine.cpp:10
BARE2D::ScreenList
Holds and connects all of the screens used in the program in a list. Keeps track of the previous scre...
Definition: ScreenList.hpp:13
BARE2D::GLErrorSeverity::LOW
@ LOW
BARE2D::App::m_inputManager
InputManager * m_inputManager
Definition: App.hpp:51
BARE2D::BARECEGUI
Definition: BARECEGUI.hpp:17
BARE2D::InputManager
This is the input manager. It does what you expect! Nearly completely copied from GLEngine,...
Definition: InputManager.hpp:12
BARE2D::InputManager::releaseKey
void releaseKey(unsigned int keyID)
Sets a key as 'released'.
Definition: InputManager.cpp:31
BARE2D::App::m_window
Window * m_window
Definition: App.hpp:55
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
App.hpp
BARE2D::ScreenState::EXIT_APPLICATION
@ EXIT_APPLICATION
BARE2D::Window::getWidth
unsigned int getWidth() const
Returns the width of the window.
Definition: Window.cpp:93
BARE2D::InputManager::setMousePosition
void setMousePosition(float x, float y)
Sets the mouse position to (x,y)
Definition: InputManager.cpp:37
BARE2D::initGLErrorCallback
void initGLErrorCallback(GLErrorSeverity minSeverity)
Initializes the GL Debug Message Callback function, and enables debug output straight from OpenGL.
Definition: BAREErrors.cpp:211
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
BARE2D::InputManager::setMouseScrollwheelPosition
void setMouseScrollwheelPosition(int position)
Sets the "position" of the scrollwheel - essentially, how much it has moved since the last frame.
Definition: InputManager.cpp:48
BARE2D::App::exitApp
void exitApp()
Calls the screen's exit function, and then cleans this class up for destruction.
Definition: App.cpp:179