PDA

View Full Version : How to use TBGL_PrintFont?



Michael Hartlef
18-03-2007, 16:51
Hi Petr,

how does someone use TBGL_PrintFont?

I wanted to add the FPS to your Boxmaster sample, but no text is showing:


'
' Very simple third person character control
'

Uses "TBGL"
Uses "LL"
Uses "MATH"
Uses "FILE"
uses "UI"

Dim hWnd As Dword

MsgBox (0, "Use arrows to move"+$CRLF+"Hold [SHIFT] for run, [CTRL] to interact", %MB_OK or %MB_ICONinformation, "Info")

hWnd = TBGL_CreateWindow("Character animation")
TBGL_ShowWindow


tbgl_m15InitModelBuffers 2, 3000 ' Our game character has just about 3000 vertices
tbgl_M15LoadModel "Models\gameCharacter2.m15", "Textures", 1, 0, %TBGL_NORMAL_SMOOTH
tbgl_M15LoadModel "Models\le_floor1(2).m15", "Textures", 2, 2, %TBGL_NORMAL_PRECISE
tbgl_m15ClearModel 2
tbgl_M15LoadModel "Models\le_floor2(2).m15", "Textures", 2, 3, %TBGL_NORMAL_PRECISE
tbgl_m15ClearModel 2

tbgl_UseLighting 1
tbgl_UseLightSource %GL_LIGHT0, 1

' Bones
%BONE_BODYUP = 1
%BONE_BODYDOWN = 2

%BONE_LEFTARMUP = 3
%BONE_LEFTARMDOWN = 4
%BONE_LEFTHAND = 5

%BONE_RIGHTARMUP = 6
%BONE_RIGHTARMDOWN = 7
%BONE_RIGHTHAND = 8

%BONE_LEFTLEGUP = 9
%BONE_LEFTLEGDOWN = 10
%BONE_LEFTFOOT = 11

%BONE_RIGHTLEGUP = 12
%BONE_RIGHTLEGDOWN = 13
%BONE_RIGHTFOOT = 14

%BONE_HEAD = 15

' Animations
%ANIM_WALK = 1
%ANIM_RUN = 2
%ANIM_STAND = 3

defineBones()

dim GlobalTime as double

type tCharacter
x as double
z as double

angleLR as double

deltamove as double
deltaangleLR as double

BodyAngle as double
Interaction as double
InteractionFlag as byte

end type

dim Hero as tCharacter
dim FrameRate as double
dim MoveType as long

tbgl_GetAsyncKeyState(-1)
LoadLevel(APP_SOURCEPATH+"Levels\Level1.txt")

DIM hFont AS DWORD = Font_Create("Courier New", 9)
TBGL_BuildFont hFont

While TBGL_IsWindow(hWnd)

FrameRate = tbgl_getFrameRate


tbgl_ClearFrame
TBGL_PrintFont "Fps:"+Str$(framerate), 10, 10, 10

GlobalTime = GetTickCount/100

tbgl_Camera Hero.X+1, 2, Hero.z+5, Hero.X, 1, Hero.z

tbgl_m15ResetBones 1
if Hero.BodyAngle > 0 and MoveType <> %ANIM_RUN then Hero.BodyAngle -= Hero.BodyAngle/FrameRate
if Hero.DeltaMove = 0 then
MoveType = 0
animateModel(%ANIM_STAND)

if Hero.BodyAngle < 0 then Hero.BodyAngle = 0
end if
if Hero.DeltaMove < 0 then
Hero.InteractionFlag = 0
if MoveType = %ANIM_RUN then
if Hero.BodyAngle < 20 then Hero.BodyAngle += 20/FrameRate
end if

animateModel(MoveType)
end if

tbgl_m15ApplyBones 1

tbgl_CallList 1

tbgl_PushMatrix

tbgl_translate hero.X, 0, hero.Z
tbgl_Rotate -radtodeg(Hero.angleLR),0,1,0

tbgl_m15DrawModel 1

tbgl_PopMatrix

tbgl_SetupLightsource %GL_LIGHT0, Hero.X+1, 1, Hero.Z+3, 46,32,32,128


tbgl_DrawFrame

' Keys
Hero.DeltaMove = 0
Hero.DeltaAngleLR = 0
Hero.Interaction -= 25/FrameRate
if Hero.Interaction < 0 then Hero.Interaction = 0

If TBGL_GetWindowKeyState( hWnd, %VK_ESCAPE) Then Exit While

