Page 1 of 1

Using SDL and OpenGL with wxWidgets to create a game editor

Posted: Wed Mar 11, 2009 1:20 pm
by riruilo
Hi dudes!

I would like to create a level editor for my game, a 2D platform usign opengl and a physics engine written in C, because of this, I must use C++ (Firstly I wanted to use Delphi, cause it is a RAD, but as it was not possible to integrate the physics engine, I discard it).

Questions:
Can I do this with wxWidgets? I guess so.

Is it possible to integrate SDL into wxWidgets? Is it necessary or not? Does using SDL has any advantage?
Can I use all mouse and keyboard events without SDL?

When I simulate a game (motion with physics) should I use a timer or a thread?

Thanks a lot for your time.

Posted: Wed Mar 11, 2009 1:54 pm
by jacmoe
I would just use the OpenGL control, and use wxWidgets input.
Look at the OpenGL sample and go from there.
SDL, AFAIK, will only get in the way.
I Ogre3D with wxWidgets, using wxWidgets input, and it works nicely. :wink:

Posted: Wed Mar 11, 2009 1:59 pm
by computerquip
I recently have been researching the subjects. I think there are a couple of examples floating around that demonstrates that this is possible. I know you can integrate a basic Windows window with OpenGL.

SDL is more of a library that is used for Events, simple OpenGL window management, basic Audio rendering, etc. The reason people would want to integrate the two is too have native widgets integrated into there little program.

http://code.technoplaza.net/wx-sdl/

That is a tutorial based on how.

Also, SDL is very organized with the events and actually extremely easy to integrate.

Also, aren't Timers and Threads two completely different things?

Posted: Thu Mar 12, 2009 6:29 pm
by matthew180
I'm currently writing an emulator and so far using wx's wxGLCanvas class with a wxTimer is working out very well for frame rendering (I need a refresh every 16.6ms).

Basically I merged the wxTimer based "render loop" and OpenGL examples available on the wx wiki. Using OpenGL also gives you the acceleration advantage available with the hardware.

SDL is nice, and I still might use parts of it for the sound, but I won't be using SDL's even loop or input functions since wx has everything I need.

I did initially try a wxThread for my VDP refresh, but I could not get a custom event to work. The VDP thread needed to let the rest of the emulator know when a vertical refresh had taken place and that the frame buffer was updated and should be redrawn (since you are not supposed to draw from any thread other than the main process.) Anyway, I could not get it to work. The wxTimer does work though, very well, and it is in the main process so I don't have to worry about threads, custom messages, or synchronization.

I'm currently planning on using another wxTimer for my main emulator tick as well. Something to keep in mind is the OS quantization time (the time slice your application gets to run), which on most modern GUI OSes is about 10ms. This was a problem for my VDP timer because on the real hardware the frames are drawn 60 times a second (every 16.6ms). You can see that 16.6ms does not divide evenly by 10ms, so when the wxTimer event runs the VDP code, the first thing I do is see how much *actual* time has elapsed since the last frame. If the actual time is over 16.6ms I generate a frame and subtract 16.6 from the actual time to keep as an "error" value that is added to the elapsed time on the next wxTimer event.

It actually works very well. Before adding the error tracking I was off by about 4 seconds (240 frames) after 1 minute of running. With the error tracking, the frame rate is still accurate after 30 minutes (and I got tired of checking it.)

In your case the idle event might work better. In my tests is happened much too often (much faster than the wxTimer set to 10ms) and was just wasting CPU. However, for your simulation where you don't have to worry about matching the CPU clock of a legacy computer system, running "all out" as fast as you can would probably be desirable.

The other option is to have a polling type of event loop (which is what games usually do), but I don't know if wx supports that. Also, you would not want that anyway for your editor.

Hope this helps somehow.

Matthew

Posted: Sat Mar 14, 2009 11:12 am
by riruilo
matthew180 wrote:I'm currently writing an emulator and so far using wx's wxGLCanvas class with a wxTimer is working out very well for frame rendering (I need a refresh every 16.6ms).

Basically I merged the wxTimer based "render loop" and OpenGL examples available on the wx wiki. Using OpenGL also gives you the acceleration advantage available with the hardware.

SDL is nice, and I still might use parts of it for the sound, but I won't be using SDL's even loop or input functions since wx has everything I need.

I did initially try a wxThread for my VDP refresh, but I could not get a custom event to work. The VDP thread needed to let the rest of the emulator know when a vertical refresh had taken place and that the frame buffer was updated and should be redrawn (since you are not supposed to draw from any thread other than the main process.) Anyway, I could not get it to work. The wxTimer does work though, very well, and it is in the main process so I don't have to worry about threads, custom messages, or synchronization.

