Skip to content

Commit

Permalink
Migrate to C++17 (ihhub#5472)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz authored Jun 5, 2022
1 parent 1ff1681 commit 8746465
Show file tree
Hide file tree
Showing 26 changed files with 713 additions and 688 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ SpacesBeforeTrailingComments: 1
SpacesInCStyleCastParentheses: false
SpacesInParentheses: true
SpacesInSquareBrackets: false
Standard: Cpp11
Standard: c++17
TabWidth: 4
UseTab: Never
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ file(STRINGS ${CMAKE_SOURCE_DIR}/version.txt FHEROES2_VERSION LIMIT_COUNT 1 REGE

project(fheroes2 VERSION ${FHEROES2_VERSION} LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

include(CMakeDependentOption)
include(GNUInstallDirs)
Expand Down
2 changes: 2 additions & 0 deletions VisualStudio/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\src\engine;$(MSBuildThisFileDirectory)..\src\fheroes2\gui;$(MSBuildThisFileDirectory)..\src\fheroes2\maps;$(MSBuildThisFileDirectory)..\src\fheroes2\kingdom;$(MSBuildThisFileDirectory)..\src\fheroes2\campaign;$(MSBuildThisFileDirectory)..\src\fheroes2\game;$(MSBuildThisFileDirectory)..\src\fheroes2\dialog;$(MSBuildThisFileDirectory)..\src\fheroes2\system;$(MSBuildThisFileDirectory)..\src\fheroes2\spell;$(MSBuildThisFileDirectory)..\src\fheroes2\monster;$(MSBuildThisFileDirectory)..\src\fheroes2\castle;$(MSBuildThisFileDirectory)..\src\fheroes2\agg;$(MSBuildThisFileDirectory)..\src\fheroes2\heroes;$(MSBuildThisFileDirectory)..\src\fheroes2\resource;$(MSBuildThisFileDirectory)..\src\fheroes2\ai;$(MSBuildThisFileDirectory)..\src\fheroes2\army;$(MSBuildThisFileDirectory)..\src\fheroes2\battle;$(MSBuildThisFileDirectory)..\src\fheroes2\h2d;$(MSBuildThisFileDirectory)..\src\fheroes2\objects;$(MSBuildThisFileDirectory)..\src\fheroes2\world;$(MSBuildThisFileDirectory)..\src\fheroes2\audio;$(MSBuildThisFileDirectory)..\src\fheroes2\image;$(MSBuildThisFileDirectory)..\src\thirdparty\libsmacker;$(MSBuildThisFileDirectory)packages\installed\zlib\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
Expand Down
2 changes: 1 addition & 1 deletion docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Please follow the instructions below to be able to build the project from source
be missing depending on the version of the original game) to the project root directory.
* Alternatively, go to the directory `script/demo` and run the file `download_demo_version.bat`.
This script will download a demo version of the original game, which is the minimum required for development.
* Open the file `fheroes2-vs2015.vcxproj` or `fheroes2-vs2019.vcxproj` depending on the version of Microsoft Visual
* Open the file `fheroes2-vs2017.vcxproj` or `fheroes2-vs2019.vcxproj` depending on the version of Microsoft Visual
Studio installed and build the project.
* Visual Studio will automatically copy game files in the root directory to the build directory.

Expand Down
1,162 changes: 581 additions & 581 deletions fheroes2-vs2015.vcxproj → fheroes2-vs2017.vcxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ CPPFLAGS_TP := $(CPPFLAGS)
# *FLAGS_FH2 can be passed from platform-specific Makefiles
CCFLAGS := $(CCFLAGS) $(CCFLAGS_FH2)
CFLAGS := $(CFLAGS) $(CFLAGS_FH2)
CXXFLAGS := $(CXXFLAGS) $(CXXFLAGS_FH2) -std=c++11
CXXFLAGS := $(CXXFLAGS) $(CXXFLAGS_FH2) -std=c++17
CPPFLAGS := $(CPPFLAGS) $(CPPFLAGS_FH2)

LIBS := $(LIBS) -lz
Expand Down
7 changes: 0 additions & 7 deletions src/engine/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ std::string InsertString( const std::string &, size_t, const char * );
bool SaveMemToFile( const std::vector<uint8_t> & data, const std::string & path );
std::vector<uint8_t> LoadFileToMem( const std::string & path );

// std::clamp replacement until we can use C++17
template <typename T>
T clamp( const T & value, const T & min, const T & max )
{
return ( value < min ) ? min : ( max < value ) ? max : value;
}

namespace fheroes2
{
double GetAngle( const Point & start, const Point & target );
Expand Down
18 changes: 10 additions & 8 deletions src/fheroes2/ai/normal/ai_normal_kingdom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
***************************************************************************/

#include <cassert>
#include <tuple>
#include <utility>

#include "ai_normal.h"
Expand Down Expand Up @@ -96,15 +97,16 @@ namespace AI
// Re-hiring a hero related to any of the WINS_HERO or LOSS_HERO conditions is not allowed
const auto heroesToIgnore = std::make_pair( world.GetHeroesCondWins(), world.GetHeroesCondLoss() );

Heroes * firstRecruit = rec.GetHero1();
if ( firstRecruit == heroesToIgnore.first || firstRecruit == heroesToIgnore.second ) {
firstRecruit = nullptr;
}
auto useIfPossible = [&heroesToIgnore]( Heroes * hero ) -> Heroes * {
if ( std::apply( [hero]( const auto... heroToIgnore ) { return ( ( hero == heroToIgnore ) || ... ); }, heroesToIgnore ) ) {
return nullptr;
}

Heroes * secondRecruit = rec.GetHero2();
if ( secondRecruit == heroesToIgnore.first || secondRecruit == heroesToIgnore.second ) {
secondRecruit = nullptr;
}
return hero;
};

Heroes * firstRecruit = useIfPossible( rec.GetHero1() );
Heroes * secondRecruit = useIfPossible( rec.GetHero2() );

if ( firstRecruit && secondRecruit ) {
if ( secondRecruit->getRecruitValue() > firstRecruit->getRecruitValue() ) {
Expand Down
9 changes: 5 additions & 4 deletions src/fheroes2/dialog/dialog_quickinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <algorithm>
#include <cassert>
#include <cstdlib>

#include "agg_image.h"
Expand All @@ -43,8 +45,6 @@
#include "ui_text.h"
#include "world.h"

#include <cassert>

namespace
{
void outputInTextSupportMode( const Maps::Tiles & tile, const std::string & info )
Expand Down Expand Up @@ -446,8 +446,9 @@ namespace
int32_t ypos = my + TILEWIDTH - ( imageBox.height() / 2 );

// clamp box to edges of adventure screen game area
xpos = clamp( xpos, BORDERWIDTH, ( ar.width - imageBox.width() ) + BORDERWIDTH );
ypos = clamp( ypos, BORDERWIDTH, ( ar.height - imageBox.height() ) + BORDERWIDTH );
assert( ar.width >= imageBox.width() && ar.height >= imageBox.height() );
xpos = std::clamp( xpos, BORDERWIDTH, ( ar.width - imageBox.width() ) + BORDERWIDTH );
ypos = std::clamp( ypos, BORDERWIDTH, ( ar.height - imageBox.height() ) + BORDERWIDTH );

return { xpos, ypos, imageBox.width(), imageBox.height() };
}
Expand Down
4 changes: 2 additions & 2 deletions src/fheroes2/heroes/heroes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,8 +1968,8 @@ StreamBase & operator>>( StreamBase & msg, Heroes & hero )
// Heroes
msg >> hero.name >> col;

static_assert( LAST_SUPPORTED_FORMAT_VERSION < FORMAT_VERSION_0916_RELEASE, "Remove the check below." );
if ( Game::GetLoadVersion() < FORMAT_VERSION_0916_RELEASE ) {
static_assert( LAST_SUPPORTED_FORMAT_VERSION < FORMAT_VERSION_PRE_0916_RELEASE, "Remove the check below." );
if ( Game::GetLoadVersion() < FORMAT_VERSION_PRE_0916_RELEASE ) {
ColorBase dummyColor;

msg >> dummyColor;
Expand Down
18 changes: 12 additions & 6 deletions src/fheroes2/kingdom/kingdom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Kingdom::Kingdom()
, visited_tents_colors( 0 )
, _topItemInKingdomView( -1 )
{
heroes_cond_loss.reserve( 4 );
// Do nothing.
}

void Kingdom::Init( int clr )
Expand Down Expand Up @@ -93,7 +93,6 @@ void Kingdom::clear()

recruits.Reset();

heroes_cond_loss.clear();
puzzle_maps.reset();
}

Expand Down Expand Up @@ -914,8 +913,7 @@ cost_t Kingdom::_getKingdomStartingResources( const int difficulty ) const
StreamBase & operator<<( StreamBase & msg, const Kingdom & kingdom )
{
return msg << kingdom.modes << kingdom.color << kingdom.resource << kingdom.lost_town_days << kingdom.castles << kingdom.heroes << kingdom.recruits
<< kingdom.visit_object << kingdom.puzzle_maps << kingdom.visited_tents_colors << kingdom.heroes_cond_loss << kingdom._lastBattleWinHeroID
<< kingdom._topItemInKingdomView;
<< kingdom.visit_object << kingdom.puzzle_maps << kingdom.visited_tents_colors << kingdom._lastBattleWinHeroID << kingdom._topItemInKingdomView;
}

StreamBase & operator>>( StreamBase & msg, Kingdom & kingdom )
Expand All @@ -934,8 +932,16 @@ StreamBase & operator>>( StreamBase & msg, Kingdom & kingdom )
}
}

msg >> kingdom.visit_object >> kingdom.puzzle_maps >> kingdom.visited_tents_colors >> kingdom.heroes_cond_loss >> kingdom._lastBattleWinHeroID
>> kingdom._topItemInKingdomView;
msg >> kingdom.visit_object >> kingdom.puzzle_maps >> kingdom.visited_tents_colors;

static_assert( LAST_SUPPORTED_FORMAT_VERSION < FORMAT_VERSION_0916_RELEASE, "Remove the check below." );
if ( Game::GetLoadVersion() < FORMAT_VERSION_0916_RELEASE ) {
KingdomHeroes temp;

msg >> temp;
}

msg >> kingdom._lastBattleWinHeroID >> kingdom._topItemInKingdomView;

return msg;
}
Expand Down
2 changes: 0 additions & 2 deletions src/fheroes2/kingdom/kingdom.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ class Kingdom : public BitModes, public Control
Puzzle puzzle_maps;
uint32_t visited_tents_colors;

KingdomHeroes heroes_cond_loss;

// Used to remember which item was selected in Kingdom View dialog.
int _topItemInKingdomView;
};
Expand Down
5 changes: 3 additions & 2 deletions src/fheroes2/kingdom/luck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <algorithm>

#include "luck.h"
#include "tools.h"
#include "translations.h"

std::string Luck::String( int luck )
Expand Down Expand Up @@ -71,5 +72,5 @@ std::string Luck::Description( int luck )

int Luck::Normalize( const int luck )
{
return clamp( luck, static_cast<int>( Luck::CURSED ), static_cast<int>( Luck::IRISH ) );
return std::clamp( luck, static_cast<int>( Luck::CURSED ), static_cast<int>( Luck::IRISH ) );
}
5 changes: 3 additions & 2 deletions src/fheroes2/kingdom/morale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <algorithm>

#include "morale.h"
#include "tools.h"
#include "translations.h"

std::string Morale::String( int morale )
Expand Down Expand Up @@ -71,5 +72,5 @@ std::string Morale::Description( int morale )

int Morale::Normalize( const int morale )
{
return clamp( morale, static_cast<int>( Morale::TREASON ), static_cast<int>( Morale::BLOOD ) );
return std::clamp( morale, static_cast<int>( Morale::TREASON ), static_cast<int>( Morale::BLOOD ) );
}
18 changes: 10 additions & 8 deletions src/fheroes2/kingdom/view_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <algorithm>
#include <cassert>

#include "view_world.h"
#include "agg_image.h"
#include "color.h"
Expand All @@ -32,8 +35,6 @@
#include "tools.h"
#include "world.h"

#include <cassert>

// #define VIEWWORLD_DEBUG_ZOOM_LEVEL // Activate this when you want to debug this window. It will provide an extra zoom level at 1:1 scale

#if defined( VIEWWORLD_DEBUG_ZOOM_LEVEL )
Expand Down Expand Up @@ -261,10 +262,11 @@ namespace
const int32_t marginForRightSide = ( 2 * BORDERWIDTH + RADARWIDTH ) / tileSize + 1;

// add a margin of 2 tiles because icons outside of view can still show on the view
const int32_t minTileX = clamp( roiTiles.x - 2, 0, worldWidth );
const int32_t maxTileX = clamp( roiTiles.x + roiTiles.width + marginForRightSide + 2, 0, worldWidth );
const int32_t minTileY = clamp( roiTiles.y - 2, 0, worldHeight );
const int32_t maxTileY = clamp( roiTiles.y + roiTiles.height + 2, 0, worldHeight );
assert( worldWidth >= 0 && worldHeight >= 0 );
const int32_t minTileX = std::clamp( roiTiles.x - 2, 0, worldWidth );
const int32_t maxTileX = std::clamp( roiTiles.x + roiTiles.width + marginForRightSide + 2, 0, worldWidth );
const int32_t minTileY = std::clamp( roiTiles.y - 2, 0, worldHeight );
const int32_t maxTileY = std::clamp( roiTiles.y + roiTiles.height + 2, 0, worldHeight );

for ( int32_t posY = minTileY; posY < maxTileY; ++posY ) {
const int dsty = posY * tileSize - offsetY + BORDERWIDTH;
Expand Down Expand Up @@ -456,14 +458,14 @@ bool ViewWorld::ZoomROIs::ChangeCenter( const fheroes2::Point & centerInPixels )
newCenter.x = worldSize.width / 2;
}
else {
newCenter.x = clamp( centerInPixels.x, currentRect.width / 2, worldSize.width - currentRect.width / 2 );
newCenter.x = std::clamp( centerInPixels.x, currentRect.width / 2, worldSize.width - currentRect.width / 2 );
}

if ( worldSize.height <= currentRect.height ) {
newCenter.y = worldSize.height / 2;
}
else {
newCenter.y = clamp( centerInPixels.y, currentRect.height / 2, worldSize.height - currentRect.height / 2 );
newCenter.y = std::clamp( centerInPixels.y, currentRect.height / 2, worldSize.height - currentRect.height / 2 );
}

if ( newCenter == _center ) {
Expand Down
3 changes: 2 additions & 1 deletion src/fheroes2/system/save_format_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
enum SaveFileFormat : uint16_t
{
// TODO: if you're adding a new version you must assign it to CURRENT_FORMAT_VERSION located at the bottom.
FORMAT_VERSION_0916_RELEASE = 9900,
FORMAT_VERSION_0916_RELEASE = 9901,
FORMAT_VERSION_PRE_0916_RELEASE = 9900,
FORMAT_VERSION_0912_RELEASE = 9803,
FORMAT_VERSION_PRE3_0912_RELEASE = 9802,
FORMAT_VERSION_PRE2_0912_RELEASE = 9801,
Expand Down
14 changes: 7 additions & 7 deletions src/fheroes2/system/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ bool Settings::Read( const std::string & filename )
}

if ( config.Exists( "controller pointer speed" ) ) {
_controllerPointerSpeed = clamp( config.IntParams( "controller pointer speed" ), 0, 100 );
_controllerPointerSpeed = std::clamp( config.IntParams( "controller pointer speed" ), 0, 100 );
}

if ( config.Exists( "first time game run" ) && config.StrParams( "first time game run" ) == "off" ) {
Expand Down Expand Up @@ -611,19 +611,19 @@ std::string Settings::GetLastFile( const std::string & prefix, const std::string
/* set ai speed: 0 (don't show) - 10 */
void Settings::SetAIMoveSpeed( int speed )
{
ai_speed = clamp( speed, 0, 10 );
ai_speed = std::clamp( speed, 0, 10 );
}

/* set hero speed: 1 - 10 */
void Settings::SetHeroesMoveSpeed( int speed )
{
heroes_speed = clamp( speed, 1, 10 );
heroes_speed = std::clamp( speed, 1, 10 );
}

/* set battle speed: 1 - 10 */
void Settings::SetBattleSpeed( int speed )
{
battle_speed = clamp( speed, 1, 10 );
battle_speed = std::clamp( speed, 1, 10 );
}

void Settings::setBattleAutoResolve( bool enable )
Expand Down Expand Up @@ -705,7 +705,7 @@ void Settings::set3DAudio( const bool enable )
/* set scroll speed: 1 - 4 */
void Settings::SetScrollSpeed( int speed )
{
scroll_speed = clamp( speed, static_cast<int>( SCROLL_SLOW ), static_cast<int>( SCROLL_FAST2 ) );
scroll_speed = std::clamp( speed, static_cast<int>( SCROLL_SLOW ), static_cast<int>( SCROLL_FAST2 ) );
}

bool Settings::isPriceOfLoyaltySupported() const
Expand Down Expand Up @@ -793,13 +793,13 @@ void Settings::SetDebug( int d )
/* sound volume: 0 - 10 */
void Settings::SetSoundVolume( int v )
{
sound_volume = clamp( v, 0, 10 );
sound_volume = std::clamp( v, 0, 10 );
}

/* music volume: 0 - 10 */
void Settings::SetMusicVolume( int v )
{
music_volume = clamp( v, 0, 10 );
music_volume = std::clamp( v, 0, 10 );
}

void Settings::SetPreferablyCountPlayers( int c )
Expand Down
Loading

0 comments on commit 8746465

Please sign in to comment.