May 27, 2019, 01:24:12 AM

Author Topic: Duplicate type names in different modules  (Read 2732 times)

Offline col

  • Sr. Member
  • ****
  • Posts: 416
Duplicate type names in different modules
« on: October 20, 2017, 12:44:12 PM »
Is there a module scope resolution syntax for ng to identify types with a duplicate name which are in different modules.?

Something like

Code: [Select]
SuperStrict

Import sdl.glsdlmax2d
Local driver:GLMax2DDriver = [...]
[...]

if SDL.GLSDLMax2D.TGLMax2DDriver(driver) then
if BRL.GLMax2D.TGLMax2DDriver(driver) then

Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 382
Re: Duplicate type names in different modules
« Reply #1 on: October 20, 2017, 01:38:58 PM »
problem here is that driver is defined in several modules.  You will have to use scope resolution to choose the correct driver, but in order to do that, you need to know which driver is being used.

Fortunately, Max2D has a function to return the correct driver to your program.  Then your scope resolution will work
Code: BlitzMax
  1. SuperStrict
  2. SetGraphicsDriver GLMax2DDriver()
  3. 'SetGraphicsDriver D3D9Max2DDriver()
  4. 'SetGraphicsDriver D3D7Max2DDriver()
  5.  
  6. Local driver:TGraphicsDriver = GetGraphicsDriver()
  7. If brl.GLMax2D.TGLMax2DDriver(driver)
  8.         Print "Is TGLMax2DDriver"
  9. Else If brl.d3d9max2d.TD3D9Max2DDriver(driver)
  10.         Print "Is TD3D9Max2DDriver"
  11. Else
  12.         Print "Is Something Else"
  13. End If
  14.  
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #2 on: October 20, 2017, 01:44:53 PM »
I think it would be better to have a generic "GetIdentifier()" function in these modules.
Why? For now you need to import all modules to be able to access "brl.GLMax2D.TGLMax2DDriver(driver)" (and the likes).

Would be better to check versus a string (I know, this is less "strict" then as it allows for spelling errors and is error prone when a driver gets renamed).


PS: It is good to see on what you _might_ work currently... I smell R2T-NG-support :-). Glad I fiddle with r2t currently (just thought about continuing my halloween-project)

bye
Ron


Offline col

  • Sr. Member
  • ****
  • Posts: 416
Re: Duplicate type names in different modules
« Reply #3 on: October 20, 2017, 02:05:43 PM »
Quote
problem here is that driver is defined in several modules.
Uh huh, that is the problem, except your example doesn't show that  ;)

Quote
You will have to use scope resolution to choose the correct driver, but in order to do that, you need to know which driver is being used
Yes, I do know exactly which modules that they are in, and their expected scope(s).


To use your example to demonstrate the issue... What if TGraphicsDriver is defined in 2 modules?

Quote
I smell R2T-NG-support :-)
;D

There already is the
Quote
GetGraphicsDriver().tostring()
command. I can use that I guess, but like you say it's not as robust. Lets hope that names don't change hehe [EDIT] Or be duplicates ::) Which they are in the SDL drivers  ::)
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #4 on: October 20, 2017, 02:19:57 PM »
What you could do is - at least in this scenario:


Code: BlitzMax
  1. ?win32
  2.   import "myfile_win32.bmx"
  3. ?not win32
  4.   import "myfile_generic.bmx"
  5. ?
  6.  
Within that files you can do more OS-specific things. So non-win32 will not have DirectX but might have OpenGL.

Also you could add _more_ conditionals in that files again. This keeps it compatible with vanilla (which does not allow "complex conditionals" while NG also allows for "?win32 or (linux and not android)").


In this specific case you could have a "myfile_vanilla.bmx" and a "myfile_ng.bmx" which might then individually handle OSses again.


bye
Ron

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #5 on: October 20, 2017, 02:24:44 PM »
Regarding duplicates:

what about using "reflection" to retrieve the classname?

Code: BlitzMax
  1. local name:string = TTypeID.ForObject( GetGraphicsDriver() ).name()
  2.  

edit: ah, ok, they share that name - maybe it would be good to rename the sdl one then.

edit2: isn't the SDL one called TGL2Max2DDriver compared to the TGLMax2DDriver of vanilla?

bye
Ron

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #6 on: October 20, 2017, 02:39:56 PM »
Something untested:

Code: BlitzMax
  1.  
  2. Type TMyType
  3.    field runDefaultEngineHandler(driverName:string)
  4.  
  5.    Method runEngineHandler()
  6.       local driverName:string = TTypeID.ForObject( GetGraphicsDriver() ).name().toLower()
  7.  
  8.       select driverName
  9.          'normally you would do the following case-statement in an extra file,
  10.          'so you could then do vanilla-compatible checks like
  11.          '?android
  12.          '?raspberry
  13.          '...
  14.          'in that file
  15.          case "TGLMax2DDriver".ToLower()
  16.             '
  17.          ?bmxng
  18.          case "TGL2Max2DDriver".ToLower()
  19.             '
  20.          ?win32
  21.          case "TD3D7Max2DDriver".ToLower()
  22.             '
  23.          case "TD3D9Max2DDriver".ToLower()
  24.             '
  25.          ?
  26.          default
  27.             if runDefaultEngineHandler
  28.                runDefaultEngineHandler(driverName)
  29.             endif
  30.       end select
  31.    End Method
  32. End Type
  33.  
  34.  
  35. 'when adding a new engine the engine must handle the "handling"
  36. 'on its own by hooking into and doing whatever is needed then
  37. '
  38. 'of course one should store the function hooks for each "registered" engine
  39. 'so better use a TMAP with "enginename"=>functionWrapper
  40. 'functionWrapper is a simple type containing the function hook to call
  41. 'on execution
  42.  

