Browse Source

Move some state specific variables out of game context and into their respective states

master
C. J. Howard 2 years ago
parent
commit
61234452e0
6 changed files with 60 additions and 96 deletions
  1. +4
    -15
      src/game/context.hpp
  2. +0
    -4
      src/game/state/boot.cpp
  3. +19
    -41
      src/game/state/credits.cpp
  4. +7
    -0
      src/game/state/credits.hpp
  5. +25
    -36
      src/game/state/main-menu.cpp
  6. +5
    -0
      src/game/state/main-menu.hpp

+ 4
- 15
src/game/context.hpp View File

@ -195,7 +195,6 @@ struct context
gl::vertex_buffer* billboard_vbo; gl::vertex_buffer* billboard_vbo;
gl::vertex_array* billboard_vao; gl::vertex_array* billboard_vao;
render::material* fallback_material; render::material* fallback_material;
// Compositing // Compositing
render::clear_pass* ui_clear_pass; render::clear_pass* ui_clear_pass;
@ -216,15 +215,12 @@ struct context
// Scene utilities // Scene utilities
scene::collection* active_scene; scene::collection* active_scene;
geom::aabb<float> no_cull;
// UI scene // UI scene
scene::collection* ui_scene; scene::collection* ui_scene;
scene::camera* ui_camera; scene::camera* ui_camera;
scene::billboard* camera_flash_billboard; scene::billboard* camera_flash_billboard;
scene::text* title_text;
scene::text* title_press_any_key_text;
scene::text* credits_text;
float font_size; float font_size;
bool dyslexia_font; bool dyslexia_font;
ui::mouse_tracker* menu_mouse_tracker; ui::mouse_tracker* menu_mouse_tracker;
@ -235,9 +231,10 @@ struct context
std::vector<std::tuple<scene::text*, scene::text*>> menu_item_texts; std::vector<std::tuple<scene::text*, scene::text*>> menu_item_texts;
std::unordered_map<std::string, int> menu_item_indices; std::unordered_map<std::string, int> menu_item_indices;
int* menu_item_index; int* menu_item_index;
animation<float>* menu_fade_animation;
animation<float>* title_fade_animation;
scene::billboard* menu_bg_billboard; scene::billboard* menu_bg_billboard;
animation<float>* menu_fade_animation;
animation<float>* menu_bg_fade_in_animation;
animation<float>* menu_bg_fade_out_animation;
// Surface scene // Surface scene
scene::collection* surface_scene; scene::collection* surface_scene;
@ -261,14 +258,6 @@ struct context
animation<float>* equip_tool_animation; animation<float>* equip_tool_animation;
animation<float>* unequip_tool_animation; animation<float>* unequip_tool_animation;
animation<float>* camera_flash_animation; animation<float>* camera_flash_animation;
animation<float>* title_fade_in_animation;
animation<float>* title_fade_out_animation;
animation<float>* title_press_any_key_animation;
animation<float>* main_menu_fade_animation;
animation<float>* credits_fade_in_animation;
animation<float>* credits_scroll_animation;
animation<float>* menu_bg_fade_in_animation;
animation<float>* menu_bg_fade_out_animation;
// Sound // Sound
float master_volume; float master_volume;

+ 0
- 4
src/game/state/boot.cpp View File

@ -632,10 +632,6 @@ void boot::setup_scenes()
const auto& viewport_dimensions = ctx.rasterizer->get_default_framebuffer().get_dimensions(); const auto& viewport_dimensions = ctx.rasterizer->get_default_framebuffer().get_dimensions();
const float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]); const float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
// Create infinite culling mask
const float inf = std::numeric_limits<float>::infinity();
ctx.no_cull = {{-inf, -inf, -inf}, {inf, inf, inf}};
// Setup UI camera // Setup UI camera
ctx.ui_camera = new scene::camera(); ctx.ui_camera = new scene::camera();
ctx.ui_camera->set_compositor(ctx.ui_compositor); ctx.ui_camera->set_compositor(ctx.ui_compositor);

+ 19
- 41
src/game/state/credits.cpp View File

