Billboard Text

Share your advanced PureBasic knowledge/code with the community.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Billboard Text

Post by einander »

Based on Comtois : http://www.purebasic.fr/english/viewtop ... rd#p393372

Code: Select all

;Billboard Text
;PB 5.00 beta 5
;by einander
EnableExplicit
#Box=3
#Ellip=4
#Text=5  ; Text with transparent background
#BkRGB=$22
Define Ev
Global _MainNode,_CamSpeed.D=1, _Separation=40 
;
Define MyFont12=FontID(LoadFont(-1,"courier new",12,#PB_Font_HighQuality))

Define MyFont16=FontID(LoadFont(-1,"comic sans ms",16,#PB_Font_HighQuality))
;
Procedure  CenterText(X,Y,X1,Y1,Tx.S,FontID,TextRGB=0,BkRGB=$FFFFFF) 
  Protected   TextWidth=TextWidth(Tx),TextHeight=TextHeight(Tx)
  DrawingMode(#PB_2DDrawing_Transparent)
  If TextWidth>X1-X
    DrawText(X,(Y1+Y)/2-TextHeight/2 , Tx, TextRGB)
  Else
    DrawText((X1+X)/2-TextWidth/2, (Y1+Y)/2-TextHeight/2 , Tx, TextRGB)
  EndIf
EndProcedure  
;
Procedure KeyMap(Camera=0,MainNode=0)  ; Control  moving and rotating with Keyboard
  ;keys Left,Right,Up,Down,PageUp,PageDown,Insert,Delete,Home,End ,+,-, F1 to F8
  ; -------
  Macro KBP(Key1,Key2)
    (KeyboardPushed(#PB_Key_#Key1) Or KeyboardPushed(#Pb_Key_#Key2))
  EndMacro
  ; -------
  If KBP(ADD,F1)         
    MoveCamera(Camera, 0,0, -_CamSpeed*2)                 ;  move to front
  ElseIf KBP(Subtract,F2)         
    MoveCamera(Camera, 0, 0,_CamSpeed*2)                  ; move to rear
    ; ------------------------------
    ; Arrow keys; move camera to 4 absolute Directions
  ElseIf KeyboardPushed(#PB_Key_Left)             
    MoveCamera(Camera, _CamSpeed, 0, 0)                  ; Move to screen Left
  ElseIf KeyboardPushed(#PB_Key_Right)             
    MoveCamera(Camera, -_CamSpeed, 0, 0)                 ; Move to screen Right
  ElseIf KeyboardPushed(#PB_Key_Up)             
    MoveCamera(Camera, 0,-_CamSpeed, 0)                  ; Move to screen Top
  ElseIf KeyboardPushed(#PB_Key_Down)             
    MoveCamera(Camera, 0,_CamSpeed, 0)                   ; Move to screen Bottom
    ; -------------------
    ;PageUp - Insert : Home - End : Delete - PageDown ; 6 rotations relative to object position
  ElseIf KBP(F3,PageUp)
    RotateNode(MainNode,0,0,_CamSpeed*2,#PB_Relative)        ; Turn Left
  ElseIf KBP(F4,Insert)
    RotateNode(MainNode,0,0,-_CamSpeed*2,#PB_Relative)       ; Turn Right
    ; ---------------------
    ;Home / End     
  ElseIf KBP(F5,Home)
    RotateNode(MainNode,-_CamSpeed,0,0,#PB_Relative)       ; Turn back
  ElseIf KBP(F6,End)
    RotateNode(MainNode,_CamSpeed,0,0,#PB_Relative)        ; Turn front
    ; -------------
    ;Delete / PageDown
  ElseIf KBP(F7,Delete)
    RotateNode(MainNode,0, _CamSpeed,0,#PB_Relative)       ; Rotation Left
  ElseIf KBP(F8,PageDown)
    RotateNode(MainNode,0, -_CamSpeed,0,#PB_Relative)      ; Rotation Right
  EndIf
  NodeLookAt(MainNode,0,0,0)  
EndProcedure
;
Procedure Link(Node1,Node2,RGB1=$FFFFFF,RGB2=$FFFFFF)
  Static NR
  CreateLine3D(Nr,NodeX(Node1),NodeY(Node1),NodeZ(Node1),RGB1,NodeX(Node2),NodeY(Node2),NodeZ(Node2),RGB2)
  Nr+1
EndProcedure
;
Procedure  BBText(Index,Wi,He,Shape,T$,X,Y,Z,FontID,TextRGB,BkRGB,GradRGB=-1)
  CreateTexture(Index,Wi,He)
  StartDrawing(TextureOutput(Index))
  Box(0,0,Wi,He,0)
  DrawingFont(FontID)
  If GradRGB=-1:GradRGB=BkRGB:EndIf
  Protected Po=1,A$,Yy=2
  Select Shape
    Case #Text 
      CenterText(0,2,Wi,He,T$,FontID,TextRGB) 
    Case #Ellip
      DrawingMode(#PB_2DDrawing_Gradient)      
      BackColor(BkRGB)
      GradientColor(0.5, GradRGB)
      EllipticalGradient(Wi/2,He/2,Wi,He)
      Ellipse(Wi/2,He/2,WI/2,HE/2) 
      CenterText(0,0,WI,HE,T$,#TRANSPARENT,TextRGB) 
    Case #Box
      DrawingMode(#PB_2DDrawing_Gradient)      
      BackColor(BkRGB)
      GradientColor(0.8, GradRGB)
      LinearGradient(0, 0, Wi,He)    
      Box(0,0,Wi,He)
      DrawingMode(#PB_2DDrawing_Transparent)
      If FindString(T$,"|")
        PO=01
        Repeat
          A$=StringField(T$,Po,"|") ;separator for multiLines
          If A$="":Break:EndIf
          DrawText(0,Yy,A$,TextRGB)
          Po+1
          Yy+TextHeight("T")
        ForEver
      EndIf
  EndSelect
  StopDrawing()
  CreateMaterial(Index,TextureID(Index) )
  MaterialBlendingMode(Index, #PB_Material_Color) ; make transparent background; comment to test
  CreateBillboardGroup(Index, MaterialID(Index), Wi,He)
  AddBillboard(Index, Index,0,0,0)
  CreateNode(Index,X*_Separation,Y*_Separation,Z*_Separation)
  AttachNodeObject(Index,BillboardGroupID(Index))
EndProcedure
;
InitEngine3D()
InitKeyboard()
InitSprite()
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  
Define T$="Keys Left,Right,Up,Down,PageUp,PageDown,Insert,Delete,Home,End ,+,-, F1 to F8"
OpenWindow(0, 0, 0, 800,600,T$,#PB_Window_Maximize)
SetWindowColor(0,0)
Define Wi=WindowWidth(0)
Define He=WindowHeight(0)
AntialiasingMode(#PB_AntialiasingMode_x4) 
; ----------------------------  
OpenWindowedScreen(WindowID(0), 0, 30, Wi-130,He-30, 0, 0, 0,#PB_Screen_SmartSynchronization)
_MainNode=CreateNode(-1,0,0,0)
;
BBText(1,180,30,#Text,"Transparent Text 1",-1, -1, 3,Myfont16, $FF,0,0) 
BBText(2,180,40,#Ellip,"Ellipse 2",1, 3, 0,Myfont16, #Green,$FABADA,$FFFFFF)
BBText(3,180,66,#Box," Rectangle 3| MultiLine| yellow & green",-3, 0, 0,Myfont12, $FF0000,$FFFF,$FFFF00)
Link(1,2,$FF)
Link(2,3,$FF)
Link(3,1,$FF)
;
CreateLight(0, #White, 0, 1200, 0, #PB_Light_Spot)
SpotLightRange(0, 1, 10, 3)
AmbientColor($BBFF) 
;
CreateCamera(0, 0, 0, 100, 100)
CameraLocate(0, 0, 0, 600)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, #BkRGB)
AttachNodeObject(_MainNode,CameraID(0))
Define Draw=1
Repeat
  Repeat
    Ev=WindowEvent()
    If Ev=#PB_Event_CloseWindow:Break 2:EndIf
  Until Ev=0
  If ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Escape) : Break :  EndIf
    Keymap(0,_MainNode) 
    Draw=1
  EndIf
  If Draw
    RenderWorld()
    FlipBuffers()
    Draw=0  
  EndIf
ForEver
End
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Billboard Text

Post by netmaestro »

Looks very nice! Good work 8)
BERESHEIT
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Billboard Text

Post by jassing »

will the attachnodeobject() code be "backwards compatible"? ie: Code built today will work in 5 when it's released?
infratec
Always Here
Always Here
Posts: 7615
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Billboard Text

Post by infratec »

If you add the types for AttachNodeObject()
and comment out AntialiasingMode() :cry: :cry: :cry:
Else it crashes with an IMA at _MainNode=CreateNode(#PB_Any,0,0,0)
and the OpenWindowedScreen() fails.

It works also in PB 4.61 (WinXP 32)

Bernd

Oh!!!
If you put OpenWindowedScreen() before AntialiasingMode()
it works :!: :!: :!:

But I see no difference at the borders :cry:
Maybe because it is not in Fullscreen-Mode like written in the help of AntialiasingMode().
infratec
Always Here
Always Here
Posts: 7615
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Billboard Text

Post by infratec »

Hi,

adapted version:

Code: Select all

Macro _AttachNodeObject(A, B, C)
  CompilerIf #PB_Compiler_Version < 500
    AttachNodeObject(A, B, C)
  CompilerElse
    AttachNodeObject(A, B)
  CompilerEndIf
EndMacro


;Billboard Text
;PB 5.00 beta 5 and PB 4.61
;by einander
EnableExplicit
#Box=3
#Ellip=4
#Text=5  ; Text with transparent background
#BkRGB=$22
Define Ev.i
Global _MainNode.i ,_CamSpeed.D=1, _Separation.i=40
;
Define MyFont12=FontID(LoadFont(-1,"courier new",12,#PB_Font_HighQuality))

Define MyFont16=FontID(LoadFont(-1,"comic sans ms",16,#PB_Font_HighQuality))
;
Procedure  CenterText(X,Y,X1,Y1,Tx.S,FontID,TextRGB=0,BkRGB=$FFFFFF)
  Protected   TextWidth=TextWidth(Tx),TextHeight=TextHeight(Tx)
  DrawingMode(#PB_2DDrawing_Transparent)
  If TextWidth>X1-X
    DrawText(X,(Y1+Y)/2-TextHeight/2 , Tx, TextRGB)
  Else
    DrawText((X1+X)/2-TextWidth/2, (Y1+Y)/2-TextHeight/2 , Tx, TextRGB)
  EndIf
EndProcedure 
;
Procedure KeyMap(Camera=0,MainNode=0)  ; Control  moving and rotating with Keyboard
  ;keys Left,Right,Up,Down,PageUp,PageDown,Insert,Delete,Home,End ,+,-, F1 to F8
  ; -------
  Macro KBP(Key1,Key2)
    (KeyboardPushed(#PB_Key_#Key1) Or KeyboardPushed(#Pb_Key_#Key2))
  EndMacro
  ; -------
  If KBP(ADD,F1)         
    MoveCamera(Camera, 0,0, -_CamSpeed*2)                 ;  move to front
  ElseIf KBP(Subtract,F2)         
    MoveCamera(Camera, 0, 0,_CamSpeed*2)                  ; move to rear
    ; ------------------------------
    ; Arrow keys; move camera to 4 absolute Directions
  ElseIf KeyboardPushed(#PB_Key_Left)             
    MoveCamera(Camera, _CamSpeed, 0, 0)                  ; Move to screen Left
  ElseIf KeyboardPushed(#PB_Key_Right)             
    MoveCamera(Camera, -_CamSpeed, 0, 0)                 ; Move to screen Right
  ElseIf KeyboardPushed(#PB_Key_Up)             
    MoveCamera(Camera, 0,-_CamSpeed, 0)                  ; Move to screen Top
  ElseIf KeyboardPushed(#PB_Key_Down)             
    MoveCamera(Camera, 0,_CamSpeed, 0)                   ; Move to screen Bottom
    ; -------------------
    ;PageUp - Insert : Home - End : Delete - PageDown ; 6 rotations relative to object position
  ElseIf KBP(F3,PageUp)
    RotateNode(MainNode,0,0,_CamSpeed*2,#PB_Relative)        ; Turn Left
  ElseIf KBP(F4,Insert)
    RotateNode(MainNode,0,0,-_CamSpeed*2,#PB_Relative)       ; Turn Right
    ; ---------------------
    ;Home / End     
  ElseIf KBP(F5,Home)
    RotateNode(MainNode,-_CamSpeed,0,0,#PB_Relative)       ; Turn back
  ElseIf KBP(F6,End)
    RotateNode(MainNode,_CamSpeed,0,0,#PB_Relative)        ; Turn front
    ; -------------
    ;Delete / PageDown
  ElseIf KBP(F7,Delete)
    RotateNode(MainNode,0, _CamSpeed,0,#PB_Relative)       ; Rotation Left
  ElseIf KBP(F8,PageDown)
    RotateNode(MainNode,0, -_CamSpeed,0,#PB_Relative)      ; Rotation Right
  EndIf
  NodeLookAt(MainNode,0,0,0) 
EndProcedure
;
Procedure Link(Node1,Node2,RGB1=$FFFFFF,RGB2=$FFFFFF)
  Static NR
  CreateLine3D(Nr,NodeX(Node1),NodeY(Node1),NodeZ(Node1),RGB1,NodeX(Node2),NodeY(Node2),NodeZ(Node2),RGB2)
  Nr+1
EndProcedure
;
Procedure  BBText(Index,Wi,He,Shape,T$,X,Y,Z,FontID,TextRGB,BkRGB,GradRGB=-1)
  CreateTexture(Index,Wi,He)
  StartDrawing(TextureOutput(Index))
  Box(0,0,Wi,He,0)
  DrawingFont(FontID)
  If GradRGB=-1:GradRGB=BkRGB:EndIf
  Protected Po=1,A$,Yy=2
  Select Shape
    Case #Text
      CenterText(0,2,Wi,He,T$,FontID,TextRGB)
    Case #Ellip
      DrawingMode(#PB_2DDrawing_Gradient)     
      BackColor(BkRGB)
      GradientColor(0.5, GradRGB)
      EllipticalGradient(Wi/2,He/2,Wi,He)
      Ellipse(Wi/2,He/2,WI/2,HE/2)
      CenterText(0,0,WI,HE,T$,#TRANSPARENT,TextRGB)
    Case #Box
      DrawingMode(#PB_2DDrawing_Gradient)     
      BackColor(BkRGB)
      GradientColor(0.8, GradRGB)
      LinearGradient(0, 0, Wi,He)   
      Box(0,0,Wi,He)
      DrawingMode(#PB_2DDrawing_Transparent)
      If FindString(T$,"|")
        PO=01
        Repeat
          A$=StringField(T$,Po,"|") ;separator for multiLines
          If A$="":Break:EndIf
          DrawText(0,Yy,A$,TextRGB)
          Po+1
          Yy+TextHeight("T")
        ForEver
      EndIf
  EndSelect
  StopDrawing()
  CreateMaterial(Index,TextureID(Index) )
  MaterialBlendingMode(Index, #PB_Material_Color) ; make transparent background; comment to test
  CreateBillboardGroup(Index, MaterialID(Index), Wi,He)
  AddBillboard(Index, Index,0,0,0)
  CreateNode(Index,X*_Separation,Y*_Separation,Z*_Separation)
  _AttachNodeObject(Index,BillboardGroupID(Index), #PB_Node_BillboardGroup)
EndProcedure
;
InitEngine3D()
InitKeyboard()
InitSprite()
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
Define T$="Keys Left,Right,Up,Down,PageUp,PageDown,Insert,Delete,Home,End ,+,-, F1 to F8"
OpenWindow(0, 0, 0, 800,600,T$,#PB_Window_Maximize)
SetWindowColor(0,0)
Define Wi=WindowWidth(0)
Define He=WindowHeight(0)
OpenWindowedScreen(WindowID(0), 0, 30, Wi-130,He-30, 0, 0, 0,#PB_Screen_SmartSynchronization)
AntialiasingMode(#PB_AntialiasingMode_x4)
; ---------------------------- 
_MainNode=CreateNode(#PB_Any,0,0,0)
;
BBText(1,180,30,#Text,"Transparent Text 1",-1, -1, 3,Myfont16, $FF,0,0)
BBText(2,180,40,#Ellip,"Ellipse 2",1, 3, 0,Myfont16, #Green,$FABADA,$FFFFFF)
BBText(3,180,66,#Box," Rectangle 3| MultiLine| yellow & green",-3, 0, 0,Myfont12, $FF0000,$FFFF,$FFFF00)
Link(1,2,$FF)
Link(2,3,$FF)
Link(3,1,$FF)
;
CreateLight(0, #White, 0, 1200, 0, #PB_Light_Spot)
SpotLightRange(0, 1, 10, 3)
AmbientColor($BBFF)
;
CreateCamera(0, 0, 0, 100, 100)
CameraLocate(0, 0, 0, 600)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, #BkRGB)
_AttachNodeObject(_MainNode,CameraID(0), #PB_Node_Camera)
Define Draw=1
Repeat
  Repeat
    Ev=WindowEvent()
    If Ev=#PB_Event_CloseWindow:Break 2:EndIf
  Until Ev=0
  If ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Escape) : Break :  EndIf
    Keymap(0,_MainNode)
    Draw=1
  EndIf
  If Draw
    RenderWorld()
    FlipBuffers()
    Draw=0 
  EndIf
ForEver
End
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: Billboard Text

Post by einander »

@Netmaestro: You're welcome.

@ifratec: Thanks for the bakward compatibility!

@jassing: For 3D testing I'm using a small reusable WindowedScreen template, with room for some gadgets, (not needed here) like http://www.purebasic.fr/english/viewtop ... 1&start=15, so I must only take care of the new procedure(s), and the rest is already tested and (usually) working.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Billboard Text

Post by falsam »

@einander & infratec
Whaouuuu I'm impressed. Congratulations and thank you for sharing.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Billboard Text

Post by jassing »

einander wrote: @jassing: For 3D testing I'm using a small reusable WindowedScreen template, with room for some gadgets, (not needed here) like http://www.purebasic.fr/english/viewtop ... 1&start=15, so I must only take care of the new procedure(s), and the rest is already tested and (usually) working.
but it doesn't work in pb4; showing an incompatibility with a function -- so i'm curious if fred maintained compatibility with ver 4 or if it's an upgrade that breaks older sources...
Post Reply