From 04ab1a3420b46af046a898ee5510e0d9b25ed24c Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 27 Aug 2017 20:24:28 +0500 Subject: 2017-08-27 --- src/Render.cpp | 178 ++++++++++++++++++++++++++------------------------------- 1 file changed, 82 insertions(+), 96 deletions(-) (limited to 'src/Render.cpp') diff --git a/src/Render.cpp b/src/Render.cpp index 7e88068..c739b77 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -6,12 +6,12 @@ #include "Event.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(0)) { - InitSfml(windowWidth, windowHeight, windowTitle); - glCheckError(); - InitGlew(); - glCheckError(); - PrepareToRendering(); - glCheckError(); + InitSfml(windowWidth, windowHeight, windowTitle); + glCheckError(); + InitGlew(); + glCheckError(); + PrepareToRendering(); + glCheckError(); } Render::~Render() { @@ -29,7 +29,8 @@ void Render::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string glCheckError(); window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2, sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2)); - SetMouseCapture(false); + window->setKeyRepeatEnabled(false); + SetMouseCapture(false); renderState.WindowWidth = WinWidth; renderState.WindowHeight = WinHeight; } @@ -59,6 +60,23 @@ void Render::PrepareToRendering() { AssetManager::Instance().GetTextureAtlasIndexes(); } +void Render::UpdateKeyboard() { + sf::Keyboard::Key toUpdate[] = { sf::Keyboard::A,sf::Keyboard::W,sf::Keyboard::S,sf::Keyboard::D,sf::Keyboard::Space }; + for (auto key : toUpdate) { + bool isPressed = sf::Keyboard::isKeyPressed(key); + if (!isKeyPressed[key] && isPressed) { + EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ key }); + } + if (isKeyPressed[key] && isPressed) { + //KeyHeld + } + if (isKeyPressed[key] && !isPressed) { + EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ key }); + } + isKeyPressed[key] = isPressed; + } +} + void Render::RenderFrame() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -75,92 +93,62 @@ void Render::RenderFrame() { void Render::HandleEvents() { sf::Event event; - while (window->pollEvent(event)) { - switch (event.type) { - case sf::Event::Closed: - LOG(INFO) << "Received close event by window closing"; - isRunning = false; - 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{ "10.1.1.2", 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; - case sf::Keyboard::Space: - EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::Space }); - 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; - case sf::Keyboard::Space: - EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::Space }); - break; - default: - break; + while (window->pollEvent(event)) { + switch (event.type) { + case sf::Event::Closed: + LOG(INFO) << "Received close event by window closing"; + isRunning = false; + 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{ "10.1.1.2", 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{}); } - default: - break; - } - } + break; + case sf::Keyboard::L: + if (renderWorld) { + world->MaxRenderingDistance++; + LOG(INFO) << "Increased rendering distance: " << world->MaxRenderingDistance; + EventAgregator::PushEvent(EventType::UpdateSectionsRender, UpdateSectionsRenderData{}); + } + break; + default: + break; + } + case sf::Event::KeyReleased: + if (!window->hasFocus()) break; + switch (event.key.code) { + default: + break; + } + default: + break; + } + } } void Render::HandleMouseCapture() { @@ -216,6 +204,7 @@ void Render::ExecuteRenderLoop() { while (isRunning) { HandleEvents(); + if (window->hasFocus()) UpdateKeyboard(); if (isMouseCaptured) HandleMouseCapture(); glCheckError(); @@ -223,10 +212,7 @@ void Render::ExecuteRenderLoop() { while (listener.IsEventsQueueIsNotEmpty()) listener.HandleEvent(); if (renderWorld) { - world->renderDataMutex.lock(); - size_t size = world->renderData.size(); - world->renderDataMutex.unlock(); - window->setTitle("Size: " + std::to_string(size) + " FPS: " + std::to_string(1.0 / timer.GetRealDeltaS())); + window->setTitle("FPS: " + std::to_string(1.0 / timer.GetRealDeltaS())); } timer.Update(); } -- cgit v1.2.3