Skip to content

Commit

Permalink
Add new UI Text rendering classes (ihhub#3653)
Browse files Browse the repository at this point in the history
New code is faster as it doesn't copy strings for multi-line rendering, it's scalable for font types and it supports multi-font rendering. However, we'll need an extensive testing to fully use this code.
  • Loading branch information
ihhub authored Jun 9, 2021
1 parent d18fca1 commit 79dc87a
Show file tree
Hide file tree
Showing 10 changed files with 864 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VisualStudio/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<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\pocketpc;$(MSBuildThisFileDirectory)..\src\fheroes2\objects;$(MSBuildThisFileDirectory)..\src\fheroes2\world;$(MSBuildThisFileDirectory)..\src\fheroes2\image;$(MSBuildThisFileDirectory)..\src\thirdparty\libsmacker;$(MSBuildThisFileDirectory)packages\installed\zlib\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;WITH_ZLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<TreatSpecificWarningsAsErrors>4018;4028;4100;4189;4245;4267;4309;4319;4389;4456;4458;4554;4592;4702;4706;4715;4800</TreatSpecificWarningsAsErrors>
<TreatSpecificWarningsAsErrors>4018;4028;4100;4114;4189;4245;4267;4309;4319;4389;4456;4458;4554;4592;4702;4706;4715;4800</TreatSpecificWarningsAsErrors>
</ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\src\fheroes2\system;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
Expand Down
4 changes: 4 additions & 0 deletions fheroes2-vs2015.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@
<ClCompile Include="src\fheroes2\gui\statusbar.cpp" />
<ClCompile Include="src\fheroes2\gui\text.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_button.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_dialog.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_scrollbar.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_text.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_tool.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_window.cpp" />
<ClCompile Include="src\fheroes2\heroes\direction.cpp" />
Expand Down Expand Up @@ -478,7 +480,9 @@
<ClInclude Include="src\fheroes2\gui\statusbar.h" />
<ClInclude Include="src\fheroes2\gui\text.h" />
<ClInclude Include="src\fheroes2\gui\ui_button.h" />
<ClInclude Include="src\fheroes2\gui\ui_dialog.h" />
<ClInclude Include="src\fheroes2\gui\ui_scrollbar.h" />
<ClInclude Include="src\fheroes2\gui\ui_text.h" />
<ClInclude Include="src\fheroes2\gui\ui_tool.h" />
<ClInclude Include="src\fheroes2\gui\ui_window.h" />
<ClInclude Include="src\fheroes2\heroes\direction.h" />
Expand Down
4 changes: 4 additions & 0 deletions fheroes2-vs2019.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@
<ClCompile Include="src\fheroes2\gui\statusbar.cpp" />
<ClCompile Include="src\fheroes2\gui\text.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_button.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_dialog.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_scrollbar.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_text.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_tool.cpp" />
<ClCompile Include="src\fheroes2\gui\ui_window.cpp" />
<ClCompile Include="src\fheroes2\heroes\direction.cpp" />
Expand Down Expand Up @@ -479,7 +481,9 @@
<ClInclude Include="src\fheroes2\gui\statusbar.h" />
<ClInclude Include="src\fheroes2\gui\text.h" />
<ClInclude Include="src\fheroes2\gui\ui_button.h" />
<ClInclude Include="src\fheroes2\gui\ui_dialog.h" />
<ClInclude Include="src\fheroes2\gui\ui_scrollbar.h" />
<ClInclude Include="src\fheroes2\gui\ui_text.h" />
<ClInclude Include="src\fheroes2\gui\ui_tool.h" />
<ClInclude Include="src\fheroes2\gui\ui_window.h" />
<ClInclude Include="src\fheroes2\heroes\direction.h" />
Expand Down
67 changes: 65 additions & 2 deletions src/fheroes2/agg/agg_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include <cassert>
#include <cstring>
#include <map>
#include <vector>
Expand All @@ -32,8 +33,7 @@
#include "screen.h"
#include "text.h"
#include "til.h"

#include "image_tool.h"
#include "ui_text.h"

namespace
{
Expand Down Expand Up @@ -1105,5 +1105,68 @@ namespace fheroes2

return height;
}

uint32_t getCharacterLimit( const FontSize fontSize )
{
switch ( fontSize ) {
case FontSize::SMALL:
return static_cast<uint32_t>( GetMaximumICNIndex( ICN::SMALFONT ) ) + 0x20 - 1;
case FontSize::NORMAL:
case FontSize::LARGE:
return static_cast<uint32_t>( GetMaximumICNIndex( ICN::FONT ) ) + 0x20 - 1;
default:
assert( 0 ); // Did you add a new font size? Please add implementation.
}

return 0;
}

const Sprite & getChar( const uint8_t character, const FontType & fontType )
{
if ( character < 0x21 ) {
return errorImage;
}

switch ( fontType.size ) {
case FontSize::SMALL:
switch ( fontType.color ) {
case FontColor::WHITE:
return GetICN( ICN::SMALFONT, character - 0x20 );
case FontColor::GRAY:
return GetICN( ICN::GRAY_SMALL_FONT, character - 0x20 );
case FontColor::YELLOW:
return GetICN( ICN::YELLOW_SMALLFONT, character - 0x20 );
default:
break;
}
break;
case FontSize::NORMAL:
switch ( fontType.color ) {
case FontColor::WHITE:
return GetICN( ICN::FONT, character - 0x20 );
case FontColor::GRAY:
return GetICN( ICN::GRAY_FONT, character - 0x20 );
case FontColor::YELLOW:
return GetICN( ICN::YELLOW_FONT, character - 0x20 );
default:
break;
}
break;
case FontSize::LARGE:
switch ( fontType.color ) {
case FontColor::WHITE:
return GetICN( ICN::WHITE_LARGE_FONT, character - 0x20 );
default:
break;
}
break;
default:
break;
}

assert( 0 ); // Did you add a new font size? Please add implementation.

return errorImage;
}
}
}
5 changes: 5 additions & 0 deletions src/fheroes2/agg/agg_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace fheroes2
{
class Image;
class Sprite;
enum class FontSize : uint8_t;
struct FontType;

namespace AGG
{
Expand All @@ -41,5 +43,8 @@ namespace fheroes2
uint32_t ASCIILastSupportedCharacter( const uint32_t fontType );

int32_t GetAbsoluteICNHeight( int icnId );

uint32_t getCharacterLimit( const FontSize fontSize );
const Sprite & getChar( const uint8_t character, const FontType & fontType );
}
}
17 changes: 13 additions & 4 deletions src/fheroes2/game/game_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "system.h"
#include "text.h"
#include "ui_button.h"
#include "ui_dialog.h"
#include "ui_text.h"

