diff --git a/.gitignore b/.gitignore index 05b3f326..d84be798 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,13 @@ #Visual Studio Debug/ Release/ - +*.sln *.vcxproj *.filters +*.users *.user - *.dll +.vs # Visual Studio Code .vscode/ diff --git a/Source/Application.cpp b/Source/Application.cpp index a341ecbb..d06d9ca4 100644 --- a/Source/Application.cpp +++ b/Source/Application.cpp @@ -52,6 +52,7 @@ void Application::handleEvents() sf::Event e; while (m_context.window.pollEvent(e)) { + m_states.back()->handleEvent(e); switch(e.type) { case sf::Event::Closed: diff --git a/Source/Input/Keyboard.cpp b/Source/Input/Keyboard.cpp new file mode 100644 index 00000000..d805adb4 --- /dev/null +++ b/Source/Input/Keyboard.cpp @@ -0,0 +1,31 @@ +#include "Keyboard.h" + + +Keyboard::Keyboard() { + std::fill(m_keys.begin(), m_keys.end(), false); +} + +void Keyboard::update(sf::Event e) { + m_recentlyReleased = sf::Keyboard::KeyCount; + switch (e.type) { + case sf::Event::KeyReleased: + m_keys[e.key.code] = false; + break; + + case sf::Event::KeyPressed: + m_recentlyReleased = e.key.code; + m_keys[e.key.code] = true; + break; + + default: + break; + } +} + +bool Keyboard::isKeyDown(sf::Keyboard::Key key) const { + return m_keys[key]; +} + +bool Keyboard::keyReleased(sf::Keyboard::Key key) const { + return m_recentlyReleased == key; +} \ No newline at end of file diff --git a/Source/Input/Keyboard.h b/Source/Input/Keyboard.h new file mode 100644 index 00000000..8c0aaebe --- /dev/null +++ b/Source/Input/Keyboard.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +#include + +class Keyboard { +public: + Keyboard(); + + void update(sf::Event e); + + bool isKeyDown(sf::Keyboard::Key key) const; + bool keyReleased(sf::Keyboard::Key key) const; + +private: + std::array m_keys; + sf::Keyboard::Key m_recentlyReleased; +}; \ No newline at end of file diff --git a/Source/ToggleKey.cpp b/Source/Input/ToggleKey.cpp similarity index 100% rename from Source/ToggleKey.cpp rename to Source/Input/ToggleKey.cpp diff --git a/Source/ToggleKey.h b/Source/Input/ToggleKey.h similarity index 100% rename from Source/ToggleKey.h rename to Source/Input/ToggleKey.h diff --git a/Source/Maths/NoiseGenerator.cpp b/Source/Maths/NoiseGenerator.cpp index eaad04a7..80ba0a54 100644 --- a/Source/Maths/NoiseGenerator.cpp +++ b/Source/Maths/NoiseGenerator.cpp @@ -19,8 +19,6 @@ void NoiseGenerator::setParameters(const NoiseParameters& params) noexcept m_noiseParameters = params; } - -//wtf? double NoiseGenerator::getNoise(int n) const noexcept { n += m_seed; @@ -32,7 +30,7 @@ double NoiseGenerator::getNoise(int n) const noexcept double NoiseGenerator::getNoise(double x, double z) const noexcept { - return getNoise(x + z * 57); + return getNoise(x + z * 57.0); } double NoiseGenerator::lerp(double a, double b, double z) const noexcept diff --git a/Source/Player/Player.cpp b/Source/Player/Player.cpp index c2c9ad43..5ab44af7 100644 --- a/Source/Player/Player.cpp +++ b/Source/Player/Player.cpp @@ -6,8 +6,9 @@ #include #include -#include "../World/World.h" +#include "../Input/Keyboard.h" +#include "../World/World.h" #include "../Renderer/RenderMaster.h" sf::Font f; @@ -74,9 +75,9 @@ ItemStack& Player::getHeldItems() } -void Player::handleInput(const sf::Window& window) +void Player::handleInput(const sf::Window& window, Keyboard& keyboard) { - keyboardInput(); + keyboardInput(keyboard); mouseInput(window); if(m_itemDown.isKeyPressed()) @@ -205,36 +206,36 @@ void Player::collide(World& world, const glm::vec3& vel, float dt) float speed = 0.2f; -void Player::keyboardInput() +void Player::keyboardInput(Keyboard& keyboard) { - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) + if (keyboard.isKeyDown(sf::Keyboard::W)) { float s = speed; if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) s *= 5; m_acceleration.x += -glm::cos(glm::radians(rotation.y + 90)) * s; m_acceleration.z += -glm::sin(glm::radians(rotation.y + 90)) * s; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) + if (keyboard.isKeyDown(sf::Keyboard::S)) { m_acceleration.x += glm::cos(glm::radians(rotation.y + 90)) * speed; m_acceleration.z += glm::sin(glm::radians(rotation.y + 90)) * speed; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) + if (keyboard.isKeyDown(sf::Keyboard::A)) { m_acceleration.x += -glm::cos(glm::radians(rotation.y)) * speed; m_acceleration.z += -glm::sin(glm::radians(rotation.y)) * speed; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) + if (keyboard.isKeyDown(sf::Keyboard::D)) { m_acceleration.x += glm::cos(glm::radians(rotation.y)) * speed; m_acceleration.z += glm::sin(glm::radians(rotation.y)) * speed; } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) + if (keyboard.isKeyDown(sf::Keyboard::Space)) { jump(); } - else if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) && m_isFlying) + else if (keyboard.isKeyDown(sf::Keyboard::LShift) && m_isFlying) { m_acceleration.y -= speed * 3; } @@ -255,7 +256,7 @@ void Player::mouseInput(const sf::Window& window) return; } - static float const BOUND = 89.9999; + static float const BOUND = 89.f; static auto lastMousePosition = sf::Mouse::getPosition(window); auto change = sf::Mouse::getPosition() - lastMousePosition; diff --git a/Source/Player/Player.h b/Source/Player/Player.h index 0e0340ae..40ae191a 100644 --- a/Source/Player/Player.h +++ b/Source/Player/Player.h @@ -6,8 +6,9 @@ #include "../Entity.h" #include "../Item/ItemStack.h" -#include "../ToggleKey.h" +#include "../Input/ToggleKey.h" +class Keyboard; class World; class RenderMaster; @@ -16,7 +17,7 @@ class Player : public Entity public: Player(); - void handleInput(const sf::Window& window); + void handleInput(const sf::Window& window, Keyboard& keyboard); void update(float dt, World& wolrd); void collide(World& world, const glm::vec3& vel, float dt); @@ -30,7 +31,7 @@ class Player : public Entity private: void jump(); - void keyboardInput(); + void keyboardInput(Keyboard& keyboard); void mouseInput(const sf::Window& window); bool m_isOnGround = false; bool m_isFlying = false; diff --git a/Source/States/PlayingState.cpp b/Source/States/PlayingState.cpp index af7a9918..c3c384da 100644 --- a/Source/States/PlayingState.cpp +++ b/Source/States/PlayingState.cpp @@ -14,33 +14,27 @@ StatePlaying::StatePlaying(Application& app, const Config& config) , m_world (app.getCamera(), config, m_player) { app.getCamera().hookEntity(m_player); - - m_chTexture.loadFromFile("Res/Textures/ch.png"); - m_crosshair.setTexture(&m_chTexture); - m_crosshair.setSize({21, 21}); - m_crosshair.setOrigin(m_crosshair.getGlobalBounds().width / 2, - m_crosshair.getGlobalBounds().height / 2); - m_crosshair.setPosition(app.getWindow().getSize().x / 2, - app.getWindow().getSize().y / 2); } void StatePlaying::handleEvent(sf::Event e) -{ } +{ + m_keyboard.update(e); +} void StatePlaying::handleInput() { - m_player.handleInput(m_pApplication->getWindow()); + m_player.handleInput(m_pApplication->getWindow(), m_keyboard); static sf::Clock timer; glm::vec3 lastPosition; for (Ray ray({m_player.position.x, m_player.position.y + 0.6f, m_player.position.z}, m_player.rotation); //Corrected for camera offset ray.getLength() < 6; - ray.step(0.05)) + ray.step(0.05f)) { - int x = ray.getEnd().x; - int y = ray.getEnd().y; - int z = ray.getEnd().z; + int x = static_cast(ray.getEnd().x); + int y = static_cast(ray.getEnd().y); + int z = static_cast(ray.getEnd().z); auto block = m_world.getBlock(x, y, z); auto id = (BlockId)block.id; @@ -71,15 +65,12 @@ void StatePlaying::handleInput() void StatePlaying::update(float deltaTime) { - if (m_player.position.x < 0) m_player.position.x = 0; if (m_player.position.z < 0) m_player.position.z = 0; m_fpsCounter.update(); m_player.update(deltaTime, m_world); m_world.update(m_pApplication->getCamera()); - - } void StatePlaying::render(RenderMaster& renderer) @@ -97,7 +88,6 @@ void StatePlaying::render(RenderMaster& renderer) if (drawGUI) { m_fpsCounter.draw(renderer); - renderer.drawSFML(m_crosshair); m_player.draw(renderer); } diff --git a/Source/States/PlayingState.h b/Source/States/PlayingState.h index 2d767f99..2bac2a31 100644 --- a/Source/States/PlayingState.h +++ b/Source/States/PlayingState.h @@ -7,7 +7,7 @@ #include "../World/Chunk/Chunk.h" #include "../World/World.h" #include "../Util/FPSCounter.h" - +#include "../Input/Keyboard.h" class StatePlaying : public StateBase @@ -25,12 +25,10 @@ class StatePlaying : public StateBase void onOpen() override; private: + Keyboard m_keyboard; Player m_player; World m_world; - sf::RectangleShape m_crosshair; - sf::Texture m_chTexture; - FPSCounter m_fpsCounter; }; diff --git a/Source/Texture/TextureAtlas.cpp b/Source/Texture/TextureAtlas.cpp index 99cab7b4..1f257cf5 100644 --- a/Source/Texture/TextureAtlas.cpp +++ b/Source/Texture/TextureAtlas.cpp @@ -21,8 +21,8 @@ std::array TextureAtlas::getTexture(const sf::Vector2i& coords) static const GLfloat INDV_TEX_SIZE = 1.0f / TEX_PER_ROW; static const GLfloat PIXEL_SIZE = 1.0f / (float)m_imageSize; - GLfloat xMin = (coords.x * INDV_TEX_SIZE) + 0.5 * PIXEL_SIZE; - GLfloat yMin = (coords.y * INDV_TEX_SIZE) + 0.5 * PIXEL_SIZE; + GLfloat xMin = (coords.x * INDV_TEX_SIZE) + 0.5f * PIXEL_SIZE; + GLfloat yMin = (coords.y * INDV_TEX_SIZE) + 0.5f * PIXEL_SIZE; GLfloat xMax = (xMin + INDV_TEX_SIZE) - PIXEL_SIZE; GLfloat yMax = (yMin + INDV_TEX_SIZE) - PIXEL_SIZE; diff --git a/Source/Util/Random.cpp b/Source/Util/Random.cpp index 617fa8fd..f471150b 100644 --- a/Source/Util/Random.cpp +++ b/Source/Util/Random.cpp @@ -8,7 +8,10 @@ RandomSingleton& RandomSingleton::get() RandomSingleton::RandomSingleton() { - m_randomEngine.seed(std::time(nullptr)); - for (int i = 0; i < 5; i++) - intInRange(i, i * 5); + m_randomEngine.seed( + static_cast(std::time(nullptr)) + ); + for (int i = 0; i < 5; i++) { + intInRange(i, i * 5); + } } diff --git a/Source/World/Event/PlayerDigEvent.cpp b/Source/World/Event/PlayerDigEvent.cpp index 93a5eb0d..1bad2e61 100644 --- a/Source/World/Event/PlayerDigEvent.cpp +++ b/Source/World/Event/PlayerDigEvent.cpp @@ -12,7 +12,11 @@ PlayerDigEvent::PlayerDigEvent(sf::Mouse::Button button, const glm::vec3& locati void PlayerDigEvent::handle(World& world) { - auto chunkLocation = World::getChunkXZ(m_digSpot.x, m_digSpot.z); + auto chunkLocation = World::getChunkXZ( + static_cast(m_digSpot.x), + static_cast(m_digSpot.z) + ); + if (world.getChunkManager().chunkLoadedAt(chunkLocation.x, chunkLocation.z)) { dig(world); @@ -21,10 +25,13 @@ void PlayerDigEvent::handle(World& world) void PlayerDigEvent::dig(World& world) { + int x = static_cast(m_digSpot.x); + int y = static_cast(m_digSpot.y); + int z = static_cast(m_digSpot.z); switch (m_buttonPress) { case sf::Mouse::Button::Left:{ - auto block = world.getBlock(m_digSpot.x, m_digSpot.y, m_digSpot.z); + auto block = world.getBlock(x, y, z); const auto& material = Material::toMaterial((BlockId)block.id); m_pPlayer->addItem(material); /* @@ -39,8 +46,8 @@ void PlayerDigEvent::dig(World& world) world.updateChunk (newX, newY, newZ); world.setBlock (newX, newY, newZ, 0); */ - world.updateChunk (m_digSpot.x, m_digSpot.y, m_digSpot.z); - world.setBlock (m_digSpot.x, m_digSpot.y, m_digSpot.z, 0); + world.updateChunk (x, y, z); + world.setBlock (x, y, z, 0); //} break; } @@ -56,8 +63,8 @@ void PlayerDigEvent::dig(World& world) else { stack.remove(); - world.updateChunk (m_digSpot.x, m_digSpot.y, m_digSpot.z); - world.setBlock (m_digSpot.x, m_digSpot.y, m_digSpot.z, material.toBlockID()); + world.updateChunk (x, y, z); + world.setBlock (x, y, z, material.toBlockID()); break; } } diff --git a/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp b/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp index 5ae899cc..5fe82a8d 100644 --- a/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp +++ b/Source/World/Generation/Terrain/ClassicOverWorldGenerator.cpp @@ -80,10 +80,10 @@ void ClassicOverWorldGenerator::getHeightIn (int xMin, int zMin, int xMax, int z m_pChunk->getLocation().y); }; - int bottomLeft = getHeightAt(xMin, zMin); - int bottomRight = getHeightAt(xMax, zMin); - int topLeft = getHeightAt(xMin, zMax); - int topRight = getHeightAt(xMax, zMax); + float bottomLeft = static_cast(getHeightAt(xMin, zMin)); + float bottomRight = static_cast(getHeightAt(xMax, zMin)); + float topLeft = static_cast(getHeightAt(xMin, zMax)); + float topRight = static_cast(getHeightAt(xMax, zMax)); for (int x = xMin; x < xMax; ++x) for (int z = zMin; z < zMax; ++z) @@ -93,14 +93,14 @@ void ClassicOverWorldGenerator::getHeightIn (int xMin, int zMin, int xMax, int z if (z == CHUNK_SIZE) continue; - float h = smoothInterpolation(bottomLeft, topLeft, bottomRight, topRight, - xMin, xMax, - zMin, zMax, - x, z); + float h = smoothInterpolation( + bottomLeft, topLeft, bottomRight, topRight, + static_cast(xMin), static_cast(xMax), + static_cast(zMin), static_cast(zMax), + static_cast(x), static_cast(z)); - m_heightMap.get(x, z) = h; + m_heightMap.get(x, z) = static_cast(h); } - //exit(0); } @@ -123,8 +123,8 @@ void ClassicOverWorldGenerator::getBiomeMap() for (int x = 0; x < CHUNK_SIZE + 1; x++) for (int z = 0; z < CHUNK_SIZE + 1; z++) { - int h = m_biomeNoiseGen.getHeight(x, z, location.x + 10, location.y + 10); - m_biomeMap.get(x, z) = h; + double h = m_biomeNoiseGen.getHeight(x, z, location.x + 10, location.y + 10); + m_biomeMap.get(x, z) = static_cast(h); } } diff --git a/Source/World/World.cpp b/Source/World/World.cpp index 6622095c..38e0f00b 100644 --- a/Source/World/World.cpp +++ b/Source/World/World.cpp @@ -5,7 +5,7 @@ #include "../Maths/Vector2XZ.h" #include "../Camera.h" -#include "../ToggleKey.h" +#include "../Input/ToggleKey.h" #include "../Renderer/RenderMaster.h" #include "../Player/Player.h" #include "../Util/Random.h" @@ -18,7 +18,7 @@ World::World(const Camera& camera, const Config& config, Player& player) setSpawnPoint(); player.position = m_playerSpawnPoint; - for (int i = 0; i < 2; i++) + for (int i = 0; i < 1; i++) { std::this_thread::sleep_for(std::chrono::milliseconds(50)); m_chunkLoadThreads.emplace_back([&]()