@ -36,17 +36,17 @@ credits::credits(game::context& ctx):
ctx.logger->push_task("Entering credits state"); ctx.logger->push_task("Entering credits state");
// Construct credits text // Construct credits text
ctx.credits_text = new scene::text();
ctx.credits_text->set_material(&ctx.menu_font_material);
ctx.credits_text->set_font(&ctx.menu_font);
ctx.credits_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
ctx.credits_text->set_content((*ctx.strings)["credits"]);
credits_text.set_material(&ctx.menu_font_material);
credits_text.set_font(&ctx.menu_font);
credits_text.set_color({1.0f, 1.0f, 1.0f, 0.0f});
credits_text.set_content((*ctx.strings)["credits"]);
// Align credits text // Align credits text
const auto& credits_aabb = static_cast<const geom::aabb<float>&>(ctx.credits_text->get_local_bounds());
const auto& credits_aabb = static_cast<const geom::aabb<float>&>(credits_text.get_local_bounds());
float credits_w = credits_aabb.max_point.x - credits_aabb.min_point.x; float credits_w = credits_aabb.max_point.x - credits_aabb.min_point.x;
float credits_h = credits_aabb.max_point.y - credits_aabb.min_point.y; float credits_h = credits_aabb.max_point.y - credits_aabb.min_point.y;
ctx.credits_text->set_translation({std::round(-credits_w * 0.5f), std::round(-credits_h * 0.5f), 0.0f});
credits_text.set_translation({std::round(-credits_w * 0.5f), std::round(-credits_h * 0.5f), 0.0f});
credits_text.update_tweens();
// Load animation timing configuration // Load animation timing configuration
double credits_fade_in_duration = 0.0; double credits_fade_in_duration = 0.0;
@ -56,47 +56,33 @@ credits::credits(game::context& ctx):
if (ctx.config->contains("credits_scroll_duration")) if (ctx.config->contains("credits_scroll_duration"))
credits_scroll_duration = (*ctx.config)["credits_scroll_duration"].get<double>(); credits_scroll_duration = (*ctx.config)["credits_scroll_duration"].get<double>();
auto set_credits_opacity = [&ctx](int channel, const float& opacity)
auto set_credits_opacity = [this](int channel, const float& opacity)
{ {
ctx.credits_text->set_color({1.0f, 1.0f, 1.0f, opacity});
this->credits_text.set_color({1.0f, 1.0f, 1.0f, opacity});
}; };
// Build credits fade in animation // Build credits fade in animation
ctx.credits_fade_in_animation = new animation<float>();
animation_channel<float>* credits_fade_in_opacity_channel = ctx.credits_fade_in_animation->add_channel(0);
ctx.credits_fade_in_animation->set_interpolator(ease<float>::in_quad);
credits_fade_in_animation.set_interpolator(ease<float>::in_quad);
animation_channel<float>* credits_fade_in_opacity_channel = credits_fade_in_animation.add_channel(0);
credits_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f}); credits_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f}); credits_fade_in_opacity_channel->insert_keyframe({credits_fade_in_duration, 1.0f});
ctx.credits_fade_in_animation->set_frame_callback(set_credits_opacity);
// Build credits scroll in animation
ctx.credits_scroll_animation = new animation<float>();
// Trigger credits scroll animation after credits fade in animation ends
ctx.credits_fade_in_animation->set_end_callback
(
[&ctx]()
{
ctx.credits_scroll_animation->play();
}
);
credits_fade_in_animation.set_frame_callback(set_credits_opacity);
// Add credits animations to animator // Add credits animations to animator
ctx.animator->add_animation(ctx.credits_fade_in_animation);
ctx.animator->add_animation(ctx.credits_scroll_animation);
ctx.animator->add_animation(&credits_fade_in_animation);
// Start credits fade in animation // Start credits fade in animation
ctx.credits_fade_in_animation->play();
credits_fade_in_animation.play();
// Set up credits skipper // Set up credits skipper
ctx.input_listener->set_callback ctx.input_listener->set_callback
( (
[&ctx](const event_base& event)
[this, &ctx](const event_base& event)
{ {
auto id = event.get_event_type_id(); auto id = event.get_event_type_id();
if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id) if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
{ {
if (ctx.credits_text->get_color()[3] > 0.0f)
if (this->credits_text.get_color()[3] > 0.0f)
{ {
ctx.input_listener->set_enabled(false); ctx.input_listener->set_enabled(false);
@ -109,8 +95,7 @@ credits::credits(game::context& ctx):
); );
ctx.input_listener->set_enabled(true); ctx.input_listener->set_enabled(true);
ctx.ui_scene->add_object(ctx.credits_text);
ctx.credits_text->update_tweens();
ctx.ui_scene->add_object(&credits_text);
ctx.logger->pop_task(EXIT_SUCCESS); ctx.logger->pop_task(EXIT_SUCCESS);
} }
@ -124,17 +109,10 @@ credits::~credits()
ctx.input_listener->set_callback(nullptr); ctx.input_listener->set_callback(nullptr);
// Destruct credits text // Destruct credits text
ctx.ui_scene->remove_object(ctx.credits_text);
delete ctx.credits_text;
ctx.credits_text = nullptr;
ctx.ui_scene->remove_object(&credits_text);
// Destruct credits animations // Destruct credits animations
ctx.animator->remove_animation(ctx.credits_fade_in_animation);
ctx.animator->remove_animation(ctx.credits_scroll_animation);
delete ctx.credits_fade_in_animation;
delete ctx.credits_scroll_animation;
ctx.credits_fade_in_animation = nullptr;
ctx.credits_scroll_animation = nullptr;
ctx.animator->remove_animation(&credits_fade_in_animation);
ctx.logger->pop_task(EXIT_SUCCESS); ctx.logger->pop_task(EXIT_SUCCESS);
} }

+ 7
- 0
src/game/state/credits.hpp View File

@ -21,6 +21,8 @@
#define ANTKEEPER_GAME_STATE_CREDITS_HPP #define ANTKEEPER_GAME_STATE_CREDITS_HPP
#include "game/state/base.hpp" #include "game/state/base.hpp"
#include "scene/text.hpp"
#include "animation/animation.hpp"
namespace game { namespace game {
namespace state { namespace state {
@ -30,6 +32,11 @@ class credits: public game::state::base
public: public:
credits(game::context& ctx); credits(game::context& ctx);
virtual ~credits(); virtual ~credits();
private:
scene::text credits_text;
animation<float> credits_fade_in_animation;
animation<float> credits_scroll_animation;
}; };
} // namespace state } // namespace state

+ 25
- 36
src/game/state/main-menu.cpp View File

@ -45,36 +45,34 @@ main_menu::main_menu(game::context& ctx, bool fade_in):
ctx.ui_clear_pass->set_cleared_buffers(true, true, false); ctx.ui_clear_pass->set_cleared_buffers(true, true, false);
// Construct title text // Construct title text
ctx.title_text = new scene::text();
ctx.title_text->set_material(&ctx.title_font_material);
ctx.title_text->set_font(&ctx.title_font);
ctx.title_text->set_color({1.0f, 1.0f, 1.0f, 1.0f});
ctx.title_text->set_content((*ctx.strings)["title_antkeeper"]);
title_text.set_material(&ctx.title_font_material);
title_text.set_color({1.0f, 1.0f, 1.0f, (fade_in) ? 1.0f : 0.0f});
title_text.set_font(&ctx.title_font);
title_text.set_content((*ctx.strings)["title_antkeeper"]);
// Align title text // Align title text
const auto& title_aabb = static_cast<const geom::aabb<float>&>(ctx.title_text->get_local_bounds());
const auto& title_aabb = static_cast<const geom::aabb<float>&>(title_text.get_local_bounds());
float title_w = title_aabb.max_point.x - title_aabb.min_point.x; float title_w = title_aabb.max_point.x - title_aabb.min_point.x;
float title_h = title_aabb.max_point.y - title_aabb.min_point.y; float title_h = title_aabb.max_point.y - title_aabb.min_point.y;
ctx.title_text->set_translation({std::round(-title_w * 0.5f), std::round(-title_h * 0.5f + (ctx.app->get_viewport_dimensions().y / 3.0f) / 2.0f), 0.0f});
ctx.title_text->update_tweens();
title_text.set_translation({std::round(-title_w * 0.5f), std::round(-title_h * 0.5f + (ctx.app->get_viewport_dimensions().y / 3.0f) / 2.0f), 0.0f});
title_text.update_tweens();
// Add title text to UI // Add title text to UI
ctx.ui_scene->add_object(ctx.title_text);
ctx.ui_scene->add_object(&title_text);
// Construct title fade animation // Construct title fade animation
ctx.title_fade_animation = new animation<float>();
animation_channel<float>* opacity_channel = ctx.title_fade_animation->add_channel(0);
ctx.title_fade_animation->set_frame_callback
title_fade_animation.set_interpolator(ease<float>::out_cubic);
animation_channel<float>* opacity_channel = title_fade_animation.add_channel(0);
title_fade_animation.set_frame_callback
( (
[&ctx](int channel, const float& opacity)
[this, &ctx](int channel, const float& opacity)
{ {
float4 color = ctx.title_text->get_color();
float4 color = this->title_text.get_color();
color[3] = opacity; color[3] = opacity;
ctx.title_text->set_color(color);
this->title_text.set_color(color);
} }
); );
ctx.animator->add_animation(ctx.title_fade_animation);
ctx.animator->add_animation(&title_fade_animation);
// Construct menu item texts // Construct menu item texts
scene::text* start_text = new scene::text(); scene::text* start_text = new scene::text();
@ -218,16 +216,13 @@ main_menu::main_menu(game::context& ctx, bool fade_in):
if (fade_in) if (fade_in)
{ {
// Fade in from black
ctx.fade_transition->transition(0.5f, true, ease<float>::out_cubic); ctx.fade_transition->transition(0.5f, true, ease<float>::out_cubic);
} }
else else
{ {
// Fade in title
ctx.title_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
ctx.title_text->update_tweens();
// Fade in text
fade_in_title(); fade_in_title();
// Fade in menu
game::menu::fade_in(ctx, nullptr); game::menu::fade_in(ctx, nullptr);
} }
@ -246,38 +241,32 @@ main_menu::~main_menu()
game::menu::delete_text(ctx); game::menu::delete_text(ctx);
// Destruct title animation // Destruct title animation
ctx.animator->remove_animation(ctx.title_fade_animation);
delete ctx.title_fade_animation;
ctx.title_fade_animation = nullptr;
ctx.animator->remove_animation(&title_fade_animation);
// Destruct title text // Destruct title text
ctx.ui_scene->remove_object(ctx.title_text);
delete ctx.title_text;
ctx.title_text = nullptr;
ctx.ui_scene->remove_object(&title_text);
ctx.logger->pop_task(EXIT_SUCCESS); ctx.logger->pop_task(EXIT_SUCCESS);
} }
void main_menu::fade_in_title() void main_menu::fade_in_title()
{ {
ctx.title_fade_animation->set_interpolator(ease<float>::out_cubic);
animation_channel<float>* opacity_channel = ctx.title_fade_animation->get_channel(0);
animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
opacity_channel->remove_keyframes(); opacity_channel->remove_keyframes();
opacity_channel->insert_keyframe({0.0, 0.0f}); opacity_channel->insert_keyframe({0.0, 0.0f});
opacity_channel->insert_keyframe({game::menu::fade_in_duration, 1.0f}); opacity_channel->insert_keyframe({game::menu::fade_in_duration, 1.0f});
ctx.title_fade_animation->stop();
ctx.title_fade_animation->play();
title_fade_animation.stop();
title_fade_animation.play();
} }
void main_menu::fade_out_title() void main_menu::fade_out_title()
{ {
ctx.title_fade_animation->set_interpolator(ease<float>::out_cubic);
animation_channel<float>* opacity_channel = ctx.title_fade_animation->get_channel(0);
animation_channel<float>* opacity_channel = title_fade_animation.get_channel(0);
opacity_channel->remove_keyframes(); opacity_channel->remove_keyframes();
opacity_channel->insert_keyframe({0.0, 1.0f}); opacity_channel->insert_keyframe({0.0, 1.0f});
opacity_channel->insert_keyframe({game::menu::fade_out_duration, 0.0f}); opacity_channel->insert_keyframe({game::menu::fade_out_duration, 0.0f});
ctx.title_fade_animation->stop();
ctx.title_fade_animation->play();
title_fade_animation.stop();
title_fade_animation.play();
} }
} // namespace state } // namespace state

+ 5
- 0
src/game/state/main-menu.hpp View File

@ -21,6 +21,8 @@
#define ANTKEEPER_GAME_STATE_MAIN_MENU_HPP #define ANTKEEPER_GAME_STATE_MAIN_MENU_HPP
#include "game/state/base.hpp" #include "game/state/base.hpp"
#include "scene/text.hpp"
#include "animation/animation.hpp"
namespace game { namespace game {
namespace state { namespace state {
@ -34,6 +36,9 @@ public:
private: private:
void fade_in_title(); void fade_in_title();
void fade_out_title(); void fade_out_title();
scene::text title_text;
animation<float> title_fade_animation;
}; };
} // namespace state } // namespace state

Loading…
Cancel
Save