namespace
{
Expand Down Expand Up @@ -176,10 +178,17 @@ fheroes2::GameMode Game::MainMenu( bool isFirstGameRun )
fheroes2::drawMainMenuScreen();
}

Dialog::Message( _( "Please Remember" ),
_( "You can always change game resolution by clicking on the door on the left side of main menu. To switch between windowed "
"and full screen modes press 'F4' key on the keyboard. Enjoy the game!" ),
Font::BIG, Dialog::OK );
fheroes2::Text header( _( "Please Remember" ), { fheroes2::FontSize::NORMAL, fheroes2::FontColor::YELLOW } );

fheroes2::MultiFontText body;
body.add( { _( "You can always change game resolution by clicking on the " ), { fheroes2::FontSize::NORMAL, fheroes2::FontColor::WHITE } } );
body.add( { _( "door" ), { fheroes2::FontSize::NORMAL, fheroes2::FontColor::YELLOW } } );
body.add( { _( " on the left side of main menu.\n\nTo switch between windowed and full screen modes\npress " ),
{ fheroes2::FontSize::NORMAL, fheroes2::FontColor::WHITE } } );
body.add( { _( "F4" ), { fheroes2::FontSize::NORMAL, fheroes2::FontColor::YELLOW } } );
body.add( { _( " key on the keyboard.\n\nEnjoy the game!" ), { fheroes2::FontSize::NORMAL, fheroes2::FontColor::WHITE } } );

fheroes2::showMessage( header, body, Dialog::OK );

conf.resetFirstGameRun();
conf.Save( "fheroes2.cfg" );
Expand Down
67 changes: 67 additions & 0 deletions src/fheroes2/gui/ui_dialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***************************************************************************
* Free Heroes of Might and Magic II: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2021 *
* *
* 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 "ui_dialog.h"
#include "cursor.h"
#include "dialog.h"
#include "localevent.h"
#include "screen.h"
#include "ui_button.h"

namespace
{
const int32_t textOffsetY = 10;
}

namespace fheroes2
{
int showMessage( const TextBase & header, const TextBase & body, const int buttons )
{
// setup cursor
const CursorRestorer cursorRestorer( buttons != 0, ::Cursor::POINTER );

const int32_t headerHeight = header.empty() ? 0 : header.height( BOXAREA_WIDTH ) + textOffsetY;

Dialog::FrameBox box( textOffsetY + headerHeight + body.height( BOXAREA_WIDTH ), buttons != 0 );
const Rect & pos = box.GetArea();

Display & display = Display::instance();
header.draw( pos.x, pos.y + textOffsetY, BOXAREA_WIDTH, display );
body.draw( pos.x, pos.y + textOffsetY + headerHeight, BOXAREA_WIDTH, display );

ButtonGroup group( pos, buttons );
group.draw();

display.render();

int result = Dialog::ZERO;
LocalEvent & le = LocalEvent::Get();

while ( result == Dialog::ZERO && le.HandleEvents() ) {
if ( !buttons && !le.MousePressRight() ) {
break;
}

result = group.processEvents();
}

return result;
}
}
30 changes: 30 additions & 0 deletions src/fheroes2/gui/ui_dialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/***************************************************************************
* Free Heroes of Might and Magic II: https://github.com/ihhub/fheroes2 *
* Copyright (C) 2021 *
* *
* 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 <string>

#include "ui_text.h"

namespace fheroes2
{
int showMessage( const TextBase & header, const TextBase & body, const int buttons );
}
Loading

0 comments on commit 79dc87a

Please sign in to comment.