Antkeeper  0.0.1
splash-state.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
21 #include "game/game.hpp"
27 #include <engine/debug/log.hpp>
28 #include <engine/math/vector.hpp>
31 #include <engine/gl/pipeline.hpp>
32 
34  game_state(ctx)
35 {
36  debug::log_trace("Entering splash state...");
37 
38  const math::fvec2 viewport_size = math::fvec2(ctx.window->get_viewport_size());
39  const math::fvec2 viewport_center = viewport_size * 0.5f;
40 
41  // Load splash texture
42  auto splash_texture = ctx.resource_manager->load<gl::texture_2d>("splash.tex");
43 
44  // Get splash texture dimensions
45  const auto& splash_dimensions = splash_texture->get_image_view()->get_image()->get_dimensions();
46 
47  // Construct splash billboard material
48  splash_billboard_material = std::make_shared<render::material>();
49  splash_billboard_material->set_blend_mode(render::material_blend_mode::translucent);
50  splash_billboard_material->set_shader_template(ctx.resource_manager->load<gl::shader_template>("ui-element-textured.glsl"));
51  splash_billboard_material->set_variable("background", std::make_shared<render::matvar_texture_2d>(1, splash_texture));
52 
53  auto splash_tint = std::make_shared<render::matvar_fvec4>(1, math::fvec4{1, 1, 1, 0});
54  splash_billboard_material->set_variable("tint", splash_tint);
55 
56  // Construct splash billboard
57  splash_billboard.set_material(splash_billboard_material);
58  splash_billboard.set_scale({static_cast<float>(splash_dimensions[0]) * 0.5f, static_cast<float>(splash_dimensions[1]) * 0.5f, 1.0f});
59  splash_billboard.set_translation({std::round(viewport_center.x()), std::round(viewport_center.y()), 0.0f});
60 
61  // Add splash billboard to UI scene
62  ctx.ui_scene->add_object(splash_billboard);
63 
64  // Load animation timing configuration
65  const float splash_fade_in_duration = 0.5;
66  const float splash_duration = 2.0;
67  const float splash_fade_out_duration = 0.5;
68 
69  // Construct splash fade in animation
70  splash_fade_in_animation.set_interpolator(ease<float>::out_cubic);
71  animation_channel<float>* splash_fade_in_opacity_channel = splash_fade_in_animation.add_channel(0);
72  splash_fade_in_opacity_channel->insert_keyframe({0.0f, 0.0f});
73  splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration, 1.0f});
74  splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration + splash_duration, 1.0f});
75 
76  // Build splash fade out animation
77  splash_fade_out_animation.set_interpolator(ease<float>::out_cubic);
78  animation_channel<float>* splash_fade_out_opacity_channel = splash_fade_out_animation.add_channel(0);
79  splash_fade_out_opacity_channel->insert_keyframe({0.0f, 1.0f});
80  splash_fade_out_opacity_channel->insert_keyframe({splash_fade_out_duration, 0.0f});
81 
82  // Setup animation frame callbacks
83  auto set_splash_opacity = [splash_tint](int channel, const float& opacity)
84  {
85  splash_tint->set(math::fvec4{1, 1, 1, opacity});
86  };
87  splash_fade_in_animation.set_frame_callback(set_splash_opacity);
88  splash_fade_out_animation.set_frame_callback(set_splash_opacity);
89 
90  // Trigger splash fade out animation when splash fade in animation ends
91  splash_fade_in_animation.set_end_callback
92  (
93  [this]()
94  {
95  this->splash_fade_out_animation.play();
96  }
97  );
98 
99  // Trigger a state change when the splash fade out animation ends
100  splash_fade_out_animation.set_end_callback
101  (
102  [&ctx]()
103  {
104  // Queue change to main menu state
105  ctx.function_queue.push
106  (
107  [&ctx]()
108  {
109  ctx.state_machine.pop();
110  ctx.state_machine.emplace(std::make_unique<main_menu_state>(ctx, true));
111  }
112  );
113  }
114  );
115 
116  // Add splash fade animations to animator
117  ctx.animator->add_animation(&splash_fade_in_animation);
118  ctx.animator->add_animation(&splash_fade_out_animation);
119 
120  // Start splash fade in animation
121  splash_fade_in_animation.play();
122 
123  // Setup window resized callback
124  window_resized_subscription = ctx.window->get_resized_channel().subscribe
125  (
126  [&](const auto& event)
127  {
128  const math::fvec2 viewport_size = math::fvec2(event.window->get_viewport_size());
129  const math::fvec2 viewport_center = viewport_size * 0.5f;
130  splash_billboard.set_translation({std::round(viewport_center.x()), std::round(viewport_center.y()), 0.0f});
131  }
132  );
133 
134  // Construct splash skip function
135  auto skip = [&](const auto& event)
136  {
137  ctx.function_queue.emplace
138  (
139  [&]()
140  {
141  // Black out screen
142  ctx.window->get_graphics_pipeline().clear_attachments(gl::color_clear_bit, {{0.0f, 0.0f, 0.0f, 0.0f}});
143  ctx.window->swap_buffers();
144 
145  // Change to main menu state
146  ctx.state_machine.pop();
147  ctx.state_machine.emplace(std::make_unique<main_menu_state>(ctx, true));
148  }
149  );
150  };
151 
152  // Set up splash skippers
153  input_mapped_subscriptions.emplace_back
154  (
156  (
157  skip
158  )
159  );
160  input_mapped_subscriptions.emplace_back
161  (
163  (
164  skip
165  )
166  );
167  input_mapped_subscriptions.emplace_back
168  (
170  (
171  skip
172  )
173  );
174 
175  // Enable splash skippers next frame
176  ctx.function_queue.push
177  (
178  [&]()
179  {
180  ctx.input_mapper.connect(ctx.input_manager->get_event_dispatcher());
181  }
182  );
183 
184  debug::log_trace("Entered splash state");
185 }
186 
188 {
189  debug::log_trace("Exiting splash state...");
190 
191  // Disable splash skippers
193  input_mapped_subscriptions.clear();
194 
195  // Remove splash fade animations from animator
196  ctx.animator->remove_animation(&splash_fade_in_animation);
197  ctx.animator->remove_animation(&splash_fade_out_animation);
198 
199  // Remove splash billboard from UI scene
200  ctx.ui_scene->remove_object(splash_billboard);
201 
202  debug::log_trace("Exited splash state");
203 }
void set_end_callback(std::function< void()> callback)
Sets the callback that's executed when a non-looped animation has finished.
Definition: animation.cpp:87
void play()
Plays the animation.
Definition: animation.cpp:54
Single channel in a keyframe animation.
void insert_keyframe(const keyframe &k)
Adds a keyframe to the animation.
void set_interpolator(interpolator_type interpolator)
Sets the frame interpolator function object.
Definition: animation.hpp:339
channel * add_channel(int id)
Adds a channel to the animation.
Definition: animation.hpp:317
void set_frame_callback(std::function< void(int, const T &)> callback)
Sets the callback that's executed on each frame of animation.
Definition: animation.hpp:345
Abstract base class for game states.
Definition: game-state.hpp:29
::game & ctx
Definition: game-state.hpp:44
Definition: game.hpp:121
std::unique_ptr< scene::collection > ui_scene
Definition: game.hpp:333
hsm::state_machine< game_state > state_machine
Definition: game.hpp:299
std::unique_ptr< animator > animator
Definition: game.hpp:368
std::unique_ptr< resource_manager > resource_manager
Definition: game.hpp:152
std::shared_ptr< app::window > window
Definition: game.hpp:166
std::queue< std::function< void()> > function_queue
Definition: game.hpp:303
input::mapper input_mapper
Definition: game.hpp:214
std::unique_ptr< app::input_manager > input_manager
Definition: game.hpp:172
Template used to for generating one or more shader variants from a single source.
2D texture.
Definition: texture.hpp:141
constexpr const std::shared_ptr< image_view_2d > & get_image_view() const noexcept
Returns the image view.
Definition: texture.hpp:162
::event::channel< key_mapped_event > & get_key_mapped_channel() noexcept
Returns the channel through which key mapped events are published.
Definition: mapper.hpp:66
void disconnect()
Disconnects all input event signals from the mapper.
Definition: mapper.cpp:35
::event::channel< mouse_button_mapped_event > & get_mouse_button_mapped_channel() noexcept
Returns the channel through which mouse button mapped events are published.
Definition: mapper.hpp:72
::event::channel< gamepad_button_mapped_event > & get_gamepad_button_mapped_channel() noexcept
Returns the channel through which gamepad button mapped events are published.
Definition: mapper.hpp:60
void connect(::event::dispatcher &dispatcher)
Connects the input event signals of an event dispatcher to the mapper.
Definition: mapper.cpp:25
void set_material(std::shared_ptr< render::material > material)
Sets the billboard material.
Definition: billboard.cpp:125
void set_scale(const vector_type &scale)
Sets the scale of the object.
Definition: object.hpp:108
void set_translation(const vector_type &translation)
Sets the translation of the object.
Definition: object.hpp:85
splash_state(::game &ctx)
virtual ~splash_state()
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
Definition: log.hpp:88
Publish-subscribe messaging.
Definition: channel.hpp:32
@ color_clear_bit
Indicates the color buffer should be cleared.
Definition: clear-bits.hpp:31
constexpr vector< T, N > round(const vector< T, N > &x)
Performs a element-wise round operation.
Definition: vector.hpp:1489
@ translucent
Material is translucent.
Container for templated easing functions.
Definition: ease.hpp:71
n-dimensional vector.
Definition: vector.hpp:44
constexpr element_type & x() noexcept
Returns a reference to the first element.
Definition: vector.hpp:164
constexpr element_type & y() noexcept
Returns a reference to the second element.
Definition: vector.hpp:180