diff --git a/CMakeLists.txt b/CMakeLists.txt index 699f890..89c0d64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ else() find_package(SFML COMPONENTS system window graphics REQUIRED) endif() +add_definitions(-DFONT_DIR="${CMAKE_CURRENT_SOURCE_DIR}/fonts/") + include_directories(src/ ${CMAKE_HOME_DIRECTORY}/extern/genieutils/include ${SFML_INCLUDE_DIR}) @@ -33,7 +35,7 @@ set (GENIEUTILS_STATIC_BUILD True) add_subdirectory( extern/genieutils ) # additional compiler flags -#add_definitions(-Wall ) +#add_definitions(-Wall -Wextra) #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") diff --git a/fonts/Alegreya-Bold.latin b/fonts/Alegreya-Bold.latin new file mode 100644 index 0000000..743f141 Binary files /dev/null and b/fonts/Alegreya-Bold.latin differ diff --git a/fonts/OFL.txt b/fonts/OFL.txt new file mode 100644 index 0000000..b70a060 --- /dev/null +++ b/fonts/OFL.txt @@ -0,0 +1,95 @@ +Copyright (c) 2011, Juan Pablo del Peral (juan@huertatipografica.com.ar), +with Reserved Font Names "Alegreya" "Alegreya SC" + + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/src/Engine.cpp b/src/Engine.cpp index 75d759b..637a7b8 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -52,6 +52,8 @@ void Engine::start() while (renderWindow_->isOpen()) { std::shared_ptr state = state_manager_.getActiveState(); + const int renderStart = GameClock.getElapsedTime().asMilliseconds(); + bool updated = false; // Process events @@ -72,7 +74,14 @@ void Engine::start() // Clear screen renderWindow_->clear(sf::Color::Green); state->draw(); - drawFps(); + const int renderTime = GameClock.getElapsedTime().asMilliseconds() - renderStart; + + if (renderTime > 0) { + fps_label_.setString("fps: " + std::to_string(1000/renderTime)); + } + + renderWindow_->draw(fps_label_); + // Update the window renderWindow_->display(); } else { @@ -112,31 +121,11 @@ bool Engine::setup(const std::string &scnFile) renderWindow_->setSize(gameState->uiSize()); renderTarget_->setSize(gameState->uiSize()); - fps_label_.setPosition(sf::Vector2f(10,10)); - fps_label_.setFillColor(sf::Color::Green); + fps_label_.setPosition(sf::Vector2f(gameState->uiSize().width - 75, 5)); + fps_label_.setFillColor(sf::Color::White); + font_.loadFromFile(FONT_DIR "Alegreya-Bold.latin"); + fps_label_.setFont(font_); + fps_label_.setCharacterSize(15); return true; } - -//------------------------------------------------------------------------------ -void Engine::drawFps() -{ - /* - static sf::Clock clock; - - if (clock.GetElapsedTime() >= 1000) - { - float fps = 1000 / render_window_->GetFrameTime(); - - std::stringstream ss; - - ss << fps; - sf::String dfps; - dfps = "FPS: " + ss.str(); - fps_label_.SetString(dfps); - - clock.Reset(); - } - render_window_->Draw(fps_label_); - */ -} diff --git a/src/Engine.h b/src/Engine.h index 5c504bc..c44e12c 100644 --- a/src/Engine.h +++ b/src/Engine.h @@ -48,8 +48,7 @@ class Engine StateManager state_manager_; sf::Text fps_label_; - - void drawFps(); + sf::Font font_; }; #endif // ENGINE_H diff --git a/src/core/Entity.cpp b/src/core/Entity.cpp index 1f7282a..05e6dc9 100644 --- a/src/core/Entity.cpp +++ b/src/core/Entity.cpp @@ -61,8 +61,11 @@ Unit::Unit(const genie::Unit &data_, int playerId_, std::shared_ptrgetGraphic(data.StandingGraphic.first), - movingGraphics = ResourceManager::Inst()->getGraphic(data.Moving.WalkingGraphic); + defaultGraphics = ResourceManager::Inst()->getGraphic(data.StandingGraphic.first); + if (data.Moving.WalkingGraphic >= 0) { + movingGraphics = ResourceManager::Inst()->getGraphic(data.Moving.WalkingGraphic); + } + if (!defaultGraphics) { WARN << "Failed to load default graphics"; } diff --git a/src/global/Types.h b/src/global/Types.h index 4213314..adeced9 100644 --- a/src/global/Types.h +++ b/src/global/Types.h @@ -64,6 +64,17 @@ struct Size { inline Size operator/(float divisor) const { return Size(width / divisor, height / divisor); } + + inline bool operator==(const Size &other) const { + return ( + other.width == width && + other.height == height + ); + } + + inline bool operator!=(const Size &other) const { + return !(*this == other); + } }; struct ScreenPos; diff --git a/src/mechanics/ActionMove.cpp b/src/mechanics/ActionMove.cpp index 963f6f8..151752b 100644 --- a/src/mechanics/ActionMove.cpp +++ b/src/mechanics/ActionMove.cpp @@ -71,8 +71,8 @@ static const float PATHFINDING_HEURISTIC_WEIGHT = 1.; MoveOnMap::MoveOnMap(MapPos destination, MapPtr map, Unit::Ptr unit, UnitManager *unitManager) : IAction(Type::Move), m_map(map), - target_reached(false), m_unitManager(unitManager), + target_reached(false), m_unit(unit) { dest_ = destination; diff --git a/src/mechanics/Civilization.cpp b/src/mechanics/Civilization.cpp index 742a51a..c62bbd6 100644 --- a/src/mechanics/Civilization.cpp +++ b/src/mechanics/Civilization.cpp @@ -30,7 +30,7 @@ Civilization::Civilization(const int civId, const genie::DatFile &dataFile) : } } - for (int i=0; iposition = new_pos_; return true; diff --git a/src/mechanics/GameState.cpp b/src/mechanics/GameState.cpp index d0ee1e7..be6a2a6 100644 --- a/src/mechanics/GameState.cpp +++ b/src/mechanics/GameState.cpp @@ -121,7 +121,7 @@ bool GameState::init() } const std::vector &civilizations = DataManager::Inst().civilizations(); - for (int i=0; i(i, DataManager::Inst().datFile())); } if (m_civilizations.empty()) { @@ -136,7 +136,7 @@ bool GameState::init() DBG << "Setting up scenario:" << scenario_->scenarioInstructions; map_->create(scenario_->map); - for (int playerNum = 0; playerNum < scenario_->playerUnits.size(); playerNum++) { + for (size_t playerNum = 0; playerNum < scenario_->playerUnits.size(); playerNum++) { Player::Ptr player = std::make_shared(playerNum, m_civilizations[0]); m_players.push_back(player); for (const genie::ScnUnit &scnunit : scenario_->playerUnits[playerNum].units) { @@ -304,7 +304,7 @@ void GameState::handleEvent(sf::Event event) if (event.mouseMove.x < MOUSE_MOVE_EDGE_SIZE) { m_cameraDeltaX = -1; - } else if (event.mouseMove.x > renderTarget_->getSize().x - MOUSE_MOVE_EDGE_SIZE) { + } else if (event.mouseMove.x > renderTarget_->getSize().width - MOUSE_MOVE_EDGE_SIZE) { m_cameraDeltaX = 1; } else { m_cameraDeltaX = 0; @@ -312,7 +312,7 @@ void GameState::handleEvent(sf::Event event) if (event.mouseMove.y < MOUSE_MOVE_EDGE_SIZE) { m_cameraDeltaY = 1; - } else if (event.mouseMove.y > renderTarget_->getSize().y - MOUSE_MOVE_EDGE_SIZE) { + } else if (event.mouseMove.y > renderTarget_->getSize().height - MOUSE_MOVE_EDGE_SIZE) { m_cameraDeltaY = -1; } else { m_cameraDeltaY = 0; diff --git a/src/mechanics/Map.cpp b/src/mechanics/Map.cpp index 90d37f2..82414d7 100644 --- a/src/mechanics/Map.cpp +++ b/src/mechanics/Map.cpp @@ -89,7 +89,7 @@ void Map::create(genie::ScnMap mapDescription) tiles_.resize(cols_ * rows_); - for (int i = 0; i < tiles_.size(); i++) { + for (size_t i = 0; i < tiles_.size(); i++) { genie::MapTile tile = mapDescription.tiles[i]; tiles_[i].elevation_ = tile.elevation; @@ -205,14 +205,14 @@ void Map::updateMapData() tile.reset(); } - for (unsigned int col = 0; col < cols_; col++) { - for (unsigned int row = 0; row < rows_; row++) { + for (int col = 0; col < cols_; col++) { + for (int row = 0; row < rows_; row++) { updateTileBlend(col, row); } } - for (unsigned int col = 0; col < cols_; col++) { - for (unsigned int row = 0; row < rows_; row++) { + for (int col = 0; col < cols_; col++) { + for (int row = 0; row < rows_; row++) { updateTileSlopes(col, row); } } diff --git a/src/mechanics/Map.h b/src/mechanics/Map.h index fbc9b82..8a8aa66 100644 --- a/src/mechanics/Map.h +++ b/src/mechanics/Map.h @@ -113,7 +113,7 @@ class Map res::TileSlopes::Slope slopeAt(const int x, const int y); // cols_ = x, rows_ = y - unsigned int rows_, cols_; + int rows_, cols_; typedef std::vector MapTileArray; MapTileArray tiles_; diff --git a/src/mechanics/UnitManager.cpp b/src/mechanics/UnitManager.cpp index 76faefb..282cab5 100644 --- a/src/mechanics/UnitManager.cpp +++ b/src/mechanics/UnitManager.cpp @@ -74,8 +74,8 @@ void UnitManager::render(std::shared_ptr renderTarget) m_previousCameraPos = camera->targetPosition(); } - if (m_outlineOverlay.getSize() != renderTarget->getSize()) { - m_outlineOverlay.create(renderTarget->getSize().x, renderTarget->getSize().y); + if (Size(m_outlineOverlay.getSize()) != renderTarget->getSize()) { + m_outlineOverlay.create(renderTarget->getSize().width, renderTarget->getSize().height); } m_outlineOverlay.clear(sf::Color::Transparent); @@ -176,7 +176,7 @@ void UnitManager::render(std::shared_ptr renderTarget) } } -void UnitManager::onLeftClick(const MapPos &mapPos) +void UnitManager::onLeftClick(const MapPos &/*mapPos*/) { if (m_buildingToPlace) { for (const Unit::Ptr &unit : m_selectedUnits) { diff --git a/src/render/ActionPanel.cpp b/src/render/ActionPanel.cpp index a3868ef..f1b0b87 100644 --- a/src/render/ActionPanel.cpp +++ b/src/render/ActionPanel.cpp @@ -23,7 +23,7 @@ bool ActionPanel::init() WARN << "Failed to load unit icons"; return false; } - for (int i=0; igetFrameCount(); i++) { + for (size_t i=0; igetFrameCount(); i++) { m_unitIcons[i].loadFromImage(res::Resource::convertFrameToImage(unitIconsSlp->getFrame(i))); } @@ -33,7 +33,7 @@ bool ActionPanel::init() WARN << "Failed to load building icons"; return false; } - for (int i=0; igetFrameCount(); i++) { + for (size_t i=0; igetFrameCount(); i++) { m_buildingIcons[i].loadFromImage(res::Resource::convertFrameToImage(buildingIconsSlp->getFrame(i))); } @@ -42,7 +42,7 @@ bool ActionPanel::init() WARN << "Failed to load research icons"; return false; } - for (int i=0; igetFrameCount(); i++) { + for (size_t i=0; igetFrameCount(); i++) { m_researchIcons[i].loadFromImage(res::Resource::convertFrameToImage(researchIconsSlp->getFrame(i))); } @@ -51,7 +51,7 @@ bool ActionPanel::init() WARN << "Failed to load action icons"; return false; } - for (int i=0; i= commandIconsSlp->getFrameCount()) { WARN << "icon out of range " << i; return false; @@ -178,7 +178,7 @@ ScreenRect ActionPanel::rect() const r.width = 5 * 55; r.x = 35; // r.y = 845; - r.y = m_renderTarget->getSize().y - r.height - 25; + r.y = m_renderTarget->getSize().height - r.height - 25; return r; } @@ -336,7 +336,7 @@ ScreenPos ActionPanel::buttonPosition(const int index) const position.x = (position.x) * 50 + 55; position.y = index / 5; position.y *= 50; - position.y += m_renderTarget->getSize().y - 170; + position.y += m_renderTarget->getSize().height - 170; return position; } diff --git a/src/render/GraphicRender.h b/src/render/GraphicRender.h index 900a59b..84c999d 100644 --- a/src/render/GraphicRender.h +++ b/src/render/GraphicRender.h @@ -67,7 +67,7 @@ class GraphicRender res::GraphicPtr graphic_; - unsigned int current_frame_; + int current_frame_; private: struct GraphicDelta { diff --git a/src/render/IRenderTarget.h b/src/render/IRenderTarget.h index 4355d97..5308611 100644 --- a/src/render/IRenderTarget.h +++ b/src/render/IRenderTarget.h @@ -35,7 +35,7 @@ class IRenderTarget virtual ~IRenderTarget() {} //---------------------------------------------------------------------------- - virtual Vector2u getSize(void) const = 0; + virtual Size getSize(void) const = 0; virtual void setSize(const Size size) const = 0; diff --git a/src/render/MapRenderer.cpp b/src/render/MapRenderer.cpp index c84caa7..c08f2b4 100644 --- a/src/render/MapRenderer.cpp +++ b/src/render/MapRenderer.cpp @@ -41,7 +41,7 @@ MapRenderer::~MapRenderer() { } -bool MapRenderer::update(Time time) +bool MapRenderer::update(Time /*time*/) { if (!m_map) { return false; @@ -50,20 +50,20 @@ bool MapRenderer::update(Time time) const MapPos cameraPos = renderTarget_->camera()->targetPosition(); if (!m_camChanged && m_lastCameraPos == cameraPos && - m_mapRenderTexture.getSize() == renderTarget_->getSize()) { + Size(m_mapRenderTexture.getSize()) == renderTarget_->getSize()) { return false; } //TODO: split up (refactor) // Get the absolute map positions of the rendertarget corners - const ScreenPos camCenter(renderTarget_->getSize().x / 2.0, renderTarget_->getSize().y / 2.0); + const ScreenPos camCenter(renderTarget_->getSize().width / 2.0, renderTarget_->getSize().height / 2.0); // relative map positions (from center) //only changes if renderTargets resolution does const MapPos center = camCenter.toMap(); - const MapPos bottomLeft = ScreenPos(0, renderTarget_->getSize().y).toMap(); - const MapPos topRight = ScreenPos(renderTarget_->getSize().x, 0).toMap(); - const MapPos bottomRight = ScreenPos(renderTarget_->getSize().x, renderTarget_->getSize().y).toMap(); + const MapPos bottomLeft = ScreenPos(0, renderTarget_->getSize().height).toMap(); + const MapPos topRight = ScreenPos(renderTarget_->getSize().width, 0).toMap(); + const MapPos bottomRight = ScreenPos(renderTarget_->getSize().width, renderTarget_->getSize().height).toMap(); // absolute map positions MapPos topLeftMp = cameraPos - center; @@ -120,7 +120,7 @@ bool MapRenderer::update(Time time) void MapRenderer::display(void) { - if (m_mapRenderTexture.getSize() != renderTarget_->getSize()) { + if (Size(m_mapRenderTexture.getSize()) != renderTarget_->getSize()) { updateTexture(); } @@ -140,8 +140,8 @@ void MapRenderer::setMap(MapPtr map) void MapRenderer::updateTexture() { - if (m_mapRenderTexture.getSize().x != renderTarget_->getSize().x || m_mapRenderTexture.getSize().y != renderTarget_->getSize().y) { - m_mapRenderTexture.create(renderTarget_->getSize().x, renderTarget_->getSize().y); + if (m_mapRenderTexture.getSize().x != renderTarget_->getSize().width || m_mapRenderTexture.getSize().y != renderTarget_->getSize().height) { + m_mapRenderTexture.create(renderTarget_->getSize().width, renderTarget_->getSize().height); } m_mapRenderTexture.clear(); diff --git a/src/render/SfmlRenderTarget.cpp b/src/render/SfmlRenderTarget.cpp index 91ddbef..47af028 100644 --- a/src/render/SfmlRenderTarget.cpp +++ b/src/render/SfmlRenderTarget.cpp @@ -35,7 +35,7 @@ SfmlRenderTarget::~SfmlRenderTarget() { } -Vector2u SfmlRenderTarget::getSize(void) const +Size SfmlRenderTarget::getSize(void) const { return sf::Vector2u(renderTarget_->getSize().x / SCALE, renderTarget_->getSize().y / SCALE); } diff --git a/src/render/SfmlRenderTarget.h b/src/render/SfmlRenderTarget.h index 9e5204b..bc82adf 100644 --- a/src/render/SfmlRenderTarget.h +++ b/src/render/SfmlRenderTarget.h @@ -36,7 +36,7 @@ class SfmlRenderTarget : public IRenderTarget virtual ~SfmlRenderTarget(); //---------------------------------------------------------------------------- - Vector2u getSize(void) const override; + Size getSize(void) const override; void setSize(const Size size) const override; //---------------------------------------------------------------------------- diff --git a/src/resource/Graphic.cpp b/src/resource/Graphic.cpp index 409164d..23c9816 100644 --- a/src/resource/Graphic.cpp +++ b/src/resource/Graphic.cpp @@ -105,8 +105,8 @@ const sf::Texture &Graphic::texture(uint32_t frame, float angleRadians, uint8_t const genie::SlpFramePtr frame = slp_->getFrame(state.frame); const genie::SlpFrameData &frameData = frame->img_data; - const int width = frame->getWidth(); - const int height = frame->getHeight(); + const uint32_t width = frame->getWidth(); + const uint32_t height = frame->getHeight(); // fuck msvc std::vector pixelsBuf(width * height * 4); diff --git a/src/resource/Graphic.h b/src/resource/Graphic.h index 083ad51..8fb3cfb 100644 --- a/src/resource/Graphic.h +++ b/src/resource/Graphic.h @@ -172,7 +172,7 @@ class Graphic private: struct FrameInfo { - int frameNum = 0; + uint32_t frameNum = 0; bool mirrored = false; }; FrameInfo calcFrameInfo(uint32_t num, float angle) const; diff --git a/src/resource/Resource.cpp b/src/resource/Resource.cpp index b7b1401..36eaf1f 100644 --- a/src/resource/Resource.cpp +++ b/src/resource/Resource.cpp @@ -91,8 +91,8 @@ sf::Image Resource::convertFrameToImage(const genie::SlpFramePtr frame, return img; } - const int width = frame->getWidth(); - const int height = frame->getHeight(); + const uint32_t width = frame->getWidth(); + const uint32_t height = frame->getHeight(); const genie::SlpFrameData &frameData = frame->img_data; // fuck msvc diff --git a/src/resource/Terrain.cpp b/src/resource/Terrain.cpp index a2a2ce1..5b5ed98 100644 --- a/src/resource/Terrain.cpp +++ b/src/resource/Terrain.cpp @@ -158,7 +158,7 @@ const sf::Texture &Terrain::blendImage(const Blend blends, int tileX, int tileY) alphamask.resize(blend.alphaValues[i].size(), 0x7f); } - for (int j=0; j