6 #include <imgui/imgui.h>
7 #include <imgui/imgui_impl_sdl.h>
8 #include <imgui/imgui_impl_opengl3.h>
11 #include "nta/ScreenManager.h"
12 #include "nta/WindowManager.h"
13 #include "nta/CallbackManager.h"
14 #include "nta/Logger.h"
15 #include "nta/utils.h"
18 std::mutex ScreenManager::m_window_creation_lock;
21 std::lock_guard<std::mutex> g(m_window_creation_lock);
44 " (\"" + newScreen->
getName() +
"\") to ScreenManager...");
57 if (utils::in_range<int>(newIndex, 0,
m_screens.size()-1)) {
73 }
else if (newIndex == -1) {
81 case SDL_WINDOWEVENT:
return m_window->
getID() ==
event.window.windowID;
82 case SDL_KEYDOWN:
case SDL_KEYUP:
return m_window->hasKeyboardFocus();
83 case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEWHEEL:
return m_window->hasMouseFocus();
98 std::queue<SDL_Event> skipped_events;
101 while (SDL_PollEvent(&e)) {
103 skipped_events.push(e);
108 case SDL_WINDOWEVENT: {
109 switch(e.window.event) {
110 case SDL_WINDOWEVENT_RESIZED:
111 glViewport(0, 0, e.window.data1, e.window.data2);
114 case SDL_WINDOWEVENT_ENTER:
115 Window::setMouseFocus(e.window.windowID);
117 case SDL_WINDOWEVENT_FOCUS_GAINED:
118 Window::setKeyboardFocus(e.window.windowID);
120 case SDL_WINDOWEVENT_CLOSE:
129 ImGui_ImplSDL2_ProcessEvent(&e);
132 while (!skipped_events.empty()) {
133 SDL_PushEvent(&skipped_events.front());
134 skipped_events.pop();
143 Screen* currScreen =
nullptr;
150 while (currScreen->
getState() == ScreenState::RUNNING) {
156 ImGui_ImplOpenGL3_NewFrame();
166 case ScreenState::SWITCH:
switchScreen(currScreen->getNextIndex());
break;
168 case ScreenState::SWITCH_X:
switchScreen(currScreen->getXIndex());
break;
172 IMPOSSIBLE_BEHAVIOR);
break;
std::string getName() const
gets name of screen
void setDimensions(int width, int height)
updates the window's stored dimensions
void run(void *initFocusData=nullptr)
void setManager(ScreenManager *manager, SetManagerKey key)
Sets the manager of this screen.
void setWindow(crstring title, SetWindowKey key)
sets the window to associate with this screen
Key unlocking the setManager() "private" function of class Screen.
void setFontFolder(const utils::Path &fldr)
Sets the font folder.
void addScreen(Screen *newScreen, int escIndex=-1, int xIndex=-1, crstring title="")
adds a screen and sets some of its properties
virtual void render()=0
renders screen
Info passed to Screen::onFocus.
void destroy()
Deletes all data (does not reset folder paths)
void setGLSLFolder(const utils::Path &fldr)
Sets the shader folder.
int m_currScreen
the index of the currently active screen
~ScreenManager()
basic destructor
void setResourceFolders(const utils::Path &tex_fldr, const utils::Path &glsl_fldr, const utils::Path &font_fldr)
Sets the location of textures, shaders, and fonts.
void setIndices(int index, int escIndex, int xIndex, SetIndicesKey key)
sets various screen indices
virtual void offFocus()
called when the screen is no longer active
static void writeToLog(crstring entry)
writes an entry in the log
static std::mutex m_event_lock
Lock for access the event queue.
FPSLimiter m_limiter
used to cap the FPS
virtual void onFocus(const ScreenSwitchInfo &info)
called when the screen becomes active
void destroy()
destroys screens
A (case-sensitive) Path in a file system.
static void unindent(size_t tab_size=TAB_SIZE)
unindents entries
static void increment_frame()
Informs CallbackManager that a frame has passed.
ScreenManager(crstring title, float maxFPS, int width=640, int height=480)
sets the max fps and the window to use
Key for unlocking the GetSDLWindow() "private" function of class Window.
std::vector< Screen * > m_screens
the screens
ScreenState getState() const
returns state of screen
WindowID getID() const
return the window's id
SDL_Window * getSDLWindow(GetSDLWindowKey key) const
returns the underlying window
float getFPS() const
gets most recently calculated fps
Key unlocking the setWindow() "private" function of class Screen.
void setTextureFolder(const utils::Path &fldr)
Sets the texture folder.
Screen * getCurrScreen() const
returns the active screen
bool owns_event(const SDL_Event &event) const
std::string getTitle() const
returns the window's title
void update_input()
Updates the state of m_input.
Represents a game screen.
virtual void init()=0
initializes the screen
Key unlocking the setIndices() "private" function of class Screen.
ContextData m_context_data
GLTextures and GLSLPrograms and whatnot.
Window * m_window
the main window used by the manager
InputManager m_input
Keeps track of all input received in this window.
long double end()
ends fps calculations, delaying if necessary to cap fps
virtual void update()=0
updates screen
static Window * getWindow(crstring windowTitle, int width=640, int height=480, int flags=0)
Gets a Window with specified title, width, height, and flags.
void setMaxFPS(float maxFPS)
sets maximum allowed fps
void switchScreen(int newIndex)
switches the to a new screen
virtual void begin()
begins timer
std::string to_string(const T &input, std::size_t precision=0)
converts input to a std::string
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
int getEscIndex() const
gets various screen indices
static void handle_errors()
Handles all outstanding errors.
static void indent(size_t tab_size=TAB_SIZE)
indents entries
float getFPS() const
returns the current fps
bool check_error()
Checks for and logs errors.