I'm currently planning on using another wxTimer for my main emulator tick as well. Something to keep in mind is the OS quantization time (the time slice your application gets to run), which on most modern GUI OSes is about 10ms. This was a problem for my VDP timer because on the real hardware the frames are drawn 60 times a second (every 16.6ms). You can see that 16.6ms does not divide evenly by 10ms, so when the wxTimer event runs the VDP code, the first thing I do is see how much *actual* time has elapsed since the last frame. If the actual time is over 16.6ms I generate a frame and subtract 16.6 from the actual time to keep as an "error" value that is added to the elapsed time on the next wxTimer event.

It actually works very well. Before adding the error tracking I was off by about 4 seconds (240 frames) after 1 minute of running. With the error tracking, the frame rate is still accurate after 30 minutes (and I got tired of checking it.)

In your case the idle event might work better. In my tests is happened much too often (much faster than the wxTimer set to 10ms) and was just wasting CPU. However, for your simulation where you don't have to worry about matching the CPU clock of a legacy computer system, running "all out" as fast as you can would probably be desirable.

The other option is to have a polling type of event loop (which is what games usually do), but I don't know if wx supports that. Also, you would not want that anyway for your editor.

Hope this helps somehow.

Matthew

Thanks matthew180 and all. Very helpfull.
I think I'm not going to use SDL, I don't need sound for my level editor and timing and events can be provided by wxwidgets.


@matthew180 or any other: I'm curious, how do you design menus and UI? coding it or do you use any other program?

Thanks a lot.

Posted: Sat Mar 14, 2009 3:00 pm
by Auria
Just to add on what others said, the following articles will help you create an OpenGL canvas, catch events and render in a loop :

http://wiki.wxwidgets.org/Making_a_render_loop
http://wiki.wxwidgets.org/WxGLCanvas#wxGLCanvas_sample

About coding GUIs or using an editor, I guess it's a matter of personal preference :) wxFormBuilder seems quite good, though in general I code my GUIs by hand, the old-school way :)

Posted: Sat Mar 14, 2009 7:04 pm
by riruilo
Auria wrote:Just to add on what others said, the following articles will help you create an OpenGL canvas, catch events and render in a loop :

http://wiki.wxwidgets.org/Making_a_render_loop
http://wiki.wxwidgets.org/WxGLCanvas#wxGLCanvas_sample

About coding GUIs or using an editor, I guess it's a matter of personal preference :) wxFormBuilder seems quite good, though in general I code my GUIs by hand, the old-school way :)
Thanks Auria, that is very usefull.
I would like the easiest way to create my UI, but I don't know if programs like wxFormBuilder are compatible with OpenGL canvas (I'm a wxBeginner). Can I create a UI with menus and buttos and the main area a opengl animation using wxFormBuilder?

By the way, apart from wxWidgets sampls, do you know any other program which uses wx and opengl?

Thanks!

Posted: Sat Mar 14, 2009 11:06 pm
by Auria
About wxFormBuilder + wxGlCanvas, see here : http://forum.wxformbuilder.org/index.ph ... topic=36.0

In short, it's not officially supported, but you can do everything else in wxFB and then add the Gl canvas by hand.

Posted: Sun Mar 15, 2009 3:04 am
by matthew180
riruilo wrote: @matthew180 or any other: I'm curious, how do you design menus and UI? coding it or do you use any other program?

Thanks a lot.
Actually, I have not quite figured it out yet myself. I'm doing the emulator in wx primarily because I'm making a "programmer's" emulator which will allow single stepping, access to all the CPU registers, RAM, the ability to change *ROM*, on the fly assembly, etc., so I need a ton of UI stuff.

The GLcanvas is only for the display and I'm thinking the debug window will be separate from the GLcanvas. I'm still not sure where I'm going to put the menu, toolbar, status bar, and other things. In the same window with the GLcanvas makes sense until you invoke the debug window.

Matthew

Posted: Sun Mar 15, 2009 12:14 pm
by riruilo
Auria wrote:About wxFormBuilder + wxGlCanvas, see here : http://forum.wxformbuilder.org/index.ph ... topic=36.0

In short, it's not officially supported, but you can do everything else in wxFB and then add the Gl canvas by hand.
Interesting.
Bt the way, Is there any other program like wxFormBuilder (I don't care if I have to pay) with better opengl support?
(note: I'm going to use codeblocks)

Thanks a lot, all of you are very helpful for me.

Posted: Sun Mar 15, 2009 3:17 pm
by Auria
riruilo wrote:
Auria wrote:About wxFormBuilder + wxGlCanvas, see here : http://forum.wxformbuilder.org/index.ph ... topic=36.0

In short, it's not officially supported, but you can do everything else in wxFB and then add the Gl canvas by hand.
Interesting.
Bt the way, Is there any other program like wxFormBuilder (I don't care if I have to pay) with better opengl support?
(note: I'm going to use codeblocks)

Thanks a lot, all of you are very helpful for me.
Other GUI builders to check are wxSmith (inside Code::Blocks), wxGlade (somewhat outdated), and DialogBlocks (http://www.dialogblocks.com/, commercial) - i think windows wxDev-C++ includes a GUI builder for wx too.

Posted: Sun Mar 15, 2009 4:34 pm
by computerquip