Mailing lists / fpc-pascal / 2007-11
Linux : Using SDL with GTK2 ?
6 messages · 4 people · –
- AAndrew Haines 2
- TT.Guilleminot 2
- DDarius Blaszyk 1
- TTom 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Tom wrote 12 lines
uses sdl, gtk, gdk, X;
var
SDLWindowHack: String
X11Window: X.TWindow;
begin
...
// this is for gtk1 only. gtk2 is similar but different
X11Window :=
GDK_WINDOW_XWINDOW(PGdkWindowPrivate(GTK_WIDGET(APanel.Handle)^.window ) );
SDLWindowHack := 'SDL_WINDOWID='+ IntToStr(PtrUint(X11Window));
putenv (PChar(SDLWindowhack));
end;You have to for sure set the environment variable SDL_WINDOWID before you create the window with sdl and maybe before sdl_init.
IIRC using this method somehow makes sdl not listen for keyboard/mouse events because it is using a window you have made and expects you to already be processing those events in some way.
The other way to embed a window is to create the window normally with sdl without setting SDL_WINDOWID and then accessing the PSurface record get the X11 window ID and use XReparentWindow to embed it into your app. unfortunatly the window is created outside your app and is briefly visible before you have stuck it inside your app. Sdl will behave as if the window is not embedded in your app. Setting keyboard focus and mouse focus in the window may have quirks.
Overall I think the first method is best.
Setting the SDL_WINDOWID works on windows too if you set it to the (hwnd)TWinControl.Handle of whatever component you want sdl to draw to.
Andrew
Thanks for your answer. I'm a newbie so I still have a few questions :o) :
1. I'm unsure of chronology of things to do in the program body. Is the following correct :
a) Create GTK stuff
b) SDL Hack/SDL_WINDOWID
c) SDL_INIT
d) Show GTK widgets
2. You mentioned "this is for gtk1 only. gtk2 is similar but different". What to do for GTK2 ?
3. Which kind of GTK Object "APanel" can be ?
4. FPC complains on "handle". What's wrong ?
Free Pascal Compiler version 2.2.0 [2007/08/30] for i386 Target OS: Linux for i386
Compiling test.pas
test.pas(45,77) Error: Illegal qualifier
test.pas(45,77) Fatal: Syntax error, ")" expected but "identifier HANDLE" found
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
Thanks Again.
Tom
Andrew Haines wrote 56 lines
T.Guilleminot wrote 7 lines
Yes that's right. With the only exception being that you have to use gtk_widget_realize on whatever gtk widget you are using for sdl so that the x11 window will be allocated.
2. You mentioned "this is for gtk1 only. gtk2 is similar but different". What to do for GTK2 ?
I've attached a simple program that will compile for gtk1 or gtk2. you can change a define at the top of the source to switch between gtk 1 or 2.
3. Which kind of GTK Object "APanel" can be ? 4. FPC complains on "handle". What's wrong ?
Sorry I thought you were using the LCL and so my example was for that. I attached an example program that doesn't use the LCL.
Thanks Again. Tom
It's worth noting that you cannot have more than one sdl window per application. It's a limitation of sdl.
Andrew
Thanks a LOT Andrew ! Your example is very nice !
Tom
Yes that's right. With the only exception being that you have to use gtk_widget_realize…
Hi Andrew,
I found this mail from you regarding attaching SDL to the used widgetset. But whatever I do I cannot get it to work. The example you attached in the email below works fine, so it has to be possible. So why not when using the LCL?
I hope you don;t mind me bugging you about this, but I've been staring at the code for two days now, and you're my last resort...
The code I've been using is pasted below. If you don't feel like fixing my mess (or simply don't have the time to) just let me know and I'll understand.
function GetX11Window(AWidget: PGtkWidget): X.TWindow;
begin
// first make sure the x11 window is created for the gtk widget
if GTK_WIDGET_REALIZED(AWidget) = False then
gtk_widget_realize(AWidget);
// now retrieve the X window
{$IFDEF GTK2}
Result := GDK_WINDOW_XWINDOW(PGdkDrawAble(AWidget^.window));
{$ENDIF}
{$IFDEF GTK1}
Result := GDK_WINDOW_XWINDOW(PGdkWindowPrivate(AWidget^.window));
{$ENDIF}
end;
procedure SetSDLWidget(AWidget: PGtkWidget);
var
X11Window: QWord;
SDL_WINDOWID: string;
begin
X11Window := GetX11Window(AWidget);
SDL_WINDOWID := 'SDL_WINDOWID=' + IntToStr(PtrUint(X11Window));
_putenv(PChar(SDL_WINDOWID));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetSDLWidget(PGtkWidget(Panel));
end;
In a Button1Click handler SDL is initialized like so;if SDL_Init(SDL_INIT_VIDEO or SDL_INIT_AUDIO or SDL_INIT_TIMER) 0 then
raise Exception.CreateFmt('Could not initialize SDL - %s', [SDL_GetError]);
Regards, Darius
