diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-12-29 13:11:10 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:33 +0100 |
commit | 968206877c2ee1cf220abdafb39a876c16afb0fa (patch) | |
tree | 6c265e41f2f2e2e7eee4465c1dc15fa18aa9f759 | |
parent | Implemented new event system (diff) | |
download | AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.tar AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.tar.gz AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.tar.bz2 AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.tar.lz AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.tar.xz AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.tar.zst AltCraft-968206877c2ee1cf220abdafb39a876c16afb0fa.zip |
-rw-r--r-- | src/GameState.cpp | 18 | ||||
-rw-r--r-- | src/GlobalState.cpp | 71 | ||||
-rw-r--r-- | src/Render.cpp | 132 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 52 | ||||
-rw-r--r-- | src/World.cpp | 8 | ||||
-rw-r--r-- | src/main.cpp | 5 |
6 files changed, 140 insertions, 146 deletions
diff --git a/src/GameState.cpp b/src/GameState.cpp index cea92d6..f5eb8d4 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -58,7 +58,7 @@ void GameState::UpdatePacket(NetworkClient *nc) entity.pitch = packet->Pitch / 256.0; entity.renderColor = glm::vec3(0,1,0); world.AddEntity(entity); - EventAgregator::PushEvent(EventType::EntityChanged, EntityChangedData{ entity.entityId }); + PUSH_EVENT("EntityChanged", entity.entityId); break; } case SpawnExperienceOrb: @@ -76,7 +76,7 @@ void GameState::UpdatePacket(NetworkClient *nc) entity.pitch = packet->Pitch / 256.0; entity.renderColor = glm::vec3(0,0,1); world.AddEntity(entity); - EventAgregator::PushEvent(EventType::EntityChanged, EntityChangedData{ entity.entityId }); + PUSH_EVENT("EntityChanged", entity.entityId); break; } case SpawnPainting: @@ -93,7 +93,7 @@ void GameState::UpdatePacket(NetworkClient *nc) entity.height = 1.8; entity.width = 0.6; world.AddEntity(entity); - EventAgregator::PushEvent(EventType::EntityChanged, EntityChangedData{ entity.entityId }); + PUSH_EVENT("EntityChanged", entity.entityId); break; } case AnimationCB: @@ -120,7 +120,7 @@ void GameState::UpdatePacket(NetworkClient *nc) case ChatMessageCB: { auto packet = std::static_pointer_cast<PacketChatMessageCB>(ptr); LOG(INFO) << "Message (" << int(packet->Position) << "): " << packet->JsonData.text; - EventAgregator::PushEvent(EventType::ChatMessageReceived, ChatMessageReceivedData{ packet->JsonData,packet->Position }); + PUSH_EVENT("ChatMessageReceived", std::make_tuple(packet->JsonData, packet->Position)); break; } case MultiBlockChange: { @@ -134,7 +134,7 @@ void GameState::UpdatePacket(NetworkClient *nc) try { playerInventory.ConfirmTransaction(*packet); } catch (std::exception &e) { - EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Transaction failed" }); + PUSH_EVENT("Disconnected", std::string("Transaction failed")); } } break; @@ -173,7 +173,7 @@ void GameState::UpdatePacket(NetworkClient *nc) case DisconnectPlay: { auto packet = std::static_pointer_cast<PacketDisconnectPlay>(ptr); LOG(INFO) << "Disconnect reason: " << packet->Reason; - EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ packet->Reason }); + PUSH_EVENT("Disconnected", packet->Reason); break; } case EntityStatus: @@ -217,7 +217,7 @@ void GameState::UpdatePacket(NetworkClient *nc) g_ReducedDebugInfo = packet->ReducedDebugInfo; LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int)g_Difficulty << ", Level Type is " << g_LevelType; - EventAgregator::PushEvent(EventType::PlayerConnected, PlayerConnectedData{}); + PUSH_EVENT("PlayerConnected", 0); break; } case Map: @@ -295,12 +295,12 @@ void GameState::UpdatePacket(NetworkClient *nc) player->pos.z = packet->Z; } - EventAgregator::PushEvent(EventType::PlayerPosChanged, PlayerPosChangedData{ player->pos }); + PUSH_EVENT("PlayerPosChanged", player->pos); LOG(INFO) << "PlayerPos is " << player->pos << "\t\tAngle: " << player->yaw << "," << player->pitch;; if (!g_IsGameStarted) { LOG(INFO) << "Game is started"; - EventAgregator::PushEvent(EventType::RemoveLoadingScreen, RemoveLoadingScreenData{}); + PUSH_EVENT("RemoveLoadingScreen",0); } g_IsGameStarted = true; diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp index 297d5e8..2bd4943 100644 --- a/src/GlobalState.cpp +++ b/src/GlobalState.cpp @@ -26,60 +26,59 @@ void InitEvents() { * Network Events */ - listener.RegisterHandler(EventType::Exit, [](EventData eventData) { + listener.RegisterHandler("Exit", [](const Event&) { isRunning = false; }); - listener.RegisterHandler(EventType::ConnectToServer, [](EventData eventData) { - auto data = std::get<ConnectToServerData>(eventData); - if (data.address == "" || data.port == 0) + listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { + auto data = eventData.get <std::tuple<std::string, unsigned short, std::string>>(); //address,port,username + if (std::get<0>(data) == "" || std::get<1>(data) == 0) LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; if (nc != nullptr) { LOG(ERROR) << "Already connected"; return; } LOG(INFO) << "Connecting to server"; - EventAgregator::PushEvent(EventType::Connecting, ConnectingData{}); + PUSH_EVENT("Connecting",0); try { - nc = std::make_unique<NetworkClient>(data.address, data.port, data.username); + nc = std::make_unique<NetworkClient>(std::get<0>(data), std::get<1>(data), std::get<2>(data)); } catch (std::exception &e) { LOG(WARNING) << "Connection failed"; - EventAgregator::PushEvent(EventType::ConnectionFailed, ConnectionFailedData{ e.what() }); + PUSH_EVENT("ConnectionFailed", e.what()); return; } LOG(INFO) << "Connected to server"; - EventAgregator::PushEvent(EventType::ConnectionSuccessfull, ConnectionSuccessfullData{}); + PUSH_EVENT("ConnectionSuccessfull", 0); }); - listener.RegisterHandler(EventType::Disconnect, [](EventData eventData) { - auto data = std::get<DisconnectData>(eventData); - EventAgregator::PushEvent(EventType::Disconnected, DisconnectedData{ data.reason }); - LOG(INFO) << "Disconnected: " << data.reason; + listener.RegisterHandler("Disconnect", [](const Event& eventData) { + auto data = eventData.get<std::string>(); + PUSH_EVENT("Disconnected", data); + LOG(INFO) << "Disconnected: " << data; nc.reset(); }); - listener.RegisterHandler(EventType::NetworkClientException, [](EventData eventData) { - auto data = std::get<NetworkClientExceptionData>(eventData); - EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ data.what }); + listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { + auto data = eventData.get < std::string>(); + PUSH_EVENT("Disconnect", data); }); /* * GameState Events */ - listener.RegisterHandler(EventType::Exit, [](EventData eventData) { + listener.RegisterHandler("Exit", [](const Event&) { isRunning = false; }); - listener.RegisterHandler(EventType::ConnectionSuccessfull, [](EventData eventData) { - auto data = std::get<ConnectionSuccessfullData>(eventData); + listener.RegisterHandler("ConnectionSuccessfull", [](const Event&) { gs = std::make_unique<GameState>(); isPhysRunning = true; threadPhys = std::thread(&PhysExec); }); - listener.RegisterHandler(EventType::Disconnected, [](EventData eventData) { + listener.RegisterHandler("Disconnected", [](const Event&) { if (!gs) return; isPhysRunning = false; @@ -87,18 +86,19 @@ void InitEvents() { gs.reset(); }); - listener.RegisterHandler(EventType::SendChatMessage, [](EventData eventData) { - nc->SendPacket(std::make_shared<PacketChatMessageSB>(std::get<SendChatMessageData>(eventData).message)); + listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { + auto message = eventData.get<std::string>(); + nc->SendPacket(std::make_shared<PacketChatMessageSB>(message)); }); } void PhysExec() { EventListener listener; - listener.RegisterHandler(EventType::KeyPressed, [](EventData eventData) { + listener.RegisterHandler("KeyPressed", [](const Event& eventData) { if (!gs) return; - switch (std::get<KeyPressedData>(eventData).key) { + switch (eventData.get<SDL_Scancode>()) { case SDL_SCANCODE_W: isMoving[GameState::FORWARD] = true; break; @@ -119,10 +119,10 @@ void PhysExec() { } }); - listener.RegisterHandler(EventType::KeyReleased, [](EventData eventData) { + listener.RegisterHandler("KeyReleased", [](const Event& eventData) { if (!gs) return; - switch (std::get<KeyReleasedData>(eventData).key) { + switch (eventData.get<SDL_Scancode>()) { case SDL_SCANCODE_W: isMoving[GameState::FORWARD] = false; break; @@ -143,11 +143,11 @@ void PhysExec() { } }); - listener.RegisterHandler(EventType::MouseMoved, [](EventData eventData) { + listener.RegisterHandler("MouseMoved", [](const Event& eventData) { if (!gs) return; - auto data = std::get<MouseMovedData>(eventData); - gs->HandleRotation(data.x, data.y); + auto data = eventData.get<std::tuple<float,float>>(); + gs->HandleRotation(std::get<0>(data),std::get<1>(data)); }); LoopExecutionTimeController timer(std::chrono::milliseconds(8)); @@ -170,8 +170,7 @@ void PhysExec() { gs->Update(timer.GetRealDeltaS()); - while (listener.IsEventsQueueIsNotEmpty()) - listener.HandleEvent(); + listener.HandleAllEvents(); timer.Update(); } @@ -186,15 +185,13 @@ void GsExec() { nc->UpdatePacket(); gs->UpdatePacket(nc.get()); - while (listener.IsEventsQueueIsNotEmpty()) - listener.HandleEvent(); + listener.HandleAllEvents(); } } catch (std::exception &e) { - EventAgregator::PushEvent(EventType::NetworkClientException, NetworkClientExceptionData{ e.what() }); + PUSH_EVENT("NetworkClientException", e.what()); } - while (listener.IsEventsQueueIsNotEmpty()) - listener.HandleEvent(); + listener.HandleAllEvents(); timer.Update(); } @@ -230,7 +227,7 @@ State GlobalState::GetState() { } void GlobalState::SetState(const State &newState) { - if (newState != state) - EventAgregator::PushEvent(EventType::StateUpdated, StateUpdatedData{}); + if (newState != state) + PUSH_EVENT("StateUpdated", 0); state = newState; }
\ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index 5c58985..3daa8ff 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -101,13 +101,13 @@ void Render::UpdateKeyboard() { for (auto key : toUpdate) { bool isPressed = kbState[key]; if (!isKeyPressed[key] && isPressed) { - EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ key }); + PUSH_EVENT("KeyPressed", key); } if (isKeyPressed[key] && isPressed) { //KeyHeld } if (isKeyPressed[key] && !isPressed) { - EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ key }); + PUSH_EVENT("KeyReleased", key); } isKeyPressed[key] = isPressed; } @@ -214,7 +214,7 @@ void Render::HandleEvents() { double deltaY = event.motion.yrel; deltaX *= sensetivity; deltaY *= sensetivity * -1; - EventAgregator::DirectEventCall(EventType::MouseMoved, MouseMovedData{ deltaX,deltaY }); + DIRECT_EVENT_CALL("MouseMove", std::make_tuple(deltaX, deltaY)); } default: break; @@ -245,82 +245,78 @@ void Render::SetMouseCapture(bool IsCaptured) { void Render::ExecuteRenderLoop() { EventListener listener; - listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { - auto data = std::get<ConnectionSuccessfullData>(eventData); - stateString = "Logging in..."; + listener.RegisterHandler("ConnectionSuccessfull", [this](const Event&) { + stateString = "Logging in..."; }); - listener.RegisterHandler(EventType::PlayerConnected, [this](EventData eventData) { - auto data = std::get<PlayerConnectedData>(eventData); - stateString = "Loading terrain..."; - world = std::make_unique<RendererWorld>(GlobalState::GetGameState()); + listener.RegisterHandler("PlayerConnected", [this](const Event&) { + stateString = "Loading terrain..."; + world = std::make_unique<RendererWorld>(GlobalState::GetGameState()); }); - listener.RegisterHandler(EventType::RemoveLoadingScreen, [this](EventData eventData) { - stateString = "Playing"; - renderWorld = true; - GlobalState::SetState(State::Playing); - glClearColor(0, 0, 0, 1.0f); + listener.RegisterHandler("RemoveLoadingScreen", [this](const Event&) { + stateString = "Playing"; + renderWorld = true; + GlobalState::SetState(State::Playing); + glClearColor(0, 0, 0, 1.0f); }); - listener.RegisterHandler(EventType::ConnectionFailed, [this](EventData eventData) { - stateString = "Connection failed: " + std::get<ConnectionFailedData>(eventData).reason; - renderWorld = false; - world.reset(); - GlobalState::SetState(State::MainMenu); - glClearColor(0.8, 0.8, 0.8, 1.0f); - }); - - listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) { - stateString = "Disconnected: " + std::get<DisconnectedData>(eventData).reason; - renderWorld = false; - world.reset(); - GlobalState::SetState(State::MainMenu); - glClearColor(0.8, 0.8, 0.8, 1.0f); - }); - - listener.RegisterHandler(EventType::Connecting, [this](EventData eventData) { - stateString = "Connecting to the server..."; - GlobalState::SetState(State::Loading); - }); - - listener.RegisterHandler(EventType::ChatMessageReceived, [this](EventData eventData) { - auto data = std::get<ChatMessageReceivedData>(eventData); - std::string msg = "(" + std::to_string((int)data.position) + ") " + data.message.text; - chatMessages.push_back(msg); - }); - - listener.RegisterHandler(EventType::StateUpdated, [this](EventData eventData) { - switch (GlobalState::GetState()) { - case State::Playing: - SetMouseCapture(true); - break; - case State::InitialLoading: - case State::MainMenu: - case State::Loading: - case State::Paused: - case State::Inventory: - case State::Chat: - SetMouseCapture(false); - break; - } - }); + listener.RegisterHandler("ConnectionFailed", [this](const Event& eventData) { + stateString = "Connection failed: " + eventData.get <std::string>(); + renderWorld = false; + world.reset(); + GlobalState::SetState(State::MainMenu); + glClearColor(0.8, 0.8, 0.8, 1.0f); + }); + + listener.RegisterHandler("Disconnected", [this](const Event& eventData) { + stateString = "Disconnected: " + eventData.get<std::string>(); + renderWorld = false; + world.reset(); + GlobalState::SetState(State::MainMenu); + glClearColor(0.8, 0.8, 0.8, 1.0f); + }); + + listener.RegisterHandler("Connecting", [this](const Event&) { + stateString = "Connecting to the server..."; + GlobalState::SetState(State::Loading); + }); + + listener.RegisterHandler("ChatMessageReceived", [this](const Event& eventData) { + auto data = eventData.get<std::tuple<std::string, unsigned char>>(); + std::string msg = "(" + std::to_string((int)std::get<1>(data)) + ") " + std::get<0>(data); + chatMessages.push_back(msg); + }); + + listener.RegisterHandler("StateUpdated", [this](const Event& eventData) { + switch (GlobalState::GetState()) { + case State::Playing: + SetMouseCapture(true); + break; + case State::InitialLoading: + case State::MainMenu: + case State::Loading: + case State::Paused: + case State::Inventory: + case State::Chat: + SetMouseCapture(false); + break; + } + }); + + GlobalState::SetState(State::MainMenu); - GlobalState::SetState(State::MainMenu); - while (isRunning) { HandleEvents(); - if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard(); + if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); glCheckError(); RenderFrame(); - while (listener.IsEventsQueueIsNotEmpty()) { - listener.HandleEvent(); - } + listener.HandleAllEvents(); timer.Update(); } - EventAgregator::PushEvent(EventType::Exit, ExitData{}); + PUSH_EVENT("Exit", 0); } void Render::RenderGui() { @@ -361,7 +357,7 @@ void Render::RenderGui() { static int port = 25565; static char buffName[512] = "HelloOne"; if (ImGui::Button("Connect")) { - EventAgregator::PushEvent(EventType::ConnectToServer, ConnectToServerData{ std::string(buffName), buff, (unsigned short)port }); + PUSH_EVENT("ConnectToServer", std::make_tuple(std::string(buff), (unsigned short)port, std::string(buffName))); } ImGui::InputText("Username", buffName, 512); ImGui::InputText("Address", buff, 512); @@ -385,7 +381,7 @@ void Render::RenderGui() { ImGui::InputText("", buff, 256); ImGui::SameLine(); if (ImGui::Button("Send")) { - EventAgregator::PushEvent(EventType::SendChatMessage, SendChatMessageData{ buff }); + PUSH_EVENT("SendChatMessage", std::string(buff)); } ImGui::End(); break; @@ -503,7 +499,7 @@ void Render::RenderGui() { if (ImGui::Button("Apply settings")) { if (distance != world->MaxRenderingDistance) { world->MaxRenderingDistance = distance; - EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + PUSH_EVENT("UpdateSectionsRender", 0); } if (sense != sensetivity) @@ -515,7 +511,7 @@ void Render::RenderGui() { ImGui::Separator(); if (ImGui::Button("Disconnect")) { - EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Disconnected by user" }); + PUSH_EVENT("Disconnect", std::string("Disconnected by user")); } ImGui::End(); break; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 4f77aca..b9745be 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -16,11 +16,11 @@ void RendererWorld::WorkerFunction(size_t workerId) { EventListener tasksListener; - tasksListener.RegisterHandler(EventType::RendererWorkerTask, [&](EventData eventData) { - auto data = std::get<RendererWorkerTaskData>(eventData); - if (data.WorkerId != workerId) + tasksListener.RegisterHandler("RendererWorkerTask", [&](const Event& eventData) { + auto data = eventData.get<std::tuple<size_t, Vector>>(); + if (std::get<0>(data) != workerId) return; - Vector vec = data.Task; + Vector vec = std::get<1>(data); sectionsMutex.lock(); auto result = sections.find(vec); @@ -31,7 +31,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { renderDataMutex.lock(); renderData.push(std::move(data)); renderDataMutex.unlock(); - EventAgregator::PushEvent(EventType::NewRenderDataAvailable, NewRenderDataAvailableData{}); + PUSH_EVENT("NewRenderDataAvailable", 0); sectionsMutex.lock(); } else { @@ -46,7 +46,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { renderDataMutex.lock(); renderData.push(std::move(data)); renderDataMutex.unlock(); - EventAgregator::PushEvent(EventType::NewRenderDataAvailable, NewRenderDataAvailableData{}); + PUSH_EVENT("NewRenderDataAvailable", 0); sectionsMutex.lock(); } sectionsMutex.unlock(); @@ -54,7 +54,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { LoopExecutionTimeController timer(std::chrono::milliseconds(50)); while (isRunning) { - while (tasksListener.IsEventsQueueIsNotEmpty() && isRunning) + while (tasksListener.NotEmpty() && isRunning) tasksListener.HandleEvent(); timer.Update(); } @@ -83,7 +83,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) sectionsMutex.unlock(); for (auto& it : toRemove) { - EventAgregator::PushEvent(EventType::DeleteSectionRender, DeleteSectionRenderData{ it }); + PUSH_EVENT("DeleteSectionRender", 0); } playerChunk.y = std::floor(gs->player->pos.y / 16.0); @@ -94,7 +94,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) }); for (auto& it : suitableChunks) { - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ it }); + PUSH_EVENT("ChunkChanged", it); } } @@ -108,8 +108,8 @@ RendererWorld::RendererWorld(GameState* ptr) { PrepareRender(); - listener->RegisterHandler(EventType::DeleteSectionRender, [this](EventData eventData) { - auto vec = std::get<DeleteSectionRenderData>(eventData).pos; + listener->RegisterHandler("DeleteSectionRender", [this](const Event& eventData) { + auto vec = eventData.get<Vector>(); sectionsMutex.lock(); auto it = sections.find(vec); if (it == sections.end()) { @@ -120,7 +120,7 @@ RendererWorld::RendererWorld(GameState* ptr) { sectionsMutex.unlock(); }); - listener->RegisterHandler(EventType::NewRenderDataAvailable,[this](EventData eventData) { + listener->RegisterHandler("NewRenderDataAvailable",[this](const Event&) { renderDataMutex.lock(); int i = 0; while (!renderData.empty() && i++ < 20) { @@ -149,17 +149,17 @@ RendererWorld::RendererWorld(GameState* ptr) { renderDataMutex.unlock(); }); - listener->RegisterHandler(EventType::EntityChanged, [this](EventData eventData) { - auto data = std::get<EntityChangedData>(eventData); + listener->RegisterHandler("EntityChanged", [this](const Event& eventData) { + auto data = eventData.get<unsigned int>(); for (unsigned int entityId : gs->world.GetEntitiesList()) { - if (entityId == data.EntityId) { + if (entityId == data) { entities.push_back(RendererEntity(&gs->world, entityId)); } } }); - listener->RegisterHandler(EventType::ChunkChanged, [this](EventData eventData) { - auto vec = std::get<ChunkChangedData>(eventData).chunkPosition; + listener->RegisterHandler("ChunkChanged", [this](const Event& eventData) { + auto vec = eventData.get<Vector>(); Vector playerChunk(std::floor(gs->player->pos.x / 16), 0, std::floor(gs->player->pos.z / 16)); double distanceToChunk = (Vector(vec.x, 0, vec.z) - playerChunk).GetLength(); @@ -177,22 +177,22 @@ RendererWorld::RendererWorld(GameState* ptr) { isParsing[vec] = true; isParsingMutex.unlock(); - EventAgregator::PushEvent(EventType::RendererWorkerTask, RendererWorkerTaskData{ currentWorker++,vec }); + PUSH_EVENT("RendererWorkerTask", std::make_tuple(currentWorker++, vec)); if (currentWorker >= numOfWorkers) currentWorker = 0; }); - listener->RegisterHandler(EventType::UpdateSectionsRender, [this](EventData eventData) { + listener->RegisterHandler("UpdateSectionsRender", [this](const Event&) { UpdateAllSections(gs->player->pos); }); - listener->RegisterHandler(EventType::PlayerPosChanged, [this](EventData eventData) { - auto pos = std::get<PlayerPosChangedData>(eventData).newPos; + listener->RegisterHandler("PlayerPosChanged", [this](const Event& eventData) { + auto pos = eventData.get<VectorF>(); UpdateAllSections(pos); }); - listener->RegisterHandler(EventType::ChunkDeleted, [this](EventData eventData) { - auto pos = std::get<ChunkDeletedData>(eventData).pos; + listener->RegisterHandler("ChunkDeleted", [this](const Event& eventData) { + auto pos = eventData.get<Vector>(); sectionsMutex.lock(); auto it = sections.find(pos); if (it != sections.end()) @@ -203,7 +203,7 @@ RendererWorld::RendererWorld(GameState* ptr) { for (int i = 0; i < numOfWorkers; i++) workers.push_back(std::thread(&RendererWorld::WorkerFunction, this, i)); - EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + PUSH_EVENT("UpdateSectionsRender", 0); } RendererWorld::~RendererWorld() { @@ -376,10 +376,10 @@ void RendererWorld::PrepareRender() { void RendererWorld::Update(double timeToUpdate) { static auto timeSincePreviousUpdate = std::chrono::steady_clock::now(); int i = 0; - while (listener->IsEventsQueueIsNotEmpty() && i++ < 50) + while (listener->NotEmpty() && i++ < 50) listener->HandleEvent(); if (std::chrono::steady_clock::now() - timeSincePreviousUpdate > std::chrono::seconds(5)) { - EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + PUSH_EVENT("UpdateSectionsRender", 0); timeSincePreviousUpdate = std::chrono::steady_clock::now(); } diff --git a/src/World.cpp b/src/World.cpp index c6f3fc8..9df8889 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -26,7 +26,7 @@ void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { using std::swap; swap(*sections.at(chunkPosition).get(), section); } - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ chunkPosition }); + PUSH_EVENT("ChunkChanged", chunkPosition); } } } @@ -272,7 +272,7 @@ void World::ParseChunkData(std::shared_ptr<PacketBlockChange> packet) { SetBlockId(packet->Position, BlockId{(unsigned short) (packet->BlockId >> 4),(unsigned char) (packet->BlockId & 0xF) }); Vector sectionPos(std::floor(packet->Position.x / 16.0), std::floor(packet->Position.y / 16.0), std::floor(packet->Position.z / 16.0)); - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ sectionPos }); + PUSH_EVENT("ChunkChanged", sectionPos); } void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) { @@ -290,7 +290,7 @@ void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) { } for (auto& sectionPos : changedSections) - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ sectionPos }); + PUSH_EVENT("ChunkChanged", sectionPos); } void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) { @@ -300,7 +300,7 @@ void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) { toRemove.push_back(it); } for (auto& it : toRemove) { - EventAgregator::PushEvent(EventType::ChunkDeleted, ChunkDeletedData{ it->first }); + PUSH_EVENT("ChunkDeleted", it->first); sections.erase(it); } UpdateSectionsList(); diff --git a/src/main.cpp b/src/main.cpp index 2b213b3..8be8412 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include <set> +#include <SDL.h> #include <easylogging++.h> const char *getTimeSinceProgramStart(void) { @@ -36,11 +37,11 @@ void initLogger() { int main(int argc, char** argv) { srand(time(0)); initLogger(); - LOG(WARNING) << "Sizeof EventData is " << sizeof(EventData); + //LOG(WARNING) << "Sizeof EventData is " << sizeof(EventData); try { if (SDL_Init(0) == -1) - throw std::runtime_error("SDL initialization failed: " + std::string(SDL_GetError())); + throw std::runtime_error(std::string("SDL initialization failed: ") + SDL_GetError()); } catch (std::exception& e) { LOG(ERROR) << e.what(); return -1; |