Skip to content

Commit 901cfe2

Browse files
committed
Custom font rendering(FreeType-based)
1 parent 351a9ee commit 901cfe2

33 files changed

+3726
-130
lines changed

BaseMenu.cpp

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright (C) 1997-2001 Id Software, Inc.
3+
Copyright (C) 2017 a1batross
34
45
This program is free software; you can redistribute it and/or
56
modify it under the terms of the GNU General Public License
@@ -33,6 +34,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3334
#include "BtnsBMPTable.h"
3435
#include "YesNoMessageBox.h"
3536
#include "ConnectionProgress.h"
37+
#include "FontManager.h"
3638

3739
cvar_t *ui_precache;
3840
cvar_t *ui_showmodels;
@@ -254,28 +256,12 @@ void UI_DrawRectangleExt( int in_x, int in_y, int in_w, int in_h, const int colo
254256
UI_FillRect( x, y, w, h, color );
255257
}
256258

257-
/*
258-
=================
259-
UI_DrawCharacter
260-
=================
261-
*/
262-
void UI_DrawCharacter( int x, int y, int width, int height, int ch, int ulRGBA, HIMAGE hFont )
263-
{
264-
#if 1
265-
EngFuncs::DrawCharacter( x, y, width, height, ch, ulRGBA, hFont );
266-
#else
267-
// TODO: Custom font rendering!
268-
#endif
269-
}
270-
271-
272-
273259
/*
274260
=================
275261
UI_DrawString
276262
=================
277263
*/
278-
void UI_DrawString( int x, int y, int w, int h, const char *string, const int color, int forceColor, int charW, int charH, ETextAlignment justify, bool shadow, EVertAlignment vertAlignment )
264+
void UI_DrawString( HFont font, int x, int y, int w, int h, const char *string, const int color, int forceColor, int charW, int charH, ETextAlignment justify, bool shadow, EVertAlignment vertAlignment )
279265
{
280266
int modulate, shadowModulate;
281267
char line[1024], *l;
@@ -330,9 +316,15 @@ void UI_DrawString( int x, int y, int w, int h, const char *string, const int co
330316
// align the text as appropriate
331317
switch( justify )
332318
{
333-
case QM_LEFT: xx = x; break;
334-
case QM_CENTER: xx = x + ((w - (ColorStrlen( line ) * charW )) / 2); break;
335-
case QM_RIGHT: xx = x + (w - (ColorStrlen( line ) * charW )); break;
319+
case QM_LEFT:
320+
xx = x;
321+
break;
322+
case QM_CENTER:
323+
xx = x + (w - g_FontMgr.GetTextWideScaled( font, line, charH )) / 2;
324+
break;
325+
case QM_RIGHT:
326+
xx = x + (w - g_FontMgr.GetTextWideScaled( font, line, charH ));
327+
break;
336328
}
337329

338330
// draw it
@@ -368,14 +360,13 @@ void UI_DrawString( int x, int y, int w, int h, const char *string, const int co
368360
ch = EngFuncs::UtfProcessChar( (unsigned char) ch );
369361
if( !ch )
370362
continue;
371-
if( ch != ' ' )
363+
if( shadow )
372364
{
373-
if( shadow ) UI_DrawCharacter( xx + ofsX, yy + ofsY, charW, charH, ch, shadowModulate, uiStatic.hFont );
374-
UI_DrawCharacter( xx, yy, charW, charH, ch, modulate, uiStatic.hFont );
365+
g_FontMgr.DrawCharacter( font, ch, Point( xx + ofsX, yy + ofsX ), Size( charW, charH ), shadowModulate );
375366
}
376-
xx += charW;
367+
xx += g_FontMgr.DrawCharacter( font, ch, Point( xx, yy ), Size( charW, charH ), modulate );
377368
}
378-
yy += charH;
369+
yy += charH;
379370
}
380371
}
381372

@@ -710,6 +701,8 @@ void UI_UpdateMenu( float flTime )
710701
EngFuncs::PlayLocalSound( uiSoundIn );
711702
uiStatic.enterSound = -1;
712703
}
704+
705+
g_FontMgr.DebugDraw( uiStatic.hDefaultFont );
713706
}
714707

