Petr Schreiber
24-07-2013, 21:22
Rene's wish is to save geometry in form of ThinBASIC TBGL script and then load it by interpreting. While this is one of the possible approaches, there is one way which will probably will be slightly slower, but much more space efficient. Each TBGL command (for now just a few) is encoded using binary signature (4 byte) + all parameters are in binary form.
Take for example "TBGL_Vertex 1.100, 2.256, 3.456", this would take 32 bytes to save in script form. With binary form, it is packed to SizeOf(signature) + 3 * SizeOf(single) = 16 bytes.
We saved 50% on size and reading binary value does not involve string-to-number conversion, which speeds up the load time too.
I attach the unit file, fileformat_BG.tBasicU, + test script. The usage is as simple as:
#INCLUDE "fileFormat_BG.tBasicU"
Uses "TBGL"
$BG_FILE = APP_SourcePath + "test.bg"
Function TBMain()
Local hWnd As DWord
Local FrameRate As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Test of BG file format - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Define geometry to file
BG_BeginSaveFile($BG_FILE)
BG_BeginPoly(%GL_TRIANGLES)
BG_Color(255, 0, 0)
BG_Vertex(-1,-1, 0)
BG_Color( 0, 255, 0)
BG_Vertex( 1,-1, 0)
BG_Color( 0, 0, 255)
BG_Vertex( 0, 1, 0)
BG_EndPoly
BG_BeginPoly(%GL_QUADS)
BG_Color(255, 255, 255)
BG_Vertex(-2, 2, -1)
BG_Vertex( 2, 2, -1)
BG_Color(0, 0, 0)
BG_Vertex( 2,-2, -1)
BG_Vertex(-2,-2, -1)
BG_EndPoly
BG_EndSaveFile()
' -- Load it back
%listHouse = BG_LoadFileToList($BG_FILE)
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera(0, 0, 5, 0, 0, 0)
TBGL_CallList(%listHouse)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
End Function
Of course, more features can be added, this is just a concept for possible expansion.
Petr
Take for example "TBGL_Vertex 1.100, 2.256, 3.456", this would take 32 bytes to save in script form. With binary form, it is packed to SizeOf(signature) + 3 * SizeOf(single) = 16 bytes.
We saved 50% on size and reading binary value does not involve string-to-number conversion, which speeds up the load time too.
I attach the unit file, fileformat_BG.tBasicU, + test script. The usage is as simple as:
#INCLUDE "fileFormat_BG.tBasicU"
Uses "TBGL"
$BG_FILE = APP_SourcePath + "test.bg"
Function TBMain()
Local hWnd As DWord
Local FrameRate As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("Test of BG file format - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Define geometry to file
BG_BeginSaveFile($BG_FILE)
BG_BeginPoly(%GL_TRIANGLES)
BG_Color(255, 0, 0)
BG_Vertex(-1,-1, 0)
BG_Color( 0, 255, 0)
BG_Vertex( 1,-1, 0)
BG_Color( 0, 0, 255)
BG_Vertex( 0, 1, 0)
BG_EndPoly
BG_BeginPoly(%GL_QUADS)
BG_Color(255, 255, 255)
BG_Vertex(-2, 2, -1)
BG_Vertex( 2, 2, -1)
BG_Color(0, 0, 0)
BG_Vertex( 2,-2, -1)
BG_Vertex(-2,-2, -1)
BG_EndPoly
BG_EndSaveFile()
' -- Load it back
%listHouse = BG_LoadFileToList($BG_FILE)
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera(0, 0, 5, 0, 0, 0)
TBGL_CallList(%listHouse)
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
End Function
Of course, more features can be added, this is just a concept for possible expansion.
Petr