PDA

View Full Version : Maxim: Use of SDK for wrapping the Irrlicht



maxim
02-07-2011, 23:35
Hi Largo_Winch, if include file works, the module would be redundant I guess. Some time ago, the DECLAREd functions in includes had slower execution than module based keywords, but I think the difference is no more so big. That is the reason why for example OpenGL is provided via header and not module. The general rule I follow is:

if it is just a wrapper of existing DLL -> make include
if it is something more, computionaly expensive and so on, then module is the way to go
Petr

Thanks Petr, now i better understand Largo's question. I use includes becouse not want use compiler and yes, it is enough fast (develop and launch). My question about new functional with classes: can use this dynamically?


%thinBasic_ReturnNone = &h0
%thinBasic_ReturnCodeByte = &h1
%thinBasic_ReturnCodeInteger = &h2
%thinBasic_ReturnCodeWord = &h3
%thinBasic_ReturnCodeDWord = &h4
%thinBasic_ReturnCodeLong = &h5
%thinBasic_ReturnCodeQuad = &h6
%thinBasic_ReturnCodeSingle = &h7
%thinBasic_ReturnCodeDouble = &h8
%thinBasic_ReturnCodeCurrency = &h9
%thinBasic_ReturnCodeExt = &h10
%thinBasic_ReturnNumber = &h20
%thinBasic_ReturnString = &h30

Declare Function FunctionGetPtr Lib "thinCore.dll" Alias "thinBasic_FunctionGetPtr" (ByVal fName As String) As Long
Declare Function thinBasic_LoadSymbol Lib "thinCore.dll" Alias "thinBasic_LoadSymbol" (ByVal SymbolName As String, ByVal ReturnCode As Long, ByVal FunctionOrSubPointer As Dword, Optional ByVal ForceOverWrite As Long) As Long
Declare Function thinBasic_Class_Add Lib "thinCore.dll" Alias "thinBasic_Class_Add" (ByVal sClassName As String, ByVal pClassFunc As Long) As Long
Declare Function thinBasic_Class_AddMethod Lib "thinCore.dll" Alias "thinBasic_Class_AddMethod" (ByVal pClass As Long, ByVal sMethodName As String, ByVal MethodReturnType As Long, ByVal pMethodFunc As Long) As Long
Local RetCode As Long
TYPE IrrlichtDevice DWORD
getVersion As ASCIIZ
END TYPE
Local IrrlichtDevicePtr As Long = thinBasic_Class_Add("IrrlichtDevice", Function_GetPtr(IrrlichtDevice_run))
If IrrlichtDevicePtr Then
RetCode = thinBasic_Class_AddMethod(IrrlichtDevicePtr, "run", %thinBasic_ReturnNumber, Function_GetPtr(IrrlichtDevice_run))
RetCode = thinBasic_Class_AddMethod(IrrlichtDevicePtr, "getVersion", %thinBasic_ReturnString, Function_GetPtr(IrrlichtDevice_getVersion))
Else
Uses "console"
PrintL "ERROR thinBasic_Class_Add IrrlichtDevice"
End If


Function_GetPtr is not valid for this target, what is correct function for get code pointer?

Petr Schreiber
03-07-2011, 11:39
Hi Maxim,

the code in one line is a bit hard on the eyes, do you think you could break it to lines?

thinBasic_FunctionGetPtr is function, which serves to retrieve something like code pointer to function defined in ThinBASIC code, but it is not synonym of classic CODEPTR in compiled code. The value returned by thinBasic_FunctionGetPtr is designed to be used with thinBasic_FunctionSimpleCall_ByPtr or thinBasic_FunctionCall_ByPtr.

Please have a look to your ThinBASIC/SDK directory, unzip the ZIP file, and check out for example the sample module in thinBasic\SDK\thinBasic BASIC SDK\PB\.

You will see the function referenced by function pointer must perform the parsing of parameters on its own, via stuff calls to things like thinBasic_ParseNumber and so on, you cannot directly wrap functions from DLL this way.