If TBGL_GetWindowKeyState( hWnd, %VK_UP) Then
if TBGL_GetWindowKeyState( hWnd, %VK_SHIFT) then
MoveType = 2
Hero.DeltaMove -= 4/FrameRate
else
MoveType = 1
Hero.DeltaMove -= 0.6/FrameRate
end if
end if


If TBGL_GetWindowKeyState( hWnd, %VK_RIGHT) Then Hero.DeltaAngleLR += 5/FrameRate
If TBGL_GetWindowKeyState( hWnd, %VK_LEFT) Then Hero.DeltaAngleLR -= 5/FrameRate

If TBGL_GetWindowKeyState( hWnd, %VK_Control) and Hero.InteractionFlag = 0 and Hero.DeltaMove = 0 Then Hero.InteractionFlag = 1

if Hero.InteractionFlag then
IF Hero.InteractionFlag = 1 then
Hero.BodyAngle += 50/FrameRate
Hero.Interaction += 160/FrameRate
if Hero.Interaction > 35 then Hero.InteractionFlag = -1 ' Going back
else ' -1
Hero.Interaction -= 50/FrameRate
Hero.BodyAngle -= 50/FrameRate
if Hero.Interaction < 0 then
Hero.Interaction = 0
Hero.InteractionFlag = 0
end if
end if
end if



end if

' Some math to calculate the next position
Hero.AngleLR += Hero.deltaAngleLR

Hero.X += Hero.DeltaMove * SIN(Hero.AngleLR)
Hero.Z += Hero.DeltaMove * -COS(Hero.AngleLR)

if abs(Hero.Z) > 0.6 then Hero.Z = 0.6 * sgn(Hero.Z)


Wend
TBGL_KillFont
TBGL_DestroyWindow

sub defineBones()

' Body
tbgl_m15DefBoneBox 1, %BONE_BODYUP, _
-0.22, 0.22, _
1, 1.55,_
-1, 1,_
0, 1, 0

tbgl_m15DefBoneBox 1, %BONE_BODYDOWN, _
-0.25, 0.25, _
0, 1,_
-1, 1,_
0, 1, 0

' Left arm
tbgl_m15DefBoneBox 1, %BONE_LEFTARMUP, _
0.22001, 0.4, _
1.22, 2,_
-1, 1,_
0.24, 1.49, -0.04

tbgl_m15DefBoneBox 1, %BONE_LEFTARMDOWN, _
0.24, 0.4, _
0.95, 1.22,_
-1, 1,_
0.32, 1.22, -0.04

tbgl_m15DefBoneBox 1, %BONE_LEFTHAND, _
0.24, 0.4, _
0.75, 0.95,_
-1, 1,_
0.32, 0.96, -0.04

' Right arm
tbgl_m15DefBoneBox 1, %BONE_RIGHTARMUP, _
-0.4, -0.22001, _
1.22, 2,_
-1, 1,_
-0.24, 1.49, -0.04

tbgl_m15DefBoneBox 1, %BONE_RIGHTARMDOWN, _
-0.4, -0.24, _
0.95, 1.22,_
-1, 1,_
-0.32, 1.22, -0.04

tbgl_m15DefBoneBox 1, %BONE_RIGHTHAND, _
-0.4, -0.24, _
0.75, 0.95,_
-1, 1,_
-0.32, 0.96, -0.04

' Left leg
tbgl_m15DefBoneBox 1, %BONE_LEFTLEGUP, _
0, 0.24, _
0.52, 0.9,_
-1, 1,_
0.13, 0.88, -0.03

tbgl_m15DefBoneBox 1, %BONE_LEFTLEGDOWN, _
0, 0.24, _
0.08, 0.52,_
-1, 1,_
0.13, 0.52, -0.03

tbgl_m15DefBoneBox 1, %BONE_LEFTFOOT, _
0, 0.24, _
0, 0.08,_
-1, 1,_
0.13, 0.08, -0.03

' Right leg
tbgl_m15DefBoneBox 1, %BONE_RIGHTLEGUP, _
-0.24, 0, _
0.52, 0.9,_
-1, 1,_
0.13, 0.88, -0.03

tbgl_m15DefBoneBox 1, %BONE_RIGHTLEGDOWN, _
-0.24, 0, _
0.08, 0.52,_
-1, 1,_
0.13, 0.52, -0.03

