modifying image on backbuffer

BlitzMax Forums/BlitzMax Programming/modifying image on backbuffer

Okay, I'm stuck. I'm trying to create a ASCII emulator; the reason is that OSX doesn't have support for ASCII and I want to convert some of my old ASCII Turbo Pascal games over to BM.

I'm taking a text file that has the pixel data and plotting it out on the pixmap. I then grab a 8x8 image and put it into an array, which I can use to print a character to the screen.

That works fine. But now I want to have code that can change the color of the text. NOT text that's already shown to the user, but go back and change the text color (foreground and background) for all future printed text. And that's where I'm stuck.

Any help?

P.S., one of the main reasons that I'm using a text file to store the data is I also plan on re-using this to create character sets that look like Atari and Commodore character sets for other conversion projects.

You'll need this text file for testing:

Click here!

' IBM ASCII Simulator

SetGraphicsDriver GLMax2DDriver()
Global GrabLine:String=""
Global Lop   :Int=0            ' Generic looping variable.  Re-useable.
Global Lop1  :Int=0            ' Generic looping variable.  Re-useable.
Global AscNum:Int=-1
Global AscRow:Int=0
Global AscCol         :Int=-1
Global xPos           :Int=-1
Global MoveToNextChar :Int=0   
Global Char_Array[256]         ' Stores graphics in an array that corresponds to ASCII codes
Global r1,g1,b1       :Int=1
Global r2,g2,b2       :Int=1
Global bgcolor        :Int=1   ' background color of text
Global fgcolor        :Int=1   ' foreground color of text

  Graphics 640,480,0,60
  Load_N_Plot(4,5) ' Syntax: Load_N_Plot(foreground color[range 0-16], background color[range 0-16]) 
  Snag
  Cls  
  put_xy(072,100,100)
  put_xy(105,108,100)
  put_xy(105,116,100)
  WaitKey

  put_xy(105,128,100)
  WaitKey
End


Function Load_N_Plot(fgcolor,bgcolor) 
  file=OpenFile("IBM_ASCII.txt")
  If Not file
    Print "Could not load"
  Else
    While Not Eof(file)
      xPos=-1
      AscNum=AscNum+1
      GrabLine = ReadLine(file)
      ConvertFromLineToGFX(GrabLine,fgcolor,bgcolor)
    Wend
    CloseStream file
  End If
End Function

' function put_xy   SYNTAX: put_xy(character, x, y)
'   - character : ascii code for the character you wish to plot
'   - x         : draw character at x-coord
'   - y         : draw character at y-coord
'
Function put_xy(cc:Int, xx:Int, yy:Int)
  DrawPixmap(Char_Array[cc], xx, yy)
  Flip
End Function

' function Snag - converts the plotted image into individual array elements.
Function Snag()
  Local xx=0
  Local yy=0
  For lop=0 To 255
    Select lop
      Case 64  ;xx=0 ;yy=10
      Case 128 ;xx=0 ;yy=20
      Case 192 ;xx=0 ;yy=30
    End Select
    Char_Array[lop]=GrabPixmap(xx, yy, 10, 10)
    xx=xx+10
  Next
End Function

Function WipeCharArray()
  For lop=0 To 255
    Char_Array[lop]=Null
  Next
End Function

Function ConvertFromLineToGFX(GrabLine$,fgcolor,bgcolor)
  Local IncLine=0
  AscCol=AscCol+1
  For Lop=0 To 63   'length of charset data (8x8=64)
    Local temp$=GrabLine[Lop..Lop+1]
    xPos=xPos+1
    Select fgcolor
      Case  0 ;r1=000 ;g1=000 ;b1=000 
      Case  1 ;r1=000 ;g1=000 ;b1=170 
      Case  2 ;r1=000 ;g1=170 ;b1=000 
      Case  3 ;r1=000 ;g1=170 ;b1=170 
      Case  4 ;r1=170 ;g1=000 ;b1=000 
      Case  5 ;r1=170 ;g1=000 ;b1=170 
      Case  6 ;r1=170 ;g1=085 ;b1=000 
      Case  7 ;r1=170 ;g1=170 ;b1=170 
      Case  8 ;r1=170 ;g1=170 ;b1=170 
      Case  9 ;r1=170 ;g1=170 ;b1=255 
      Case 10 ;r1=170 ;g1=255 ;b1=170 
      Case 11 ;r1=170 ;g1=255 ;b1=255 
      Case 12 ;r1=255 ;g1=170 ;b1=170 
      Case 13 ;r1=255 ;g1=170 ;b1=255 
      Case 14 ;r1=255 ;g1=255 ;b1=170 
      Case 15 ;r1=255 ;g1=255 ;b1=255 
    End Select
    Select bgcolor
      Case  0 ;r2=000 ;g2=000 ;b2=000 
      Case  1 ;r2=000 ;g2=000 ;b2=170 
      Case  2 ;r2=000 ;g2=170 ;b2=000 
      Case  3 ;r2=000 ;g2=170 ;b2=170 
      Case  4 ;r2=170 ;g2=000 ;b2=000 
      Case  5 ;r2=170 ;g2=000 ;b2=170 
      Case  6 ;r2=170 ;g2=085 ;b2=000 
      Case  7 ;r2=170 ;g2=170 ;b2=170 
      Case  8 ;r2=170 ;g2=170 ;b2=170 
      Case  9 ;r2=170 ;g2=170 ;b2=255 
      Case 10 ;r2=170 ;g2=255 ;b2=170 
      Case 11 ;r2=170 ;g2=255 ;b2=255 
      Case 12 ;r2=255 ;g2=170 ;b2=170 
      Case 13 ;r2=255 ;g2=170 ;b2=255 
      Case 14 ;r2=255 ;g2=255 ;b2=170 
      Case 15 ;r2=255 ;g2=255 ;b2=255 
    End Select
    If temp$="." Then SetColor r2,b2,g2
    If temp$="X" Then SetColor r1,b1,g1
    Select Lop     ' This part plots each line from data into each image character
      Case 8  ;IncLine=1 ;xPos=0
      Case 16 ;IncLine=2 ;xPos=0
      Case 24 ;IncLine=3 ;xPos=0
      Case 32 ;IncLine=4 ;xPos=0
      Case 40 ;IncLine=5 ;xPos=0
      Case 48 ;IncLine=6 ;xPos=0
      Case 56 ;IncLine=7 ;xPos=0
      Case 64 ;IncLine=8 ;xPos=0
    End Select
    Select AscNum    'Positioning on Window
      Case 64  ;AscRow=10 ;AscCol=0
      Case 128 ;AscRow=20 ;AscCol=0
      Case 192 ;AscRow=30 ;AscCol=0
    End Select  
    Plot xPos+(AscCol)*10,IncLine+AscRow
  Next
End Function


I should also mention that once this code is working I'll be posting it on here for everyone to use in their projects, I'm certain that I'm not the only one who's needed something like this. :)

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


Wow, that works quite well... now I just have to figure out how to implement it (already have it plotting to white and pink (aka transparent). I'm guessing I'll have to retrofit my code with more object-oriented stuff (which I've been trying to avoid because I haven't really made the mental leap yet).

Thanks!

"I'll have to retrofit my code with more object-oriented stuff (which I've been trying to avoid because I haven't really made the mental leap yet)."

That's what's nice about Bmax, the cool OO stuff is there when/if you need it. ;)