Michael Clease
27-02-2010, 03:24
' Example: Section 8.1 (page 246), Follow the boucing ball
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Michael Clease
' Last modified: February 27, 2010
Uses "TBGL"
' Handle for our window
Global hWnd As DWord
Global Width As Long
Global Height As Long
Global anim As Long
Global x,y As Double
Global dx,dy As Double
Global axrng As Double
Dim Title As String Value "Follow the boucing ball - Press 'A' to start, 'S' to stop or 'Esc' to quit"
'# Initial values of width And height
width = 400
height = 400
'
' Create and show window
hWnd = TBGL_CreateWindowEx(Title, Width, Height, 32, %TBGL_WS_WINDOWED )
TBGL_ShowWindow
Init()'Anim,x,y,dx,dy,axrng)
' Define "Callback" to be fired + that it should be fired each 40ms
TBGL_BindPeriodicFunction(hWnd, "PlotFunc", 20)
' -- Once the command below is executed, further script execution
' -- is halted and only periodic calling of the bound function is performed
TBGL_ProcessPeriodicFunction(hWnd)
TBGL_DestroyWindow
Sub Init() 'ByRef anim,ByRef x,ByRef y,ByRef dx,ByRef dy,axrng)
'# initial position of the ball
x = -0.67
y = 0.34
'# Direction "sign" of the ball's motion
dx = 1
dy = 1
'# Window dimensions
width = 500
height = 500
axrng = 1.0
'# No animation To start
anim = 0
TBGL_Color(255,255,255)
TBGL_BackColor(0,0,0)
' Resets status of all keys
TBGL_ResetKeyState()
End Sub
Sub PlotFunc()
' -- Which window is calling?
Local hWnd As DWord = TBGL_CallingWindow
If Anim Then
TBGL_ClearFrame
TBGL_RenderMatrix2D(-axrng, -axrng, axrng, axrng)
x += 0.005*dx
y += 0.005*dy
TBGL_PushMatrix
TBGL_Translate(x,y,0)
TBGL_Sphere(0.1)
TBGL_PopMatrix
If x >= (axrng - 0.1) Or x <= (-axrng + 0.1) Then dx = -1*dx
If y >= (axrng - 0.1) Or y <= (-axrng + 0.1) Then dy = -1*dy
TBGL_DrawFrame
EndIf
If TBGL_GetWindowKeyState(hWnd, %VK_A) Then Anim = 1
If TBGL_GetWindowKeyState(hWnd, %VK_S) Then Anim = 0
' ESCAPE key to disable callback
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then
TBGL_UnBindPeriodicFunction( hWnd )
Exit Sub
End If
End Sub
' From Stan Blank's Book:
' "Python Programming in OpenGL
' "A Graphical Approach to Programming
' Converted by Michael Clease
' Last modified: February 27, 2010
Uses "TBGL"
' Handle for our window
Global hWnd As DWord
Global Width As Long
Global Height As Long
Global anim As Long
Global x,y As Double
Global dx,dy As Double
Global axrng As Double
Dim Title As String Value "Follow the boucing ball - Press 'A' to start, 'S' to stop or 'Esc' to quit"
'# Initial values of width And height
width = 400
height = 400
'
' Create and show window
hWnd = TBGL_CreateWindowEx(Title, Width, Height, 32, %TBGL_WS_WINDOWED )
TBGL_ShowWindow
Init()'Anim,x,y,dx,dy,axrng)
' Define "Callback" to be fired + that it should be fired each 40ms
TBGL_BindPeriodicFunction(hWnd, "PlotFunc", 20)
' -- Once the command below is executed, further script execution
' -- is halted and only periodic calling of the bound function is performed
TBGL_ProcessPeriodicFunction(hWnd)
TBGL_DestroyWindow
Sub Init() 'ByRef anim,ByRef x,ByRef y,ByRef dx,ByRef dy,axrng)
'# initial position of the ball
x = -0.67
y = 0.34
'# Direction "sign" of the ball's motion
dx = 1
dy = 1
'# Window dimensions
width = 500
height = 500
axrng = 1.0
'# No animation To start
anim = 0
TBGL_Color(255,255,255)
TBGL_BackColor(0,0,0)
' Resets status of all keys
TBGL_ResetKeyState()
End Sub
Sub PlotFunc()
' -- Which window is calling?
Local hWnd As DWord = TBGL_CallingWindow
If Anim Then
TBGL_ClearFrame
TBGL_RenderMatrix2D(-axrng, -axrng, axrng, axrng)
x += 0.005*dx
y += 0.005*dy
TBGL_PushMatrix
TBGL_Translate(x,y,0)
TBGL_Sphere(0.1)
TBGL_PopMatrix
If x >= (axrng - 0.1) Or x <= (-axrng + 0.1) Then dx = -1*dx
If y >= (axrng - 0.1) Or y <= (-axrng + 0.1) Then dy = -1*dy
TBGL_DrawFrame
EndIf
If TBGL_GetWindowKeyState(hWnd, %VK_A) Then Anim = 1
If TBGL_GetWindowKeyState(hWnd, %VK_S) Then Anim = 0
' ESCAPE key to disable callback
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then
TBGL_UnBindPeriodicFunction( hWnd )
Exit Sub
End If
End Sub