Rewrote ScreenManager push/pop handling again, should now be able to handle multiple...
[supertux.git] / src / supertux / screen_manager.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //                2014 Ingo Ruhnke <grumbel@gmail.com>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #ifndef HEADER_SUPERTUX_SUPERTUX_MAINLOOP_HPP
19 #define HEADER_SUPERTUX_SUPERTUX_MAINLOOP_HPP
20
21 #include <memory>
22 #include <cstddef>
23
24 #include "scripting/thread_queue.hpp"
25 #include "supertux/screen.hpp"
26
27 class Console;
28 class DrawingContext;
29 class MenuManager;
30 class MenuStorage;
31 class Screen;
32 class ScreenFade;
33
34 /**
35  * Manages, updates and draws all Screens, Controllers, Menus and the Console.
36  */
37 class ScreenManager
38 {
39 public:
40   ScreenManager();
41   ~ScreenManager();
42
43   void run(DrawingContext &context);
44   void quit(std::unique_ptr<ScreenFade> fade = {});
45   void set_speed(float speed);
46   float get_speed() const;
47   bool has_pending_fadeout() const;
48
49   /**
50    * requests that a screenshot be taken after the next frame has been rendered
51    */
52   void take_screenshot();
53
54   // push new screen on screen_stack
55   void push_screen(std::unique_ptr<Screen> screen, std::unique_ptr<ScreenFade> fade = {});
56   void pop_screen(std::unique_ptr<ScreenFade> fade = {});
57   void set_screen_fade(std::unique_ptr<ScreenFade> fade);
58
59   /// threads that wait for a screenswitch
60   scripting::ThreadQueue m_waiting_threads;
61
62 private:
63   void draw_fps(DrawingContext& context, float fps);
64   void draw(DrawingContext& context);
65   void update_gamelogic(float elapsed_time);
66   void process_events();
67   void handle_screen_switch();
68
69 private:
70   std::unique_ptr<MenuStorage> m_menu_storage;
71   std::unique_ptr<MenuManager> m_menu_manager;
72
73   float m_speed;
74   struct Action
75   {
76     enum Type { PUSH_ACTION, POP_ACTION, QUIT_ACTION };
77     Type type;
78     std::unique_ptr<Screen> screen;
79
80     Action(Type type_,
81            std::unique_ptr<Screen> screen_ = {}) :
82       type(type_),
83       screen(std::move(screen_))
84     {}
85   };
86
87   std::vector<Action> m_actions;
88
89   /// measured fps
90   float m_fps;
91   std::unique_ptr<ScreenFade> m_screen_fade;
92   std::vector<std::unique_ptr<Screen> > m_screen_stack;
93   bool m_screenshot_requested; /**< true if a screenshot should be taken after the next frame has been rendered */
94 };
95
96 #endif
97
98 /* EOF */