Page 1 of 1

Glow draw text

Posted: Mon Jul 18, 2011 9:54 am
by xorc1zt
Drawtext with glow border

Code: Select all

Procedure DrawGlowText(x.i, y.i, text$,glowcolor,textcolor=16777215) 
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(x-2, y  , text$,glowcolor)
  DrawText(x+2, y  , text$,glowcolor)
  DrawText(x  , y+2, text$,glowcolor)
  DrawText(x  , y-2, text$,glowcolor)
  DrawText(x-1, y-1, text$,glowcolor)
  DrawText(x+1, y+1, text$,glowcolor)
  DrawText(x+1, y-1, text$,glowcolor)
  DrawText(x-1, y+1, text$,glowcolor)
  DrawText(x  ,   y, text$,textcolor)
EndProcedure

Procedure DrawGlowMouseText(x.i, y.i, text$, glowcolor, textcolor, mousexpos, mouseypos) 
  DrawingMode(#PB_2DDrawing_Transparent) 
  If mousexpos>x And mousexpos<x+TextWidth(text$) And mouseypos>y And mouseypos<y+TextHeight(text$)
    DrawText(x-2, y  , text$,glowcolor)
    DrawText(x+2, y  , text$,glowcolor)
    DrawText(x  , y+2, text$,glowcolor)
    DrawText(x  , y-2, text$,glowcolor)
    DrawText(x-1, y-1, text$,glowcolor)
    DrawText(x+1, y+1, text$,glowcolor)
    DrawText(x+1, y-1, text$,glowcolor)
    DrawText(x-1, y+1, text$,glowcolor)
  EndIf
  DrawText(x  ,   y, text$,textcolor)
EndProcedure
example:

Code: Select all

;draw glow text example

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Modules can't be initialized", 0)
  End
EndIf

Procedure DrawGlowText(x.i, y.i, text$,glowcolor,textcolor=16777215) 
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawText(x-2, y  , text$,glowcolor)
  DrawText(x+2, y  , text$,glowcolor)
  DrawText(x  , y+2, text$,glowcolor)
  DrawText(x  , y-2, text$,glowcolor)
  DrawText(x-1, y-1, text$,glowcolor)
  DrawText(x+1, y+1, text$,glowcolor)
  DrawText(x+1, y-1, text$,glowcolor)
  DrawText(x-1, y+1, text$,glowcolor)
  DrawText(x  ,   y, text$,textcolor)
EndProcedure

Procedure DrawGlowMouseText(x.i, y.i, text$, glowcolor, textcolor, mousexpos, mouseypos) 
  DrawingMode(#PB_2DDrawing_Transparent) 
  If mousexpos>x And mousexpos<x+TextWidth(text$) And mouseypos>y And mouseypos<y+TextHeight(text$)
    DrawText(x-2, y  , text$,glowcolor)
    DrawText(x+2, y  , text$,glowcolor)
    DrawText(x  , y+2, text$,glowcolor)
    DrawText(x  , y-2, text$,glowcolor)
    DrawText(x-1, y-1, text$,glowcolor)
    DrawText(x+1, y+1, text$,glowcolor)
    DrawText(x+1, y-1, text$,glowcolor)
    DrawText(x-1, y+1, text$,glowcolor)
  EndIf
  DrawText(x  ,   y, text$,textcolor)
EndProcedure


If OpenWindow(0, 0, 0, 650, 490, "Game Menu", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)

  If OpenWindowedScreen(WindowID(0), 5, 5, 640, 480, 0, 0, 0) = 0
    MessageRequester("Error", "Can't open windowed screen!", 0)
    End
  EndIf
  
  ; == Main loop ==
  Repeat
         
    ; window event loop
    Repeat 
      Event = WindowEvent()
   
      Select Event
        Case #PB_Event_CloseWindow
          End
      EndSelect   
    Until Event = 0
   

    ExamineKeyboard()
    ExamineMouse()
      
    mouseX = MouseX() 
    mouseY = MouseY() 
    ClearScreen(RGB(55,55,55))
    StartDrawing(ScreenOutput())
    DrawGlowMouseText(200,200,"Draw Glow Mouse Text 1",RGB(255,0,0),RGB(255,255,255),mouseX,mouseY)
    DrawGlowMouseText(200,225,"Draw Glow Mouse Text 2",RGB(255, 255, 0),RGB(0,0,0),mouseX,mouseY)
    DrawGlowText(50, 100, "X: "+Str(mouseX)+" Y: "+Str(mousey),RGB(0,0,255))
    DrawGlowText(50, 125, "Hello World !",RGB(0,0,0))
    DrawGlowText(50, 150, "Hello World !",RGB(0,0,255),RGB(255,0,0))
    DrawText(50, 175, "Hello World !", RGB(255,255,255))
    Box(mouseX, mouseY, 2, 2) ; epic mouse pointer :)
    StopDrawing()
   
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape)
  
EndIf
End

Re: Glow draw text

Posted: Mon Jul 18, 2011 10:18 am
by c4s
I would call it outline or border. :wink:

Too bad your little "hack" doesn't work when alpha blending is involved. However, take a look at this code (it works with alpha and has some other neat features): http://www.purebasic.fr/english/viewtop ... 12&t=45192

The code I've linked above still has a rather big drawback as it doesn't create the outline correctly when font smoothing ("ClearType") is enabled in the system settings. My not so nice workaround for this is to force the font I'm using to be nonanialiased (Quality=#NONANTIALIASED_QUALITY):

Code: Select all

