diff --git a/src/Common.h b/src/Common.h index 3873299..70e71ea 100644 --- a/src/Common.h +++ b/src/Common.h @@ -18,8 +18,8 @@ #include #include -#include -#include +#include +#include /// Constants @@ -36,4 +36,4 @@ constexpr float LIGHT_ABSOLUTE = 80.f; #define _METHOD(x) std::bind(x, this, std::placeholders::_1) // Only usable in TileMap -#define FOR_EACH_TILE(action) for (uint x = 0; x < width; x++) { for (uint y = 0; y < height; y++) { action; } } \ No newline at end of file +#define FOR_EACH_TILE(action) for (uint x = 0; x < width; x++) { for (uint y = 0; y < height; y++) { action; } } diff --git a/src/app/Application.h b/src/app/Application.h index 9c16233..ddab7e6 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -20,7 +20,7 @@ #include "../events/Events.h" constexpr bool VSYNC_ENABLED = true; -constexpr bool VSYNC_DISABLED = false; +constexpr bool VSYNC_DISABLED = !VSYNC_DISABLED; class Application : public Events::IEventListener { diff --git a/src/graphics/Animator.cpp b/src/graphics/Animator.cpp index d6a27c8..512b2aa 100644 --- a/src/graphics/Animator.cpp +++ b/src/graphics/Animator.cpp @@ -38,12 +38,12 @@ namespace Graphics if (!animFound) return; - float animLength = anim->length / (float)anim->fps; + const float animLength = anim->length / (float)anim->fps; if (m_timer >= animLength) m_timer -= animLength; - uint frameIndex = static_cast((m_timer / animLength) * anim->length); - uint textureX = anim->xpos + frameIndex * anim->stride; - uint textureY = anim->ypos; + const uint frameIndex = static_cast((m_timer / animLength) * anim->length); + const uint textureX = anim->xpos + frameIndex * anim->stride; + const uint textureY = anim->ypos; destsprite.setTextureRect(sf::IntRect(textureX, textureY, destsprite.getTextureRect().width, destsprite.getTextureRect().height)); } -} \ No newline at end of file +} diff --git a/src/graphics/Label.cpp b/src/graphics/Label.cpp index 56b4c72..71151fd 100644 --- a/src/graphics/Label.cpp +++ b/src/graphics/Label.cpp @@ -32,8 +32,8 @@ namespace Graphics void Label::updateBounds() { - sf::FloatRect rect = m_text.getLocalBounds(); - Vec2 size(rect.width, rect.height); + const sf::FloatRect rect = m_text.getLocalBounds(); + const Vec2 size(rect.width, rect.height); switch (m_alignment) { case Alignment::LEFT: @@ -50,4 +50,4 @@ namespace Graphics } m_text.setPosition(m_position + m_alignmentOffset); } -} \ No newline at end of file +} diff --git a/src/graphics/gui/Button.cpp b/src/graphics/gui/Button.cpp index 2d800cb..80c5642 100644 --- a/src/graphics/gui/Button.cpp +++ b/src/graphics/gui/Button.cpp @@ -53,7 +53,7 @@ namespace GUI bool Button::mouseMoved(Events::MouseMovedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); if (!m_bounds.contains(mouse)) m_state = ButtonState::UNPRESSED; @@ -62,7 +62,7 @@ namespace GUI void Button::update() { - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); m_hovered = m_bounds.contains(mouse); updateTexture(); } @@ -73,4 +73,4 @@ namespace GUI window.draw(*m_sprite, m_states); m_label->render(window, m_states); } -} \ No newline at end of file +} diff --git a/src/graphics/gui/Panel.cpp b/src/graphics/gui/Panel.cpp index cf040b8..c55a616 100644 --- a/src/graphics/gui/Panel.cpp +++ b/src/graphics/gui/Panel.cpp @@ -57,10 +57,9 @@ namespace GUI bool Panel::mousePressed(Events::MousePressedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); - for (uint i = 0; i < m_widgets.size(); i++) + const Vec2 mouse = Application::get().uiMousePos(); + for (Widget* const widget : m_widgets) { - Widget* widget = m_widgets[i]; if (widget->getBounds().contains(mouse)) { if (widget->isActive()) @@ -73,10 +72,9 @@ namespace GUI bool Panel::mouseReleased(Events::MouseReleasedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); - for (uint i = 0; i < m_widgets.size(); i++) + const Vec2 mouse = Application::get().uiMousePos(); + for (Widget* const widget : m_widgets) { - Widget* widget = m_widgets[i]; if (widget->getBounds().contains(mouse)) { if (widget->isActive()) @@ -91,7 +89,7 @@ namespace GUI { for (uint i = 0; i < m_widgets.size(); i++) { - Widget* widget = m_widgets[i]; + Widget* const widget = m_widgets[i]; if (widget->isActive()) if (widget->mouseMoved(e)) return true; @@ -101,7 +99,7 @@ namespace GUI void Panel::update() { - for (Widget* widget : m_widgets) + for (Widget* const widget : m_widgets) { if (widget->isActive()) widget->update(); @@ -110,10 +108,10 @@ namespace GUI void Panel::render(sf::RenderWindow& window) { - for (Widget* widget : m_widgets) + for (Widget* const widget : m_widgets) { if (widget->isActive()) widget->render(window); } } -} \ No newline at end of file +} diff --git a/src/graphics/gui/Slider.cpp b/src/graphics/gui/Slider.cpp index 1bf1d21..346f5fd 100644 --- a/src/graphics/gui/Slider.cpp +++ b/src/graphics/gui/Slider.cpp @@ -33,7 +33,7 @@ namespace GUI , m_atlas((sf::Texture*)&Application::get().getTexture("/gui/slider")) , m_bgSprite(new sf::RectangleShape(m_bounds.size)) { - float size = vertical ? bounds.width : bounds.height; + const float size = vertical ? bounds.width : bounds.height; m_headBounds = FloatRectangle(bounds.x, bounds.y, size, size); m_headSprite = new sf::RectangleShape(m_headBounds.size); @@ -44,7 +44,7 @@ namespace GUI bool Slider::mousePressed(Events::MousePressedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); if (m_hovered) { m_state = SliderState::PRESSEDHEAD; @@ -62,7 +62,7 @@ namespace GUI bool Slider::mouseMoved(Events::MouseMovedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); if (m_state == SliderState::PRESSEDHEAD) { if (m_vertical) @@ -79,7 +79,7 @@ namespace GUI if (!sf::Mouse::isButtonPressed(sf::Mouse::Left)) m_state = SliderState::UNPRESSED; - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); m_hovered = m_headBounds.contains(mouse); updateTexture(); diff --git a/src/graphics/gui/Window.cpp b/src/graphics/gui/Window.cpp index 40f6e2c..69e7201 100644 --- a/src/graphics/gui/Window.cpp +++ b/src/graphics/gui/Window.cpp @@ -18,7 +18,7 @@ namespace GUI updateButtons(); // Setup shader and texture - sf::Shader* transparency = new sf::Shader(); + sf::Shader* const transparency = new sf::Shader(); transparency->loadFromMemory(getFileContents("res/shaders/transparency.frag"), sf::Shader::Fragment); transparency->setUniform("u_Opacity", .9f); transparency->setUniform("u_Texture", sf::Shader::CurrentTexture); @@ -32,7 +32,7 @@ namespace GUI bool Window::mousePressed(Events::MousePressedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); if (close_button.contains(mouse)) { @@ -64,7 +64,7 @@ namespace GUI bool Window::mouseMoved(Events::MouseMovedEvent& e) { - Vec2 mouse = Application::get().uiMousePos(); + const Vec2 mouse = Application::get().uiMousePos(); if (m_resizing.active) { @@ -123,8 +123,8 @@ namespace GUI { m_vao.clear(); - float width = m_bounds.width; - float height = m_bounds.height; + const float width = m_bounds.width; + const float height = m_bounds.height; ///@TODO: Get rid of these hardcoded values // Top left corner diff --git a/src/item/ItemFactory.cpp b/src/item/ItemFactory.cpp index 32c3b65..dd55daa 100644 --- a/src/item/ItemFactory.cpp +++ b/src/item/ItemFactory.cpp @@ -21,17 +21,15 @@ namespace Item void ItemFactory::createTemplate(std::string filePath) { - std::string source = getFileContents("res/items" + filePath); + const std::string source = getFileContents("res/items" + filePath); nlohmann::json json = nlohmann::json::parse(source.c_str()); std::unique_ptr item = std::make_unique(); - std::vector componentsJSON = json["components"]; + const std::vector componentsJSON = json["components"]; - for (unsigned int i=0; i < componentsJSON.size(); i++) + for (const auto& componentJSON : componentsJSON) { - nlohmann::json componentJSON = componentsJSON[i]; - using namespace Components; if (componentJSON["componentType"].get() == "Sprite") diff --git a/src/level/Level.cpp b/src/level/Level.cpp index 537571b..4312aea 100644 --- a/src/level/Level.cpp +++ b/src/level/Level.cpp @@ -16,9 +16,7 @@ namespace Level auto data = m_worldGen.getMap(); - TileMap::AddList addList; - - addList = data.addList; + TileMap::AddList addList = data.addList; m_tiles.addTiles(0, addList); @@ -48,8 +46,8 @@ namespace Level std::sort(m_entities.begin(), m_entities.end(), [](const std::unique_ptr& lhs, const std::unique_ptr& rhs) { - Components::PhysicsComponent* c_lhs = lhs.get()->getComponent(); - Components::PhysicsComponent* c_rhs = rhs.get()->getComponent(); + const auto* c_lhs = lhs.get()->getComponent(); + const auto* c_rhs = rhs.get()->getComponent(); return c_lhs->pos.y + c_lhs->sortOffset < c_rhs->pos.y + c_rhs->sortOffset; }); @@ -86,9 +84,9 @@ namespace Level m_tiles.light(); - Components::PhysicsComponent* c_pos = player->getComponent(); + const Components::PhysicsComponent* const c_pos = player->getComponent(); - Vec2 offset = (Vec2(Application::get().getInputManager()->getMouse()) - Vec2(Application::get().getWindow().getSize()) / 2.f) * .1f; + const Vec2 offset = (Vec2(Application::get().getInputManager()->getMouse()) - Vec2(Application::get().getWindow().getSize()) / 2.f) * .1f; m_view.setCenter(c_pos->pos + offset); /* @@ -100,8 +98,8 @@ namespace Level for (int i = -1; i <= 1; i++) for (int j = -1; j <= 1; j++) { - int x = int(c_pos->pos.x) / 32 + i; - int y = int(c_pos->pos.y + 16) / 32 + j; + const int x = int(c_pos->pos.x) / 32 + i; + const int y = int(c_pos->pos.y + 16) / 32 + j; if (m_tiles.getTile(0, x, y)->id != TileID::Dungeon_BrickFloor) x3.push_back({x, y, byte(TileID::Dungeon_BrickFloor), 0 }); diff --git a/src/level/gen/Leaf.cpp b/src/level/gen/Leaf.cpp index c08439b..36ee37f 100644 --- a/src/level/gen/Leaf.cpp +++ b/src/level/gen/Leaf.cpp @@ -21,10 +21,10 @@ namespace WGenerator if (block.width > block.height && block.width / block.height >= 1.25) splitOrientation = false; else if (block.height > block.width && block.height / block.width >= 1.25) splitOrientation = true; - uint max = (splitOrientation ? block.height : block.width) - m_minSize; + const uint max = (splitOrientation ? block.height : block.width) - m_minSize; if (max <= m_minSize) return false; - uint splitPosition = (uint)generator->uint64InRange(m_minSize, max); + const uint splitPosition = (uint)generator->uint64InRange(m_minSize, max); if (splitOrientation) { @@ -75,8 +75,8 @@ namespace WGenerator point1 = std::make_pair((uint)(left->x + (left->width / 2)), (uint)(left->y + (left->height / 2))); point2 = std::make_pair((uint)(right->x + (right->width / 2)), (uint)(right->y + (right->height) / 2)); - int w = point2.first - point1.first; - int h = point2.second - point1.second; + const int w = point2.first - point1.first; + const int h = point2.second - point1.second; uint hall = (uint)generator->uint64InRange(m_minHall, m_maxHall); diff --git a/src/level/tile/Tile.cpp b/src/level/tile/Tile.cpp index 77b7c1e..b6363c3 100644 --- a/src/level/tile/Tile.cpp +++ b/src/level/tile/Tile.cpp @@ -25,8 +25,8 @@ namespace Level renderTilePos.x = x * TILE_SIZE; renderTilePos.y = y * TILE_SIZE; - float tx = m_texture.x * TILE_SIZE; - float ty = m_texture.y * TILE_SIZE; + const float tx = m_texture.x * TILE_SIZE; + const float ty = m_texture.y * TILE_SIZE; // Pointer arithmetic is faster than [] operator Vec2* uvPtr = const_cast(&renderable->getUVs()[0]); @@ -55,8 +55,8 @@ namespace Level { renderTilePos.y = y * TILE_SIZE; - float tx = m_sideTexture.x * TILE_SIZE; - float ty = m_sideTexture.y * TILE_SIZE; + const float tx = m_sideTexture.x * TILE_SIZE; + const float ty = m_sideTexture.y * TILE_SIZE; // Pointer arithmetic is faster than [] operator Vec2* uvPtr = const_cast(&renderable->getUVs()[0]); @@ -71,8 +71,8 @@ namespace Level /// Render top renderTilePos.y = y * TILE_SIZE - 32; - float tx = m_topTexture.x * TILE_SIZE; - float ty = m_topTexture.y * TILE_SIZE; + const float tx = m_topTexture.x * TILE_SIZE; + const float ty = m_topTexture.y * TILE_SIZE; // Pointer arithmetic is faster than [] operator Vec2* uvPtr = const_cast(&renderable->getUVs()[0]); @@ -83,4 +83,4 @@ namespace Level renderer.submit(renderable, Graphics::Renderer2D::RenderOrder::AFTER); } -} \ No newline at end of file +} diff --git a/src/level/tile/TileCollision.cpp b/src/level/tile/TileCollision.cpp index 9e1cb5a..cf98422 100644 --- a/src/level/tile/TileCollision.cpp +++ b/src/level/tile/TileCollision.cpp @@ -6,14 +6,14 @@ namespace Level { std::pair tileCollision(Vec2 position, Vec2 velocity, FloatRectangle& bounds, const Timestep& ts) { - Vec2 dest = position + velocity * ts.asSeconds(); + const Vec2 dest = position + velocity * ts.asSeconds(); - Vec2i from = Vec2i((dest + bounds.getMinimumBound()) / TILE_SIZE); - Vec2i to = Vec2i((dest + bounds.getMaximumBound()) / TILE_SIZE) + Vec2i{ 2, 2 }; + const Vec2i from = Vec2i((dest + bounds.getMinimumBound()) / TILE_SIZE); + const Vec2i to = Vec2i((dest + bounds.getMaximumBound()) / TILE_SIZE) + Vec2i{ 2, 2 }; bool collidingX = false, collidingY = false; - for (int32 x = from.x; x < to.x; x++) - for (int32 y = from.y; y < to.y; y++) + for (int32 x = from.x; x < to.x; ++x) + for (int32 y = from.y; y < to.y; ++y) { if (!State::Playing::get().isTilePassable(0, x, y)) { @@ -39,4 +39,4 @@ namespace Level return std::make_pair(collidingX, collidingY); } -} \ No newline at end of file +} diff --git a/src/level/tile/TileFlooding.cpp b/src/level/tile/TileFlooding.cpp index fd980fc..433cc9d 100644 --- a/src/level/tile/TileFlooding.cpp +++ b/src/level/tile/TileFlooding.cpp @@ -14,11 +14,11 @@ namespace Util { for (const auto& val : tiles) { - Vec2i pos = val.first; - Vec2i up = pos + Vec2i(0, 32); - Vec2i down = pos + Vec2i(0, -32); - Vec2i left = pos + Vec2i(-32, 0); - Vec2i right = pos + Vec2i(32, 0); + const Vec2i pos = val.first; + const Vec2i up = pos + Vec2i(0, 32); + const Vec2i down = pos + Vec2i(0, -32); + const Vec2i left = pos + Vec2i(-32, 0); + const Vec2i right = pos + Vec2i(32, 0); if (!hasCoord(tiles, up)) temp.push_back(std::make_pair(up, currentCost - 1)); @@ -69,4 +69,4 @@ namespace Util return false; } -} \ No newline at end of file +} diff --git a/src/level/tile/TileMap.cpp b/src/level/tile/TileMap.cpp index 80850e8..43c0660 100644 --- a/src/level/tile/TileMap.cpp +++ b/src/level/tile/TileMap.cpp @@ -114,8 +114,8 @@ namespace Level // -- // Do not render tiles that can not be seen (dark) - int32 xa = x - 1 <= 0 ? 1 : x - 1, xb = x + 1 >= height ? height - 1 : x + 1; - int32 ya = y - 1 <= 0 ? 1 : y - 1, yb = y + 1 >= height ? height - 1 : y + 1; + const int32 xa = x - 1 <= 0 ? 1 : x - 1, xb = x + 1 >= height ? height - 1 : x + 1; + const int32 ya = y - 1 <= 0 ? 1 : y - 1, yb = y + 1 >= height ? height - 1 : y + 1; if (layer->tiles[xa][ya]->light.color == Color::Black && layer->tiles[xb][yb]->light.color == Color::Black && layer->tiles[xa][yb]->light.color == Color::Black && layer->tiles[xb][ya]->light.color == Color::Black) diff --git a/src/level/tile/TileMap.h b/src/level/tile/TileMap.h index 611fc4e..0841caa 100644 --- a/src/level/tile/TileMap.h +++ b/src/level/tile/TileMap.h @@ -21,7 +21,7 @@ namespace Level class TileMap { public: - typedef std::vector> AddList; + using AddList = std::vector>; private: struct TileLayer { @@ -30,8 +30,8 @@ namespace Level ~TileLayer() { - for (uint i = 0; i < tiles.size(); i++) - tiles[i].clear(); + for (auto& tile : tiles) + tile.clear(); delete lightMap; } diff --git a/src/maths/Vec2.h b/src/maths/Vec2.h index 539c5b3..b2088f9 100644 --- a/src/maths/Vec2.h +++ b/src/maths/Vec2.h @@ -2,13 +2,13 @@ #include "../Types.h" -#include +#include #include -typedef sf::Vector2f Vec2; -typedef sf::Vector2i Vec2i; -typedef sf::Vector2 Vec2ui; +using Vec2 = sf::Vector2f; +using Vec2i = sf::Vector2i; +using Vec2ui = sf::Vector2; inline bool operator<(const Vec2& v1, const Vec2& v2) { @@ -82,4 +82,4 @@ inline float dot(const Vec2i& a, const Vec2i& b) auto y = a.y * b.y; return static_cast(x + y); -} \ No newline at end of file +} diff --git a/src/maths/maths_func.h b/src/maths/maths_func.h index 20cb72f..d19fff5 100644 --- a/src/maths/maths_func.h +++ b/src/maths/maths_func.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #define PI 3.14159265358f @@ -17,4 +17,4 @@ inline float toDegrees(float radians) inline float clamp(float value, float minimum, float maximum) { return (value > minimum) ? (value < maximum) ? value : maximum : minimum; -} \ No newline at end of file +}