PDA

View Full Version : TBGL entity system and dynamic CALLs



Petr Schreiber
15-05-2008, 08:21
Hi Kent,

yes, you are right. Basic version with user data can be the following:


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


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

kryton9
15-05-2008, 23:23
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.

ErosOlmi
15-05-2008, 23:34
Above two posts splitted in order to keep things organized.

ErosOlmi
16-05-2008, 00:40
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:

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
avoiding to use "" in front of the string expression.

Thanks
Eros

kryton9
16-05-2008, 01:39
Oh yes that is a really nice touch Eros. Easier to understand the code looking at it this way!