Results 1 to 5 of 5

Thread: Fern Demo

  1. #1
    Member
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    406
    Rep Power
    56

    Fern Demo

    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
    

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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 tool to see if you have OpenGL installed correctly.


    Petr
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Petr Schreiber; 07-10-2012 at 16:01.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Member
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    406
    Rep Power
    56
    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

  4. #4
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159
    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
    Attached Files Attached Files
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Nice tweak! Thanks for the tip

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Example: Section 6.4 (page 129), The Barnsley Fern
    By Michael Clease in forum ThinBASIC programming in OpenGL/TBGL
    Replies: 3
    Last Post: 27-02-2010, 05:51
  2. Art Thankies Demo 1.1a
    By Lionheart008 in forum TBGL Scripts and Projects
    Replies: 7
    Last Post: 23-04-2009, 18:54
  3. Demo
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 10
    Last Post: 12-03-2009, 15:39
  4. Artificial Screensaver Demo :-D
    By Lionheart008 in forum TBGL ScreenSavers
    Replies: 8
    Last Post: 04-02-2009, 13:48
  5. Fractal Fern
    By Michael Clease in forum TBGL General
    Replies: 17
    Last Post: 08-08-2007, 11:58

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •