February 27, 2021, 03:59:57 AM

Author Topic: [bb] 3D Font /Text comands by _PJ_ [ 1+ years ago ]  (Read 1231 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 3D Font /Text comands by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : 3D Font /Text comands
Author : _PJ_
Posted : 1+ years ago

Description : ; This code owes much to Jim Brown for SpriteControl.

; ************************ SPECIAL NOTICE FOR SPRITECONTROL USERS!!!! ************************

; The following functions overwrite those of SpriteControl in case of duplicates, please keep these ones and do not
;       include the duplicate. Provided any 3D Text routines use the functions from here, there should be no conflicts
;      with any other functionality of SpriteControl.

; Text3D
; ModifyText3D

;The following are identical to their original sources:
; DrawImage3D
; ImageToSprite
; CreateSpritePivot
; CreateImage3D

;      SpriteControl users may also require to remove the declaration of "Global spritepivot,spritecamera" from this library.


Code :
Code: BlitzBasic
  1. ;;; Example (Remove from include)
  2.  
  3. Graphics3D 800,600
  4. SetBuffer BackBuffer()
  5.  
  6. ;SpriteControl Initialisation
  7. Global spritepivot,spritecamera
  8.  
  9. ;BitmapFont Initialisation
  10. Global CurrentFont
  11.         spritecamera=CreateCamera()
  12.         spritepivot=CreateSpritePivot(spritecamera)
  13.  
  14. InitialiseBitmapFonts()
  15.  
  16. ;Times New Roman, Size 24 and Bold
  17. TimesNewRoman=LoadFont3D("Times new Roman",24,True)
  18.  
  19. ;Arial, Size 20 and Italic
  20. Arial=LoadFont3D("Arial",20,False,True)
  21.  
  22. ;Verdana, Size 36, Bold and Underline
  23. Verdana=LoadFont3D("Verdana",36,True,False,True)
  24.  
  25. SetFont3D(TimesNewRoman)
  26. TimesNewRomanSprite=Text3D(400,150,"Times New Roman")
  27. SetFont3D(Arial)
  28. ArialSprite=Text3D(400,300,"Arial")
  29. SetFont3D(Verdana)
  30. VerdanaSprite=Text3D(400,450,"Verdana")
  31.                        
  32. FreeAll3DFonts
  33.                        
  34. While Not KeyDown(1)
  35.         UpdateWorld
  36.         RenderWorld
  37.         Flip
  38. Wend
  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. ; This code owes much to Jim Brown for SpriteControl.
  96.  
  97. ; ************************ SPECIAL NOTICE FOR SPRITECONTROL USERS!!!! ************************
  98.  
  99. ; The following functions overwrite those of SpriteControl in case of duplicates, please keep these ones and do not
  100. ;               include the duplicate. Provided any 3D Text routines use the functions from here, there should be no conflicts
  101. ;               with any other functionality of SpriteControl.
  102.  
  103. ; Text3D
  104. ; ModifyText3D
  105.  
  106. ;The following are identical to their original sources:
  107. ; DrawImage3D
  108. ; ImageToSprite
  109. ; CreateSpritePivot
  110. ; CreateImage3D
  111.  
  112. ;               SpriteControl users may also require to remove the declaration of "Global spritepivot,spritecamera" from this library.
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. ; Notes on Use:
  124.  
  125. ; Include this file into your program.
  126.  
  127. ; It is important to call the "InitialiseBitmapFonts()" function before using any of the 3D Text commandset.
  128. ; Once this has been done, the full functionality can be explored.
  129.  
  130. ; Either LoadExisting bitmap fonts created with the "MAKEFONT.bb" code ( http://www.blitzbasic.com/codearcs/codearcs.php?code=2533#comments )
  131. ;               with the LoadBitmapFont() command, ehich will return a valid Handle for use with SetFont3D()
  132. ;               Alternatively To create and utilise fonts during runtime, use the LoadFont3D() command. Again, this will return
  133. ;               a handle for use.
  134.  
  135. ; Just as with Blitz3D's standard 2D Font/Text commands, once a Font is loaded, it can be:
  136. ;       Free'd                                  -               FreeFont3D( FontHandle )
  137. ;       Set as current Font             -               SetFont3D( FontHandle )
  138.  
  139. ;       To display text in the currently set Font, use the Text3D() command.
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. ;SpriteControl Initialisation - ONLY COMMENTED OUT DUE TO EXAMPLE!
  164. ;Global spritepivot,spritecamera
  165.  
  166. ;Bitmap Font Initialisation - ONLY COMMENTED OUT DUE TO EXAMPLE!
  167. ;Global CurrentFont
  168.  
  169. Type BitmapFont
  170.         Field Image%
  171.         Field CharWidth%
  172.         Field CharHeight%
  173. End Type
  174.  
  175.  
  176. ; Bitmap Font Functions;                                        ########################################################################################
  177.  
  178. ; Similar to how Blitz3D handles Fonts normally, as a safety precaution, the initialisation function
  179. ; Ensures we have a valid font set as "CurrentFont". This same font is used if SetFont is used on an invalid font too.
  180. Function InitialiseBitmapFonts()
  181.         FreeAll3DFonts()
  182.         CurrentFont=LoadFont3D("Blitz",12)
  183. End Function
  184.  
  185. Function FreeAll3DFonts()
  186.         Local AllFonts.BitmapFont
  187.         For AllFonts = Each BitmapFont
  188.                 If (AllFonts.BitmapFont <> Null)
  189.                         If (AllFontsImage)
  190.                                 FreeImage AllFontsImage
  191.                                 AllFontsImage = False
  192.                                 AllFontsCharWidth=False
  193.                                 AllFontsCharHeight=False
  194.                         End If
  195.                 End If
  196.                 Delete AllFonts.BitmapFont
  197.         Next
  198. End Function
  199.  
  200. Function SetFont3D(FontImageHandle%)
  201.         If (FontImageHandle)
  202.                 CurrentFont=FontImageHandle
  203.         Else
  204.                 SetFontDefault3D()
  205.         End If
  206. End Function
  207.  
  208. Function LoadFont3D(RealFontName$,MyFontHeight=14,MyFontBold=False,MyFontItalic=False,MyFontULine=False)
  209.         If (RealFontName$="")
  210.                 RealFontName$="Blitz"
  211.         End If
  212.         Local FontPath$=SystemProperty ("windowsdir") +"Fonts"+RealFontName$+".ttf"
  213.         Local MyFont = LoadFont(FontPath$,MyFontHeight,MyFontBold,MyFontItalic,MyFontULine)
  214.        
  215.                 If (Not(MyFont))
  216.                         Return False
  217.                 End If 
  218.        
  219.         SetFont MyFont 
  220.         MyFontImage=CreateFontImage()
  221.         FreeFont MyFont
  222.         MyFont=False
  223.         Return MyFontImage     
  224. End Function
  225.  
  226. Function LoadBitmapFont(FilePath$)
  227.         Local TempImage=LoadImage(FilePath$)
  228.         Local IterCharsX,IterCharsY
  229.         If (TempImage)
  230.                 BMF.BitmapFont= New BitmapFont
  231.                 BMFCharWidth=ImageWidth(TempImage) Shr 4
  232.                 BMFCharHeight=ImageHeight(TempImage) Shr 4
  233.                 BMFImage=CreateImage(CharWidth,CharHeight,256)
  234.                 For IterCharsY=0 To 15
  235.                         For IterCharsX=0 To 15         
  236.                                 SetBuffer ImageBuffer(BMFImage,(IterCharsY*16)+IterCharsX)
  237.                                 DrawImageRect TempImage,0,0,IterCharsX,IterCharsY,BMFCharWidth,BMFCharHeight
  238.                         Next
  239.                 Next
  240.                 SetBuffer BackBuffer
  241.                 FreeImage tempImage
  242.                 TempImage=False
  243.                 CurrentFont=BMFImage
  244.                 Return BMFImage
  245.         End If
  246.         Return False
  247. End Function
  248.  
  249. Function GetBitmapFontByImageHandle.BitmapFont(ImageHandle)
  250.         If (ImageHandle)
  251.                 For BMF.BitmapFont = Each BitmapFont
  252.                         If BMFImage=ImageHandle
  253.                                 Return BMF.BitmapFont
  254.                                 Exit
  255.                         End If
  256.                 Next
  257.         Else
  258.                 Return Null
  259.         End If
  260. End Function
  261.  
  262. Function FreeFont3D(ImageHandle)       
  263.         If (ImageHandle)
  264.                 Free.BitmapFont=GetBitmapFontByImageHandle.BitmapFont(ImageHandle)
  265.                 If Free.BitmapFont <> Null
  266.                         If (FreeImage)
  267.                                 FreeImage FreeImage
  268.                                 FreeImage=False
  269.                                 Delete Free.BitmapFont
  270.                         End If
  271.                 End If
  272.         End If
  273.  
  274.         ;Safety check, let's keep the default font in memory.
  275.         Local FirstFont.BitmapFont=First BitmapFont
  276.         If (FirstFont.BitmapFont = Null)
  277.                 CurrentFont=LoadFont3D("Arial",12)             
  278.         End If
  279.         If (CurrentFont=ImageHandle)
  280.                 SetFontDefault3D()
  281.         End If
  282. End Function
  283.  
  284. Function BitmapFontText(BitmapFontImageHandle,TextString$)
  285.         TextString$=TextString$+" "
  286.         Local IterChars
  287.         Local Frame
  288.         TextImage.BitmapFont=GetBitmapFontByImageHandle.BitmapFont(BitmapFontImageHandle)
  289.         Local CWidth=TextImageCharWidth
  290.         For IterChars=1 To Len(TextString$)
  291.                 Frame=Asc(Mid$(TextString$,IterChars,1))
  292.                 DebugLog Str(Frame)
  293.                 DrawImage TextImageImage,IterChars*CWidth,0,Frame
  294.         Next
  295. End Function
  296.  
  297. Function SetFontDefault3D()
  298.         Local FirstFont.BitmapFont=First BitmapFont
  299.        
  300.         If FirstFont.BitmapFont = Null
  301.                 CurrentFont=LoadFont3D("Blitz",12)
  302.         Else
  303.                 CurrentFont=FirstFontImage
  304.         End If 
  305. End Function
  306.  
  307. Function GetFontCharMaxWidth%()
  308.         Local Max%=False
  309.         Local Char%=False
  310.         Local Width%=0
  311.         For Char%=31 To 126
  312.                 Width=StringWidth(Chr$(Char))
  313.                 If (Width>Max)
  314.                         Max=Width
  315.                 End If
  316.         Next
  317.        
  318.         Return Max
  319. End Function
  320.  
  321. Function GetFontCharMaxHeight%()
  322.         Local Max%=False
  323.         Local Char%=False
  324.         Local Height%=0
  325.         For Char%=31 To 126
  326.                 Height=StringHeight(Chr$(Char))
  327.                 If (Height>Max)
  328.                        
  329.                         Max=Height
  330.                 End If
  331.         Next
  332.         Return Max
  333. End Function
  334.  
  335. Function CreateFontImage()
  336.  
  337.         BMF.BitmapFont= New BitmapFont
  338.  
  339.         BMFCharWidth=GetFontCharMaxWidth%()
  340.         BMFCharHeight=GetFontCharMaxHeight%()
  341.         Local XPlus%=(BMFCharWidth Shr True)
  342.         Local YPlus%=(BMFCharHeight Shr True)
  343.  
  344.         Local Frame%
  345.  
  346.         BMFImage=CreateImage(BMFCharWidth,BMFCharHeight,256)
  347.                
  348.         For Frame=0 To 255
  349.                        
  350.                 SetBuffer ImageBuffer(BMFImage,Frame)
  351.                 Text XPlus,YPlus,Chr$(Frame),True,True
  352.         Next
  353.        
  354.         SetBuffer BackBuffer()
  355.         ; Returns the Font Image handle to be used as a Handle for the Font itself. useful for checking.
  356.         Return BMFImage
  357.  
  358. End Function
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418. ; Create a single 'sprite pivot' and scale to screen resolution
  419. Function CreateSpritePivot(parentcam=0,dist#=1.0)
  420.         Local gw=GraphicsWidth()
  421.         Local gh=GraphicsHeight()
  422.         spritepivot=CreatePivot(parentcam)
  423.         Local aspect#=Float(gh)/Float(gw)
  424.         Local scale#=2.0/gw
  425.         PositionEntity spritepivot,-1,aspect,dist
  426.         ScaleEntity spritepivot,scale,scale,scale
  427.         NameEntity spritepivot,"SpriteControl Pivot"
  428.         Return spritepivot
  429. End Function
  430.  
  431. Function Text3D(xpos,ypos,txt$,flags=5,par=-1)
  432.         If par=-1 par=spritepivot
  433.         If txt$="" Then txt$=" "
  434.         If (Not(CurrentFont))
  435.                 SetFontDefault3D()
  436.         End If
  437.         CFont.BitmapFont=GetBitmapFontByImageHandle(CurrentFont)
  438.         Local txtwidth=CFontCharWidth * (Len(Txt$)+1)
  439.         Local txtheight=CFontCharHeight
  440.  
  441.         Local gbuffer=GraphicsBuffer()
  442.         Local tmpimage=CreateImage(txtwidth,txtheight)
  443.         SetBuffer ImageBuffer(tmpimage)
  444.         Cls
  445.         BitmapFontText(CFontImage,txt$)
  446.         SetBuffer gbuffer
  447.         textsprite=ImageToSprite(tmpimage,flags)
  448.         FreeImage tmpimage
  449.         DrawImage3D textsprite,xpos,ypos
  450.         Return textsprite
  451. End Function
  452.  
  453. ; Modify existing text in quad sprite
  454. Function ModifyText3D(sprite,xpos,ypos,t$,flags=5)
  455.         If t$="" Then t$=" "
  456.         Local par=GetParent(sprite)
  457.         FreeImage3D sprite
  458.         Local newstextsprite=Text3D(xpos,ypos,t$,flags,par)
  459.         Return newsprite
  460. End Function
  461.  
  462. Function CreateImage3D(w=1,h=1,par=-1)
  463.         If par=-1 par=spritepivot
  464.         Local sprite=CreateMesh(par)
  465.         Local s=CreateSurface(sprite)
  466.         AddVertex s,0,0,0 ,0,0 : AddVertex s,2,0,0 , 1,0
  467.         AddVertex s,0,-2,0 ,0,1 : AddVertex s,2,-2,0 , 1,1
  468.         AddTriangle s,0,1,2 : AddTriangle s,3,2,1
  469.         ScaleEntity sprite,Float(w)/2,Float(h)/2,1
  470.         EntityFX sprite,1+16
  471.         EntityOrder sprite,-100
  472.         PositionEntity sprite,-10000,-10000,0
  473.         Return sprite
  474. End Function
  475.  
  476. ; Position quad sprite at 2D screen coordinates
  477. Function DrawImage3D(sprite,x,y,frame=-99999,z#=0)
  478.         PositionEntity sprite,x+0.5,-y+0.5,z
  479.         If frame<>-99999
  480.                 Local tex=GetSpriteTexture(sprite)
  481.                 Local en$=EntityName(sprite)
  482.                 Local fw#=Float(Mid$(en$,Instr(en$,"W")+1,Instr(en$,"H")-Instr(en$,"W")))
  483.                 Local fh#=Float(Mid$(en$,Instr(en$,"H")+1,Instr(en$,"X")-Instr(en$,"H")))
  484.                 Local framesH=Int(Mid$(en$,Instr(en$,"X")+1,Instr(en$,"Y")-Instr(en$,"X")))
  485.                 Local framesV=Int(Mid$(en$,Instr(en$,"Y")+1,Instr(en$,"E")-Instr(en$,"Y")))
  486.                 Local numframes=framesH*framesV
  487.                 If numframes>0
  488.                         If frame>numframes frame=(frame Mod numframes)
  489.                         If frame<1 frame=numframes-(Abs(frame) Mod numframes)
  490.                         frame=frame-1
  491.                         Local oy#=frame/framesH
  492.                         Local ox#=frame-(oy*framesH)
  493.                         PositionTexture tex,-ox/fw,-oy/fh
  494.                         FreeTexture tex
  495.                 EndIf
  496.         EndIf
  497. End Function
  498.  
  499. ; Convert an existing 2D image to quad sprite
  500. Function ImageToSprite(img,texflags=5,numframesX=1,numframesY=1,par=-1)
  501.         If par=1 par=spritepivot
  502.         Local iw=ImageWidth(img) , ih=ImageHeight(img)
  503.         Local tw=2 Shl (Len(Int(Bin(iw-1)))-1)
  504.         Local th=2 Shl (Len(Int(Bin(ih-1)))-1)
  505.         Local tex=CreateTexture(tw,th,texflags)
  506.         Local ib=ImageBuffer(img) : LockBuffer ib
  507.         Local tb=TextureBuffer(tex) : LockBuffer tb
  508.         Local x,y
  509.         For x=0 To iw-1
  510.                 For y=0 To ih-1
  511.                         rgbc=ReadPixelFast(x,y,ib) And $00ffffff
  512.                         If rgbc=((r Shl 16)+(g Shl 8)+b)
  513.                                 WritePixelFast x,y,($00000000),tb
  514.                         Else
  515.                                 WritePixelFast x,y,(rgbc Or $ff000000),tb
  516.                         EndIf
  517.                 Next
  518.         Next
  519.         UnlockBuffer ib : UnlockBuffer tb
  520.         ScaleTexture tex,Float(tw)/Float(iw/numframesX),Float(th)/Float(ih/numframesY)
  521.         Local sprite=CreateImage3D(iw,ih,par)
  522.         EntityTexture sprite,tex
  523.         EntityFX sprite,16+1 : EntityOrder sprite,-100
  524.         ScaleEntity sprite,Float(iw/numframesX)/2,Float(ih/numframesY)/2,1
  525.         Return sprite
  526. End Function


Comments :


_PJ_(Posted 1+ years ago)

 ; Notes on Use:; Include this file into your program.; It is important to call the "InitialiseBitmapFonts()" function before using any of the 3D Text commandset.; Once this has been done, the full functionality can be explored.; Either LoadExisting bitmap fonts created with the "MAKEFONT.bb" code ( <a href="codearcsf885.html?code=2533#comments" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=2533#comments[/url] );       with the LoadBitmapFont() command, which will return a valid Handle for use with SetFont3D();      Alternatively To create and utilise fonts during runtime, use the LoadFont3D() command. Again, this will return;      a handle for use.; Just as with Blitz3D's standard 2D Font/Text commands, once a Font is loaded, it can be:;   Free'd               -      FreeFont3D( FontHandle );   Set as current Font      -      SetFont3D( FontHandle );   To display text in the currently set Font, use the Text3D() command.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal