Using SDL and OpenGL with wxWidgets to create a game editor Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Using SDL and OpenGL with wxWidgets to create a game editor

Post 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.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
jacmoe
Experienced Solver
Experienced Solver
Posts: 64
Joined: Sun Feb 22, 2009 3:34 pm
Location: Denmark
Contact:

Post 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:
computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post 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?
matthew180
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Jul 31, 2007 5:01 pm
Contact:

Post 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
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post 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.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post 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 :)
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post 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!
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post 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.
matthew180
In need of some credit
In need of some credit
Posts: 9
Joined: Tue Jul 31, 2007 5:01 pm
Contact:

Post 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
riruilo
Knows some wx things
Knows some wx things
Posts: 48
Joined: Sat Jul 14, 2007 10:38 pm

Post 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.
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post 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.
computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip »

Post Reply