Procedure FontExLoad(Name.s, Size, Style=0, Quality=#DEFAULT_QUALITY)
	Protected lplf.LOGFONT

	With lplf
		\lfHeight = -Size
		\lfWidth = 0
		\lfEscapement = 0
		\lfOrientation = 0
		If Style & #PB_Font_Bold : \lfWeight = #FW_BOLD : Else : \lfWeight = #FW_NORMAL : EndIf ;;#FW_DONTCARE
		If Style & #PB_Font_Italic : \lfItalic = #True : Else : \lfItalic = #False : EndIf 
		If Style & #PB_Font_Underline : \lfUnderline = #True : Else : \lfUnderline = #False : EndIf 
		If Style & #PB_Font_StrikeOut : \lfStrikeOut = #True : Else : \lfStrikeOut = #False : EndIf 
		\lfCharSet = #DEFAULT_CHARSET
		\lfOutPrecision = #OUT_DEFAULT_PRECIS
		\lfClipPrecision = #CLIP_DEFAULT_PRECIS
		\lfQuality = Quality
		\lfPitchAndFamily = #DEFAULT_PITCH | #FF_DONTCARE
		PokeS(@\lfFaceName[0], Name)
	EndWith

	ProcedureReturn CreateFontIndirect_(@lplf)
EndProcedure
; Note: The returned value is a valid FontID (same as FontID(#FontNr))

Macro FontExFree(hLogFont)
	DeleteObject_(hLogFont)
EndMacro

Re: Glow draw text

Posted: Mon Jul 18, 2011 12:04 pm
by xorc1zt
this one work with alpha blend

Code: Select all

;draw glow text example

If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
  MessageRequester("Error", "Modules can't be initialized", 0)
  End
EndIf

Procedure DrawGlowText(x.i, y.i, text$, glowcolor, textcolor=-1) 
  DrawText(x-2, y  , text$,glowcolor,0)
  DrawText(x+2, y  , text$,glowcolor,0)
  DrawText(x  , y+2, text$,glowcolor,0)
  DrawText(x  , y-2, text$,glowcolor,0)
  DrawText(x-1, y-1, text$,glowcolor,0)
  DrawText(x+1, y+1, text$,glowcolor,0)
  DrawText(x+1, y-1, text$,glowcolor,0)
  DrawText(x-1, y+1, text$,glowcolor,0)
  DrawText(x  ,   y, text$,textcolor,0)
EndProcedure

If OpenWindow(0, 0, 0, 650, 490, "Game Menu", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)

  If OpenWindowedScreen(WindowID(0), 5, 5, 640, 480, 0, 0, 0) = 0
    MessageRequester("Error", "Can't open windowed screen!", 0)
    End
  EndIf
  LoadFont (0, "Arial", 15)

  minus=0
  ; == Main loop ==
  Repeat
    
    If a>255
      minus=1
    ElseIf a<2
      minus=0
    EndIf 
      
    If minus
      a-2
    Else
      a+2
    EndIf
    
    ; window event loop
    Repeat
      Event = WindowEvent()
   
      Select Event
        Case #PB_Event_CloseWindow
          End
      EndSelect   
    Until Event = 0
   

    ExamineKeyboard()
    ExamineMouse()
     
    mouseX = MouseX()
    mouseY = MouseY()
    
    CreateImage(0, 640, 480, 32)
    StartDrawing(ImageOutput(0))
    DrawingMode(#PB_2DDrawing_AlphaChannel)
    Box(0, 0, 800, 600, $00000000)
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    DrawGlowText(10,10,"Hello World",RGBA(255,0,0,255))
    DrawGlowText(10,35,"Blink Hello World",RGBA(255,255,0,a),RGBA(0,0,0,255))
    DrawGlowText(10,60,"Hello World",RGBA(255,0,255,255),RGBA(255,255,255,255))
    DrawGlowText(10,85,"Hello World",RGBA(255,255,255,255),RGBA(0,0,255,255))
    DrawGlowText(10,110,"Hello World",RGBA(0,255,0,255),RGBA(255,255,0,255))
    DrawGlowText(10, 135, "X: "+Str(mouseX)+" Y: "+Str(mousey),RGBA(255,0,0,255),RGBA(255,255,255,255)) 
    DrawingFont(FontID(0)) 
    DrawGlowText(10,160,"Blink Hello World 2",RGBA(255,0,0,a),RGBA(0,0,0,255))
    DrawGlowText(10,185,"Blink Hello World 3",RGBA(255,125,0,a),RGBA(0,0,0,255))
    StopDrawing()
    
    ; Solid image for display
  ;
  If CreateImage(1, 640, 480, 24) And StartDrawing(ImageOutput(1))
 
    ; create the background effect
    #box_size = 7
   
    Box(0, 0, 640, 480, $FFFFFF)
    For y = 0 To 480 Step #box_size*2
      For x = 0 To 640 Step #box_size*2
        Box(x, y, #box_size, #box_size, $C0C0C0)
        Box(x+#box_size, y+#box_size, #box_size, #box_size, $C0C0C0)
      Next x
    Next y   

    ; draw the alpha image
    DrawAlphaImage(ImageID(0), 0, 0)


    StopDrawing()
  EndIf

    StartDrawing(ScreenOutput())
    DrawingMode(#PB_2DDrawing_Default)
    DrawImage(ImageID(1), 0, 0)
    Box(mouseX, mouseY, 4, 4,RGB(0,0,0)) ;
    StopDrawing()
   
    FlipBuffers()
  Until KeyboardPushed(#PB_Key_Escape)
 
EndIf
End