Hooks are there to allow for 3rd-party integration (X amount of engines which handle these things on their own - while you only prepared the "known" ones).


Another Idea is to do it similar to the "driver()"-approach of max2D (or audio). Means you provide some drivers doing the render2texture and is up to the user to load the driver corresponding to the graphics renderer driver..

As brl.mod and pub.mod are open source I think it should be possible to add render2texture "officially". And then add the new functionality to sdl.mod too ? In that case the "stub"-functions/methods should contain throw("implement function xyz") so new drivers/engines are aware of missing functionality.




bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 416
Re: Duplicate type names in different modules
« Reply #7 on: October 20, 2017, 02:56:17 PM »
Easiest is best :p

For the time being I changed the names in the SDL drivers, so now I have

[From BRL modules]
DirectX7
DirectX9
OpenGL

[From SDL modules]
SDLDirectX9
SDLOpenGL
SDLOpenGL2

etc.
If it all works out I'll submit a pull to the repo when I've finished what I'm doing.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #8 on: October 20, 2017, 03:01:34 PM »
Just a personal reminder (to myself):

If Brucey kept names that way by intention:
- add "GetFamily:string()" and "GetFamilyVersion:string()" to the basic driver
- directx returns "directx" and "7" (or "9" or "11")
- opengl returns "opengl" and "" (or "sdl")
...
That way an identification would be still possible (if there is some kind of "auto-replaceability check" is planned).


@ easiest is best
My approach above (with the hooks) is there to avoid dependencies. No need to import Brl.glgraphics if you run directx only...

But of course: first make it run, then you can always optimize things.

bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 416
Re: Duplicate type names in different modules
« Reply #9 on: October 20, 2017, 03:22:27 PM »
I appreciate the ideas :) thanks!!

Yeah getting it run first is priority.
The problems come when needing to instantiate the required types, so those modules will need to be included anyway. I wonder if its possible to write code so that gcc will optimize out types that aren't actually instanciated?

I have duplicate types in places:

TD3D9ImageFrame in brl.d3d9max2d and sdl.d3d9sdlmax2d
TGLImageFrame in brl.glmax2d, sdl.glsdlmax2d and sdl.gl2sdlmax2d.

I've opened a can of worms already haha.
Nothing that some easy renaming won't fix though 8)

Maybe I'll make the originals as base types and extends from them... just in case people are using those 'base' types.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #10 on: October 20, 2017, 03:33:03 PM »
GCC wont optimize out ... why? Because reflection allows for instantiation by "strings": fetch the TTypeID of the corresponding driver class and create a new instance via reflection.




@ base types
Yes, good idea. One can never have enough types - TVTower's game source is full of them (classBase, classSimple, class...)


Also base types allow for easier "stubbing" (creating empty functions which you fill in the real types). Just to avoid "interfaces" (NG only, not available in vanilla).




bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 416
Re: Duplicate type names in different modules
« Reply #11 on: October 22, 2017, 10:33:37 AM »
Just for the record...

I have 'render to texture' working in NG 0.87 for brls d3d9 backend for x86 and x64 :-)
There's a couple of module changes to be made, some of the lower level back end function parameters too.

It was easy to keep to the original architecture going after I'd changed the driver names to unique names
[glsdlmax2d.bmx]TGLMax2DDriver -> TGLSDLMax2DDriver
[gl2sdlmax2d.bmx]TGLMax2DDriver -> TGL2SDLMax2DDriver
etc

SDL drivers are next. I have a feeling some of NGs targets won't support rendering to a texturs? Am I correct?

But it's time for some 'me time'. A relaxing game of golf with some pals and tons of fresh air  :D
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 2091
Re: Duplicate type names in different modules
« Reply #12 on: October 22, 2017, 02:18:19 PM »
Which targets should fail? Graphic "engines/drivers" are (at least I think so):

  • directx 7,9,11 (Windows)
  • opengl (Windows, Linux, Raspberry PI)
  • opengl es (Android, IOS)
  • webgl
So the only special handling seems to be needed for webgl: "WebGL Lesson 16 – rendering to textures"
http://learningwebgl.com/blog/?p=1786


For me this sounds "possible" and as long as emscripten is not working properly with NG (the issue about "too many setjmps" is still open) the webgl-support is not that needed.


@ golf
Have fun outside with the buddies.


bye
Ron

Offline col

  • Sr. Member
  • ****
  • Posts: 416
Re: Duplicate type names in different modules
« Reply #13 on: October 22, 2017, 06:59:47 PM »
I was mainly thinking of the web based stuff, but it looks like thats covered too nowadays.
Thanks for the link!!
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline GW

  • Full Member
  • ***
  • Posts: 157
Re: Duplicate type names in different modules
« Reply #14 on: October 23, 2017, 08:24:47 AM »
Just for the record...

I have 'render to texture' working in NG 0.87 for brls d3d9 backend for x86 and x64 :-)
There's a couple of module changes to be made, some of the lower level back end function parameters too.


I put up a DX9 render to texture class a while back in the archives.  Maybe it's helpful, should be NG comapatable.
https://www.syntaxbomb.com/index.php/topic,559.0.html