PDA

View Full Version : Fern Demo



peter
06-10-2012, 20:30
Hi Petr,

A small TBGL demo.

I have little bit trouble with (TBGL_LoadBMPFont APP_SourcePath +"TBGL_font.bmp") and with ( %TBGL_WS_WINDOWED)

And this doesn't go.

hfont= TBGL_FontHandle("fontname",fontsize)
TBGL_BuildFont(hfont)
TBGL_SetActiveFont(1)



Uses "tbgl"
Dim hwnd As DWord

Function Key(xkey As Word) As Word
Return TBGL_GetAsyncKeyState(xkey)
End Function

hwnd= TBGL_CreateWindowEx("FERN DEMO",800,600,32, %TBGL_WS_FULLSCREEN) '%TBGL_WS_WINDOWED)
TBGL_ShowWindow
TBGL_RenderMatrix2D (0,800,600,0)
TBGL_LoadBMPFont APP_SourcePath+"TBGL_font.bmp"
TBGL_BackColor 0,0,155

Dim a,b,c,d,e,f,newx,newy As Single
Dim r,wa As Long
Dim xy(2) As Single

Sub Fern()
r = Rnd(0,100)
If r <= 10 Then
a = 0
b = 0
c = 0
d = 0.16
e = 0
f = 0
ElseIf r > 1 And r <=86 Then
a = 0.85
b = 0.04
c = -.04
d = 0.85
e = 0
f = 1.60
ElseIf r > 86 And r <=93 Then
a = 0.2
b = -.26
c = 0.23
d = 0.22
e = 0
f = 0.16
Else
a = -.15
b = 0.28
c = 0.26
d = 0.24
e = 0
f = 0.44
End If
newx = ((a * xy(1)) + (b * xy(2)) + e)
newy = ((c * xy(1)) + (d * xy(2)) + f)
xy(1) = newx
xy(2) = newy
TBGL_Color 0,210,55
TBGL_Point xy(1)*40+200,xy(2)*40+80
End Sub

'While TBGL_IsWindow(hwnd)
TBGL_ClearFrame
TBGL_Color 255,255,255
TBGL_PrintBMP "FERN",30,30
TBGL_DrawFrame

For wa=0 To 200000
fern
Next
TBGL_DrawFrame

While Key(27)=0
Sleep (10)
Wend

Petr Schreiber
07-10-2012, 15:59
Hi Peter,

I modified your code a little - it contained two TBGL_DrawFrame calls, which negated the effect of drawn text.
I also did few minor tweaks to make it draw faster - cache it first, then draw using single line of code. I hope you will like it.


#MINVERSION 1.8.9.0

Uses "TBGL"

Function TBMain()

' -- Create window
DWord hWnd = TBGL_CreateWindowEx("FERN DEMO - press escape to continue", 800, 600, 32, %TBGL_WS_WINDOWED | %TBGL_WS_DONTSIZE | %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- Setup rendering coordinates and back color
TBGL_RenderMatrix2D (0, 0, 800, 600)
TBGL_BackColor 0,0,155

DWord hFont= TBGL_FontHandle("Arial", 72)
TBGL_BuildFont(hfont)
TBGL_SetActiveFont(1)

Long wa
Long cachedFern
TBGL_NewListSpace(cachedFern)

' -- Cache the fern
TBGL_NewList cachedFern
For wa=0 To 20000
fern()
Next
TBGL_EndList

TBGL_ResetKeyState()

' -- Begin the rendering loop
While TBGL_IsWindow(hWnd)

' -- Clear the frame
TBGL_ClearFrame
' -- Set white color
TBGL_Color 255,255,255

' -- Enable printing
TBGL_PrintFont "FERN",300,350

' -- Dynamic color for the fern
TBGL_Color 127+Sin(GetTickCount/1000)*128, 127+Sin(GetTickCount/1000+2)*128, 127+Sin(GetTickCount/1000+4)*128

' -- Render the fern in one call
TBGL_CallList cachedFern
TBGL_DrawFrame

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow

End Function

Sub Fern()
' -- STATIC variables remember their values between function calls
Static a, b, c, d, e, f, newx, newy As Single
Static r, wa As Long
Static xy(2) As Single

r = Rnd(0,100)
If r <= 10 Then
a = 0
b = 0
c = 0
d = 0.16
e = 0
f = 0
ElseIf r > 1 And r <=86 Then
a = 0.85
b = 0.04
c = -.04
d = 0.85
e = 0
f = 1.60
ElseIf r > 86 And r <=93 Then
a = 0.2
b = -.26
c = 0.23
d = 0.22
e = 0
f = 0.16
Else
a = -.15
b = 0.28
c = 0.26
d = 0.24
e = 0
f = 0.44
End If

newx = ((a * xy(1)) + (b * xy(2)) + e)
newy = ((c * xy(1)) + (d * xy(2)) + f)

xy(1) = newx
xy(2) = newy

TBGL_Point xy(1)*40+200,xy(2)*40+80

End Sub


I attach source and screenshot for you.

The windowed mode should work for you, if in any doubt, try running thinBASIC diagnostic too (http://www.thinbasic.com/community/showthread.php?7848-TBGL-troubleshooting)l to see if you have OpenGL installed correctly.


Petr

peter
07-10-2012, 18:21
Hi Petr,

Looks good to me, thanks for that.
Well, I will write a circle/ellipse/rectangle algorithms and an own sprite system.

I saw, that you a sprite system, but isn't satisfactorily for me.
In general, TBGL is cool and very easy. Thanks

Michael Clease
08-10-2012, 15:02
I found in the past that calling a sub routine with declared variables inside is a lot slower than just using global variables, build time can be reduced by calling the subroutine once and doing the loop inside the from inside.

A small speed improvement but anything helps.

Mike clease

Petr Schreiber
08-10-2012, 17:41
Nice tweak! Thanks for the tip :)

Petr