diff --git a/src/net/image_pixels.cpp b/src/net/image_pixels.cpp index 2b999dda..4be10975 100644 --- a/src/net/image_pixels.cpp +++ b/src/net/image_pixels.cpp @@ -24,7 +24,7 @@ namespace banggame { } } - image_pixels image_pixels::scale_to(uint32_t new_size) const { + image_pixels image_pixels::scale_to(uint32_t new_size) && { uint32_t new_width = width; uint32_t new_height = height; @@ -41,11 +41,10 @@ namespace banggame { } if (new_width >= width && new_height >= height) { - return *this; + return std::move(*this); } image_pixels result { new_width, new_height }; - result.pixels.resize(new_width * new_height * bytes_per_pixel); for (uint32_t y = 0; y < new_height; ++y) { for (uint32_t x = 0; x < new_width; ++x) { uint32_t scaled_x = x * width / new_width; @@ -119,8 +118,7 @@ namespace banggame { // Allocate memory for the image buffer image.format = PNG_FORMAT_RGBA; - image_pixels result{image.width, image.height}; - result.pixels.resize(PNG_IMAGE_SIZE(image)); + image_pixels result{ image.width, image.height }; if (!png_image_finish_read(&image, nullptr, result.pixels.data(), 0, nullptr)) { png_image_free(&image); throw std::runtime_error("Failed to finish reading PNG image"); diff --git a/src/net/image_pixels.h b/src/net/image_pixels.h index 582604f9..72474849 100644 --- a/src/net/image_pixels.h +++ b/src/net/image_pixels.h @@ -38,13 +38,18 @@ namespace banggame { constexpr bool operator == (const image_pixels_hash &other) const = default; }; - struct image_pixels { + class image_pixels { + private: uint32_t width; uint32_t height; byte_vector pixels; - explicit operator bool () const { - return width != 0 && height != 0 && !pixels.empty(); + public: + image_pixels() = default; + image_pixels(uint32_t width, uint32_t height) + : width{width}, height{height} + { + pixels.resize(width * height * bytes_per_pixel); } operator image_pixels_view() const { @@ -54,7 +59,9 @@ namespace banggame { uint32_t get_pixel(uint32_t x, uint32_t y) const; void set_pixel(uint32_t x, uint32_t y, uint32_t value); - image_pixels scale_to(uint32_t new_size) const; + image_pixels scale_to(uint32_t new_size) &&; + + friend image_pixels image_from_png_data_url(std::string_view data_url); }; byte_vector image_to_png(image_pixels_view image); diff --git a/src/net/lobby.cpp b/src/net/lobby.cpp index 34729fb1..67b08d86 100644 --- a/src/net/lobby.cpp +++ b/src/net/lobby.cpp @@ -5,18 +5,18 @@ namespace banggame { - void game_session::set_username(const std::string &new_username) { + void game_session::set_username(std::string new_username) { static constexpr size_t max_username_size = 50; if (new_username.size() > max_username_size) { username = new_username.substr(0, max_username_size); } else { - username = new_username; + username = std::move(new_username); } } - void game_session::set_propic(const image_pixels &new_propic) { - propic.reset(new_propic.scale_to(bot_info.propic_size)); + void game_session::set_propic(image_pixels new_propic) { + propic.reset(std::move(new_propic).scale_to(bot_info.propic_size)); } static auto find_user_it(auto &list, session_ptr session) { diff --git a/src/net/lobby.h b/src/net/lobby.h index 45018f8a..46f47688 100644 --- a/src/net/lobby.h +++ b/src/net/lobby.h @@ -38,8 +38,8 @@ struct game_session { client_handle client; ticks lifetime = user_lifetime; - void set_username(const std::string &new_username); - void set_propic(const image_pixels &new_propic); + void set_username(std::string new_username); + void set_propic(image_pixels new_propic); }; using session_ptr = std::shared_ptr; diff --git a/src/net/manager.cpp b/src/net/manager.cpp index 3ac03311..63cbabe6 100644 --- a/src/net/manager.cpp +++ b/src/net/manager.cpp @@ -136,7 +136,7 @@ static id_type generate_session_id(auto &rng, auto &map, int max_iters) { throw critical_error("CANNOT_GENERATE_SESSION_ID"); } -void game_manager::handle_message(utils::tag<"connect">, client_handle client, connection &con, const connect_args &args) { +void game_manager::handle_message(utils::tag<"connect">, client_handle client, connection &con, connect_args args) { if (!std::holds_alternative(con)) { throw lobby_error("USER_ALREADY_CONNECTED"); } @@ -155,8 +155,8 @@ void game_manager::handle_message(utils::tag<"connect">, client_handle client, c kick_client(session->client, "RECONNECT_WITH_SAME_SESSION_ID"); } - session->set_username(args.username); - session->set_propic(args.propic); + session->set_username(std::move(args.username)); + session->set_propic(std::move(args.propic)); session->client = client; con.emplace(session); @@ -179,8 +179,8 @@ void game_manager::handle_message(utils::tag<"pong">, client_handle client, conn } } -void game_manager::handle_message(utils::tag<"user_set_name">, session_ptr session, const std::string &username) { - session->set_username(username); +void game_manager::handle_message(utils::tag<"user_set_name">, session_ptr session, std::string username) { + session->set_username(std::move(username)); if (game_lobby *lobby = session->lobby) { game_user &user = lobby->find_user(session); @@ -188,8 +188,8 @@ void game_manager::handle_message(utils::tag<"user_set_name">, session_ptr sessi } } -void game_manager::handle_message(utils::tag<"user_set_propic">, session_ptr session, const image_pixels &propic) { - session->set_propic(propic); +void game_manager::handle_message(utils::tag<"user_set_propic">, session_ptr session, image_pixels propic) { + session->set_propic(std::move(propic)); if (game_lobby *lobby = session->lobby) { game_user &user = lobby->find_user(session); diff --git a/src/net/manager.h b/src/net/manager.h index 8396a70a..0c100ac1 100644 --- a/src/net/manager.h +++ b/src/net/manager.h @@ -69,10 +69,10 @@ class game_manager: public net::wsserver { bool remove_user_flag(session_ptr session, game_user_flag flag); private: - void handle_message(utils::tag<"connect">, client_handle client, connection &con, const connect_args &value); + void handle_message(utils::tag<"connect">, client_handle client, connection &con, connect_args value); void handle_message(utils::tag<"pong">, client_handle client, connection &con); - void handle_message(utils::tag<"user_set_name">, session_ptr session, const std::string &username); - void handle_message(utils::tag<"user_set_propic">, session_ptr session, const image_pixels &propic); + void handle_message(utils::tag<"user_set_name">, session_ptr session, std::string username); + void handle_message(utils::tag<"user_set_propic">, session_ptr session, image_pixels propic); void handle_message(utils::tag<"lobby_make">, session_ptr session, const lobby_make_args &value); void handle_message(utils::tag<"lobby_game_options">, session_ptr session, const game_options &options); void handle_message(utils::tag<"lobby_join">, session_ptr session, const lobby_join_args &value);