Pie Noon
An open source project by FPL.
 All Classes Pages
full_screen_fader.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FULL_SCREEN_FADER_H
16 #define FULL_SCREEN_FADER_H
17 
18 #include "common.h"
19 #include "precompiled.h"
20 
21 namespace fpl {
22 
23 namespace pie_noon {
24 
25 // Renders a full screen overlay that transitions to opaque then back to
26 // transparent.
28  public:
29  FullScreenFader(fplbase::Renderer* renderer);
30  ~FullScreenFader() {}
31 
32  // Starts the fade.
33  void Start(const WorldTime& time, const WorldTime& fade_time,
34  const mathfu::vec4& color, const bool fade_in);
35 
36  // Renders the fade overlay, returning true on the frame the overlay
37  // is opaque.
38  bool Render(const WorldTime& time);
39 
40  // Returns true when the fade is complete (overlay is transparent).
41  bool Finished(const WorldTime& time) const {
42  return !fade_in_ && CalculateOffset(time) >= 1.0f;
43  }
44 
45  void set_material(fplbase::Material* material) { material_ = material; }
46  fplbase::Material* material() const { return material_; }
47  void set_shader(fplbase::Shader* shader) { shader_ = shader; }
48  fplbase::Shader* shader() const { return shader_; }
49  void set_ortho_mat(const mathfu::mat4& ortho_mat) { ortho_mat_ = ortho_mat; }
50  const mathfu::mat4& ortho_mat() const { return ortho_mat_; }
51  void set_extents(const mathfu::vec2i& extents) { extents_ = extents; }
52  const mathfu::vec2i& extents() const { return extents_; }
53 
54  private:
55  // Calculate offset from the fade start time scaled by fade time.
56  inline float CalculateOffset(const WorldTime& time) const {
57  return static_cast<float>(time - start_time_) /
58  static_cast<float>(half_fade_time_);
59  }
60 
61  // Time when the fade started.
62  WorldTime start_time_;
63  // Half the complete fade time.
64  WorldTime half_fade_time_;
65  // Whether the fader is fading in, false indicates it's fading out.
66  bool fade_in_;
67  // Color of the overlay (the alpha component is ignored).
68  mathfu::vec4 color_;
69  // Projection matrix.
70  mathfu::mat4 ortho_mat_;
71  // Extents of the fade region.
72  mathfu::vec2i extents_;
73  fplbase::Renderer* renderer_;
74  // Material used to render the overlay.
75  fplbase::Material* material_;
76  // Shader used to render the overlay material.
77  fplbase::Shader* shader_;
78 };
79 
80 } // namespace pie_noon
81 } // namespace fpl
82 
83 #endif // FULL_SCREEN_FADER_H
Definition: full_screen_fader.h:27