I've made a mistake in parsing somewhere..

Blitz3D Forums/Blitz3D Beginners Area/I've made a mistake in parsing somewhere..

BlackD(Posted 2004) [#1]
Or something. :)

Writing an alternative text command so I can use colour codes in strings (ala L.O.R.D, Quake3, etc). Now - it works, but not the way I wanted it to.

Two colour codes in use atm - WH for white and GO for gold. Now, I want to do a sentence like this: "^GO500 ^WHgold pieces" however, I screwed something up, and I have to do them like this, for the obvious intended result: "500^GO gold pieces^WH"

I want the color codes before, not after. I've looked back over it a few times and can't figure where I'm going wrong. Maybe someone else with more experience will spot the problem instantly. BTW, the WH at the end of the sentence is not needed - as it adds a WHITE colour automatically to any sentence if its not defined.

Okay - code below:

	Graphics 800,600,32,2
	Global sfont=LoadFont("Times New Roman",18,0,0,0)
	Global bfont=LoadFont("Times New Roman",18,1,0,0)
	Global ifont=LoadFont("Times New Roman",18,0,1,0)
	Global ufont=LoadFont("Times New Roman",18,0,0,1)
	
	Type Word
		Field T$
		Field C
		End Type
		
	SetBuffer BackBuffer()
	ClsColor 100,100,100
	Cls
	ctext "52760 ^GOgold pieces^WH",135,105   ; here lies the problem
	Flip
	WaitKey()
	End
	
	Function Ctext(T$,x,y,bold=1,italic=0,underline=0)
	;as for the labels - b$ is the current character
	;					 a$ is the color code detected (if any)
		
	SetFont bfont
	If bold=0 Then SetFont sfont
	If italic=1 Then SetFont ifont
	If underline=1 Then SetFont ufont
	this.word=New word
	For i = 1 To Len(t$)
		b$=Mid$(t$,i,1)
		If b$ = "^" Then
			a$=Mid$(t$,i+1,2)
			a$=Lower(a$)
			curc=-1
			If a$="wh" Then curc=1
			If a$="go" Then curc=2
			If curc > -1 Then this\c=curc Else this\c=1
			this.word=New Word
			i=i+2
			this\c=1
			Else
			this\t$=this\t$+b$
			End If
		Next
	tx=x
	For this.word = Each word
		Color 0,0,0
		Text tx,y+2,this\t$
		Select this\c
			Case 1:Color 255,255,255
			Case 2:Color 200,200,100
			End Select
		Text tx,Y,this\t$
		tx=tx+StringWidth(this\t$)
		Delete this
		Next
	End Function

sorry its long.. only if someone's bored enough to look, i guess. :) Theres lots of error checking and stuff in there (which is why it looks so bloated). :)

+BlackD


BlackD(Posted 2004) [#2]
ack! fixed it.. sorry folks..

had to change this:
If b$ = "^" Then
			this.word=New Word
			a$=Mid$(t$,i+1,2)
			a$=Lower(a$)
			curc=-1
			If a$="wh" Then curc=1
			If a$="go" Then curc=2
			If curc > -1 Then this\c=curc Else this\c=1
			i=i+2
			Else
			this\t$=this\t$+b$
			End If


now it works: ctext "^go52760 ^whgold pieces",135,105
feel free to use this code and modify it to use full color codes\whatever. :)

+BlackD


Perturbatio(Posted 2004) [#3]
I ended up rewriting the function a little:


*EDIT*
Obviously the word type needs to be changed to work with this:
Type Word
	Field txt$
	Field Col$
End Type



BlackD(Posted 2004) [#4]
mmm very tidy :) I'm so far away from tidying the code in this project it's not funny. :) Thanks


Clyde(Posted 2004) [#5]
Welldone mate! :)
All the best.