Skip to content

Commit

Permalink
Show avatar in tutorial/shooting range
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Jun 3, 2024
1 parent 138e3f9 commit 7d3f2a7
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 27 deletions.
35 changes: 31 additions & 4 deletions src/application/setups/test_scene_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ using portal_marker = editor_area_marker_node;

test_scene_setup::test_scene_setup(
std::string nickname,
std::vector<std::byte> 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);
}

Expand Down Expand Up @@ -342,6 +343,9 @@ void test_scene_setup::restart_mode() {
get_arena_handle().on_mode_with_input(
[&]<typename M>(M& mode, const auto& input) {
if constexpr(std::is_same_v<test_mode, M>) {
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<editor_point_marker_node>()) {
if (p.scene_entity_id.is_set() && p.editable.faction == faction_type::RESISTANCE) {
const auto new_id = mode.add_player(input, nickname, enemy_faction);
Expand All @@ -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);

Expand Down Expand Up @@ -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(
[&]<typename M>(M& mode, const auto&) {
if constexpr(std::is_same_v<test_mode, M>) {
mode.players[local_player_id].session.nickname = nickname;
}
}
);
}

do_tutorial_logic(step);
Expand Down Expand Up @@ -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<std::byte> bytes) {
avatar_bytes = std::move(bytes);
rebuild_player_meta_viewables = true;
}

std::optional<arena_player_metas> 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;
}
14 changes: 10 additions & 4 deletions src/application/setups/test_scene_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class test_scene_setup : public default_setup_settings, public arena_gui_mixin<t

std::optional<custom_imgui_result> special_result;

std::vector<std::byte> avatar_bytes;
arena_player_metas player_metas;
bool rebuild_player_meta_viewables = true;

template <class H, class S>
static decltype(auto) get_arena_handle_impl(S& self) {
return H {
Expand All @@ -105,6 +109,7 @@ class test_scene_setup : public default_setup_settings, public arena_gui_mixin<t

test_scene_setup(
std::string nickname,
std::vector<std::byte> avatar_bytes,
const packaged_official_content&,
const test_scene_type type
);
Expand Down Expand Up @@ -271,16 +276,17 @@ class test_scene_setup : public default_setup_settings, public arena_gui_mixin<t
return get_viewed_character_id();
}

std::nullopt_t get_new_player_metas() {
return std::nullopt;
}

std::nullopt_t get_new_ad_hoc_images() {
return std::nullopt;
}

std::optional<arena_player_metas> get_new_player_metas();

void set_new_avatar(std::vector<std::byte>);

const arena_player_metas* find_player_metas() const {
return nullptr;
return std::addressof(player_metas);
}

void after_all_drawcalls(game_frame_buffer&) {}
Expand Down
19 changes: 1 addition & 18 deletions src/augs/readwrite/byte_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::byte>& output) {
auto file = with_exceptions<std::ifstream>();
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<std::size_t>(size));
file.read(reinterpret_cast<byte_type_for_t<std::ifstream>*>(output.data()), size);
return output;
}

inline auto file_to_bytes(const path_type& path) {
std::vector<std::byte> output;
file_to_bytes(path, output);
return output;
}

template <class O>
void load_from_bytes(O& object, const path_type& path) {
auto file = open_binary_input_stream(path);
Expand Down
21 changes: 21 additions & 0 deletions src/augs/readwrite/file_to_bytes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

namespace augs {
inline auto file_to_bytes(const path_type& path, std::vector<std::byte>& output) {
auto file = with_exceptions<std::ifstream>();
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<std::size_t>(size));
file.read(reinterpret_cast<byte_type_for_t<std::ifstream>*>(output.data()), size);
return output;
}

inline auto file_to_bytes(const path_type& path) {
std::vector<std::byte> output;
file_to_bytes(path, output);
return output;
}
}
28 changes: 27 additions & 1 deletion src/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
#include "rtc/rtc.hpp"
#endif

#include "augs/readwrite/file_to_bytes.h"
#include "work_result.h"

namespace augs {
Expand Down Expand Up @@ -1553,6 +1554,23 @@ work_result work(
streaming.avatar_preview_tex = augs::graphics::texture(avatar);
}

WEBSTATIC auto get_my_avatar_bytes = [&]() {
std::vector<std::byte> avatar;

try {
const auto& path = config.client.avatar_image_path;

if (!path.empty()) {
return augs::file_to_bytes(path);
}
}
catch (...) {

}

return std::vector<std::byte>();
};

WEBSTATIC auto get_blank_texture = [&]() {
return streaming.necessary_images_in_atlas[assets::necessary_image_id::BLANK];
};
Expand Down Expand Up @@ -2154,6 +2172,7 @@ work_result work(
setup_launcher([&]() {
emplace_current_setup(std::in_place_type_t<test_scene_setup>(),
config.client.nickname,
get_my_avatar_bytes(),
*official,
test_scene_type::SHOOTING_RANGE
);
Expand All @@ -2165,6 +2184,7 @@ work_result work(
setup_launcher([&]() {
emplace_current_setup(std::in_place_type_t<test_scene_setup>(),
config.client.nickname,
get_my_avatar_bytes(),
*official,
test_scene_type::TUTORIAL
);
Expand Down Expand Up @@ -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<unsigned>(max_avatar_side_v);
avatar.scale(vec2u::square(max_s));
Expand Down

0 comments on commit 7d3f2a7

Please sign in to comment.