| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
TPixelTextureNode = class; |
| 20 |
TInternalReusedPixelTextureNode = class; |
| 21 |
|
| 22 |
{ Cache for font texture nodes (TPixelTextureNode that we need for each font). |
| 23 |
Descends from TX3DNodesCache, so can also cache textures, movies, any X3D nodes. } |
| 24 |
TX3DFontTexturesCache = class(TX3DNodesCache) |
| 25 |
strict private |
| 26 |
type |
| 27 |
{ @exclude Internal for TX3DFontTexturesCache. } |
| 28 |
TCachedFontTexture = class |
| 29 |
private |
| 30 |
Font: TCastleFont; |
| 31 |
Blending: Boolean; |
| 32 |
References: Cardinal; |
| 33 |
FontTexture: TPixelTextureNode; |
| 34 |
end; |
| 35 |
|
| 36 |
TCachedFontTextureList = {$ifdef FPC}specialize{$endif} TObjectList<TCachedFontTexture>; |
| 37 |
|
| 38 |
var |
| 39 |
CachedFontTextures: TCachedFontTextureList; |
| 40 |
public |
| 41 |
constructor Create; |
| 42 |
destructor Destroy; override; |
| 43 |
|
| 44 |
{ Return TPixelTextureNode to draw given font configuration. } |
| 45 |
function LoadFontTexture(const Font: TCastleFont; |
| 46 |
const Blending: boolean): TPixelTextureNode; |
| 47 |
|
| 48 |
procedure FreeFontTexture(var FontTexture: TPixelTextureNode); |
| 49 |
|
| 50 |
function Empty: boolean; override; |
| 51 |
end; |
| 52 |
|
| 53 |
{$endif read_interface} |
| 54 |
|
| 55 |
{$ifdef read_implementation} |
| 56 |
|
| 57 |
{ TX3DFontTexturesCache ------------------------------------------------------------ } |
| 58 |
|
| 59 |
constructor TX3DFontTexturesCache.Create; |
| 60 |
begin |
| 61 |
inherited; |
| 62 |
CachedFontTextures := TCachedFontTextureList.Create; |
| 63 |
end; |
| 64 |
|
| 65 |
destructor TX3DFontTexturesCache.Destroy; |
| 66 |
begin |
| 67 |
if CachedFontTextures <> nil then |
| 68 |
begin |
| 69 |
Assert(CachedFontTextures.Count = 0, ' Some references to font textures still exist when freeing TX3DFontTexturesCache'); |
| 70 |
FreeAndNil(CachedFontTextures); |
| 71 |
end; |
| 72 |
inherited; |
| 73 |
end; |
| 74 |
|
| 75 |
function TX3DFontTexturesCache.LoadFontTexture(const Font: TCastleFont; |
| 76 |
const Blending: boolean): TPixelTextureNode; |
| 77 |
var |
| 78 |
I: Integer; |
| 79 |
C: TCachedFontTexture; |
| 80 |
Image: TGrayscaleImage; |
| 81 |
begin |
| 82 |
{$warnings off} // using FontData, will be internal in the future |
| 83 |
if Font.FontData = nil then |
| 84 |
raise Exception.Create('Only loaded fonts can be used with TX3DFontTexturesCache.LoadFontTexture'); |
| 85 |
Image := Font.FontData.Image; |
| 86 |
{$warnings on} |
| 87 |
|
| 88 |
for I := 0 to CachedFontTextures.Count - 1 do |
| 89 |
begin |
| 90 |
C := CachedFontTextures[I]; |
| 91 |
if (C.Font = Font) and |
| 92 |
(C.Blending = Blending) then |
| 93 |
begin |
| 94 |
Inc(C.References); |
| 95 |
|
| 96 |
if Log then |
| 97 |
WritelnLog('++', 'Font texture %s, blending %s : %d', [ |
| 98 |
UriDisplay(Image.URL), |
| 99 |
BoolToStr(Blending, true), |
| 100 |
C.References |
| 101 |
]); |
| 102 |
|
| 103 |
Exit(C.FontTexture); |
| 104 |
end; |
| 105 |
end; |
| 106 |
|
| 107 |
Result := TInternalReusedPixelTextureNode.Create; |
| 108 |
Result.KeepExistingBegin; // never free it using ref-counting |
| 109 |
{ although repeat=TRUE seems counter-intuitive, in fact: |
| 110 |
- We *can* use it, since CastleTextureFontData surrounds each glyph from |
| 111 |
the right and top with GlyphPadding, so we will not accidentally pick |
| 112 |
other glyph by slightly going outside the texture area. |
| 113 |
- We *want* to use it actually, for EXTRA_GLYPH_SPACE. This way glyphs |
| 114 |
on the very left and bottom border of the font texture will also |
| 115 |
have a transparent pixel row to the left / bottom. } |
| 116 |
Result.RepeatS := true; |
| 117 |
Result.RepeatT := true; |
| 118 |
{ convert grayscale image with TreatAsAlpha to TGrayscaleAlphaImage } |
| 119 |
Result.FdImage.Value := TGrayscaleAlphaImage.Create; |
| 120 |
Result.FdImage.Value.Assign(Image); |
| 121 |
if Blending then |
| 122 |
Result.AlphaChannel := acBlending |
| 123 |
else |
| 124 |
Result.AlphaChannel := acTest; |
| 125 |
|
| 126 |
C := TCachedFontTexture.Create; |
| 127 |
CachedFontTextures.Add(C); |
| 128 |
C.References := 1; |
| 129 |
C.Font := Font; |
| 130 |
C.Blending := Blending; |
| 131 |
C.FontTexture := Result; |
| 132 |
|
| 133 |
if Log then |
| 134 |
WritelnLog('++', 'Font texture %s, blending %s : %d', [ |
| 135 |
UriDisplay(Image.URL), |
| 136 |
BoolToStr(Blending, true), |
| 137 |
1 |
| 138 |
]); |
| 139 |
end; |
| 140 |
|
| 141 |
procedure TX3DFontTexturesCache.FreeFontTexture(var FontTexture: TPixelTextureNode); |
| 142 |
var |
| 143 |
I: Integer; |
| 144 |
C: TCachedFontTexture; |
| 145 |
begin |
| 146 |
if FontTexture = nil then Exit; |
| 147 |
|
| 148 |
for I := 0 to CachedFontTextures.Count - 1 do |
| 149 |
begin |
| 150 |
C := CachedFontTextures[I]; |
| 151 |
if C.FontTexture = FontTexture then |
| 152 |
begin |
| 153 |
if Log then |
| 154 |
{$warnings off} // using FontData, will be internal in the future |
| 155 |
WritelnLog('--', 'Font texture %s, blending %s : %d', [ |
| 156 |
UriDisplay(C.Font.FontData.Image.URL), |
| 157 |
BoolToStr(C.Blending, true), |
| 158 |
C.References - 1 |
| 159 |
]); |
| 160 |
{$warnings on} |
| 161 |
|
| 162 |
FontTexture := nil; |
| 163 |
|
| 164 |
if C.References = 1 then |
| 165 |
begin |
| 166 |
{ Simple FreeAndNil would also be OK, as this node should never |
| 167 |
be a child of another node in X3D graph. |
| 168 |
(Being a TTextNode.FontTextureNode value doesn't make it a normal child, |
| 169 |
i.e. TX3DNode internal child-parent references are not set.) |
| 170 |
But for safety we use "nice way" with KeepExistingEnd + FreeIfUnusedAndNil. } |
| 171 |
C.FontTexture.KeepExistingEnd; |
| 172 |
{ Note that freeing C.FontTexture will also do |
| 173 |
C.FontTexture.InternalRendererResourceFree, and that's good. } |
| 174 |
FreeIfUnusedAndNil(C.FontTexture); |
| 175 |
|
| 176 |
CachedFontTextures.Delete(I); |
| 177 |
CheckEmpty; |
| 178 |
end else |
| 179 |
Dec(C.References); |
| 180 |
|
| 181 |
Exit; |
| 182 |
end; |
| 183 |
end; |
| 184 |
|
| 185 |
raise EInternalError.CreateFmt('FreeFontTexture: no reference found for font texture %s', |
| 186 |
[PointerToStr(FontTexture)]); |
| 187 |
end; |
| 188 |
|
| 189 |
function TX3DFontTexturesCache.Empty: boolean; |
| 190 |
begin |
| 191 |
Result := (inherited Empty) and (CachedFontTextures.Count = 0); |
| 192 |
end; |
| 193 |
|
| 194 |
{$endif read_implementation} |