Added small program to measure font atlas performance, ryzom/ryzomcore#626
[ryzomcore.git] / nel / samples / 3d / font_perf / main.cpp
blobd183d3fa8b68d789e8c0055632af2f2fe980e31e
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2020 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "nel/misc/types_nl.h"
19 #include "nel/misc/event_emitter.h"
20 #include "nel/misc/event_listener.h"
21 #include "nel/misc/path.h"
22 #include "nel/misc/random.h"
24 // look at 3dinit example
25 #include "nel/3d/nelu.h"
27 // used for font management
28 #include "nel/3d/font_manager.h"
29 #include "nel/3d/computed_string.h"
30 #include "nel/3d/text_context.h"
31 #include "nel/3d/driver_user.h"
33 #ifdef NL_OS_WINDOWS
34 #ifndef NL_COMP_MINGW
35 #define NOMINMAX
36 #endif
37 #include <windows.h>
38 #endif // NL_OS_WINDOWS
40 #ifndef FONT_DIR
41 # define FONT_DIR "."
42 #endif
44 using namespace std;
45 using namespace NL3D;
46 using namespace NLMISC;
48 int main(int argc, char **argv)
50 // look at 3dinit example
51 CNELU::init (800, 600, CViewport(), 32, true, 0, false, false);
53 NLMISC::CPath::addSearchPath(FONT_DIR);
55 // create a font manager
56 CFontManager fontManager;
58 // set the font cache to 2 megabytes (default is 1mb)
59 fontManager.setMaxMemory(2000000);
61 CTextContext tc;
63 tc.init (CNELU::Driver, &fontManager);
65 // The first param is the font name (could be ttf, pfb, fon, etc...). The
66 // second one is optional, it's the font kerning file
67 tc.setFontGenerator (NLMISC::CPath::lookup("beteckna.ttf"));
69 NLMISC::CRandom rnd;
71 uint nbCount = 100000;
72 TTicks startTick = CTime::getPerformanceTime();
73 std::string txt;
74 for(uint i = 0; i < nbCount; ++i)
76 uint fontSize = rnd.rand(200);
77 bool embolden = rnd.rand(1) == 1;
78 bool oblique = rnd.rand(1) == 1;
79 txt = toString("Lorem ipsum %03d", fontSize);
81 CComputedString cs;
82 fontManager.computeString(txt, tc.getFontGenerator(), CRGBA::White, fontSize, embolden, oblique, CNELU::Driver, cs);
85 TTicks endTick = CTime::getPerformanceTime();
87 double deltaTime = CTime::ticksToSecond(endTick-startTick);
88 std::string msg = toString("Generated %d strings in %.2fs\n", nbCount, deltaTime);
90 nlinfo("%s", msg.c_str());
91 printf("%s", msg.c_str());
93 fontManager.dumpCache ("font_pref_cache_dump.tga");
95 // look at 3dinit example
96 CNELU::release();
98 return EXIT_SUCCESS;