715708
/*
@@ -1251,6 +1244,9 @@ int UI_VidInit( void )
12511244
// reload all menu buttons
12521245
UI_LoadBmpButtons ();
12531246

1247+
// VidInit FontManager
1248+
g_FontMgr.VidInit();
1249+
12541250
// now recalc all the menus in stack
12551251
for( int i = 0; i < uiStatic.menuDepth; i++ )
12561252
{

BaseMenu.h

+24-42
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ GNU General Public License for more details.
1515

1616
#ifndef BASEMENU_H
1717
#define BASEMENU_H
18+
1819
#include "netadr.h"
1920
#include "keydefs.h"
21+
#include "wrect.h"
22+
#include "Coord.h"
23+
#include "extdll.h"
24+
#include "enginecallback.h"
25+
26+
27+
#ifdef DEBUG
28+
#define Assert(x) if( !(x) ) Host_Error("Assert failed at: %s:%s", __FILE__, __LINE__ );
29+
#else
30+
#define Assert(x)
31+
#endif
2032

2133
#define BIT( x ) ( 1U << x )
2234

@@ -35,6 +47,8 @@ enum
3547
KEY_MENU
3648
};
3749

50+
typedef int HFont;
51+
3852
#define CS_SIZE 64 // size of one config string
3953
#define CS_TIME 16 // size of time string
4054

@@ -85,49 +99,12 @@ enum
8599
#define UI_DOWNARROWFOCUS "gfx/shell/dnarrowf"
86100
#define UI_DOWNARROWPRESSED "gfx/shell/dnarrowp"
87101

88-
struct Point
89-
{
90-
Point() {}
91-
Point( int x, int y ) : x(x), y(y) {}
92-
93-
int x, y;
94-
Point Scale();
95-
friend Point operator +( Point &a, Point &b ) { return Point( a.x + b.x, a.y + b.y ); }
96-
friend Point operator -( Point &a, Point &b ) { return Point( a.x - b.x, a.y - b.y ); }
97-
98-
Point& operator+=( Point &a )
99-
{
100-
x += a.x;
101-
y += a.y;
102-
return *this;
103-
}
104-
105-
Point& operator-=( Point &a )
106-
{
107-
x -= a.x;
108-
y -= a.y;
109-
return *this;
110-
}
111-
112-
Point operator *( float scale ) { return Point( x * scale, y * scale ); }
113-
Point operator /( float scale ) { return Point( x / scale, y / scale ); }
114-
};
115-
116-
struct Size
117-
{
118-
Size() {}
119-
Size( int w, int h ) : w(w), h(h) {}
120-
121-
int w, h;
122-
Size Scale();
123-
};
124-
125-
#include "extdll.h"
126-
#include "enginecallback.h"
127102
#include "EventSystem.h"
128103
#include "Framework.h"
129104
#include "BaseItem.h"
130105
#include "BaseWindow.h"
106+
#include "FontManager.h"
107+
131108

132109
// =====================================================================
133110
// Main menu interface
@@ -165,6 +142,11 @@ typedef struct
165142

166143
HIMAGE hFont; // mainfont
167144

145+
HFont hDefaultFont;
146+
HFont hSmallFont;
147+
HFont hBigFont;
148+
HFont hConsoleFont;
149+
168150
// handle steam background images
169151
bimage_t m_SteamBackground[MAX_BACKGROUNDS];
170152
Size m_SteamBackgroundSize;
@@ -242,7 +224,7 @@ void UI_DrawPicTrans( int x, int y, int width, int height, const int color, cons
242224
void UI_DrawPicHoles( int x, int y, int width, int height, const int color, const char *pic );
243225
void UI_FillRect( int x, int y, int w, int h, const int color );
244226
void UI_DrawRectangleExt( int in_x, int in_y, int in_w, int in_h, const int color, int outlineWidth );
245-
void UI_DrawString(int x, int y, int w, int h, const char *str, const int col, int forceCol, int charW, int charH, ETextAlignment justify, bool shadow, EVertAlignment vertAlign = QM_TOP);
227+
void UI_DrawString( HFont font, int x, int y, int w, int h, const char *str, const int col, int forceCol, int charW, int charH, ETextAlignment justify, bool shadow, EVertAlignment vertAlign = QM_TOP);
246228
inline void UI_DrawRectangle( int x, int y, int w, int h, const int color )
247229
{
248230
UI_DrawRectangleExt( x, y, w, h, color, uiStatic.outlineWidth );
@@ -280,9 +262,9 @@ inline void UI_DrawRectangleExt( Point pos, Size size, const int color, int outl
280262
{
281263
UI_DrawRectangleExt( pos.x, pos.y, size.w, size.h, color, outlineWidth );
282264
}
283-
inline void UI_DrawString( Point pos, Size size, const char *str, const int col, int forceCol, Size chSize, ETextAlignment justify, bool shadow, EVertAlignment verticalAlignment = QM_TOP )
265+
inline void UI_DrawString( HFont font, Point pos, Size size, const char *str, const int col, int forceCol, Size chSize, ETextAlignment justify, bool shadow, EVertAlignment verticalAlignment = QM_TOP )
284266
{
285-
UI_DrawString( pos.x, pos.y, size.w, size.h, str, col, forceCol, chSize.w, chSize.h, justify, shadow, verticalAlignment );
267+
UI_DrawString( font, pos.x, pos.y, size.w, size.h, str, col, forceCol, chSize.w, chSize.h, justify, shadow, verticalAlignment );
286268
}
287269

288270
void UI_StartSound( const char *sound );

CMakeLists.txt

+20-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
21-
#щ
21+
#
2222

2323
cmake_minimum_required(VERSION 2.6.0)
2424
project (MAINUI)
@@ -78,6 +78,10 @@ set (MAINUI_MENUS_SOURCES
7878
)
7979

8080
set (MAINUI_SOURCES
81+
font/FontManager.cpp
82+
font/WinAPIFont.cpp
83+
font/FreeTypeFont.cpp
84+
font/BaseFontBackend.cpp
8185
EventSystem.h
8286
BaseMenu.cpp
8387
Btns.cpp
@@ -90,20 +94,33 @@ set (MAINUI_SOURCES
9094

9195
list(APPEND MAINUI_SOURCES ${MAINUI_CONTROLS_SOURCES})
9296
list(APPEND MAINUI_SOURCES ${MAINUI_MENUS_SOURCES})
97+
add_library (${MAINUI_LIBRARY} SHARED ${MAINUI_SOURCES})
9398

9499
add_compile_options( -Wall -Wextra -Wpedantic -Wno-unused-parameter -Wno-unused-variable )
95100

96101
if( NOT XASH_SDK )
97102
set( XASH_SDK "../" )
98103
endif( )
99104

100-
include_directories (${XASH_SDK}/common ${XASH_SDK}/engine ${XASH_SDK}/pm_shared . controls/ menus/)
101-
add_library (${MAINUI_LIBRARY} SHARED ${MAINUI_SOURCES})
105+
include_directories (${XASH_SDK}/common ${XASH_SDK}/engine ${XASH_SDK}/pm_shared . controls/ menus/ utl/ font/)
106+
102107

103108
if( MSVC )
104109
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
105110
endif( )
106111

112+
# Font Rendering(FreeType or WinAPI)
113+
if( MAINUI_CPP_ENABLE_FONT_RENDER )
114+
if( NOT WIN32 )
115+
find_package(PkgConfig)
116+
pkg_check_modules(FC REQUIRED fontconfig)
117+
include_directories( ${FC_INCLUDE_DIRS} )
118+
target_link_libraries( ${MAINUI_LIBRARY} ${FC_LIBRARIES} )
119+
add_definitions( -DMAINUI_USE_FREETYPE )
120+
endif( )
121+
add_definitions( -DMAINUI_USE_CUSTOM_FONT_RENDER )
122+
endif( )
123+
107124
set_target_properties (${MAINUI_LIBRARY} PROPERTIES
108125
POSITION_INDEPENDENT_CODE 1 )
109126

Coord.h

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Coord.h -- simple coordinate and size management
3+
Copyright (C) 2017 a1batross
4+
5+
6+
This program is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU General Public License
8+
as published by the Free Software Foundation; either version 2
9+
of the License, or (at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
15+
See the GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program; if not, write to the Free Software
19+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20+
*/
21+
#pragma once
22+
#ifndef COORD_H
23+
#define COORD_H
24+
25+
struct Point
26+
{
27+
Point() { x = y = 0; }
28+
Point( int x, int y ) : x(x), y(y) {}
29+
30+
int x, y;
31+
Point Scale();
32+
friend Point operator +( Point &a, Point &b ) { return Point( a.x + b.x, a.y + b.y ); }
33+
friend Point operator -( Point &a, Point &b ) { return Point( a.x - b.x, a.y - b.y ); }
34+
35+
Point& operator+=( Point &a )
36+
{
37+
x += a.x;
38+
y += a.y;
39+
return *this;
40+
}
41+
42+
Point& operator-=( Point &a )
43+
{
44+
x -= a.x;
45+
y -= a.y;
46+
return *this;
47+
}
48+
49+
Point operator *( float scale ) { return Point( x * scale, y * scale ); }
50+
Point operator /( float scale ) { return Point( x / scale, y / scale ); }
51+
};
52+
53+
struct Size
54+
{
55+
Size() { w = h = 0; }
56+
Size( int w, int h ) : w(w), h(h) {}
57+
58+
int w, h;
59+
Size Scale();
60+
};
61+
62+
#endif // COORD_H

0 commit comments

Comments
 (0)