IrrLicht without wrapper DLL

Everything else that doesn't fall into one of the other PB categories.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

IrrLicht without wrapper DLL

Post by traumatic »

By the first look at the Irrlicht engine I was convinced it was possible
to use the original DLL from PureBasic even without writing a wrapper
DLL beforehand (Hey Dracflamloc!:)).

I tried using interfaces but failed.

Yet there had to be a way...


Here's something I came up with by accident:

Code: Select all

;
;
; "Es werde Licht!" - "Let there be light!" - Irrlicht in PB ?
;
;


;
Procedure.l L(string.s)
  *out = AllocateMemory(lstrlen_(string)*4)
  MultiByteToWideChar_(#CP_ACP, 0, string, -1, *out, lstrlen_(string))
  ProcedureReturn *out
EndProcedure

;
Procedure.l Irr_Device_setWindowCaption(device.l, caption.l)
  !MOV esi, dword [esp]
  !MOV edx, [esi]
  !MOV eax, dword [edx+44]
  !MOV ecx, esi  
  !PUSH dword [esp+4]
  !CALL eax
EndProcedure

;
Procedure.l Irr_Device_isWindowActive(device.l)
  !MOV esi, dword [esp]
  !MOV edx, [esi]
  !MOV eax, dword [edx+48]
  !MOV ecx, esi  
  !CALL eax
  ProcedureReturn
EndProcedure

;
Procedure.l Irr_Device_run(device.l)
  !MOV esi, dword [esp]
  !MOV edx, dword [esi]
  !MOV eax, dword [edx+4]
  !MOV ecx, esi 
  !CALL eax
  ProcedureReturn
EndProcedure

;
Procedure.l Irr_Device_getVideoDriver(device.l)
  !MOV esi, dword [esp]
  !MOV edx, dword [esi]
  !MOV eax, dword [edx+8]
  !MOV ecx, esi 
  !CALL eax
  ProcedureReturn
EndProcedure

;
Procedure.l Irr_Device_getGUIEnvironment(device.l)
  !MOV esi, dword [esp]
  !MOV edx, dword [esi]
  !MOV eax, dword [edx+16]
  !MOV ecx, esi 
  !CALL eax
  ProcedureReturn
EndProcedure

;
Procedure.l Irr_Driver_beginScene(driver.l, backBuffer.l, zBuffer.l, color.l)
  !MOV esi, dword [esp]
  !MOV edx, dword [esi]
  !MOV eax, dword [edx+4]
  !MOV ecx, esi
  !PUSH dword [esp+12]
  !PUSH dword [esp+8]
  !PUSH dword [esp+4]
  !CALL eax
  ProcedureReturn
EndProcedure

;
Procedure.l Irr_Driver_endScene(driver.l)
  !MOV esi, dword [esp]
  !MOV edx, dword [esi]
  !MOV eax, dword [edx+8]
  !MOV ecx, esi
  !CALL eax
  ProcedureReturn
EndProcedure


;
;
;

IrrDLL.l = OpenLibrary(#PB_Any, "Irrlicht.dll")

d.POINT
d\x = 640
d\y = 480
*device = CallFunction(IrrDLL, "createDevice", 2, d, 16, #False, #False, #False, 0)

*driver = Irr_Device_getVideoDriver(*device)
*guienv = Irr_Device_getGUIEnvironment(*device)

Irr_Device_setWindowCaption(*device, L("Wello Horld!"))


;
While Irr_Device_run(*device)
  If Irr_Device_isWindowActive(*device)
    Irr_Driver_beginScene(*driver, 0, 0, $00000070)
    ;
    ; render something here
    ;
    Irr_Driver_endScene(*driver)
  EndIf
  
  Sleep_(10)
Wend


CloseLibrary(IrrDLL)
End
Maybe someone will find this useful, I have no real clue about
what I'm doing here - it just works... ;)

I'd be glad if someone could tell me what has to be done in order
to make it work with PB's interfaces. Thanks in advance.
Good programmers don't comment their code. It was hard to write, should be hard to read.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

You couldn't get interfaces to work? Hm... thats sad I was gonna try that...
dige
Addict
Addict
Posts: 1432
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Nice work, but only a blank screen and a window title? ;-)
I believe the master of 3D can do much more, isn't it?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

dracflamloc wrote:You couldn't get interfaces to work? Hm... thats sad I was gonna try that...
Well, I still don't know what I did wrong.
I must have interpreted the classes wrong or something.
The only way I got this working was how I showed above.
Everything else just crashed.

It would be cool if someone else could take a look this.
Accessing Irrlicht directly from PB without any wrapper-DLL
would be the best way to work with that engine IMHO.

Maybe a good chance for a real community project?

dige wrote:I believe the master of 3D can do much more, isn't it?
Master of 3D? Who's that?
In case you're refering to me, thanks for the flattery :D

lol, it's no fun to code like this. ;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

PB pushes the Interface pointer when calling methods, whilst this dll, from what I see in your code, puts that pointer in ecx instead. So, when returning, the stack is unbalanced by 4. Also, the called method will be using the wrong arguments.

I suggest that you recompile this dll, if it's open source, using PB scheme. Or wait for PB to handle macros.
El_Choni
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Thanks for the explanation, El_Choni!
Recompiling the DLL however is no real option IMHO.
Good programmers don't comment their code. It was hard to write, should be hard to read.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

You can solve this problem using Fasm macros right now. I'll have a look to see if I can get the proper macro code.
El_Choni
Fred
Administrator
Administrator
Posts: 18553
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

That's a strange convention to use ecx as object handler (not bad, but why doing something different than COM ?). May be it's possible to switch a compiler option when building the DLL to generate COM interfaces ?
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

I don't know how they translate to asm but could it be it's because
of the heavy usage of namespaces?
Good programmers don't comment their code. It was hard to write, should be hard to read.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

The code with PB Interfaces would look like this (as you may already have tried):

NOTE: this code won't work as it is.

Code: Select all

XIncludeFile "Macros.pb"

Interface IrrlichtDevice
  IrrlichtDevice()
  run()
  getVideoDriver()
  getFileSystem()
  getGUIEnvironment()
  getSceneManager()
  getCursorControl()
  getLogger()
  getVideoModeList()
  getOSOperator()
  getTimer()
  setWindowCaption(text)
  isWindowActive()
  closeDevice()
  getVersion()
  setEventReceiver(receiver)
  getEventReceiver()
  setResizeAble(resize)
EndInterface

Interface IVideoDriver
  IVideoDriver()
  beginScene(backBuffer, zBuffer, color)
  endScene()
EndInterface ; We don't need the rest of the members in this example

Procedure.l L(string.s)
  *out = AllocateMemory(lstrlen_(string)*4)
  MultiByteToWideChar_(#CP_ACP, 0, string, -1, *out, lstrlen_(string))
  ProcedureReturn *out
EndProcedure

IrrDLL.l = OpenLibrary(#PB_Any, "Irrlicht.dll")
If IrrDLL
  d.POINT
  d\x = 640
  d\y = 480
  *device.IrrlichtDevice = CallFunction(IrrDLL, "createDevice", 2, d, 16, #False, #False, #False, 0)
  If *device
    *driver.IVideoDriver = *device\getVideoDriver()
    *guienv = *device\getGUIEnvironment()
    *device\setWindowCaption(L("Wello Horld!"))
    If *driver
      While *device\run()
        If *device\isWindowActive()
          *driver\beginScene(1, 0, $00000070)
          ;
          ; render something here
          ;
          *driver\endScene()
        EndIf
        Sleep_(10)
      Wend
    EndIf
  EndIf
  CloseLibrary(IrrDLL)
EndIf
End 
Purebasic generates this ASM for the methid calls:

Code: Select all

  PUSH   eax
  MOV    eax, [eax]
  CALL   dword [eax+?] ; method offset
So you need a Fasm macro that translates every ocurrence of the above to:

Code: Select all

  MOV   ecx, eax
  MOV    eax, [eax]
  CALL   dword [eax+?] ; method offset
I know this can be done with Fasm macros, but I don't know how.
Last edited by El_Choni on Tue May 17, 2005 7:07 pm, edited 1 time in total.
El_Choni
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

El_Choni: Yes, that looks exactly like my interface approach ;)

I don't understand what you mean by utlizing macros as I don't see how
that could change the way PB generates its asm-output. Are you thinking
about a macro simulating the actual interface behaviour (but using different
calls) ?


Thanks for looking into this. It's not that I personally can't live without
Irrlicht but IMHO it would be good to prove it's possible to have it working
straight from PB.

Your help is very much appreciated, thank you!
Good programmers don't comment their code. It was hard to write, should be hard to read.
Fred
Administrator
Administrator
Posts: 18553
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It should be possible to do a quick ASM post processor to detect this pattern and change it, right (in place) ?
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

@traumatic: the PB compiler generates ASM output that is fed into Fasm.exe. If you insert a Fasm macro escaped with '!', Fasm will apply it.

Fasm does several passes when needed, and you can change the ASM source using Fasm macros.

Anyway, as Fred says, this would be very easy to handle for the PB compiler, so better wait to see if Fred implements it, if there's not a hurry (doesn't seem so ;).
El_Choni
Fred
Administrator
Administrator
Posts: 18553
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I probably won't implement it in PB as it seems to be only for this purpose..
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Thanks El_Choni! I works the way you suggested (of course ;)).

Image
Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply