Petr Schreiber
18-05-2009, 16:56
Hi all,
if you are using latest TB beta (http://community.thinbasic.com/index.php?topic=2588.msg19386#msg19386), you can taste a bit of next TBGL.
Do you want to create fullscreen/windowed application, but still stay CPU friendly?
Do you want to get rid of DOEVENTs, SLEEPs and other workarounds in WHILE/WEND loops?
What about periodically launched function...
Uses "TBGL"
TYPE tFan
STATIC eventSwitch AS STRING
STATIC eventRender AS STRING
STATIC eventPhysics AS STRING
running AS LONG
speed AS SINGLE
angle AS SINGLE
END TYPE
DIM Fan AS tFan
WITH Fan
.eventSwitch = "Fan_Switch"
.eventRender = "Fan_Render"
.eventPhysics = "Fan_Physics"
END WITH
GLOBAL FrameRate AS DOUBLE
GLOBAL hWnd AS DWORD
FUNCTION TBMAIN()
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Fan for Eros :D, SPACE to turn on off", 640, 480, 32, %TBGL_WS_WINDOWED or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Initialize lighting
TBGL_UseLighting %TRUE
TBGL_UseLightSource %GL_LIGHT0, %TRUE
tbgl_SetLightParameter %GL_LIGHT0, %TBGL_LIGHT_POSITION, 15, 10, 15, 1
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- We will set GameLoop function to be executed each 10ms
TBGL_PeriodicBindFunction( hWnd, "GameLoop", 10 )
' -- Once the command below is executed, further script execution
' -- is halted and only periodic calling of the bound function is performed
TBGL_PeriodicProcessFunction(hWnd)
' -- Once we leave periodically launched function, we can destroy the window
TBGL_DestroyWindow
END FUNCTION
SUB GameLoop()
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera 5, 5, 5, 0, 1.5, 0
Call Fan.eventPhysics(Fan)
Call Fan.eventRender(Fan)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then TBGL_PeriodicUnBindFunction( hWnd )
If TBGL_GetWindowKeyOnce(hWnd, %VK_SPACE) Then Call Fan.eventSwitch(Fan)
END SUB
' -- Functions to manipulate fan
FUNCTION Fan_Switch( byref property AS tFan )
property.running = not property.running
END FUNCTION
FUNCTION Fan_Physics( property AS tFan )
if property.running then
property.speed += 90/FrameRate
if property.speed > 720 then property.speed = 720
else
property.speed -= 180/FrameRate
if property.speed < 0 then property.speed = 0
end if
property.angle += property.speed/FrameRate
END FUNCTION
FUNCTION Fan_Render( property AS tFan )
static i as long
tbgl_PushMatrix
tbgl_Color 255, 128, 0
' -- Base
tbgl_PushMatrix
tbgl_Scale 4, 0.5, 4
tbgl_Sphere 0.25
tbgl_PopMatrix
tbgl_Cylinder 0.125, 0.125, 2
tbgl_PushMatrix
tbgl_Translate 0, 2, 0
tbgl_Scale 0.5, 0.5, 1
tbgl_Sphere 1
tbgl_PopMatrix
' -- Fan
tbgl_Color 255, 255, 255
tbgl_PushMatrix
tbgl_Translate 0, 2, 0.5
tbgl_Rotate property.angle, 0, 0, 1
for i = 1 to 360 step 60
tbgl_PushMatrix
tbgl_Rotate i, 0, 0, 1
tbgl_Translate 0.75, 0, 0
tbgl_Rotate -45, 1, 0, 0
tbgl_Box 1, 0.3, 0.01
tbgl_PopMatrix
next
tbgl_PopMatrix
tbgl_PopMatrix
END FUNCTION
Let us know whether your CPU usage is really kept low using this sample ;)
if you are using latest TB beta (http://community.thinbasic.com/index.php?topic=2588.msg19386#msg19386), you can taste a bit of next TBGL.
Do you want to create fullscreen/windowed application, but still stay CPU friendly?
Do you want to get rid of DOEVENTs, SLEEPs and other workarounds in WHILE/WEND loops?
What about periodically launched function...
Uses "TBGL"
TYPE tFan
STATIC eventSwitch AS STRING
STATIC eventRender AS STRING
STATIC eventPhysics AS STRING
running AS LONG
speed AS SINGLE
angle AS SINGLE
END TYPE
DIM Fan AS tFan
WITH Fan
.eventSwitch = "Fan_Switch"
.eventRender = "Fan_Render"
.eventPhysics = "Fan_Physics"
END WITH
GLOBAL FrameRate AS DOUBLE
GLOBAL hWnd AS DWORD
FUNCTION TBMAIN()
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Fan for Eros :D, SPACE to turn on off", 640, 480, 32, %TBGL_WS_WINDOWED or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Initialize lighting
TBGL_UseLighting %TRUE
TBGL_UseLightSource %GL_LIGHT0, %TRUE
tbgl_SetLightParameter %GL_LIGHT0, %TBGL_LIGHT_POSITION, 15, 10, 15, 1
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- We will set GameLoop function to be executed each 10ms
TBGL_PeriodicBindFunction( hWnd, "GameLoop", 10 )
' -- Once the command below is executed, further script execution
' -- is halted and only periodic calling of the bound function is performed
TBGL_PeriodicProcessFunction(hWnd)
' -- Once we leave periodically launched function, we can destroy the window
TBGL_DestroyWindow
END FUNCTION
SUB GameLoop()
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera 5, 5, 5, 0, 1.5, 0
Call Fan.eventPhysics(Fan)
Call Fan.eventRender(Fan)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then TBGL_PeriodicUnBindFunction( hWnd )
If TBGL_GetWindowKeyOnce(hWnd, %VK_SPACE) Then Call Fan.eventSwitch(Fan)
END SUB
' -- Functions to manipulate fan
FUNCTION Fan_Switch( byref property AS tFan )
property.running = not property.running
END FUNCTION
FUNCTION Fan_Physics( property AS tFan )
if property.running then
property.speed += 90/FrameRate
if property.speed > 720 then property.speed = 720
else
property.speed -= 180/FrameRate
if property.speed < 0 then property.speed = 0
end if
property.angle += property.speed/FrameRate
END FUNCTION
FUNCTION Fan_Render( property AS tFan )
static i as long
tbgl_PushMatrix
tbgl_Color 255, 128, 0
' -- Base
tbgl_PushMatrix
tbgl_Scale 4, 0.5, 4
tbgl_Sphere 0.25
tbgl_PopMatrix
tbgl_Cylinder 0.125, 0.125, 2
tbgl_PushMatrix
tbgl_Translate 0, 2, 0
tbgl_Scale 0.5, 0.5, 1
tbgl_Sphere 1
tbgl_PopMatrix
' -- Fan
tbgl_Color 255, 255, 255
tbgl_PushMatrix
tbgl_Translate 0, 2, 0.5
tbgl_Rotate property.angle, 0, 0, 1
for i = 1 to 360 step 60
tbgl_PushMatrix
tbgl_Rotate i, 0, 0, 1
tbgl_Translate 0.75, 0, 0
tbgl_Rotate -45, 1, 0, 0
tbgl_Box 1, 0.3, 0.01
tbgl_PopMatrix
next
tbgl_PopMatrix
tbgl_PopMatrix
END FUNCTION
Let us know whether your CPU usage is really kept low using this sample ;)