Results 1 to 5 of 5

Thread: TBGL entity system and dynamic CALLs

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,790
    Blog Entries
    3
    Rep Power
    693

    TBGL entity system and dynamic CALLs

    Hi Kent,

    yes, you are right. Basic version with user data can be the following:
    [code=thinbasic]
    Uses "TBGL"

    ' -- Create and show window
    Dim hWnd As Dword = TBGL_CreateWindowEx("UserData to hold info on object functions - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED or %TBGL_WS_CLOSEBOX)
    TBGL_ShowWindow

    ' -- Create scene
    %SCENE1 = 1
    TBGL_SceneCreate(%SCENE1)

    ' -- Create basic entities
    Begin Const
    %etAI = 0
    %etControled = 1
    %eCamera = 1
    %eLight
    %eStart = 10
    %eEnd = 20
    end const

    ' -- Create camera to look from 15, 15, 15 to 0, 0, 0
    TBGL_EntityCreateCamera(%SCENE1, %eCamera)
    TBGL_EntitySetPos(%SCENE1, %eCamera, 50, 50, 50)
    TBGL_EntitySetTargetPos(%SCENE1, %eCamera, 0, 0, 0)

    ' -- Create point light
    TBGL_EntityCreateLight(%SCENE1, %eLight)
    TBGL_EntitySetPos(%SCENE1, %eLight, 15, 10, 5)

    ' -- Create something to look at
    type TEntBox
    eType as long ' -- AI = 0, controled = 1
    onLeft as asciiz * 256
    onRight as asciiz * 256
    onUp as asciiz * 256
    onDown as asciiz * 256
    end Type

    ' -- Variable to fill data
    dim entBox as TentBox

    dim i as long

    ' -- Random objects, some controllable by arrows, some with own AI
    for i = %eStart to %eEnd
    TBGL_EntityCreateBox(%SCENE1, i, 0, 1, 1, 1, 0, rnd(128,255), rnd(128,255), rnd(128,255))
    TBGL_EntitySetPos(%SCENE1, i, rndf(-10,10), 0, rndf(-10,10))

    entBox.eType = rnd(%etAI, %etControled)
    entBox.onLeft = "entBox_GoLeft"
    entBox.onRight = "entBox_GoRight"
    entBox.onUp = "entBox_GoUp"
    entBox.onDown = "entBox_GoDown"

    TBGL_EntitySetUserData(%SCENE1, i, entBox)
    next

    DIM FrameRate AS DOUBLE

    ' -- Resets status of all keys
    TBGL_GetAsyncKeyState(-1)

    ' -- Main loop
    While TBGL_IsWindow(hWnd)
    FrameRate = TBGL_GetFrameRate

    TBGL_ClearFrame

    TBGL_SceneRender(%SCENE1)
    Entities_Process()

    TBGL_DrawFrame

    ' -- ESCAPE key to exit application
    If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

    Wend

    TBGL_DestroyWindow

    sub Entities_Process()
    local i as long
    local s as TentBox PTR

    for i = %eStart to %eEnd
    s = TBGL_EntityGetUserDataPointer(%SCENE1, i)

    if s.eType = %etAI then
    Controls_AI(i)
    ELSEIF s.eType = %etControled then
    Controls_Player(i)
    end if
    next

    end sub

    sub Controls_Player( entity as long )
    local s as TentBox PTR

    s = TBGL_EntityGetUserDataPointer(%SCENE1, entity)

    if TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
    call ""+s.onLeft(entity)
    elseif TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
    call ""+s.onRight(entity)
    endif

    if TBGL_GetWindowKeyState(hWnd, %VK_UP) Then
    call ""+s.onUp(entity)
    elseif TBGL_GetWindowKeyState(hWnd, %VK_DOWN) Then
    call ""+s.onDown(entity)
    endif

    end sub

    sub Controls_AI( entity as long )

    local s as TentBox PTR
    s = TBGL_EntityGetUserDataPointer(%SCENE1, entity)

    select case rnd(1, 3)
    case 1
    call ""+s.onLeft(entity)

    case 2
    call ""+s.onUp(entity)

    case 3
    call ""+s.onRight(entity)

    end SELECT

    end sub

    sub entBox_GoLeft( entity as long )
    tbgl_EntityTurn(%SCENE1, entity, 0, 180/FrameRate, 0)
    end sub

    sub entBox_GoRight( entity as long )
    tbgl_EntityTurn(%SCENE1, entity, 0,-180/FrameRate, 0)
    end sub

    sub entBox_GoUp( entity as long )
    tbgl_EntityPush(%SCENE1, entity, 0, 0, 10/FrameRate)
    end sub

    sub entBox_GoDown( entity as long )
    tbgl_EntityPush(%SCENE1, entity, 0, 0,-10/FrameRate)
    end sub
    [/code]

    I am very curious about Eros idea, he always comes with some surprise which would not occur to me. So I am sure he will come with some mini-revolution again. From his description it sounded almost like a OOP class, we will see what it will be once ready. But as Eros means it for tB 2.x+ then we will have to wait for a while


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    397

    TBGL entity system and dynamic CALLs

    Petr, that is a really neat demo!

    I studied it, but it still is not clear to me... but this is the kind of stuff I need to tap into for sure.
    So this will get studied a lot the next few hours
    THanks.
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,171
    Blog Entries
    2
    Rep Power
    10

    Re: TBGL entity system and dynamic CALLs

    Above two posts splitted in order to keep things organized.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,171
    Blog Entries
    2
    Rep Power
    10

    Re: TBGL entity system and dynamic CALLs

    Petr,

    thanks to your example I've seen that CALL statement was not working with with UDT elements.
    I've updated again thinBasic 1.6.0.8 with this aspect fixed.
    Now you can use:
    [code=thinbasic]sub Controls_AI( entity as long )
    local s as TentBox PTR
    s = TBGL_EntityGetUserDataPointer(%SCENE1, entity)

    select case rnd(1, 3)
    case 1
    call s.onLeft(entity)
    case 2
    call s.onUp(entity)
    case 3
    call s.onRight(entity)
    end SELECT

    end sub[/code]
    avoiding to use "" in front of the string expression.

    Thanks
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    61
    Posts
    3,869
    Rep Power
    397

    Re: TBGL entity system and dynamic CALLs

    Oh yes that is a really nice touch Eros. Easier to understand the code looking at it this way!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Posting Permissions

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