-
Notifications
You must be signed in to change notification settings - Fork 109
/
AAText.bb
337 lines (283 loc) · 8.9 KB
/
AAText.bb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
Global AATextEnable% = GetINIInt(OptionFile, "options", "antialiased text")
Global AASelectedFont%
Global AATextCam%,AATextSprite%[150]
Global AACharW%,AACharH%
Global AATextEnable_Prev% = AATextEnable
Global AACamViewW%,AACamViewH%
Type AAFont
Field texture%
Field backup% ;images don't get erased by clearworld
Field x%[128] ;not going to bother with unicode
Field y%[128]
Field w%[128]
Field h%[128]
Field lowResFont% ;for use on other buffers
Field mW%
Field mH%
Field texH%
Field isAA%
End Type
Function InitAAFont()
If AATextEnable Then
;Create Camera
Local cam% = CreateCamera()
CameraViewport cam,0,0,10,10;GraphicWidth,GraphicHeight
;CameraProjMode cam, 2
CameraZoom cam, 0.1
CameraClsMode cam, 0, 0
CameraRange cam, 0.1, 1.5
MoveEntity cam, 0, 0, -20000
AATextCam = cam
CameraProjMode cam,0
;Create sprite
Local spr% = CreateMesh(cam)
Local sf% = CreateSurface(spr)
AddVertex sf, -1, 1, 0, 0, 0 ;vertex 0; uv:0,0
AddVertex sf, 1, 1, 0, 1, 0 ;vertex 1; uv:1,0
AddVertex sf, -1, -1, 0, 0, 1;vertex 2; uv:0,1
AddVertex sf, 1, -1, 0, 1, 1 ;vertex 3; uv:1,1
AddTriangle sf, 0, 1, 2
AddTriangle sf, 3, 2, 1
EntityFX spr, 17+32
PositionEntity spr, 0, 0, 1.0001
EntityOrder spr, -100001
EntityBlend spr, 1
AATextSprite[0] = spr : HideEntity AATextSprite[0]
For i%=1 To 149
spr = CopyMesh(AATextSprite[0],cam)
EntityFX spr, 17+32
PositionEntity spr, 0, 0, 1.0001
EntityOrder spr, -100001
EntityBlend spr, 1
AATextSprite[i] = spr : HideEntity AATextSprite[i]
Next
EndIf
End Function
Function AASpritePosition(ind%,x%,y%)
;THE HORROR
Local nx# = (((Float(x-(AACamViewW/2))/Float(AACamViewW))*2))
Local ny# = -(((Float(y-(AACamViewH/2))/Float(AACamViewW))*2))
;how does this work pls help
nx = nx-((1.0/Float(AACamViewW))*(((AACharW-2) Mod 2)))+(1.0/Float(AACamViewW))
ny = ny-((1.0/Float(AACamViewW))*(((AACharH-2) Mod 2)))+(1.0/Float(AACamViewW))
PositionEntity AATextSprite[ind],nx,ny,1.0
End Function
Function AASpriteScale(ind%,w%,h%)
ScaleEntity AATextSprite[ind],1.0/Float(AACamViewW)*Float(w), 1/Float(AACamViewW)*Float(h), 1
AACharW = w : AACharH = h
End Function
Function ReloadAAFont() ;CALL ONLY AFTER CLEARWORLD
If AATextEnable Then
InitAAFont()
For font.AAFont = Each AAFont
If font\isAA Then
font\texture = CreateTexture(1024,1024,3)
LockBuffer ImageBuffer(font\backup)
LockBuffer TextureBuffer(font\texture)
For ix%=0 To 1023
For iy%=0 To font\texH
px% = ReadPixelFast(ix,iy,ImageBuffer(font\backup)) Shl 24
WritePixelFast(ix,iy,$FFFFFF+px,TextureBuffer(font\texture))
Next
Next
UnlockBuffer TextureBuffer(font\texture)
UnlockBuffer ImageBuffer(font\backup)
EndIf
Next
EndIf
End Function
Function AASetFont(fnt%)
AASelectedFont = fnt
Local font.AAFont = Object.AAFont(AASelectedFont)
If AATextEnable And font\isAA Then
For i%=0 To 149
EntityTexture AATextSprite[i],font\texture
Next
EndIf
End Function
Function AAStringWidth%(txt$)
Local font.AAFont = Object.AAFont(AASelectedFont)
If (AATextEnable) And (font\isAA) Then
Local retVal%=0
For i=1 To Len(txt)
Local char% = Asc(Mid(txt,i,1))
If char>=0 And char<=127 Then
retVal=retVal+font\w[char]-2
EndIf
Next
Return retVal+2
Else
SetFont font\lowResFont
Return StringWidth(txt)
EndIf
End Function
Function AAStringHeight%(txt$)
Local font.AAFont = Object.AAFont(AASelectedFont)
If (AATextEnable) And (font\isAA) Then
Return font\mH
Else
SetFont font\lowResFont
Return StringHeight(txt)
EndIf
End Function
Function AAText(x%,y%,txt$,cx%=False,cy%=False,a#=1.0)
If Len(txt)=0 Then Return
Local font.AAFont = Object.AAFont(AASelectedFont)
If (GraphicsBuffer()<>BackBuffer()) Or (Not AATextEnable) Or (Not font\isAA) Then
SetFont font\lowResFont
Local oldr% = ColorRed() : Local oldg% = ColorGreen() : Local oldb% = ColorBlue()
Color oldr*a,oldg*a,oldb*a
Text x,y,txt,cx,cy
Color oldr,oldg,oldb
Return
EndIf
If cx Then
x=x-(AAStringWidth(txt)/2)
EndIf
If cy Then
y=y-(AAStringHeight(txt)/2)
EndIf
If Camera<>0 Then HideEntity Camera
If ark_blur_cam<>0 Then HideEntity ark_blur_cam
Local tX% = 0
CameraProjMode AATextCam,2
Local char%
Local tw%=0
For i=1 To Len(txt)
char = Asc(Mid(txt,i,1))
If char>=0 And char<=127 Then
tw=tw+font\w[char]
EndIf
Next
AACamViewW = tw
AACamViewW = AACamViewW+(AACamViewW Mod 2)
AACamViewH = AAStringHeight(txt)
AACamViewH = AACamViewH+(AACamViewH Mod 2)
Local vx% = x : If vx<0 Then vx=0
Local vy% = y : If vy<0 Then vy=0
Local vw% = AACamViewW+(x-vx) : If vw+vx>GraphicWidth Then vw=GraphicWidth-vx
Local vh% = AACamViewH+(y-vy) : If vh+vy>GraphicHeight Then vh=GraphicHeight-vy
vw = vw-(vw Mod 2)
vh = vh-(vh Mod 2)
AACamViewH = AACamViewH+(AACamViewH Mod 2)
AACamViewW = vw : AACamViewH = vh
CameraViewport AATextCam,vx,vy,vw,vh
For i=1 To Len(txt)
EntityAlpha AATextSprite[i-1],a
EntityColor AATextSprite[i-1],ColorRed(),ColorGreen(),ColorBlue()
ShowEntity AATextSprite[i-1]
char% = Asc(Mid(txt,i,1))
If char>=0 And char<=127 Then
AASpriteScale(i-1,font\w[char],font\h[char])
AASpritePosition(i-1,tX+(x-vx)+(font\w[char]/2),(y-vy)+(font\h[char]/2))
VertexTexCoords GetSurface(AATextSprite[i-1],1),0,Float(font\x[char])/1024.0,Float(font\y[char])/1024.0
VertexTexCoords GetSurface(AATextSprite[i-1],1),1,Float(font\x[char]+font\w[char])/1024.0,Float(font\y[char])/1024.0
VertexTexCoords GetSurface(AATextSprite[i-1],1),2,Float(font\x[char])/1024.0,Float(font\y[char]+font\h[char])/1024.0
VertexTexCoords GetSurface(AATextSprite[i-1],1),3,Float(font\x[char]+font\w[char])/1024.0,Float(font\y[char]+font\h[char])/1024.0
tX = tX+font\w[char]-2
EndIf
Next
RenderWorld
CameraProjMode AATextCam,0
For i=1 To Len(txt)
HideEntity AATextSprite[i-1]
Next
If Camera<>0 Then ShowEntity Camera
If ark_blur_cam<>0 Then ShowEntity ark_blur_cam
End Function
Function AALoadFont%(file$="Tahoma", height=13, bold=0, italic=0, underline=0, AATextScaleFactor%=2)
Local newFont.AAFont = New AAFont
newFont\lowResFont = LoadFont(file,height,bold,italic,underline)
SetFont newFont\lowResFont
newFont\mW = FontWidth()
newFont\mH = FontHeight()
If AATextEnable And AATextScaleFactor>1 Then
Local hResFont% = LoadFont(file,height*AATextScaleFactor,bold,italic,underline)
Local tImage% = CreateTexture(1024,1024,3)
Local tX% = 0 : Local tY% = 1
SetFont hResFont
Local tCharImage% = CreateImage(FontWidth()+2*AATextScaleFactor,FontHeight()+2*AATextScaleFactor)
ClsColor 0,0,0
LockBuffer TextureBuffer(tImage)
Local miy% = newFont\mH*((newFont\mW*95/1024)+2)
DebugLog miy
newFont\mW = 0
For ix%=0 To 1023
For iy%=0 To miy
WritePixelFast(ix,iy,$FFFFFF,TextureBuffer(tImage))
Next
Next
For i=32 To 126
SetBuffer ImageBuffer(tCharImage)
Cls
Color 255,255,255
SetFont hResFont
Text AATextScaleFactor/2,AATextScaleFactor/2,Chr(i)
Local tw% = StringWidth(Chr(i)) : Local th% = FontHeight()
SetFont newFont\lowResFont
Local dsw% = StringWidth(Chr(i)) : Local dsh% = FontHeight()
Local wRatio# = Float(tw)/Float(dsw)
Local hRatio# = Float(th)/Float(dsh)
SetBuffer BackBuffer()
LockBuffer ImageBuffer(tCharImage)
For iy%=0 To dsh-1
For ix%=0 To dsw-1
Local rsx% = Int(Float(ix)*wRatio-(wRatio*0.0))
If (rsx<0) Then rsx=0
Local rsy% = Int(Float(iy)*hRatio-(hRatio*0.0))
If (rsy<0) Then rsy=0
Local rdx% = Int(Float(ix)*wRatio+(wRatio*1.0))
If (rdx>tw) Then rdx=tw-1
Local rdy% = Int(Float(iy)*hRatio+(hRatio*1.0))
If (rdy>th) Then rdy=th-1
Local ar% = 0
If Abs(rsx-rdx)*Abs(rsy-rdy)>0 Then
For iiy%=rsy To rdy-1
For iix%=rsx To rdx-1
ar=ar+((ReadPixelFast(iix,iiy,ImageBuffer(tCharImage)) And $FF))
Next
Next
ar = ar/(Abs(rsx-rdx)*Abs(rsy-rdy))
If ar>255 Then ar=255
ar = ((Float(ar)/255.0)^(0.5))*255
EndIf
WritePixelFast(ix+tX,iy+tY,$FFFFFF+(ar Shl 24),TextureBuffer(tImage))
Next
Next
UnlockBuffer ImageBuffer(tCharImage)
newFont\x[i]=tX
newFont\y[i]=tY
newFont\w[i]=dsw+2
If newFont\mW<newFont\w[i]-3 Then newFont\mW=newFont\w[i]-3
newFont\h[i]=dsh+2
tX=tX+newFont\w[i]+2
If (tX>1024-FontWidth()-4) Then
tX=0
tY=tY+FontHeight()+6
EndIf
Next
newFont\texH = miy
Local backup% = CreateImage(1024,1024)
LockBuffer ImageBuffer(backup)
For ix%=0 To 1023
For iy%=0 To newFont\texH
px% = ReadPixelFast(ix,iy,TextureBuffer(tImage)) Shr 24
px=px+(px Shl 8)+(px Shl 16)
WritePixelFast(ix,iy,$FF000000+px,ImageBuffer(backup))
Next
Next
UnlockBuffer ImageBuffer(backup)
newFont\backup = backup
UnlockBuffer TextureBuffer(tImage)
FreeImage tCharImage
FreeFont hResFont
newFont\texture = tImage
newFont\isAA = True
Else
newFont\isAA = False
EndIf
Return Handle(newFont)
End Function
;~IDEal Editor Parameters:
;~F#9#19#3F#4B#50#63#6D#7E#88#CB
;~C#Blitz3D