Using FLTK and SDL together

621 views
Skip to first unread message

Martin McDonough

unread,
Jun 29, 2015, 12:07:29 AM6/29/15
to fltkg...@googlegroups.com
I'm working on an editor for a game that uses SDL. I really want to use an SDL window and an FLTK window at the same time, but this raises an issue. Much like FLTK, I can only check for window events in SDL on the main thread. My current method to run both windows at once is to use Fl::wait and Fl::add_timeout to call the SDL_PollEvent 60 times a second, which is non-blocking.

This works fine, but it seems really inelegant to me. I realize I am trying to use two systems at the same time that are intended to do exactly the thing, but I was curious if it's possible in any way to basically add a checking function to Fl::wait, so that it will return immediately whenever that function returns true/1. Or is there another, better, way this kind of thing can be done?

Greg Ercolano

unread,
Jun 29, 2015, 12:43:41 AM6/29/15
to fltkg...@googlegroups.com
On 06/28/15 21:07, Martin McDonough wrote:
> I'm working on an editor for a game that uses SDL. I really want to use an SDL window and an FLTK window at the same time, but this raises an issue. Much like FLTK, I can only check for window events in SDL on the main thread. My current method to run both windows at once is to use Fl::wait and Fl::add_timeout to call the SDL_PollEvent 60 times a second, which is non-blocking.
>
> This works fine, but it seems really inelegant to me. I realize I am trying to use two systems at the same time that are intended to do exactly the thing, but I was curious if it's possible in any way to basically add a checking function to Fl::wait, so that it will return immediately whenever that function returns true/1. Or is there another, better, way this kind of thing can be done?

Normally when I see a situation of competing GUI toolkits
I split the app into separate processes and use TCP or pipes
or some form of IPC between the two processes.

It's usually a big unknown when two libraries fight over
who gets what events within the same process.

There may be other solutions, but that's what I usually try
to do first, just to avoid any unknown or potentially unknown behavior.

Albrecht Schlosser

unread,
Jun 29, 2015, 6:57:16 AM6/29/15
to fltkg...@googlegroups.com
I can't tell if this is useful for your issue, but according to your
question Fl::add_idle() may be what you want.

<http://www.fltk.org/doc-1.3/classFl.html#a4e7a1b5d18c2f073238a8466a62a3e7a>

I assume that if both functions (your idle function and Fl::wait())
return immediately if there is no event to serve, this will maybe result
in 100% cpu usage when the program loops. So you should probably add a
small wait/sleep time within your idle function, or something like that.

imm

unread,
Jun 29, 2015, 7:12:57 AM6/29/15
to general fltk

It might also be worth googling about as this has come up before... Be interesting to see what solutions they came up with!

Might have been Fl_SDL_Window or something like that.

--
Ian
Sent, perhaps surprisingly from my Fairphone FP1

Georg Potthast

unread,
Jun 30, 2015, 12:29:24 PM6/30/15
to fltkg...@googlegroups.com
Ian did send me two old examples in the past that combine SDL and FLTK in one program. I attach them now.

After getting these to work I decided to use either SDL or FLTK but not both in one program.

Georg

sdl_fltk-demo-0.0.2.tar.gz
sld-fltk-multiwin-0.0.1.zip

Martin McDonough

unread,
Jun 30, 2015, 12:38:11 PM6/30/15
to fltkg...@googlegroups.com
What I've got works acceptably, but I will certainly take a look at how these work. I doubt I have the best solution.

I mainly want to use FLTK and SDL at once because doing arbitrary drawing, particularly scaled images, is cumbersome in FLTK compared with SDL. Additionally, since the game already uses SDL, I can use almost the exact same code to draw certain scenes and images in both the game and the editor this way.

Manolo Gouy

unread,
Jun 30, 2015, 5:36:37 PM6/30/15
to fltkg...@googlegroups.com

> Le 30 juin 2015 à 13:38, Martin McDonough wrote :
>
> What I've got works acceptably, but I will certainly take a look at how these work. I doubt I have the best solution.
>
> I mainly want to use FLTK and SDL at once because doing arbitrary drawing, particularly scaled images, is cumbersome in FLTK compared with SDL.
Please, note that scaled image drawing has been recently introduced in FLTK and will appear in version 1.3.4
that will be released soon (see Fl_Shared_Image::scale()).


> Additionally, since the game already uses SDL, I can use almost the exact same code to draw certain scenes and images in both the game and the editor this way.
>
>
> Georg

Manolo Gouy

unread,
Jun 30, 2015, 5:36:48 PM6/30/15
to fltkg...@googlegroups.com

> Le 30 juin 2015 à 13:38, Martin McDonough wrote :
>
> What I've got works acceptably, but I will certainly take a look at how these work. I doubt I have the best solution.
>
> I mainly want to use FLTK and SDL at once because doing arbitrary drawing, particularly scaled images, is cumbersome in FLTK compared with SDL.
Please, note that scaled image drawing has been recently introduced in FLTK and will appear in version 1.3.4
that will be released soon (see Fl_Shared_Image::scale()).


> Additionally, since the game already uses SDL, I can use almost the exact same code to draw certain scenes and images in both the game and the editor this way.
>
>
> Georg

Martin McDonough

unread,
Jun 30, 2015, 6:26:41 PM6/30/15
to fltkg...@googlegroups.com, manol...@univ-lyon1.fr
>Please, note that scaled image drawing has been recently introduced in FLTK and will appear in version 1.3.4
> that will be released soon (see Fl_Shared_Image::scale()).

That's good to hear, I have several projects that could benefit from it!

I still will likely use SDL and fltk together for this project, though, since I also will need to use OpenGL much newer than what fltk supplies (at least 3.3) to render scenes. Of course, I would love to see fltk get support for making newer OpenGL contexts, too, but I understand that's a much bigger problem since you certainly cannot trust all machines to have much mor ethan 1.3/2.0.

Alvin Beach

unread,
Jul 1, 2015, 9:45:31 AM7/1/15
to fltkg...@googlegroups.com


On Monday, June 29, 2015 at 1:07:29 AM UTC-3, Martin McDonough wrote:
I'm working on an editor for a game that uses SDL. I really want to use an SDL window and an FLTK window at the same time, ...

Hello,

I have been able to embedded a SDL2 SDL_Window+SDL_Renderer inside a FLTK Window. Not sure if this is what you are looking to do.

As for the events, FLTK receives all the mouse events, even those that are on top of the SDL_Window. I haven't tested keyboard events.

The stock SDL2 from Mercurial needs to be patch though. If you are interested, see bug 2500 (https://bugzilla.libsdl.org/show_bug.cgi?id=2500). The patch is https://bugzilla-attachments.libsdl.org/attachment.cgi?id=1794 and I've been using it for sometime now without any noticeable side effects.

Cheers,

Alvin
Reply all
Reply to author
Forward
0 new messages