ugly text at a 3d point hack

BlitzMax Modules Forums/MiniB3D/ugly text at a 3d point hack

I wanted to be able to have some text (always facing the camera) at a specific 3d point

I also didn't want to modify minib3d

I couldnt get parenting to work like I wanted so you'll have to reposition the text entity above the entity its labeling but hey its a quick dirty hack!

use like this
Local label:TTextEnt = TTextEnt.CreateTextEnt()
label.text="Entity One"
EntityColor label,255,0,0
PositionEntity label,10,4,0


and here's the ugliest hack you've seen in ages....
just slap this type in your code where you are not too likely to see it too often

Type TTextEnt Extends TSprite
	Field text:String
	Function CreateTextEnt:TTextEnt()

		Local tt:TTextEnt=New TTextEnt
		tt.class$="Text"
		
		tt.EntityListAdd(entity_list)

		tt.UpdateMat(True)
		
		tt.view_mode = 4
		tt.scale_x#=0.05
		tt.scale_y#=0.05
		
		Return tt
	EndFunction
	
	Global fontTex:Int	

	Method update()
		
		glcolor3f brush.red , brush.green, brush.blue
		glPushMatrix()
		glMultMatrixf(mat_sp.grid)
																
		' STOP THIEF!!!! ripped from brl and bits of minib3d
		' abuse and errors all my own.....
		' its basically brl's text drawing but not in ortho mode
		glPushAttrib GL_ENABLE_BIT|GL_TEXTURE_BIT|GL_TRANSFORM_BIT
		glenable(GL_BLEND)
		glShadeModel(GL_FLAT)
		glDisable GL_CULL_FACE
		glenable GL_ALPHA_TEST
		glDisable GL_LIGHTING	

		If Not fontTex
			Local pixmap:TPixmap=TPixmap.Create( 1024,16,PF_RGBA8888 )
			Local p:Byte Ptr=IncbinPtr( "gldrawtextfont.bin" )
			For Local y:Int=0 Until 16
				For Local x:Int=0 Until 96
					Local b:Byte=p[x]
					For Local n:Int=0 Until 8
						If b & (1 Shl n) 
							pixmap.WritePixel x*8+n,y,~0
						Else
							pixmap.WritePixel x*8+n,y,0
						EndIf
					Next
				Next
				p:+96
			Next
			GLGraphicsDriver().SwapSharedContext
			fontTex=GLTexFromPixmap( pixmap )
			GLGraphicsDriver().SwapSharedContext
		EndIf
		
		glenable GL_TEXTURE_2D
		glBindTexture GL_TEXTURE_2D,fontTex
		Local x:Int , y:Int
		x = - 4 * text.length
		y=-8
		For Local i:Int=0 Until text.length
			Local c:Byte=text[i]-32
			If c>=0 And c<96
				Const adv#=8/1024.0
				Local t#=c*adv;
				glBegin GL_QUADS
				glTexcoord2f t,1
				glVertex2f x,y
				glTexcoord2f t+adv,1
				glVertex2f x+8,y
				glTexcoord2f t+adv,0
				glVertex2f x+8,y+16
				glTexcoord2f t,0
				glVertex2f x,y+16
				glEnd
			EndIf
			x:+8
		Next
		glPopAttrib		
		glPopMatrix()
		glenable GL_LIGHTING	

	EndMethod
	
EndType


apologies to everyone who's code I've abused

hey. That's Pretty Cool. I got it working in my game. You are now officially in the opening credits

question: I have a 'label' on several entities. Only one label will display at a time. (i.e the one the camera is closest to) the other labels disappear. is there a way to have multiple sprites show at once?

I've since added background speech bubbles in my own code, not too sure what you mean by one label at a time, basically make one TTextEnt for *each* entity you wish to "label"

Feel free to mail me via the address in my profile if you have any further problems with it

Its only so "hacky" cause I wanted to keep it separate from the minib3d code.

You are now officially in the opening credits

Oh the fame...! (thanks ;) )

Thanks. I'll keep playing with it and post some code here if I get stuck.

I'll be sure to contact you before release so you don't get blindsided by the media attention.

did he say freebie :D lol

This text is great, the only problem I have with it is that you have to create it on startup. You cant create it on the fly when and where you need it otherwise you just get a white box :\.

Also sometimes the bottoms of the g and y are missed out.

Any chance of fixing that? :)

> Any chance of fixing that?

Unlikely I was going to look at the update for max but theres still bugs that are more than 8 months old and a cant bare the Linux ide (I no longer own windows)

Maybe once some of the bugs have have been sorted I may look at max again