Page 1 of 1

Drawing text on a 3D screen?

Posted: Thu Apr 14, 2016 3:43 pm
by mrw
Hi,

I was wondering what the best practises are when drawing simple text on a 3D screen?
Up until now I was simply using DrawText() to print out some debugging information.
Something like this:

Code: Select all

Repeat
  Repeat
    Event = WindowEvent()
  Until Event = 0 Or #PB_Event_CloseWindow
  
  If Event = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
    End
  EndIf

  ;-- Do 3D stuff
  ;-- here..
  RenderWorld()

  ;--2D stuff--
  StartDrawing()
  DrawText(10,100, "Hello")
  DrawText(10.115, "Hello again")
  StopDrawing()
  ;---
  
  FlipBuffers()
ForEver
This works as I want, until the framerate starts to decrease below VBL. Then the text simply disappears.
So I´m assuming that DrawText() isn´t reliable to use for this? Or is there a workaround?
I looked at CreateText3D() but since that requires a node or entity its just not what I want. I would like to just rely on 2D coordinates for this, if possible.

Re: Drawing text on a 3D screen?

Posted: Thu Apr 14, 2016 8:13 pm
by Samuel
Hello,

I've read that it's better to draw to a sprite and then display that sprite on the screen instead of directly drawing to the screen.
Here's a basic example showing the current frames per second using a sprite.

Code: Select all

Enumeration
  #Window
  #Camera
  #Mesh
  #Entity
  #Light
  #Font
  #Sprite
EndEnumeration

If InitEngine3D() = 0
  End
EndIf
If InitSprite() = 0
  End
EndIf
If InitKeyboard() = 0
  End
EndIf
 
ExamineDesktops()
Define.i DesktopW = 800
Define.i DesktopH = 600

Define.i Event
Define.i Quit
Define.i FPS

If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "Frames Per Second", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)
    
    CreateSphere(#Mesh, 2, 64, 32)
    CreateEntity(#Entity, MeshID(#Mesh), #PB_Material_None)
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    MoveCamera(#Camera, 0, 0, 10, #PB_Absolute)
    CameraLookAt(#Camera,0,0,0)
    CameraBackColor(#Camera,RGB(20,20,20))
    
    CreateLight(#Light, RGB(255,255,255), 10, 10, 10)
    
    LoadFont(#Font, "Times New Roman", 20, #PB_Font_Bold)
    
    CreateSprite(#Sprite, 128, 64)
    StartDrawing(SpriteOutput(#Sprite))
      DrawingMode(#PB_2DDrawing_AlphaChannel)
      Box(0, 0, 50, 50)
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawingFont(FontID(#Font))
      DrawText(0, 0, "FPS : 60", RGB(255,255,255))
    StopDrawing()
    
    Repeat
      Event = WindowEvent()
      
      If ExamineKeyboard()
        If KeyboardReleased(#PB_Key_Escape)
          Quit = 1
        EndIf
      EndIf

      RenderWorld()
      
      StartDrawing(SpriteOutput(#Sprite))
        Box(0, 0, 128, 64, RGB(0,0,0))
        DrawingMode(#PB_2DDrawing_AlphaChannel)
        Box(0, 0, 128, 64)
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(FontID(#Font))
        FPS = Engine3DStatus(#PB_Engine3D_CurrentFPS)
        DrawText(0, 0, "FPS : "+Str(FPS), RGB(255,255,255))
      StopDrawing()
      DisplayTransparentSprite(#Sprite, 0, 0)
      
      FlipBuffers()

    Until Quit = 1 Or Event = #PB_Event_CloseWindow
  EndIf
EndIf
End

Re: Drawing text on a 3D screen?

Posted: Fri Apr 15, 2016 3:19 pm
by mrw
Thank you Samuel!
That solved it for me perfectly!

Re: Drawing text on a 3D screen?

Posted: Fri Apr 15, 2016 9:53 pm
by Roger Hågensen
If you have multiple monitors you might also want to consider a alternative way to show debug info.
Download DebugView from https://technet.microsoft.com/en-us/sys ... gview.aspx

Then you can use the Win32 API function OutputDebugString to print/show debug strings.

And example of use (with source) is found at http://www.purebasic.fr/english/viewtop ... ebugString



PS! Be aware that other software might also use OutputDebugString, and debug info via Kernel-mode DbgPrint may also show up. WHich is why DebugView let you select filters for the debug output.

DebugView is a nice way to have testers help you debug without having to include the PB debugger with your beta etc.