Jump to content
  • Advertisement
Sign in to follow this  
Lodeman

OpenGL Advice on creating a dll from C++ project and use it in C# winForms project

This topic is 2105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All,

 

Straight to the point: I have an OpenGL C++ application which features things like a scene graph, scene nodes, some custom made GUI widgets that allow the user to place scene nodes and adjust their properties. However, using this custom GUI doesn't seem like the ideal way to develop an editor in the quickest way possible. It's rather cumbersome upon adding in new features. Ideally I'd like to use Windows Forms to quickly prototype new editor functions.

 

I'm currently wondering how I would best somehow wrap up this C++ code into perhaps a .dll, and then reference this in a C# project using Windows Forms, I am also wondering what requirements I need to keep in mind to make this migration as smooth as possible.

 

The main question I'm having most trouble with at the moment is how to embed my scene rendering into a windows form. Is such a thing possible, or would I need to create my window by calling some of the C++ code?

 

Any advice or helpful tips would be greatly appreciated!

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Assuming your rendering code is decoupled from the window, i.e. it simply takes a handle to the window, you can retrieve window handles from WinForms.

Share this post


Link to post
Share on other sites

Thank you for your reply!

 

So I could essentially do something such as:

1) Create a new WinForms "main" form, retrieve its window handle

2) Using my C++ .dll (which I have yet to create, that is also new for me), set the "target window handle" to that which I just retrieved in my WinForms app, then create the OpenGL context in this window

3) Call the .dll's main render code and do my thing

 

 

And then to adjust my properties, I'd create things like buttons, textboxes, and call the .dll's scene editing functions?

This is just to get a bit of an overview before jumping into this.

 

Cheers,

Share this post


Link to post
Share on other sites

Have a look at CsGL http://csgl.sourceforge.net/ I used this a couple years ago for our game, where the simulation codes is written in C++.  The codes were use for both the game and on the tool side.  On the tool side, it was exported to DLLs, and our C# tool would import it.  The tool rendering were done on the tool side with CsGL.

Edited by nhatkthanh

Share this post


Link to post
Share on other sites

I have made something similar for my game editor, here how it goes : 

 

I created a custom panel class derived from the existing panel control, that allow me to override function like the OnPaint function. I pass the panel handle to a native function in my C DLL that do all the opengl setup stuff.

 

In my panel OnPaint function, I add a call to my native render function, which draw one frame each time it's called. Now, a simple call to myPanel.Invalidate() will automatically re-render the game scene on the panel.

 

If I need to have a steady 60fps directly in editor,  I call myPanel.Invalidate() in  the Application_Idle event of the mainform. I manage the 60fps limit directly into the native render function.

 

About the wrapping, in the C# side, I declare each native functions like this : 

[source]

[DllImport(enginePath, CallingConvention = CallingConvention.Cdecl)]

public static extern void editor_draw_frame();

[/source]

 

don't forget to export properly your functions in your C++ DLL, otherwise .NET won't find them. (a mistake I made at first smile.png )

Edited by dlan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!