It also seems you are trying to use SDK functions in ThinBASIC code. It is possible, but you won't create module this way. You need some compiler, such as PowerBASIC, to produce the module DLL.
If you want to create Module Class, you currently need to create the class internally as COM class, and then wrap it.

I hope this text helped you a bit. If not please do not hesitate to ask, but please do it in the dedicated thread in ThinBASIC SDK (http://www.thinbasic.com/community/forumdisplay.php?358-thinBasic-SDK) forum, to not interfere with effort of Largo_Winch.


Petr

maxim
03-07-2011, 11:54
Hi Maxim,the code in one line is a bit hard on the eyes, do you think you could break it to lines?thinBasic_FunctionGetPtr is function, which serves to retrieve something like code pointer to function defined in ThinBASIC code, but it is not synonym of classic CODEPTR in compiled code. The value returned by thinBasic_FunctionGetPtr is designed to be used with thinBasic_FunctionSimpleCall_ByPtr or thinBasic_FunctionCall_ByPtr.Please have a look to your ThinBASIC/SDK directory, unzip the ZIP file, and check out for example the sample module in thinBasic\SDK\thinBasic BASIC SDK\PB\.You will see the function referenced by function pointer must perform the parsing of parameters on its own, via stuff calls to things like thinBasic_ParseNumber and so on, you cannot directly wrap functions from DLL this way.It also seems you are trying to use SDK functions in ThinBASIC code. It is possible, but you won't create module this way. You need some compiler, such as PowerBASIC, to produce the module DLL.If you want to create Module Class, you currently need to create the class internally as COM class, and then wrap it.I hope this text helped you a bit. If not please do not hesitate to ask, but please do it in the dedicated thread in ThinBASIC SDK (http://www.thinbasic.com/community/forumdisplay.php?358-thinBasic-SDK) forum, to not interfere with effort of Largo_Winch.Petr Again thanks Petr.I do code with many lines, but when I press "Submit Reply" button, I have result as you see (my browser was Firefox).Sorry for my question here, I do this question before with class article topic, but not get answer and repeated here.I promise not to do so more.

Petr Schreiber
03-07-2011, 12:07
No problem,

I just split the thread.
Regarding posting - I use Firefox 5.0, and if I paste from ThinAIR, the code aligns properly. Do you have the same FF version?


Petr

maxim
03-07-2011, 12:23
No problem,I just split the thread.Regarding posting - I use Firefox 5.0, and if I paste from ThinAIR, the code aligns properly. Do you have the same FF version?Petr FF was 4.0.1 now I do update to 5.0 (thank you) and I paste code from Notepad

maxim
03-07-2011, 12:44
I do as you say and press "Preview Post" button - same result. My post also edited as one line, but I write many lines. I try ie browser - same result. I do attached file to better understand my question.

maxim
03-07-2011, 13:20
%thinBasic_ReturnNone = &h0
%thinBasic_ReturnCodeByte = &h1
%thinBasic_ReturnCodeInteger = &h2
%thinBasic_ReturnCodeWord = &h3
%thinBasic_ReturnCodeDWord = &h4
%thinBasic_ReturnCodeLong = &h5
%thinBasic_ReturnCodeQuad = &h6
%thinBasic_ReturnCodeSingle = &h7
%thinBasic_ReturnCodeDouble = &h8
%thinBasic_ReturnCodeCurrency = &h9
%thinBasic_ReturnCodeExt = &h10
%thinBasic_ReturnNumber = &h20
%thinBasic_ReturnString = &h30

Declare Function FunctionGetPtr Lib "thinCore.dll" Alias "thinBasic_FunctionGetPtr" (ByVal fName As String) As Long
Declare Function thinBasic_LoadSymbol Lib "thinCore.dll" Alias "thinBasic_LoadSymbol" (ByVal SymbolName As String, ByVal ReturnCode As Long, ByVal FunctionOrSubPointer As Dword, Optional ByVal ForceOverWrite As Long) As Long
Declare Function thinBasic_Class_Add Lib "thinCore.dll" Alias "thinBasic_Class_Add" (ByVal sClassName As String, ByVal pClassFunc As Long) As Long
Declare Function thinBasic_Class_AddMethod Lib "thinCore.dll" Alias "thinBasic_Class_AddMethod" (ByVal pClass As Long, ByVal sMethodName As String, ByVal MethodReturnType As Long, ByVal pMethodFunc As Long) As Long

Local RetCode As Long

TYPE IrrlichtDevice DWORD
getVersion As ASCIIZ
END TYPE

Local IrrlichtDevicePtr As Long = thinBasic_Class_Add("IrrlichtDevice", Function_GetPtr(IrrlichtDevice_run))
If IrrlichtDevicePtr Then
RetCode = thinBasic_Class_AddMethod(IrrlichtDevicePtr, "run", %thinBasic_ReturnNumber, Function_GetPtr(IrrlichtDevice_run))
RetCode = thinBasic_Class_AddMethod(IrrlichtDevicePtr, "getVersion", %thinBasic_ReturnString, Function_GetPtr(IrrlichtDevice_getVersion))
Else
Uses "console"
PrintL "ERROR thinBasic_Class_Add IrrlichtDevice"
End If

I change with "My Profile" "General Settings" editor from "Enhanced Interface - Full WYSIWYG Editing" (default) to "Standard Editor - Extra formatting controls" and now BB code tag and break lines is worked.

Petr Schreiber
03-07-2011, 14:45
Hi Maxim,

I think for what you need, you would need to create compiled module first, where:

you would create internal COM wrapper for each of the Irrlicht classes with all functions (phew, lot of them!)

So for example for IrrlichtDevice, you would need to do (in PowerBasic):


CLASS CIrrlichtDevice
INSTANCE pDevice AS DWORD

INTERFACE IIrrlichtDevice
INHERIT IUnknown

METHOD CreateDevice(BYVAL deviceType AS LONG, BYVAL windowSize AS DWORD, BYVAL BITS AS WORD, BYVAL fullscreen AS BYTE, BYVAL stencilbuffer AS BYTE, BYVAL vsync AS BYTE, BYVAL receiver AS DWORD)
pDevice = IrrlichtDevice_createDevice(deviceType, windowSize, bits, fullscreen, stencilbuffer, vsync, receiver)
END METHOD

METHOD Run()
IrrlichtDevice_run(pDevice) ' -- This we have stored thanks to previous CreateDevice method call
END METHOD

' .. and so on, wrapper for everything

END INTERFACE
END CLASS


Then you would need to create the ThinBASIC wrappers for methods:


' -- pClass identifies the class as such in later code (when adding methods)
LOCAL pClass AS LONG
pClass = thinBasic_Class_Add("IrrlichtDevice", 0)

' -- If the creation of class succeeded, we can proceed adding methods
IF pClass THEN
' -- _Create method is used for constructor
thinBasic_Class_AddMethod(pClass, "_Create" , %thinBasic_ReturnNone , CODEPTR(keyword_IrrlichtDevice_Create))

' -- _Destroy method is used for destructor
thinBasic_Class_AddMethod(pClass, "_Destroy" , %thinBasic_ReturnNone , CODEPTR(keyword_IrrlichtDevice_Destroy))

' -- CreateDevice method
thinBasic_Class_AddMethod(pClass, "CreateDevice" , %thinBasic_ReturnNone , CODEPTR(keyword_IrrlichtDevice_CreateDevice))

' -- Run method
thinBasic_Class_AddMethod(pClass, "Run" , %thinBasic_ReturnNone , CODEPTR(keyword_IrrlichtDevice_Run))

' -- and so on for others
END IF


and of course create the wrapper functions which interact with parser



SUB keyword_IrrlichtDevice_Create(BYVAL pInstance AS LONG)

' -- Instantiate object
REDIM obj(1 to 1) AS IIrrlichtDevice AT pInstance
obj(1) = CLASS "CIrrlichtDevice"

END SUB

SUB keyword_IrrlichtDevice_CreateDevice(BYVAL pInstance AS LONG)
LOCAL deviceType, windowSize, bits, fullscreen, stencilbuffer, vsync, receiver AS EXT

' -- Get reference to object
REDIM obj(1 to 1) AS IIrrlichtDevice AT pInstance

' -- Get params for it from parser
thinBasic_Parse7Numbers(deviceType, windowSize, bits, fullscreen, stencilbuffer, vsync, receiver)

' -- Call the method
obj(1).CreateDevice(deviceType, windowSize, bits, fullscreen, stencilbuffer, vsync, receiver)

END SUB


... and so on


Petr

maxim
07-07-2011, 07:21
Thanks Petr.
If I understand all this code is PowerBasic and PowerBasic is not free?

Petr Schreiber
07-07-2011, 10:59
Hi Maxim,

modules with classes can currently be created using PowerBASIC for Windows 9. This commercial compiler is currently superceded by (also suitable) version 10 ($199), but if we are lucky, they might once provide "Classic" edition of the v9 compiler, which could be at nicer $49.

Modules with keywords, constants and user defined TYPEs can be still easily created using FreeBASIC, C, PowerBASIC and maybe even others - example is in SDK folder.

As the CLASSes development for modules is in initial stage, I am sure Eros can be open to other compilers as well - we would "just" need to find a way which is cross-compiler.
The problem is mostly one - procedures are quite standardised thing, they might differ in calling convention, but that is easy to cover, and that is why SDK for procedural interface is basically no problem in any language which can create DLLs.
But with objects, the situation is different. COM is at least partially something, which you can see implemented in multiple languages (but I have no experience with it in them).

I think I am now reaching the boundaries of my knowledge, I think Eros can tell you more. Maybe if you create COM object in C++ it will work as well?


Petr

ErosOlmi
07-07-2011, 12:39
Hi Maxim.

More info about creating thinBasic modules and their logic can be found in the great TBJ number 2 (thinBasic Journal created by Petr Schreiber): http://www.thinbasic.com/community/showthread.php?9450-ThinBasic-Journal-Issue-2
In that number I wrote an initial article describing what is a module, what is the main logic, how to use SDK functions to create and parse your own keywords.
I created a SDK for PowerBasic and others (especially Charles Pegge) created the interface inclide file for FreeBasic. There is also an interface for C but to be honest it was not much developed because its author (Roberto Bianchi) in not anymore involved in thinBasic project.
You can find all the material plus some examples inside your thinbasic installation directory under \thinBasic\SDK\SDK.ZIP

Just recently (starting from thinBasic 1.8.8.0, currently the last relese) I've introduced the concept of module classes.
Module classes are binary classes defined inside compiled modules and exposed to thinBasic interpreted language thanks to new SDK functions I've developed: thinBasic_Class_Add(...) and thinBasic_Class_AddMethod(...)
Module classes take advance from the fantastic PowerBasic language and its ability to create classes (in EXE or DLL) being used anywhere just knowing a PTR to the internal instantiated binary class.
Unfortunately this is not a compatible way and PB classes cannot be confused with (for example) C++ classes. They are not the same.
As we said, module classes is just very recent and I cannot say now which will be the final direction. I'm mostrly committed with PowerBasic but keeping thinBasic concept very general so in theory a thinBasic module classes can be also a pointer to an UDT (and not a real class) and than the module must know how to threat that PTR.

So at the moment you can create a standard thinBasic module using PowerBasic or FreeBasic.
The biggest example of thinBasic module created with FreeBasic is Oxygen from Charles Pegge. Oxygen is a just in time source code to machine code converted to in memory execution of source code.
Source code of Oxygen can be found here:
http://www.thinbasic.com/community/forumdisplay.php?350-O2h-Compiler
http://www.thinbasic.com/community/showthread.php?9653-Latest-Oxygen-Compiler-Downloads
Maybe you can find ideas in there.

Ciao
Eros

maxim
08-07-2011, 16:08
Hi Petr and Eros.
Thanks for your help. Oxygen is a very interesting project and I will try learning this.

John Spikowski
08-07-2011, 18:22
Hi Petr and Eros.
Thanks for your help. Oxygen is a very interesting project and I will try learning this.

You can find the OxygenBasic open source project site here.

http://www.oxygenbasic.org