Skip to content

Commit

Permalink
optimize game_session setters
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoilmiosi committed Oct 28, 2024
1 parent 141c14e commit 11904f9
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
8 changes: 3 additions & 5 deletions src/net/image_pixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
15 changes: 11 additions & 4 deletions src/net/image_pixels.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/net/lobby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/net/lobby.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<game_session>;
Expand Down
14 changes: 7 additions & 7 deletions src/net/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<connection_state::not_validated>(con)) {
throw lobby_error("USER_ALREADY_CONNECTED");
}
Expand All @@ -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<connection_state::connected>(session);
Expand All @@ -179,17 +179,17 @@ 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);
broadcast_message_lobby<"lobby_user_update">(*lobby, user.user_id, session->username, session->propic, user.flags);
}
}

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);
Expand Down
6 changes: 3 additions & 3 deletions src/net/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 11904f9

Please sign in to comment.