modifying image on backbuffer

BlitzMax Forums/BlitzMax Programming/modifying image on backbuffer

when you make custom font, use only White(255,255,255) and Transparent color(ex. 255,0,255). and convert pixmap to Timage..

then you can use ANY foreground color(setcolor r,g,b).. and when you want to use different background color just drawrect before chraracter.

here is example:

font image:


and code:
Strict

Type t_bitmapfont
	Field img:TImage
	Field charwidth[] = [3 , 5 , 7 , 7 , 11 , 8 , 3 , 5 , 5 , 5 , 7 , 3 , 5 , 3 , 5 , 7 , 5 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 3 , 3 , 7 , 7 , 7 , 7 , 12 , 9 , 8 , 8 , 8 , 7 , 7 , 9 , 8 , 3 , 6 , 8 , 7 , 9 , 8 , 9 , 7 , 9 , 8 , 8 , 7 , 8 , 9 , 13 , 8 , 9 , 8 , 4 , 5 , 4 , 7 , 8 , 4 , 7 , 7 , 7 , 7 , 7 , 5 , 7 , 7 , 3 , 3 , 6 , 3 , 9 , 7 , 7 , 7 , 7 , 5 , 7 , 5 , 7 , 7 , 11 , 7 , 7 , 7 , 5 , 3 , 5 , 7]
	Field xpos:Int
	Field ypos:Int
	Field fgcolor:Int[3]
	Field bgcolor:Int[3]
	
	Function Create:t_bitmapfont() 
		Local bmf:t_bitmapfont = New t_bitmapfont
		bmf.img = LoadAnimImage("font.png" , 16 , 16 , 0 , 94)
		bmf.fgcolor[0] = 255 ; bmf.fgcolor[1] = 255 ; bmf.fgcolor[2] = 255
		bmf.bgcolor[0] = 0 ; bmf.bgcolor[1] = 0 ; bmf.bgcolor[2] = 0
		Return bmf
	End Function
	
	Method Text(txt:String , x , y)
		Local pos , char , _r , _g , _b
		GetColor(_r , _g , _b) 
		xpos = x
		ypos = y
		While pos < txt.Length
			char = txt[pos] - 33
			SetColor bgcolor[0],bgcolor[1],bgcolor[2]
			If char < 0 Or char > 93 'Space or invalid character
				DrawRect xpos , ypos , 4 , 12
				xpos = xpos + 4
			Else
				DrawRect xpos , ypos , charwidth[char] , 12
				SetColor fgcolor[0] , fgcolor[1] , fgcolor[2]
				DrawImage img , xpos , ypos , char
				xpos:+ charwidth[char]
			EndIf
			pos:+ 1
		Wend
		SetColor _r , _g , _b
	End Method
	
	Method Write(txt:String) 
		text txt , xpos , ypos
	End Method
	
	Method Print(txt:String)
		Local _xpos = xpos
		text txt , xpos , ypos
		ypos:+ 12
		xpos = _xpos
	End Method
	
	Method Locate(x,y)
		xpos=x
		ypos=y
	End Method
	
	Method TextWidth(txt:String) 'get text width in pixels
		Local width = 0 , char , pos
		While pos < txt.length
			char = txt[pos] - 33
			If char < 0 Or char > 93
				width:+ 4
			Else
				width:+ charwidth[char]
			End If
			pos:+ 1
		Wend
		Return width
	End Method
	
	Method SetBGcolor(r , g , b) 
		bgcolor[0] = r ; bgcolor[1] = g ; bgcolor[2] = b
	End Method
	
	Method SetFGcolor(r , g , b) 
		fgcolor[0] = r ; fgcolor[1] = g ; fgcolor[2] = b
	End Method
End Type

Graphics 800 , 600
SetMaskColor 255 , 0 , 255
Global bm:t_bitmapfont = t_bitmapfont.Create()

While Not (KeyHit(key_escape) Or AppTerminate() ) 
	bm.setfgcolor(255 , 255 , 255)
	bm.setbgcolor(0 , 0 , 0) 
	bm.text "Testing..." , 10 , 0
	
	bm.setfgcolor(250 , 0 , 0) 
	bm.text "Red text..." , 10 , 20
	
	bm.setfgcolor(255 , 255 , 255)
	bm.setbgcolor(0 , 250 , 0) 
	bm.text "Green background and white text..." , 10 , 40
	
	
	Flip
	Cls
	Delay 10
Wend