From 7d3f2a74c4c733d00aa60dfe0fdfd11c32c1992f Mon Sep 17 00:00:00 2001 From: geneotech Date: Mon, 3 Jun 2024 23:42:06 +0200 Subject: [PATCH] Show avatar in tutorial/shooting range --- src/application/setups/test_scene_setup.cpp | 35 ++++++++++++++++++--- src/application/setups/test_scene_setup.h | 14 ++++++--- src/augs/readwrite/byte_file.h | 19 +---------- src/augs/readwrite/file_to_bytes.h | 21 +++++++++++++ src/work.cpp | 28 ++++++++++++++++- 5 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 src/augs/readwrite/file_to_bytes.h diff --git a/src/application/setups/test_scene_setup.cpp b/src/application/setups/test_scene_setup.cpp index aad558113..def36c7b3 100644 --- a/src/application/setups/test_scene_setup.cpp +++ b/src/application/setups/test_scene_setup.cpp @@ -25,10 +25,11 @@ using portal_marker = editor_area_marker_node; test_scene_setup::test_scene_setup( std::string nickname, + std::vector avatar_bytes, const packaged_official_content& official, //const input_recording_type recording_type, const test_scene_type type -) : official(official), nickname(nickname), type(type) { + ) : official(official), nickname(nickname), type(type), avatar_bytes(avatar_bytes) { init(type); } @@ -342,6 +343,9 @@ void test_scene_setup::restart_mode() { get_arena_handle().on_mode_with_input( [&](M& mode, const auto& input) { if constexpr(std::is_same_v) { + local_player_id = mode.add_player(input, nickname, player_faction); + viewed_character_id = cosm[mode.lookup(local_player_id)].get_id(); + for (auto& p : project.nodes.template get_pool_for()) { if (p.scene_entity_id.is_set() && p.editable.faction == faction_type::RESISTANCE) { const auto new_id = mode.add_player(input, nickname, enemy_faction); @@ -358,9 +362,6 @@ void test_scene_setup::restart_mode() { } } - local_player_id = mode.add_player(input, nickname, player_faction); - viewed_character_id = cosm[mode.lookup(local_player_id)].get_id(); - const auto new_id = local_player_id; auto player = mode.find(new_id); @@ -611,6 +612,14 @@ void test_scene_setup::do_tutorial_logic(const logic_step step) { void test_scene_setup::pre_solve(const logic_step step) { if (const auto h = scene.world[viewed_character_id]) { cosmic::set_specific_name(h, nickname); + + get_arena_handle().on_mode_with_input( + [&](M& mode, const auto&) { + if constexpr(std::is_same_v) { + mode.players[local_player_id].session.nickname = nickname; + } + } + ); } do_tutorial_logic(step); @@ -806,4 +815,22 @@ void test_scene_setup::get_steam_rich_presence_pairs(steam_rich_presence_pairs& else { pairs.push_back({ "steam_display", "#Status_ShootingRange" }); } +} + +void test_scene_setup::set_new_avatar(std::vector bytes) { + avatar_bytes = std::move(bytes); + rebuild_player_meta_viewables = true; +} + +std::optional test_scene_setup::get_new_player_metas() { + if (rebuild_player_meta_viewables) { + auto& metas = player_metas; + + metas[mode_player_id::first().value].avatar.image_bytes = avatar_bytes; + + rebuild_player_meta_viewables = false; + return metas; + } + + return std::nullopt; } \ No newline at end of file diff --git a/src/application/setups/test_scene_setup.h b/src/application/setups/test_scene_setup.h index 7147507be..c1e9b68e1 100644 --- a/src/application/setups/test_scene_setup.h +++ b/src/application/setups/test_scene_setup.h @@ -85,6 +85,10 @@ class test_scene_setup : public default_setup_settings, public arena_gui_mixin special_result; + std::vector avatar_bytes; + arena_player_metas player_metas; + bool rebuild_player_meta_viewables = true; + template static decltype(auto) get_arena_handle_impl(S& self) { return H { @@ -105,6 +109,7 @@ class test_scene_setup : public default_setup_settings, public arena_gui_mixin avatar_bytes, const packaged_official_content&, const test_scene_type type ); @@ -271,16 +276,17 @@ class test_scene_setup : public default_setup_settings, public arena_gui_mixin get_new_player_metas(); + + void set_new_avatar(std::vector); + const arena_player_metas* find_player_metas() const { - return nullptr; + return std::addressof(player_metas); } void after_all_drawcalls(game_frame_buffer&) {} diff --git a/src/augs/readwrite/byte_file.h b/src/augs/readwrite/byte_file.h index f764556e8..56ab0eaf1 100644 --- a/src/augs/readwrite/byte_file.h +++ b/src/augs/readwrite/byte_file.h @@ -2,27 +2,10 @@ #include "augs/filesystem/file.h" #include "augs/templates/byte_type_for.h" #include "augs/readwrite/byte_readwrite.h" +#include "augs/readwrite/file_to_bytes.h" #include "augs/filesystem/path_declaration.h" namespace augs { - inline auto file_to_bytes(const path_type& path, std::vector& output) { - auto file = with_exceptions(); - file.open(path, std::ios::binary | std::ios::ate); - - const std::streamsize size = file.tellg(); - file.seekg(0, std::ios::beg); - - resize_no_init(output, static_cast(size)); - file.read(reinterpret_cast*>(output.data()), size); - return output; - } - - inline auto file_to_bytes(const path_type& path) { - std::vector output; - file_to_bytes(path, output); - return output; - } - template void load_from_bytes(O& object, const path_type& path) { auto file = open_binary_input_stream(path); diff --git a/src/augs/readwrite/file_to_bytes.h b/src/augs/readwrite/file_to_bytes.h new file mode 100644 index 000000000..85281d43b --- /dev/null +++ b/src/augs/readwrite/file_to_bytes.h @@ -0,0 +1,21 @@ +#pragma once + +namespace augs { + inline auto file_to_bytes(const path_type& path, std::vector& output) { + auto file = with_exceptions(); + file.open(path, std::ios::binary | std::ios::ate); + + const std::streamsize size = file.tellg(); + file.seekg(0, std::ios::beg); + + resize_no_init(output, static_cast(size)); + file.read(reinterpret_cast*>(output.data()), size); + return output; + } + + inline auto file_to_bytes(const path_type& path) { + std::vector output; + file_to_bytes(path, output); + return output; + } +} diff --git a/src/work.cpp b/src/work.cpp index 460eaec93..a48a73745 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -176,6 +176,7 @@ #include "rtc/rtc.hpp" #endif +#include "augs/readwrite/file_to_bytes.h" #include "work_result.h" namespace augs { @@ -1553,6 +1554,23 @@ work_result work( streaming.avatar_preview_tex = augs::graphics::texture(avatar); } + WEBSTATIC auto get_my_avatar_bytes = [&]() { + std::vector avatar; + + try { + const auto& path = config.client.avatar_image_path; + + if (!path.empty()) { + return augs::file_to_bytes(path); + } + } + catch (...) { + + } + + return std::vector(); + }; + WEBSTATIC auto get_blank_texture = [&]() { return streaming.necessary_images_in_atlas[assets::necessary_image_id::BLANK]; }; @@ -2154,6 +2172,7 @@ work_result work( setup_launcher([&]() { emplace_current_setup(std::in_place_type_t(), config.client.nickname, + get_my_avatar_bytes(), *official, test_scene_type::SHOOTING_RANGE ); @@ -2165,6 +2184,7 @@ work_result work( setup_launcher([&]() { emplace_current_setup(std::in_place_type_t(), config.client.nickname, + get_my_avatar_bytes(), *official, test_scene_type::TUTORIAL ); @@ -3068,7 +3088,13 @@ work_result work( if (auto resp = cli->Get(parsed.location)) { try { augs::image avatar; - avatar.from_png_bytes(augs::string_to_bytes(resp->body), parsed.location); + + auto avatar_bytes = augs::string_to_bytes(resp->body); + avatar.from_png_bytes(avatar_bytes, parsed.location); + + on_specific_setup([&](test_scene_setup& setup) { + setup.set_new_avatar(avatar_bytes); + }); const auto max_s = static_cast(max_avatar_side_v); avatar.scale(vec2u::square(max_s));