Skip to content

Commit

Permalink
add fps label, fix some signed/unsigned warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sandsmark committed Jul 16, 2018
1 parent b915b24 commit 1369428
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 75 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Expand Up @@ -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})
Expand All @@ -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")

Expand Down
Binary file added fonts/Alegreya-Bold.latin
Binary file not shown.
95 changes: 95 additions & 0 deletions 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.
41 changes: 15 additions & 26 deletions src/Engine.cpp
Expand Up @@ -52,6 +52,8 @@ void Engine::start()
while (renderWindow_->isOpen()) {
std::shared_ptr<GameState> state = state_manager_.getActiveState();

const int renderStart = GameClock.getElapsedTime().asMilliseconds();

bool updated = false;

// Process events
Expand All @@ -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 {
Expand Down Expand Up @@ -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_);
*/
}
3 changes: 1 addition & 2 deletions src/Engine.h
Expand Up @@ -48,8 +48,7 @@ class Engine
StateManager state_manager_;

sf::Text fps_label_;

void drawFps();
sf::Font font_;
};

#endif // ENGINE_H
7 changes: 5 additions & 2 deletions src/core/Entity.cpp
Expand Up @@ -61,8 +61,11 @@ Unit::Unit(const genie::Unit &data_, int playerId_, std::shared_ptr<Civilization
playerId(playerId_),
m_civilization(civilization)
{
defaultGraphics = ResourceManager::Inst()->getGraphic(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";
}
Expand Down
11 changes: 11 additions & 0 deletions src/global/Types.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/mechanics/ActionMove.cpp
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/mechanics/Civilization.cpp
Expand Up @@ -30,7 +30,7 @@ Civilization::Civilization(const int civId, const genie::DatFile &dataFile) :
}
}

for (int i=0; i<dataFile.Techs.size(); i++) {
for (size_t i=0; i<dataFile.Techs.size(); i++) {
const genie::Tech &research = dataFile.Techs.at(i);
if (research.ResearchLocation <= 0) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/mechanics/CompMapObject.cpp
Expand Up @@ -28,7 +28,7 @@ MapObject::~MapObject()
{
}

bool MapObject::update(Time time)
bool MapObject::update(Time /*time*/)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mechanics/CompMapObject.h
Expand Up @@ -68,7 +68,7 @@ namespace act {
{
}

virtual bool update(Time time)
virtual bool update(Time /*time*/)
{
target_->position = new_pos_;
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/mechanics/GameState.cpp
Expand Up @@ -121,7 +121,7 @@ bool GameState::init()
}

const std::vector<genie::Civ> &civilizations = DataManager::Inst().civilizations();
for (int i=0; i<civilizations.size(); i++) {
for (size_t i=0; i<civilizations.size(); i++) {
m_civilizations.push_back(std::make_shared<Civilization>(i, DataManager::Inst().datFile()));
}
if (m_civilizations.empty()) {
Expand All @@ -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<Player>(playerNum, m_civilizations[0]);
m_players.push_back(player);
for (const genie::ScnUnit &scnunit : scenario_->playerUnits[playerNum].units) {
Expand Down Expand Up @@ -304,15 +304,15 @@ 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;
}

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;
Expand Down
10 changes: 5 additions & 5 deletions src/mechanics/Map.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mechanics/Map.h
Expand Up @@ -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<MapTile> MapTileArray;
MapTileArray tiles_;
Expand Down
6 changes: 3 additions & 3 deletions src/mechanics/UnitManager.cpp
Expand Up @@ -74,8 +74,8 @@ void UnitManager::render(std::shared_ptr<SfmlRenderTarget> 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);
Expand Down Expand Up @@ -176,7 +176,7 @@ void UnitManager::render(std::shared_ptr<SfmlRenderTarget> renderTarget)
}
}

void UnitManager::onLeftClick(const MapPos &mapPos)
void UnitManager::onLeftClick(const MapPos &/*mapPos*/)
{
if (m_buildingToPlace) {
for (const Unit::Ptr &unit : m_selectedUnits) {
Expand Down

0 comments on commit 1369428

Please sign in to comment.