tbgl_m15DefBoneBox 1, %BONE_RIGHTFOOT, _
-0.24, 0, _
0, 0.08,_
-1, 1,_
0.13, 0.08, -0.03

tbgl_m15DefBoneBox 1, %BONE_HEAD, _
-0.24, 0.24, _
1.55, 2,_
-1, 1,_
0, 1.55, -0.03


tbgl_m15AddBoneTreeItem 1, %BONE_BODYUP , 1, 2
tbgl_m15AddBoneTreeItem 1, %BONE_HEAD , 2, 0

tbgl_m15AddBoneTreeItem 1, %BONE_LEFTARMUP , 2, 3
tbgl_m15AddBoneTreeItem 1, %BONE_LEFTARMDOWN , 3, 4
tbgl_m15AddBoneTreeItem 1, %BONE_LEFTHAND , 4, 0

tbgl_m15AddBoneTreeItem 1, %BONE_RIGHTARMUP , 2, 5
tbgl_m15AddBoneTreeItem 1, %BONE_RIGHTARMDOWN , 5, 6
tbgl_m15AddBoneTreeItem 1, %BONE_RIGHTHAND , 6, 0

tbgl_m15AddBoneTreeItem 1, %BONE_LEFTLEGUP , 7, 8
tbgl_m15AddBoneTreeItem 1, %BONE_LEFTLEGDOWN , 8, 9
tbgl_m15AddBoneTreeItem 1, %BONE_LEFTFOOT , 9, 0

tbgl_m15AddBoneTreeItem 1, %BONE_RIGHTLEGUP , 10, 11
tbgl_m15AddBoneTreeItem 1, %BONE_RIGHTLEGDOWN , 11, 12
tbgl_m15AddBoneTreeItem 1, %BONE_RIGHTFOOT , 12, 0




end sub

sub animateModel( atype as long )

local AnimationSpeedDivider(3) as single
array assign AnimationSpeedDivider(), = 2.75, 1.25, 2.5
local LocalTime as double = GlobalTime / AnimationSpeedDivider(atype)
local SinLocalTime as double = sin(LocalTime)
local SinLocalTime2 as double = sin(LocalTime+0.5)

select case aType
case %ANIM_RUN

tbgl_m15RotBone 1, %BONE_BODYUP , Hero.BodyAngle,0,-sin(LocalTime)*5

tbgl_m15RotBone 1, %BONE_HEAD , -Hero.BodyAngle,sin(LocalTime/2)*15,0

tbgl_m15RotBone 1, %BONE_LEFTARMUP , SinLocalTime*25,0,0
tbgl_m15RotBone 1, %BONE_LEFTARMDOWN , -45,0,0
tbgl_m15RotBone 1, %BONE_LEFTHAND , 0,0,45
'
tbgl_m15RotBone 1, %BONE_RIGHTARMUP , -SinLocalTime*25,0,0
tbgl_m15RotBone 1, %BONE_RIGHTARMDOWN , -45,0,0
tbgl_m15RotBone 1, %BONE_RIGHTHAND , 0,0,-45


tbgl_m15RotBone 1, %BONE_LEFTLEGUP , -SinLocalTime2*25-15,0,0
tbgl_m15RotBone 1, %BONE_LEFTLEGDOWN , 35-SinLocalTime2*25,0,0
tbgl_m15RotBone 1, %BONE_LEFTFOOT , SinLocalTime2*10,0,0


tbgl_m15RotBone 1, %BONE_RIGHTLEGUP , SinLocalTime2*25-15,0,0
tbgl_m15RotBone 1, %BONE_RIGHTLEGDOWN , 35+SinLocalTime2*25,0,0
tbgl_m15RotBone 1, %BONE_RIGHTFOOT , SinLocalTime2*10,0,0

case %ANIM_STAND
tbgl_m15RotBone 1, %BONE_BODYUP , Hero.BodyAngle,0,0

tbgl_m15RotBone 1, %BONE_HEAD , -Hero.BodyAngle,sin(LocalTime/2)*15,0

if Hero.Interaction then
tbgl_m15RotBone 1, %BONE_LEFTARMUP , -Hero.Interaction ,0,0
tbgl_m15RotBone 1, %BONE_LEFTARMDOWN , -Hero.Interaction ,0,0
else
tbgl_m15RotBone 1, %BONE_LEFTARMUP , SinLocalTime*5,0,0
tbgl_m15RotBone 1, %BONE_LEFTARMDOWN , -SinLocalTime*5,0,0
end if

