Sprite: DrawText

Just starting out? Need help? Post your questions and find answers here.
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Sprite: DrawText

Post by JHPJHP »

Windows 11 Pro: 24H2
PureBasic 6.21 Beta 9

The following code used to produce a transparent background but now it's black.

I'm not sure if I'm missing something obvious with all the recent changes.

If this is confirmed to be a bug, please move to the correct forum.

Code: Select all

Enumeration
  #MainWindow
  #HiScore
EndEnumeration

InitSprite() : InitKeyboard()

WindowWidth = 800 : WindowHeight = 600

If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "DrawText", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, WindowWidth, WindowHeight)
    CreateSprite(#HiScore, 220, 50, #PB_Sprite_AlphaBlending)

    Repeat
      Repeat
        Event = WindowEvent()

        Select Event
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #MainWindow : Break 2
            EndSelect
        EndSelect
      Until Not Event
      FlipBuffers() : ClearScreen($FFFFFF) : ExamineKeyboard()

      If KeyboardReleased(#PB_Key_Escape) : Break : EndIf

      FreeSprite(#HiScore)
      CreateSprite(#HiScore, 220, 50, #PB_Sprite_AlphaBlending)

      If StartDrawing(SpriteOutput(#HiScore))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawText(0, 0, "HI-SCORE: 999999", RGB(255, 140, 0))
        StopDrawing()
      EndIf
      DisplayTransparentSprite(#HiScore, 16, 25)
    ForEver
  EndIf
EndIf

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Sprite: DrawText

Post by miso »

No bug, changes has been made. I don't know if I will be able to post, I'm timing-out today with every second click...

Seemingly this will be the standard later on:

Code: Select all

Enumeration
  #MainWindow
  #HiScore
EndEnumeration

InitSprite() : InitKeyboard()

WindowWidth = 800 : WindowHeight = 600

If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "DrawText", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, WindowWidth, WindowHeight)
    CreateSprite(#HiScore, 220, 50, #PB_Sprite_AlphaBlending)

    Repeat
      Repeat
        Event = WindowEvent()

        Select Event
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #MainWindow : Break 2
            EndSelect
        EndSelect
      Until Not Event
      ClearScreen($FFFFFF) : ExamineKeyboard()

      If KeyboardReleased(#PB_Key_Escape) : Break : EndIf

      FreeSprite(#HiScore)
      
      CreateSprite(#HiScore, 220, 50, #PB_Sprite_AlphaBlending)
      If StartDrawing(SpriteOutput(#HiScore))
        DrawingMode(#PB_2DDrawing_AllChannels)
        Box(0,0,OutputWidth(),OutputHeight(),RGBA(0,0,0,0))
        DrawText(0, 0, "HI-SCORE: 999999", RGBA(255, 140, 0,255),RGBA(0,0,0,0))
        StopDrawing()
      EndIf
      
      DisplayTransparentSprite(#HiScore, 16, 25)
      FlipBuffers() 
    ForEver
  EndIf
EndIf
Last edited by miso on Thu May 08, 2025 11:28 pm, edited 1 time in total.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Sprite: DrawText

Post by miso »

Also take a look at my sprite font prerender snippet. Works with variable-width fonts, maybe it might come in handy.

Code: Select all

;Prerendering specific characters to sprites for fast display
;using variable-width fonts, works with 2d/3d screens
;To ensure font/characters exists, you might want to use fonts added with registerfontfile

EnableExplicit
#FONTNAME = "Arial"
#FONTSIZE = 24

#BASIC_NUMERICS="0123456789"
#BASIC_SYMBOLS="!@#$%^&*()-_=+[{]};:',<.>/?"+Chr(34)
#BASIC_CHARACTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
#EXTRA_HUN = "ÁÉÓÖŐŰÚÜÍáéóíöőúüű"
#EXTRA_GER = "Ääß"
#EXTRA_CYRILLIC ="ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯфисвуапршолдьтщзйкыегмцчнябБжЖхХъЪэЭюЮĆĘęąŁćłĄŚśźżŻŹŃńĂăȘȚșțČĎĚŇŘŠŤŮŽčďěňřšťůžЄєŒœіІїЇøØĞŞİğşı"
#EXTRA_GREEK="ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωΆΈΉΊΌΎΏ"
#EXTRA_EXTRA = "ÀÂÃÅÆÇÈÊËÌÎÏÐÑÒÔÕÙÛÝÞŸàâãåæçèêëìîïðñòóôõùûýþÿάέήίόύώςΐϊĹĽŔĺľŕ∞ЁёĐđ"
#EXTRA_SYMBOLS = "\`|~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿"
#JP_HIRAGANA = "ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでがとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろゎわゐゑをん"
#JP_KATAKANA = "ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶ"
#JP_SYMBOLS = "゛゜ゝゞ"
#JP_KANJI_TEST = "中主乂九了争亊事二于云互五井亘亙些亜亞亟亠亡亢交亥亦亨享京亭亮亰亳亶人什仁仂仄仆仇今介仍从仏仔仕他仗付大学小少尓尖尚屯山"

Structure myfontstructure
  spr.i
  width.i
  height.i
EndStructure

Global Dim font.myfontstructure(0)
Global font.i = LoadFont(#PB_Any,#FONTNAME,#FONTSIZE)
Global usedcharacters.s = #BASIC_NUMERICS+#BASIC_SYMBOLS+#BASIC_CHARACTERS+#EXTRA_HUN+#EXTRA_GER+#EXTRA_GREEK+#JP_HIRAGANA+#JP_KATAKANA+#JP_KANJI_TEST+#JP_SYMBOLS+#EXTRA_CYRILLIC


;********************************************************************
;This procedure prerenders the lettes you want to use in your program
;********************************************************************
Procedure prerenderfont(characters.s)
  Protected characters_length.i = Len(characters.s)
  Protected i.i, itemp.i, icount.i
  StartDrawing(ScreenOutput())
  DrawingFont(FontID(font.i))
    For i = 1 To characters_length
      itemp = Asc(Mid(characters.s,i,1))
      If itemp>icount 
        icount = itemp 
        ReDim font(icount)
      EndIf
    font(itemp)\SPR = -1
    font(itemp)\width = TextWidth(Mid(characters.s,i,1))
    font(itemp)\height = TextHeight(Mid(characters.s,i,1))
  Next i
  StopDrawing()

  For i = 1 To characters_length
    With font(Asc(Mid(characters.s,i)))
      If \SPR = -1
        \SPR = CreateSprite(#PB_Any,\width,\height,#PB_Sprite_AlphaBlending)
        If IsSprite(\SPR)
          StartDrawing(SpriteOutput(\SPR))
          DrawingMode(#PB_2DDrawing_AllChannels)
          DrawingFont(FontID(font.i))
          Box(0,0,OutputWidth(),OutputHeight(),RGBA(0,0,0,0))
          DrawText(0,0,Mid(characters.s,i,1),RGBA(255,255,255,255),RGBA(0,0,0,0))
          StopDrawing()
        Else
        EndIf
      EndIf
    EndWith
  Next i
EndProcedure

;*****************************************
;This procedure can display text on screen
;*****************************************
Procedure put(x.i,y.i,t$)
  Protected i.i,pos.i,cc.i
  Protected length.i = Len(t$)
  For i = 1 To length.i
    cc=Asc(Mid(t$,i,1))
    If cc>ArraySize(font()) : cc=0 :EndIf
    If cc = 9
      pos = pos+#FONTSIZE
    ElseIf IsSprite(font(cc)\SPR)
       DisplayTransparentSprite(font(cc)\SPR,x+pos,y,255,RGBA(255,255,255,255))
      pos+font(cc)\width
    EndIf
  Next i
EndProcedure

ExamineDesktops()
InitSprite()
InitKeyboard()
InitMouse()
OpenWindow(0,0,0,DesktopUnscaledX(DesktopWidth(0)),DesktopUnscaledY(DesktopHeight(0)),"Variable-width sprite font prerender", #PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0),1,0,0,#PB_Screen_SmartSynchronization)
SetFrameRate(60)
prerenderfont(usedcharacters.s)

Repeat
  Repeat: Until Not WindowEvent()
  ExamineKeyboard() : ExamineMouse() : ClearScreen(RGB(50,20,20))
  
  put(10,10+(5+#FONTSIZE)*1,#BASIC_NUMERICS)
  put(10,10+(5+#FONTSIZE)*2,#BASIC_CHARACTERS)
  put(10,10+(5+#FONTSIZE)*3,#BASIC_SYMBOLS)
  put(10,10+(5+#FONTSIZE)*4,#EXTRA_CYRILLIC)
  put(10,10+(5+#FONTSIZE)*5,#EXTRA_GER)
  put(10,10+(5+#FONTSIZE)*6,#EXTRA_GREEK)
  put(10,10+(5+#FONTSIZE)*7,#EXTRA_HUN)
  put(10,10+(5+#FONTSIZE)*8,#JP_HIRAGANA)
  put(10,10+(5+#FONTSIZE)*9,#JP_KATAKANA)
  put(10,10+(5+#FONTSIZE)*10,#JP_KANJI_TEST)
  put(10,10+(5+#FONTSIZE)*11,#JP_SYMBOLS)
  
  put(10,10+(5+#FONTSIZE)*13,"Hello World!!!")
  
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
Last edited by miso on Thu May 08, 2025 11:30 pm, edited 1 time in total.
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: Sprite: DrawText

Post by JHPJHP »

Hi miso,

Like you I'm timing-out with every second click, so I appreciate the extra effort.

Thank you for the update, I'm not around enough to follow all the changes. Also, thank you for posting your prerendering script, I'm sure the community will benefit from it.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Sprite: DrawText

Post by miso »

I'm not around enough to follow all the changes.
Oh, a side note then. This new method for newly created sprites I descibed is backward combatible (to a degree) with older releases, so if you change your code today, then goes back to some older PB release, it should run as it is without change.
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: Sprite: DrawText

Post by JHPJHP »

Hi miso,

Thank you for the additional information.

I also applied your method to DrawText with gradient color, and it worked great, I just had to add an additional DrawingMode.

In addition, I applied it to RoundBox with great success, especially when the DisplayTransparentSprite color parameter was used.

The end result, it fixed a bunch of drawing issues my Invading Space Aliens game had after installing the latest release of PureBasic.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: Sprite: DrawText

Post by miso »

Nice. As a retro fan I can only say: space invaders cool!
Post Reply