Skip to content

Commit

Permalink
Introduce an in-game string to support multiple languages (#9330)
Browse files Browse the repository at this point in the history
  • Loading branch information
ihhub authored Jan 22, 2025
1 parent 0305100 commit d053891
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 10 deletions.
2 changes: 2 additions & 0 deletions VisualStudio/fheroes2/sources.props
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
<ClCompile Include="src\fheroes2\game\game_scenarioinfo.cpp" />
<ClCompile Include="src\fheroes2\game\game_startgame.cpp" />
<ClCompile Include="src\fheroes2\game\game_static.cpp" />
<ClCompile Include="src\fheroes2\game\game_string.cpp" />
<ClCompile Include="src\fheroes2\game\game_video.cpp" />
<ClCompile Include="src\fheroes2\game\highscores.cpp" />
<ClCompile Include="src\fheroes2\gui\cursor.cpp" />
Expand Down Expand Up @@ -347,6 +348,7 @@
<ClInclude Include="src\fheroes2\game\game_mode.h" />
<ClInclude Include="src\fheroes2\game\game_over.h" />
<ClInclude Include="src\fheroes2\game\game_static.h" />
<ClInclude Include="src\fheroes2\game\game_string.h" />
<ClInclude Include="src\fheroes2\game\game_video.h" />
<ClInclude Include="src\fheroes2\game\game_video_type.h" />
<ClInclude Include="src\fheroes2\game\highscores.h" />
Expand Down
39 changes: 36 additions & 3 deletions src/engine/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
Expand Down Expand Up @@ -206,6 +207,25 @@ class IStreamBase : virtual public StreamBase
return *this >> v.first >> v.second;
}

template <class Type>
IStreamBase & operator>>( std::optional<Type> & v )
{
bool hasValue{};
*this >> hasValue;

if ( hasValue ) {
Type temp{};
*this >> temp;

v = std::move( temp );
}
else {
v = {};
}

return *this;
}

template <class Type>
IStreamBase & operator>>( std::vector<Type> & v )
{
Expand Down Expand Up @@ -234,11 +254,11 @@ class IStreamBase : virtual public StreamBase
v.clear();

for ( uint32_t i = 0; i < size; ++i ) {
std::pair<Type1, Type2> pr;
std::pair<Type1, Type2> temp{};

*this >> pr;
*this >> temp;

v.emplace( std::move( pr ) );
v.emplace( std::move( temp ) );
}

return *this;
Expand Down Expand Up @@ -316,6 +336,19 @@ class OStreamBase : virtual public StreamBase
return *this << v.first << v.second;
}

template <class Type>
OStreamBase & operator<<( const std::optional<Type> & v )
{
const bool hasValue = v.has_value();
*this << hasValue;

if ( hasValue ) {
*this << *v;
}

return *this;
}

template <class Type>
OStreamBase & operator<<( const std::vector<Type> & v )
{
Expand Down
7 changes: 4 additions & 3 deletions src/fheroes2/castle/castle_tavern.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2019 - 2024 *
* Copyright (C) 2019 - 2025 *
* *
* Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
* Copyright (C) 2009 by Andrey Afletdinov <[email protected]> *
Expand Down Expand Up @@ -29,6 +29,7 @@
#include "castle.h" // IWYU pragma: associated
#include "dialog.h"
#include "game_delays.h"
#include "game_string.h"
#include "icn.h"
#include "translations.h"
#include "ui_dialog.h"
Expand All @@ -37,14 +38,14 @@

void Castle::_openTavern() const
{
auto [rumor, language] = world.getCurrentRumor();
auto rumor = world.getCurrentRumor();

std::string body( _( "A generous tip for the barkeep yields the following rumor:" ) );
body += "\n\n";

auto text = std::make_shared<fheroes2::MultiFontText>();
text->add( fheroes2::Text{ std::move( body ), fheroes2::FontType::normalWhite() } );
text->add( fheroes2::Text{ std::move( rumor ), fheroes2::FontType::normalWhite(), language } );
text->add( fheroes2::Text{ std::move( rumor.text ), fheroes2::FontType::normalWhite(), rumor.language } );

const fheroes2::AnimationDialogElement imageUI( ICN::TAVWIN, { 0, 1 }, 0, Game::getAnimationDelayValue( Game::CASTLE_TAVERN_DELAY ) );
const fheroes2::TextDialogElement textBodyUI( text );
Expand Down
33 changes: 33 additions & 0 deletions src/fheroes2/game/game_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2025 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include "game_string.h"

#include "serialize.h"

OStreamBase & operator<<( OStreamBase & stream, const fheroes2::LocalizedString & string )
{
return stream << string.text << string.language;
}

IStreamBase & operator>>( IStreamBase & stream, fheroes2::LocalizedString & string )
{
return stream >> string.text >> string.language;
}
44 changes: 44 additions & 0 deletions src/fheroes2/game/game_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/***************************************************************************
* fheroes2: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2025 *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#pragma once

#include <cstdint>
#include <optional>
#include <string>

class IStreamBase;
class OStreamBase;

namespace fheroes2
{

enum class SupportedLanguage : uint8_t;

struct LocalizedString
{
std::string text;

std::optional<SupportedLanguage> language;
};
}

OStreamBase & operator<<( OStreamBase & stream, const fheroes2::LocalizedString & string );
IStreamBase & operator>>( IStreamBase & stream, fheroes2::LocalizedString & string );
3 changes: 2 additions & 1 deletion src/fheroes2/world/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <array>
#include <cassert>
#include <limits>
#include <optional>
#include <ostream>
#include <set>
#include <tuple>
Expand Down Expand Up @@ -708,7 +709,7 @@ void World::MonthOfMonstersAction( const Monster & mons )
}
}

std::pair<std::string, std::optional<fheroes2::SupportedLanguage>> World::getCurrentRumor() const
fheroes2::LocalizedString World::getCurrentRumor() const
{
const uint32_t standardRumorCount = 10;
const uint32_t totalRumorCount = static_cast<uint32_t>( _customRumors.size() ) + standardRumorCount;
Expand Down
5 changes: 2 additions & 3 deletions src/fheroes2/world/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
Expand All @@ -37,7 +36,7 @@
#include "army_troop.h"
#include "artifact_ultimate.h"
#include "castle.h"
#include "game_language.h"
#include "game_string.h"
#include "heroes.h"
#include "kingdom.h"
#include "maps.h"
Expand Down Expand Up @@ -358,7 +357,7 @@ class World : protected fheroes2::Size
void NewWeek();
void NewMonth();

std::pair<std::string, std::optional<fheroes2::SupportedLanguage>> getCurrentRumor() const;
fheroes2::LocalizedString getCurrentRumor() const;

int32_t NextTeleport( const int32_t index ) const;
MapsIndexes GetTeleportEndPoints( const int32_t index ) const;
Expand Down

0 comments on commit d053891

Please sign in to comment.