'
tbgl_m15RotBone 1, %BONE_RIGHTARMUP , SinLocalTime2*5,0,0
tbgl_m15RotBone 1, %BONE_RIGHTARMDOWN , -SinLocalTime2*5,0,0



tbgl_m15RotBone 1, %BONE_LEFTLEGUP , 0,0,0

tbgl_m15RotBone 1, %BONE_RIGHTLEGUP , -5,0,0
tbgl_m15RotBone 1, %BONE_RIGHTLEGDOWN , 5,0,0


case %ANIM_WALK

tbgl_m15RotBone 1, %BONE_BODYUP , Hero.BodyAngle,0,0

tbgl_m15RotBone 1, %BONE_HEAD , -Hero.BodyAngle,sin(LocalTime/2)*15,0

tbgl_m15RotBone 1, %BONE_LEFTARMUP , SinLocalTime*15,0,0
tbgl_m15RotBone 1, %BONE_LEFTARMDOWN , 0,0,0

'
tbgl_m15RotBone 1, %BONE_RIGHTARMUP , -SinLocalTime*15,0,0
tbgl_m15RotBone 1, %BONE_RIGHTARMDOWN , 0,0,0


tbgl_m15RotBone 1, %BONE_LEFTLEGUP , -SinLocalTime2*10,0,0
tbgl_m15RotBone 1, %BONE_LEFTLEGDOWN , 5-SinLocalTime2*10,0,0
tbgl_m15RotBone 1, %BONE_LEFTFOOT , SinLocalTime2*15,0,0


tbgl_m15RotBone 1, %BONE_RIGHTLEGUP , SinLocalTime2*10,0,0
tbgl_m15RotBone 1, %BONE_RIGHTLEGDOWN , 5+SinLocalTime2*10,0,0
tbgl_m15RotBone 1, %BONE_RIGHTFOOT , -SinLocalTime2*15,0,0


end select

end sub

sub LoadLevel( sFile as string )

local sBuffer as string = FILE_LOAD(sFile)
local LevelArray() as string
local ModelsLoaded(32) as string

LOCAL Count AS LONG
LOCAL Item AS STRING
LOCAL pList AS LONG
LOCAL nItems AS LONG



pList = DIR_List(APP_SOURCEPATH+"Models\", "le_*.*", %FILE_NORMAL)
nItems = LL_Count(pList)

IF nItems > 0 THEN
FOR Count = 1 TO nItems
Item = LL_GetItem(pList, Count)
tbgl_M15LoadModel "Models\"+item, "Textures", 2, 1+Count, %TBGL_NORMAL_PRECISE
tbgl_m15ClearModel 2
sBuffer = replace$(sBuffer, item+" ", FORMAT$(1+Count)+" ")
NEXT
END IF


local n as long = parse sBuffer, LevelArray(), $CRLF

LL_Free(pList)
local i as long
local j as long
local token as long
local multip as long
tbgl_NewList 1
tbgl_PushMatrix
for i = 1 to n
token = val(PARSE$( LevelArray(i), " ", 1))
IF token then
multip = val(parse$(LevelArray(i), " x ", 2))
if multip = 0 then multip = 1

for j = 1 to multip
tbgl_CallList token
tbgl_translate 2,0,0
next

end if
next
tbgl_PopMatrix
tbgl_EndList

end sub

Petr Schreiber
18-03-2007, 17:53
Hi Mike,

solution is relatively simple.

TBGL_PrintFont prints text at 3D position, but I think you want to use it just like 2D stuff.
Then you need to do this just before TBGL_DrawFrame:



tbgl_ResetMatrix

tbgl_UseLighting 0
TBGL_PrintFont "Fps:"+Str$(framerate), -0.05, 0.04, -0.1
tbgl_UseLighting 1


Disabling the lighting is needed, the normal is defined ... don't know how ??? so better go without light for text plot.
Maybe you are curious how I calculated the coordinates :). For Z = -0.1 the Y value goes from approximately -0.41 to 0.41, and the X coordinate can be evaluated as screenX / screenY * 0.4.

Hope it helps, on my PC it runs, but font size is quite tiny, maybe 12 would be better.
Script runs here at 90 FPS, doesn't matter which resolution.

Bye,
Petr

P.S. From performance point of view font created using bitmap will be always faster, as each character is just textured quad, here we are dealing with complex true type geometry :)

Michael Hartlef
18-03-2007, 20:01
Thanks Petr.