@@ -57,6 +57,7 @@ module TextBoxModule
57
57
this. hoverEnterEvents = hoverEnterEvents
58
58
this. hoverExitEvents = hoverExitEvents
59
59
60
+ this. font = C_NULL
60
61
this. fontPath = fontPath
61
62
this. fontSize = fontSize # Store the base font size
62
63
this. id = id
@@ -144,17 +145,26 @@ module TextBoxModule
144
145
145
146
function UI. load_font (this:: TextBox , basePath:: String , fontPath:: String )
146
147
@debug string (" loading font from $(basePath) \\ $(fontPath) " )
147
-
148
148
# Calculate the true font size based on window resolution
149
149
# trueFontSize = get_true_font_size(this.fontSize)
150
150
trueFontSize = this. fontSize
151
+
152
+ # If the font is already loaded, clean it up
153
+ if this. font != C_NULL
154
+ println (" closing font" )
155
+ println (this. font)
156
+ SDL2. TTF_CloseFont (this. font)
157
+ this. font = C_NULL
158
+ end
159
+ free_text_resources (this)
160
+
151
161
152
162
this. font = load_font_sdl (basePath, fontPath, trueFontSize)
153
163
if this. font == C_NULL
154
164
error (" Failed to load font, $(unsafe_string (SDL2. SDL_GetError ())) " )
155
165
return
156
166
end
157
- if fontPath != joinpath ( " FiraCode-Regular.ttf " )
167
+ if fontPath != " Default "
158
168
this. fontPath = fontPath
159
169
end
160
170
@@ -220,7 +230,7 @@ module TextBoxModule
220
230
if rw != C_NULL
221
231
@debug (" loading font from cache" )
222
232
@debug (" comma separated path: " , get_comma_separated_path (fontPath))
223
- return SDL2. TTF_OpenFontRW ( rw, 1 , Math. TypeConversions. safe_int32_convert (fontSize))
233
+ return CallSDLFunction ( SDL2. TTF_OpenFontRW, rw, 1 , Math. TypeConversions. safe_int32_convert (fontSize))
224
234
end
225
235
end
226
236
@debug " Loading font from disk, there are $(length (JulGame. FONT_CACHE)) fonts in cache"
@@ -254,12 +264,7 @@ module TextBoxModule
254
264
# Examples
255
265
"""
256
266
function UI. rerender_text (this:: TextBox )
257
- if this. renderText != C_NULL
258
- SDL2. SDL_FreeSurface (this. renderText)
259
- end
260
- if this. textTexture != C_NULL
261
- SDL2. SDL_DestroyTexture (this. textTexture)
262
- end
267
+ free_text_resources (this)
263
268
264
269
# Check if we need to wrap text
265
270
if this. maxLineWidth > 0
@@ -288,12 +293,23 @@ module TextBoxModule
288
293
this. size = Math. Vector2 (surface[1 ]. w, surface[1 ]. h)
289
294
290
295
this. textTexture = SDL2. SDL_CreateTextureFromSurface (JulGame. Renderer:: Ptr{SDL2.SDL_Renderer} , this. renderText)
291
-
296
+
292
297
if ! this. isWorldEntity
293
298
UI. center_text (this)
294
299
end
295
300
end
296
301
302
+ function free_text_resources (this:: TextBox )
303
+ if this. renderText != C_NULL
304
+ SDL2. SDL_FreeSurface (this. renderText)
305
+ this. renderText = C_NULL
306
+ end
307
+ if this. textTexture != C_NULL
308
+ SDL2. SDL_DestroyTexture (this. textTexture)
309
+ this. textTexture = C_NULL
310
+ end
311
+ end
312
+
297
313
# Helper function to manually wrap text at character boundaries
298
314
function wrap_text (text:: String , font, maxWidth:: Int , wrapWords:: Bool )
299
315
if maxWidth <= 0 || isempty (text)
@@ -361,7 +377,7 @@ module TextBoxModule
361
377
return
362
378
end
363
379
364
- @info " centering text $(this. name) with anchor $(this. anchor. current_state) "
380
+ # @info "centering text $(this.name) with anchor $(this.anchor.current_state)"
365
381
if this. anchor. current_state == :center
366
382
this. position = Math. Vector2 (max (MAIN. scene. camera. size. x/ 2 - this. size. x/ 2 , 0 ) + this. anchorOffset. x, max (MAIN. scene. camera. size. y/ 2 - this. size. y/ 2 , 0 ) + this. anchorOffset. y)
367
383
elseif this. anchor. current_state == :top
@@ -396,7 +412,9 @@ module TextBoxModule
396
412
397
413
# Close the current font
398
414
if this. font != C_NULL
415
+ println (" closing font from update_font_size" )
399
416
SDL2. TTF_CloseFont (this. font)
417
+ this. font = C_NULL
400
418
end
401
419
402
420
# Load the font with the scaled size
@@ -436,12 +454,11 @@ module TextBoxModule
436
454
end
437
455
438
456
function UI. destroy (this:: TextBox )
439
- if this. textTexture == C_NULL
440
- return
457
+ if this. font != C_NULL
458
+ SDL2. TTF_CloseFont (this. font)
459
+ this. font = C_NULL
441
460
end
442
-
443
- SDL2. SDL_DestroyTexture (this. textTexture)
444
- this. textTexture = C_NULL
461
+ free_text_resources (this)
445
462
end
446
463
#=
447
464
function Base.setproperty!(this::TextBox, s::Symbol, x)
@@ -494,8 +511,9 @@ module TextBoxModule
494
511
function UI. handle_window_resize (this:: TextBox )
495
512
if this. font != C_NULL
496
513
# Close the current font
514
+ println (" closing font from handle_window_resize" )
497
515
SDL2. TTF_CloseFont (this. font)
498
-
516
+ this . font = C_NULL
499
517
# Reload the font with the new scaled size
500
518
basePath = joinpath (BasePath, " assets" , " fonts" )
501
519
UI. load_font (this, basePath, joinpath (this. fontPath))
0 commit comments