NAP
app.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // External includes
8 #include <rtti/typeinfo.h>
9 #include <inputevent.h>
10 #include <utility/errorstate.h>
11 #include <nap/core.h>
12 #include <windowevent.h>
13 #include <mathutils.h>
14 
15 namespace nap
16 {
22  class NAPAPI BaseApp
23  {
24  RTTI_ENABLE()
25  public:
26  // Default Constructor
27  BaseApp(nap::Core& core);
28 
29  // Default Destructor
30  virtual ~BaseApp() = default;
31 
35  BaseApp(BaseApp&) = delete;
36  BaseApp& operator=(const BaseApp&) = delete;
37 
41  BaseApp(BaseApp&&) = delete;
42  BaseApp& operator=(BaseApp&&) = delete;
43 
51  virtual bool init(utility::ErrorState& error) { return true; }
52 
58  virtual void update(double deltaTime) { }
59 
60 
64  virtual void render() { }
65 
72  virtual bool shutdownRequested() { return true; }
73 
80  virtual int shutdown() { return 0; }
81 
85  void quit() { mQuit = true; }
86 
90  const nap::Core& getCore() const { return mCore; }
91 
95  nap::Core& getCore() { return mCore; }
96 
100  bool shouldQuit() const { return mQuit; }
101 
109  void capFramerate(bool value) { mCapFramerate = value; }
110 
119  void setFramerate(float framerate) { mRequestedFramerate = math::max<float>(framerate, 1.0f); }
120 
126  float getRequestedFramerate() const { return mRequestedFramerate; }
127 
133  float getActualFramerate() const { return mCore.getFramerate(); }
134 
139  bool framerateCapped() const { return mCapFramerate; }
140 
141  private:
142  bool mQuit = false; // When set to true the application will exit
143  nap::Core& mCore; // Core
144  float mRequestedFramerate = 60.0f; // Requested framerate, only applied when mCapFramerate is enabled.
145  bool mCapFramerate = false; // If the framerate should be capped.
146  };
147 
148 
154  class NAPAPI App : public BaseApp
155  {
156  RTTI_ENABLE(BaseApp)
157  public:
158  App(Core& core);
159 
164  virtual void inputMessageReceived(InputEventPtr inputEvent) { }
165 
170  virtual void windowMessageReceived(WindowEventPtr windowEvent) { }
171  };
172 }
nap::BaseApp::quit
void quit()
Definition: app.h:85
nap::BaseApp::setFramerate
void setFramerate(float framerate)
Definition: app.h:119
nap::BaseApp::update
virtual void update(double deltaTime)
Definition: app.h:58
nap::utility::ErrorState
Definition: errorstate.h:19
nap::App::windowMessageReceived
virtual void windowMessageReceived(WindowEventPtr windowEvent)
Definition: app.h:170
nap::BaseApp::getCore
const nap::Core & getCore() const
Definition: app.h:90
nap::BaseApp::getRequestedFramerate
float getRequestedFramerate() const
Definition: app.h:126
nap::Core
Definition: core.h:82
nap::BaseApp::framerateCapped
bool framerateCapped() const
Definition: app.h:139
nap::App::inputMessageReceived
virtual void inputMessageReceived(InputEventPtr inputEvent)
Definition: app.h:164
nap::BaseApp::shutdownRequested
virtual bool shutdownRequested()
Definition: app.h:72
nap::BaseApp::init
virtual bool init(utility::ErrorState &error)
Definition: app.h:51
nap::BaseApp::shouldQuit
bool shouldQuit() const
Definition: app.h:100
nap::BaseApp::getCore
nap::Core & getCore()
Definition: app.h:95
nap::BaseApp::shutdown
virtual int shutdown()
Definition: app.h:80
nap::BaseApp::capFramerate
void capFramerate(bool value)
Definition: app.h:109
nap::WindowEventPtr
std::unique_ptr< WindowEvent > WindowEventPtr
Definition: windowevent.h:189
nap::BaseApp::getActualFramerate
float getActualFramerate() const
Definition: app.h:133
nap
Definition: templateapp.h:17
nap::BaseApp::render
virtual void render()
Definition: app.h:64
nap::InputEventPtr
std::unique_ptr< nap::InputEvent > InputEventPtr
Definition: inputevent.h:414
nap::BaseApp
Definition: app.h:22