diff options
Diffstat (limited to '')
-rw-r--r-- | src/Render.cpp | 129 |
1 files changed, 95 insertions, 34 deletions
diff --git a/src/Render.cpp b/src/Render.cpp index 9121d1a..38a6ab0 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -30,6 +30,8 @@ void Render::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); SetMouseCapture(false); + renderState.WindowWidth = WinWidth; + renderState.WindowHeight = WinHeight; } void Render::InitGlew() { @@ -51,19 +53,18 @@ void Render::InitGlew() { } void Render::PrepareToRendering() { - shader = new Shader("./shaders/face.vs", "./shaders/face.fs"); - shader->Use(); - //TextureAtlas texture glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas()); - glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0); + glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas()); } void Render::RenderFrame() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + if (renderWorld) + world->Render(renderState); + window->display(); } @@ -77,33 +78,78 @@ void Render::HandleEvents() { break; case sf::Event::Resized: glViewport(0, 0, window->getSize().x, window->getSize().y); + renderState.WindowWidth = window->getSize().x; + renderState.WindowHeight = window->getSize().y; break; case sf::Event::KeyPressed: if (!window->hasFocus()) break; - switch (event.key.code) { - case sf::Keyboard::Escape: - LOG(INFO) << "Received close event by esc"; - isRunning = false; - break; - case sf::Keyboard::T: - SetMouseCapture(!isMouseCaptured); - break; - case sf::Keyboard::U: - EventAgregator::PushEvent(EventType::ConnectToServer, ConnectToServerData{"127.0.0.1", 25565}); + switch (event.key.code) { + case sf::Keyboard::Escape: + LOG(INFO) << "Received close event by esc"; + isRunning = false; + break; + case sf::Keyboard::T: + SetMouseCapture(!isMouseCaptured); + break; + case sf::Keyboard::U: + EventAgregator::PushEvent(EventType::ConnectToServer, ConnectToServerData{ "127.0.0.1", 25565 }); + break; + case sf::Keyboard::I: + EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Manual disconnect" }); + break; + case sf::Keyboard::K: + if (renderWorld) { + world->MaxRenderingDistance--; + if (world->MaxRenderingDistance <= 0) + world->MaxRenderingDistance = 1; + LOG(INFO) << "Decreased rendering distance: " << world->MaxRenderingDistance; + EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + } + break; + case sf::Keyboard::L: + if (renderWorld) { + world->MaxRenderingDistance++; + LOG(INFO) << "Increased rendering distance: " << world->MaxRenderingDistance; + EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + } + break; + case sf::Keyboard::W: + EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::W }); + break; + case sf::Keyboard::A: + EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::A }); + break; + case sf::Keyboard::S: + EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::S }); + break; + case sf::Keyboard::D: + EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::D }); + break; default: break; } + case sf::Event::KeyReleased: + if (!window->hasFocus()) break; + switch (event.key.code) { + case sf::Keyboard::W: + EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::W }); + break; + case sf::Keyboard::A: + EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::A }); + break; + case sf::Keyboard::S: + EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::S }); + break; + case sf::Keyboard::D: + EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::D }); + break; + default: + break; + } default: break; } - } - if (window->hasFocus()) { - /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) gameState->HandleMovement(GameState::FORWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) gameState->HandleMovement(GameState::BACKWARD, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) gameState->HandleMovement(GameState::LEFT, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) gameState->HandleMovement(GameState::RIGHT, deltaTime); - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) gameState->HandleMovement(GameState::JUMP, deltaTime);*/ - } + } } void Render::HandleMouseCapture() { @@ -112,7 +158,7 @@ void Render::HandleMouseCapture() { sf::Mouse::setPosition(center, *window); mouseXDelta = (mousePos - center).x, mouseYDelta = (center - mousePos).y; const float Sensetivity = 0.7f; - //gameState->HandleRotation(mouseXDelta * Sensetivity, mouseYDelta * Sensetivity); + EventAgregator::DirectEventCall(EventType::MouseMoved, MouseMovedData{ mouseXDelta * Sensetivity, mouseYDelta }); } void Render::SetMouseCapture(bool IsCaptured) { @@ -125,11 +171,7 @@ void Render::SetMouseCapture(bool IsCaptured) { void Render::ExecuteRenderLoop() { EventListener listener; - listener.RegisterHandler(EventType::GlobalAppState, [this](EventData eventData) { - - }); - - /*listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { + listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { auto data = std::get<ConnectionSuccessfullData>(eventData); window->setTitle("Connected"); }); @@ -137,14 +179,31 @@ void Render::ExecuteRenderLoop() { listener.RegisterHandler(EventType::PlayerConnected, [this](EventData eventData) { auto data = std::get<PlayerConnectedData>(eventData); window->setTitle("Joined the game"); + world = std::make_unique<RendererWorld>(data.ptr); }); listener.RegisterHandler(EventType::RemoveLoadingScreen, [this](EventData eventData) { - window->setTitle("Loaded"); - });*/ + window->setTitle("Playing"); + renderWorld = true; + }); + + listener.RegisterHandler(EventType::ConnectionFailed, [this](EventData eventData) { + window->setTitle("Connection failed: " + std::get<ConnectionFailedData>(eventData).reason); + renderWorld = false; + world.reset(); + }); - using namespace std::chrono_literals; - LoopExecutionTimeController timer(16ms); + listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) { + window->setTitle("Disconnected: " + std::get<DisconnectedData>(eventData).reason); + renderWorld = false; + world.reset(); + }); + + listener.RegisterHandler(EventType::Connecting, [this](EventData eventData) { + window->setTitle("Connecting"); + }); + + LoopExecutionTimeController timer(std::chrono::milliseconds(16)); while (isRunning) { HandleEvents(); if (isMouseCaptured) HandleMouseCapture(); @@ -153,7 +212,9 @@ void Render::ExecuteRenderLoop() { RenderFrame(); while (listener.IsEventsQueueIsNotEmpty()) listener.HandleEvent(); + if (renderWorld) + window->setTitle("FPS: "+std::to_string(1.0/timer.GetDeltaMs()*1000.0)); timer.Update(); } - SetGlobalState(GlobalState::Exiting); + EventAgregator::PushEvent(EventType::Exit, ExitData{}); } |