From 48823ebad6958404a58e34b4aea57094682f9db1 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Sat, 30 May 2026 20:57:50 +0300 Subject: [PATCH] Fixes to world scale up over network --- src/ClientData.cpp | 22 ++++++++++++++++------ src/ScaleUpCanvas.hpp | 6 +++++- src/World.cpp | 15 ++++++++------- src/World.hpp | 5 +++-- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/ClientData.cpp b/src/ClientData.cpp index 10a0c83..76442e1 100644 --- a/src/ClientData.cpp +++ b/src/ClientData.cpp @@ -80,10 +80,16 @@ void ClientData::register_class(World& world) { break; } case ClientDataCommand::SET_GRID_SIZE: { - uint32_t oldGridSize = o->gridSize; a(o->gridSize); - if(oldGridSize < o->gridSize) - world.scale_up(get_canvas_scale_up_amount(o->gridSize, oldGridSize)); + if(o != world.ownClientData) { + if(world.ownClientData->gridSize < o->gridSize) { + world.scale_up(get_canvas_scale_up_amount(o->gridSize, world.ownClientData->gridSize)); + world.ownClientData->gridSize = o->gridSize; + world.ownClientData.send_update_to_all(RELIABLE_COMMAND_CHANNEL, [](const NetObjTemporaryPtr& o, cereal::PortableBinaryOutputArchive & a) { + a(ClientDataCommand::SET_GRID_SIZE, o->gridSize); + }); + } + } break; } } @@ -122,10 +128,14 @@ void ClientData::register_class(World& world) { break; } case ClientDataCommand::SET_GRID_SIZE: { - uint32_t oldGridSize = o->gridSize; a(o->gridSize); - if(oldGridSize < o->gridSize) - world.scale_up(get_canvas_scale_up_amount(o->gridSize, oldGridSize)); + if(world.ownClientData->gridSize < o->gridSize) { + world.scale_up(get_canvas_scale_up_amount(o->gridSize, world.ownClientData->gridSize)); + world.ownClientData->gridSize = o->gridSize; + world.ownClientData.send_update_to_all(RELIABLE_COMMAND_CHANNEL, [](const NetObjTemporaryPtr& o, cereal::PortableBinaryOutputArchive & a) { + a(ClientDataCommand::SET_GRID_SIZE, o->gridSize); + }); + } o.send_server_update_to_all_clients(RELIABLE_COMMAND_CHANNEL, [](const NetObjTemporaryPtr& o, cereal::PortableBinaryOutputArchive & a) { a(ClientDataCommand::SET_GRID_SIZE, o->gridSize); }); diff --git a/src/ScaleUpCanvas.hpp b/src/ScaleUpCanvas.hpp index defe101..f7d8e06 100644 --- a/src/ScaleUpCanvas.hpp +++ b/src/ScaleUpCanvas.hpp @@ -26,9 +26,13 @@ WorldScalar get_canvas_scale_up_amount(uint32_t newGridSize, uint32_t oldGridSize); template void canvas_scale_up_check(T& obj, World& world, const std::shared_ptr& c) { - if(c) { + if(c) { // Server, compare server's grid size with client's (c) grid size auto clientData = world.netObjMan.get_obj_temporary_ref_from_id(NetworkingObjects::NetObjID(c->customID)); if(clientData->get_grid_size() < world.ownClientData->get_grid_size()) obj.scale_up(get_canvas_scale_up_amount(world.ownClientData->get_grid_size(), clientData->get_grid_size())); } + else { // Client, compare own grid size (world.ownClientData) with server's grid size + if(world.serverClientData->get_grid_size() < world.ownClientData->get_grid_size()) + obj.scale_up(get_canvas_scale_up_amount(world.ownClientData->get_grid_size(), world.serverClientData->get_grid_size())); + } } diff --git a/src/World.cpp b/src/World.cpp index d911a80..2b841f8 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -98,7 +98,7 @@ World::World(MainProgram& initMain, const CustomEvents::OpenInfiniPaintFileEvent void World::init_client_data_list() { clients = netObjMan.make_obj>(); - ownClientData = clients->emplace_direct(clients, ClientData::InitStruct{ + serverClientData = ownClientData = clients->emplace_direct(clients, ClientData::InitStruct{ .cursorColor = get_random_cursor_color(), .displayName = main.conf.displayName }); @@ -156,16 +156,17 @@ void World::init_client(const std::string& serverFullID) { drawProg.init_client_callbacks(); netClient->add_recv_callback(CLIENT_INITIAL_DATA, [&](cereal::PortableBinaryInputArchive& message) { std::string fileDisplayName; - NetworkingObjects::NetObjID clientDataObjID; - message(fileDisplayName, clientDataObjID); + NetworkingObjects::NetObjID clientDataObjID, serverDataObjID; + message(fileDisplayName, clientDataObjID, serverDataObjID); set_name(fileDisplayName); + clients = netObjMan.read_create_message>(message, nullptr); + ownClientData = netObjMan.get_obj_temporary_ref_from_id(clientDataObjID); + serverClientData = netObjMan.get_obj_temporary_ref_from_id(serverDataObjID); bMan.read_create_message(message); gridMan.read_create_message(message); drawProg.read_components_client(message); canvasTheme.read_create_message(message); - clients = netObjMan.read_create_message>(message, nullptr); init_client_data_list_callbacks(); - ownClientData = netObjMan.get_obj_temporary_ref_from_id(clientDataObjID); drawData.cam.smooth_move_to(*main.world, ownClientData->get_cam_coords(), ownClientData->get_window_size(), true); #ifdef ENABLE_ORDERED_LIST_TEST @@ -435,12 +436,12 @@ void World::start_hosting(const std::string& initNetSource, const std::string& s auto ss(std::make_shared()); { cereal::PortableBinaryOutputArchive a(*ss); - a(CLIENT_INITIAL_DATA, name, clientDataObjPtr.get_net_id()); + a(CLIENT_INITIAL_DATA, name, clientDataObjPtr.get_net_id(), serverClientData.get_net_id()); + clients.write_create_message(a); bMan.bookmarkListRoot.write_create_message(a); gridMan.grids.write_create_message(a); drawProg.write_components_server(a); canvasTheme.write_create_message(a); - clients.write_create_message(a); #ifdef ENABLE_ORDERED_LIST_TEST listDebugTest.write_create_message(a); #endif diff --git a/src/World.hpp b/src/World.hpp index 2f0afc5..b73eee8 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -93,13 +93,14 @@ class World { std::filesystem::path filePath; std::string name; + NetworkingObjects::NetObjTemporaryPtr serverClientData; // For server, this is equal to ownClientData NetworkingObjects::NetObjTemporaryPtr ownClientData; NetworkingObjects::NetObjOwnerPtr> clients; CanvasTheme canvasTheme; - void scale_up(const WorldScalar& scaleUpAmount); - void scale_up_step(); + void scale_up(const WorldScalar& scaleUpAmount); // Should only be called from ClientData + void scale_up_step(); // Call when scale up step happens bool should_ask_before_closing(); void set_has_unsaved_local_changes(bool newHasUnsavedLocalChangesVal);