zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Popup_State.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib 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  * zenilib 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 zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef POPUP_STATE_H
19 #define POPUP_STATE_H
20 
21 #include <Zeni/Colors.h>
23 #include <Zeni/Chronometer.hxx>
24 #include <Zeni/Sound.hxx>
25 #include <Zeni/Sound_Renderer_AL.h>
26 #include <Zeni/Sound_Source_Pool.h>
27 #include <Zeni/Widget_Gamestate.h>
28 #include <Zeni/Quit_Event.h>
29 
30 namespace Zeni {
31 
32  class ZENI_REST_DLL Popup_State : public Widget_Gamestate {
33  Popup_State(const Popup_State &);
34  Popup_State operator=(const Popup_State &);
35 
36  public:
38  : Widget_Gamestate(std::make_pair(Point2f(), Point2f(800.0f, 600.0f))),
39  m_gamestate(get_Game().get_top())
40  {
41  }
42 
43  protected:
44  void on_push() {
45  if(m_gamestate.is_pausable()) {
49  get_Core().set_screen_saver(true);
50  }
51 
53 
55  }
56 
57  void on_pop() {
59 
60  if(m_gamestate.is_pausable()) {
64  get_Core().set_screen_saver(false);
65  }
66  }
67 
68 #ifndef ANDROID
70  if(event.keysym.sym == SDLK_ESCAPE && event.state == SDL_PRESSED)
71  get_Game().pop_state();
72  else
73  m_widgets.on_event(event);
74  }
75 #endif
76 
77  void perform_logic() {
79 
80  if(!m_gamestate.is_pausable())
81  m_gamestate.perform_logic();
82  }
83 
84  void prerender() {
86 
87  m_gamestate.prerender();
88  }
89 
90  void render() {
91  m_gamestate.render();
92 
93  Video &vr = get_Video();
94  vr.clear_depth_buffer();
95  vr.set_2d(std::make_pair(Point2f(), Point2f(800.0f, 600.0f)));
96 
97  Color color = get_Colors()["title_bg"];
98  color.a = 0.5f;
100  Vertex2f_Color(Point2f(0.0f, 600.0f), color),
101  Vertex2f_Color(Point2f(800.0f, 600.0f), color),
102  Vertex2f_Color(Point2f(800.0f, 0.0f), color));
103 
104  vr.render(quad);
105 
106  vr.set_2d(get_virtual_window(), fix_aspect_ratio());
107 
108  m_widgets.render();
109  }
110 
112  };
113 
114  class ZENI_REST_DLL Popup_Menu_State : public Popup_State {
116  Popup_Menu_State operator=(const Popup_Menu_State &);
117 
118  class ZENI_REST_DLL Continue_Button : public Text_Button {
119  Continue_Button(const Continue_Button &);
120  Continue_Button operator=(const Continue_Button &);
121 
122  public:
123  Continue_Button(const Gamestate &gamestate)
124  : Text_Button(Point2f(200.0f, 150.0f), Point2f(600.0f, 210.0f),
125  "system_36_800x600", "Continue Game"),
126  m_gamestate(gamestate)
127  {
128  }
129 
130  void on_accept() {
131  get_Game().pop_state();
132  }
133 
134  private:
135  Gamestate m_gamestate;
136  } continue_button;
137 
138  class ZENI_REST_DLL Menu_Button : public Text_Button {
139  Menu_Button(const Menu_Button &);
140  Menu_Button operator=(const Menu_Button &);
141 
142  public:
143  Menu_Button()
144  : Text_Button(Point2f(200.0f, 230.0f), Point2f(600.0f, 290.0f),
145  "system_36_800x600", "Back to Menu")
146  {
147  }
148 
149  void on_accept() {
150  get_Game().pop_state();
151  get_Game().pop_state();
152  }
153  } menu_button;
154 
155  public:
156 #ifndef ANDROID
157  class ZENI_REST_DLL Configure_Video_Button : public Text_Button {
159  Configure_Video_Button & operator=(const Configure_Video_Button &);
160 
161  public:
162  Configure_Video_Button(const Point2f &upper_left, const Point2f &lower_right)
163  : Text_Button(upper_left, lower_right, "system_36_800x600", "Configure Video")
164  {
165  }
166 
167  void on_accept() {
169  }
170  } configure_video_button;
171 #endif
172 
173  class ZENI_REST_DLL Sound_Check_Box : public Check_Box {
175  Sound_Check_Box operator=(const Sound_Check_Box &);
176 
177  public:
178  Sound_Check_Box(const Point2f &upper_left, const Point2f &lower_right)
179  : Check_Box(upper_left, lower_right,
180  !get_Sound().is_listener_muted())
181  {
182  if(dynamic_cast<Sound_Renderer_NULL *>(&get_Sound().get_Renderer())) {
183  set_checked(false);
184  set_editable(false);
185  }
186  }
187 
188  void on_accept() {
190  get_Sound().set_listener_muted(!is_checked());
191  }
192 
193  void render_impl() const {
194  Colors &cr = get_Colors();
195  Video &vr = get_Video();
196  Font &font = get_Fonts()["system_36_800x600"];
197 
198  const Point2f &ul = get_upper_left();
199  const Point2f &lr = get_lower_right();
200  Color bgc = cr["title_bg"];
201  bgc.a = 0.5f;
202 
203  const float tw = font.get_text_width("Sound");
204  const float tl = 0.2f * tw;
205  const float tr = 1.4f * tw;
207  Vertex2f_Color(Point2f(ul.x, lr.y), bgc),
208  Vertex2f_Color(Point2f(lr.x + tr, lr.y), bgc),
209  Vertex2f_Color(Point2f(lr.x + tr, ul.y), bgc));
210  vr.render(bg);
211 
213 
214  const Color fgc = cr["default_button_bg_normal"];
215  font.render_text("Sound",
216  Point2f(get_lower_right().x + 15.0f,
217  0.5f * (get_lower_right().y + get_upper_left().y - font.get_text_height())),
218  fgc);
219 
220  if(dynamic_cast<Sound_Renderer_NULL *>(&get_Sound().get_Renderer())) {
221  const Line_Segment<Vertex2f_Color> ns(Vertex2f_Color(Point2f(get_lower_right().x + tl, 0.5f * (get_lower_right().y + get_upper_left().y)), fgc),
222  Vertex2f_Color(Point2f(get_lower_right().x + tr, 0.5f * (get_lower_right().y + get_upper_left().y)), fgc));
223  vr.render(ns);
224  }
225  }
226  } sound_check_box;
227 
228  class ZENI_REST_DLL Quit_Button : public Text_Button {
229  Quit_Button(const Quit_Button &);
230  Quit_Button & operator=(const Quit_Button &);
231 
232  public:
233  Quit_Button(const Point2f &upper_left, const Point2f &lower_right)
234  : Text_Button(upper_left, lower_right, "system_36_800x600", "Quit")
235  {
236  }
237 
238  void on_accept() {
239  throw Quit_Event();
240  }
241  } quit_button;
242 
244  : continue_button(m_gamestate),
245 #ifndef ANDROID
246  configure_video_button(Point2f(200.0f, 310.0f), Point2f(600.0f, 370.0f)),
247 #endif
248  sound_check_box(Point2f(200.0f, 390.0f), Point2f(260.0f, 450.0f)),
249  quit_button(Point2f(400.0f, 390.0f), Point2f(600.0f, 450.0f))
250  {
251  m_widgets.lend_Widget(continue_button);
252 #ifndef ANDROID
253  m_widgets.lend_Widget(configure_video_button);
254 #endif
255  m_widgets.lend_Widget(sound_check_box);
256  m_widgets.lend_Widget(menu_button);
257  m_widgets.lend_Widget(quit_button);
258  }
259 
260  void on_push() {
262 
263  if(get_Game().size() == 1u)
264  m_widgets.unlend_Widget(menu_button);
265  }
266 
267  void on_pop() {
268  if(get_Game().size() == 0u)
269  m_widgets.lend_Widget(menu_button);
270 
272  }
273  };
274 
275  class ZENI_REST_DLL Popup_Menu_State_Factory {
276  public:
278 
280  return new Popup_Menu_State;
281  }
282  };
283 
284  class ZENI_REST_DLL Popup_Pause_State : public Popup_State {
286  Popup_Pause_State operator=(const Popup_Pause_State &);
287 
288  public:
290  {
291  }
292 
293 #if SDL_VERSION_ATLEAST(2,0,0)
294  void on_window_event(const SDL_WindowEvent &event) {
296  get_Game().pop_state();
297 
298  Gamestate_Base::on_window_event(event);
299  }
300 #elif !defined(ANDROID)
301  void on_active(const SDL_ActiveEvent &event) {
302  if(event.gain && (event.state & SDL_APPINPUTFOCUS))
303  get_Game().pop_state();
304 
306  }
307 #endif
308 
309  void perform_logic() {
310 #ifndef ANDROID
311  if(m_gamestate.is_pausable())
312  SDL_Delay(5);
313 #endif
314  }
315 
316  void render() {
317  if(m_gamestate.is_pausable()) {
319 
320  Font &font = get_Fonts()["title"];
321 
322  font.render_text("Paused", Point2f(400.0f, 300.0f - 0.5f * font.get_text_height()), get_Colors()["title_text"], ZENI_CENTER);
323  }
324  else
325  m_gamestate.render();
326  }
327  };
328 
329  class ZENI_REST_DLL Popup_Pause_State_Factory {
330  public:
332 
333  virtual Popup_Pause_State * operator()() {
334  return new Popup_Pause_State;
335  }
336  };
337 
338 }
339 
340 #endif
virtual float get_text_width(const String &text) const =0
Get the width of text rendering using this font. Approximately text_height * text.length() / 2.0f.
Sound_Check_Box(const Point2f &upper_left, const Point2f &lower_right)
Definition: Popup_State.h:178
Controllers & get_Controllers()
Get access to the singleton.
Definition: Controllers.cpp:59
void on_event(const SDL_Event &event)
Definition: Gamestate.hxx:80
void pause_all()
Pause all Sound_Sources.
GLuint color
Definition: glew.h:7185
An Abstraction of a Line.
Definition: Line_Segment.h:36
virtual void on_accept()
Called when the cursor is released inside the button.
Definition: Widget.cpp:315
The Video Rendering Singleton.
Definition: Video.h:71
Sound_Source_Pool & get_Sound_Source_Pool()
Get access to the singleton.
An Abstraction of a Vertex in 2-space, colored.
Definition: Vertex2f.h:80
Colors & get_Colors()
Get access to the singleton.
Definition: Colors.cpp:51
GLclampf f
Definition: glew.h:3390
A Check Box.
Definition: Widget.h:501
void on_accept()
Called when the cursor is released inside the button.
Definition: Popup_State.h:167
Gamestate with some helpers for dealing with Widgets.
void perform_logic()
Then perform logic. Called by Game as part of the main gameloop.
void reset_vibration_all()
Set vibration for all controllers to &lt;0,0&gt;
EGLSurface EGLint x
Definition: eglext.h:293
virtual void clear_depth_buffer()=0
Can reset the depth buffer at any time if necessary.
Gamestate pop_state()
Pop a Gamestate off the stack. Pop the current Gamestate with caution. All members will instantly bec...
Definition: Game.cpp:90
A Color database read in from a file.
Definition: Colors.h:50
Sound_Renderer & get_Renderer()
Get the current Sound_Renderer.
Definition: Sound.hxx:30
static void pause_all()
Pause all Chronometer&lt;TIME&gt; objects.
An Abstraction of a Quadrilateral.
Definition: Quadrilateral.h:37
Configure zenilib.xml.
void on_push()
Called when the Gamestate is pushed onto the stack in Game.
Definition: Popup_State.h:260
void prerender()
No actual rendering, but any logic strictly necessary for rendering.
Definition: Popup_State.h:84
Game & get_Game()
Get access to the singleton.
Definition: Game.cpp:58
void on_accept()
Called when the cursor is released inside the button.
Definition: Popup_State.h:238
void unpause_all()
Unpause all paused Sound_Sources.
void perform_logic()
Then perform logic. Called by Game as part of the main gameloop.
Definition: Popup_State.h:309
virtual void prerender()
No actual rendering, but any logic strictly necessary for rendering.
Definition: Gamestate.h:103
Sound & get_Sound()
Get access to the singleton.
Definition: Sound.cpp:220
Quit_Button(const Point2f &upper_left, const Point2f &lower_right)
Definition: Popup_State.h:233
Keyboard button event structure (event.key.*)
Definition: SDL_events.h:176
virtual Popup_Pause_State * operator()()
Definition: Popup_State.h:333
void perform_logic()
Then perform logic. Called by Game as part of the main gameloop.
Definition: Popup_State.h:77
ALuint u
Definition: alMain.h:58
void set_screen_saver(const bool &enabled)
Enable/Disable the screen saver.
Definition: Core.cpp:99
A smartpointer for a Gamestate_Base.
Definition: Gamestate.h:169
float a
Definition: Color.h:69
virtual void render(const Renderable &renderable)=0
Render a Renderable.
void push_state(const Gamestate &state)
Push a new Gamestate onto the stack.
Definition: Game.cpp:76
void set_2d()
Set the default 2D view filling the entire display area.
Definition: Video.hxx:93
void on_push()
Called when the Gamestate is pushed onto the stack in Game.
Definition: Popup_State.h:44
DECLSPEC void SDLCALL SDL_Delay(Uint32 ms)
Wait a specified number of milliseconds before returning.
Definition: SDL_systimer.c:70
Font Abstraction.
Definition: Font.h:70
void on_pop()
Called when the Gamestate is popped off the stack in Game.
SDL_Keysym keysym
Definition: SDL_events.h:185
void set_listener_muted(const bool &muted)
Set whether the listener is muted.
Definition: Sound.cpp:114
void on_key(const SDL_KeyboardEvent &event)
Override this input callback in your Gamestates. See SDL documentation for details.
Definition: Popup_State.h:69
void render()
Then render. Called by Game as part of the main gameloop.
Definition: Popup_State.h:316
A class to signal that the program is quitting.
Definition: Quit_Event.h:37
void on_pop()
Called when the Gamestate is popped off the stack in Game.
Definition: Popup_State.h:57
A Text Button Widget.
Definition: Widget.h:492
Window state change event data (event.window.*)
Definition: SDL_events.h:160
virtual void render_impl() const
Definition: Widget.cpp:40
void render()
Then render. Called by Game as part of the main gameloop.
Definition: Popup_State.h:90
EGLSurface EGLint EGLint y
Definition: eglext.h:293
void on_push()
Called when the Gamestate is pushed onto the stack in Game.
void on_pop()
Called when the Gamestate is popped off the stack in Game.
Definition: Popup_State.h:267
Gamestate m_gamestate
Definition: Popup_State.h:111
virtual void on_active(const SDL_ActiveEvent &event)
Override this input callback in your Gamestates. See SDL documentation for details.
Definition: Gamestate.cpp:143
Core & get_Core()
Get access to the singleton.
Definition: Core.cpp:71
SDL_Keycode sym
Definition: SDL_keyboard.h:50
void on_active(const SDL_ActiveEvent &event)
Override this input callback in your Gamestates. See SDL documentation for details.
Definition: Popup_State.h:301
void on_accept()
Called when the cursor is released inside the button.
Definition: Popup_State.h:188
Fonts & get_Fonts()
Get access to the singleton.
Definition: Fonts.cpp:60
Video & get_Video()
Get access to the singleton.
Definition: Video.cpp:149
float get_text_height() const
Get the height of the font. The width is usually half the height, by default.
Definition: Font.hxx:29
#define SDL_PRESSED
Definition: SDL_events.h:50
virtual Popup_Menu_State * operator()()
Definition: Popup_State.h:279
A 2D Point represented with floats.
Definition: Coordinate.h:98
static void unpause_all()
Unpause all Chronometer&lt;TIME&gt; objects.
Color.
Definition: Color.h:41
cl_event event
Definition: glew.h:3556
virtual void render_text(const String &text, const Point2f &position, const Color &color, const JUSTIFY &justify=ZENI_DEFAULT_JUSTIFY) const =0
Render text at screen position (x, y), with justification JUSTIFY.
GLsizei size
Definition: gl2ext.h:1467
Configure_Video_Button(const Point2f &upper_left, const Point2f &lower_right)
Definition: Popup_State.h:162