-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathun.Text.pas
More file actions
136 lines (109 loc) · 3.81 KB
/
un.Text.pas
File metadata and controls
136 lines (109 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{*****************************************************************************
Copyright (C) 2026 NormalUser
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.
*****************************************************************************}
unit un.Text;
{$Mode objfpc} {$H+} { "$H+" necessary due to conversion from String to PChar !!; AnsiString }
{$COPERATORS OFF}
// ******************** interface *************************
interface
USES SDL2, SDL2_Image;
TYPE TAlignment = (TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT);
TFontName = (FONT_SMALL, FONT_BIG, FONT_MAX);
procedure drawTextLine(renderer : PSDL_Renderer; text : String; x, y, r, g, b, fontType : Integer; align : TAlignment);
procedure init_Fonts;
// ******************** implementation ********************
implementation
USES sysutils,
un.Structs,
un.Utils;
CONST NUM_GLYPHS = 128;
VAR glyphs : Array[0..Pred(ORD(FONT_MAX)), 0..NUM_GLYPHS] of TSDL_RECT;
fontTextures : Array[0..Pred(ORD(FONT_MAX))] of PSDL_Texture;
// ***************** TEXT *****************
procedure calcTextDimensions(text : String; fontType : Integer; VAR w, h : Integer);
VAR i, le, character : Integer;
g : TSDL_Rect;
begin
w := 0;
h := 0;
i := 0;
le := length(text);
for i := 1 to le do
begin
character := ORD(text[i]);
g := glyphs[fontType][character];
w := w + g.w;
h := MAX(g.h, h);
end;
end;
procedure drawTextLine(renderer : PSDL_Renderer; text : String; x, y, r, g, b, fontType : Integer; align : TAlignment);
VAR i, le, character, w, h : Integer;
glyph, dest : TSDL_Rect;
begin
if (align <> TEXT_ALIGN_LEFT) then
begin
calcTextDimensions(text, fontType, w, h);
if (align = TEXT_ALIGN_CENTER) then
x := x - (w DIV 2)
else if (align = TEXT_ALIGN_RIGHT) then
x := x - w;
end;
SDL_SetTextureColorMod(fontTextures[fontType], r, g, b);
le := length(text);
for i:= 1 to le do
begin
character := ORD(text[i]);
glyph := glyphs[fontType][character];
dest.x := x; dest.w := glyph.w;
dest.y := y; dest.h := glyph.h;
SDL_RenderCopy(renderer, fontTextures[fontType], @glyph, @dest);
INC(x, glyph.w);
end;
end;
procedure initFont(renderer : PSDL_Renderer; fontType : Integer);
VAR g : TSDL_Rect;
F : File;
countread : Integer;
begin
if fonttype = 0 then
begin
fontTextures[fontType] := img_loadtexture(renderer, 'fonts/small.png');
{$i-}
Assign (F,'fonts/small.dat');
FileMode := fmOpenRead;
Reset(F, sizeof(g));
{$i+}
If IOresult <> 0 then errorMessage('fonts/small.dat not found!');
BlockRead(F, glyphs[0], 123, countread); { ascii "z" = 122 + 1 ==> last record }
close(F);
end;
if fonttype = 1 then
begin
fontTextures[fontType] := img_loadtexture(renderer, 'fonts/big.png');
{$i-}
Assign (F,'fonts/big.dat');
FileMode := fmOpenRead;
Reset(F, sizeof(g));
{$i+}
If IOresult <> 0 then errorMessage('fonts/big.dat not found!');
BlockRead(F, glyphs[1], 123, countread); { ascii "z" = 122 + 1 ==> last record }
close(F);
end;
end;
procedure init_Fonts;
begin
initFont(app.renderer, ORD(FONT_SMALL));
initFont(app.renderer, ORD(FONT_BIG));
end;
end.