Making a GUI system in SDL

Hello everyone!

My system works like this:

Control (base class)
– GUI (control container)
– Button (sample control)
– …other controls

And the event system works like this (in this case we’ll use mouse
clicks): the main application calls the “OnClick()” method in the GUI
class with the mouse pointer’s position as a parameter. The GUI class
then sees which control was clicked (based on the mouse’s position), and
calls the “OnClick()” method in that control (in this case we’ll use a
button). But here’s the problem; how can this control (button) class let
the main application know that it was clicked?

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

Any ideas? Thanks!

An often used solution to this problem (which applies generally to all
sorts of callbacks) is to define the callback function as

OnClickEvent(Application *data){
data->OnClickEvent();
}

And have it as a static method, or a function outside of a class (C-
style), but inside the function you call the instance method on the
supplied object.

In general(e.g. in a library), the data would be a void* and you could
cast it down to whatever class you want to call a method on.

Cheers

  • IainOn 11 Feb 2008, at 16:39, L-28C wrote:

Hello everyone!

My system works like this:

Control (base class)
– GUI (control container)
– Button (sample control)
– …other controls

And the event system works like this (in this case we’ll use mouse
clicks): the main application calls the “OnClick()” method in the GUI
class with the mouse pointer’s position as a parameter. The GUI class
then sees which control was clicked (based on the mouse’s position),
and
calls the “OnClick()” method in that control (in this case we’ll use a
button). But here’s the problem; how can this control (button) class
let
the main application know that it was clicked?

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the
button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

Any ideas? Thanks!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the
button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

C++ does.

Look here:
http://www.newty.de/fpt/fpt.html

But you need also to give the pointer to the instance of course.Am 11.02.2008 um 18:39 schrieb L-28C:

Most solutions to this problem I’m aware of use a Callback class which
you subclass. (There has GOT to be a better portable way!)
-:sigma.SBOn Feb 11, 2008 9:39 AM, L-28C wrote:

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

There’s an example of doing nearly exactly what you want using boost::bind
and boost::function here:

http://www.boost.org/libs/bind/bind.html#with_boost_function

Might upset old C fogeys on this list, but give it a try if you’re looking
for a more modern way to do callbacks :wink:

GregoryOn Mon, 11 Feb 2008, L-28C wrote:

L-28C wrote:

And the event system works like this (in this case we’ll use mouse
clicks): the main application calls the “OnClick()” method in the GUI
class with the mouse pointer’s position as a parameter. The GUI class
then sees which control was clicked (based on the mouse’s position), and
calls the “OnClick()” method in that control (in this case we’ll use a
button). But here’s the problem; how can this control (button) class let
the main application know that it was clicked?

What I do in my GUI right now is send a message object in response to an event
(like a mouse click). It’s sent as a base class reference that stores the
message type, and is dynamically cast to the appropriate class type when
processed. The message is sent to the widget’s parent, and recursively ends up
getting sent all the way to the point where it gets processed (yes, a bit of
stack overhead there).

The nice thing about this is that it doesn’t require any callbacks. The not so
nice things are that it requires dynamic casting, and the potentially large
stack size due to recursive sending (for heavily nested widgets). I have some
other ideas that I’ll be trying now instead of this, but it’s definitely one of
the ways you can do it.

On Behalf Of L-28C

But here’s the problem; how can this control (button) class let
the main application know that it was clicked?

Some good answers posted already, but if you don’t need the main app to handle the click immediately, you could also use these:

SDL_PushEvent: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fPushEvent
SDL_UserEvent: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fUserEvent

Then just handle your button action event in your regular event loop.> -----Original Message-----

Subject: [SDL] Making a GUI system in SDL


Jukka-Pekka Manninen
?

you can avoid any form of callback troubles of you go the route of

“immediate mode gui” - IMGUI

http://sol.gfxile.net/imgui/

https://mollyrocket.com/forums/viewforum.php?f=10

on molly rocket, there is also a video tutorial.

imguis are the way to go if you want a quick solution and if you want to save a
lot of time.

but if you are hardcore and have lots of time to waste, just go on and design
"yet another gui system".

if not, you might also rather use guichan, crazy eddies gui , agar , fltk, …
just browse this mailing list a bit.

L-28C wrote:> Hello everyone!

My system works like this:

Control (base class)
– GUI (control container)
– Button (sample control)
– …other controls

And the event system works like this (in this case we’ll use mouse
clicks): the main application calls the “OnClick()” method in the GUI
class with the mouse pointer’s position as a parameter. The GUI class
then sees which control was clicked (based on the mouse’s position), and
calls the “OnClick()” method in that control (in this case we’ll use a
button). But here’s the problem; how can this control (button) class let
the main application know that it was clicked?

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

Any ideas? Thanks!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks all! I’ll be using the first method for now. =)

Andre Krause wrote:> you can avoid any form of callback troubles of you go the route of

“immediate mode gui” - IMGUI

http://sol.gfxile.net/imgui/

https://mollyrocket.com/forums/viewforum.php?f=10

on molly rocket, there is also a video tutorial.

imguis are the way to go if you want a quick solution and if you want to save a
lot of time.

but if you are hardcore and have lots of time to waste, just go on and design
"yet another gui system".

if not, you might also rather use guichan, crazy eddies gui , agar , fltk, …
just browse this mailing list a bit.

L-28C wrote:

Hello everyone!

My system works like this:

Control (base class)
– GUI (control container)
– Button (sample control)
– …other controls

And the event system works like this (in this case we’ll use mouse
clicks): the main application calls the “OnClick()” method in the GUI
class with the mouse pointer’s position as a parameter. The GUI class
then sees which control was clicked (based on the mouse’s position), and
calls the “OnClick()” method in that control (in this case we’ll use a
button). But here’s the problem; how can this control (button) class let
the main application know that it was clicked?

I tried to place a function pointer called “m_pfnOnClickEvent” and let
the main application set a function to it when it creates the button…
but C++ doesn’t let you put member methods onto function pointers, and
my program is 100% object-oriented.

Any ideas? Thanks!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org