Recent

Author Topic: Embedding SDL into a GTK form  (Read 2446 times)

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Embedding SDL into a GTK form
« on: May 19, 2017, 10:40:32 pm »
Hello,

I am trying to embed an SQL surface into a GTK  from :
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormCreate(Sender : TObject);
  2. var
  3.     Screen: PSDL_Surface;
  4.     Rect: PSDL_Rect;
  5. begin
  6.     SDL_putenv(PChar('SDL_WINDOWID='+IntToStr(frmMain.Handle)));
  7.     SDL_Init(SDL_INIT_VIDEO);
  8.     Screen := SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE or SDL_DOUBLEBUF);
  9.     Rect^.h := 50;
  10.     Rect^.w := 50;
  11.     Rect^.x := 50;
  12.     Rect^.h := 50;
  13.     SDL_FillRect(Screen, Rect, SDL_MapRGB(Screen^.format, 0, 255, 0));
  14. end;

It does not work. I get the following error :
Quote
The program 'test02' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadWindow (invalid Window parameter)'.
  (Details: serial 53 error_code 3 request_code 3 minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)

Neuromancer

  • New Member
  • *
  • Posts: 44
    • My personal website
Re: Embedding SDL into a GTK form
« Reply #1 on: May 22, 2017, 11:13:49 pm »
Hello,

Could someone help me ?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Embedding SDL into a GTK form
« Reply #2 on: May 22, 2017, 11:46:18 pm »
afaik there is no easy solution to the problem. see also here. Basically, if you get things working then you have to 'fix' the double event loop and things get ugly pretty fast.

edit: But, to answer your question:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     Panel1: TPanel;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.     procedure WriteM(S: String);
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. uses
  37.   SDL;
  38.  
  39. var
  40.   SDLScreen: PSDL_Surface;
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.FormCreate(Sender: TObject);
  45. begin
  46.   SDL_putenv(PChar('SDL_WINDOWID=' + IntToStr(Panel1.handle)));
  47. end;
  48.  
  49. procedure TForm1.Button1Click(Sender: TObject);
  50. var
  51.   ARect: TSDL_Rect;
  52. begin
  53.   if (SDL_Init(SDL_INIT_VIDEO) = 0) then
  54.   begin
  55.     WriteM('SDL_Init succeeded');
  56.     SDLScreen := SDL_SetVideoMode(Panel1.WIdth, Panel1.Height, 32, SDL_SWSURFACE or SDL_DOUBLEBUF);
  57.     if assigned(SDLScreen) then
  58.     begin
  59.       WriteM('SDL_SetVideoMode returned a valid SDL surface');
  60.       ARect.h := 50;
  61.       ARect.w := 50;
  62.       ARect.x := 50;
  63.       ARect.h := 50;
  64.       SDL_FillRect(SDLScreen, @ARect, SDL_MapRGB(SDLScreen^.format, 0, 255, 0));
  65.       SDL_FLIP(SDLScreen);
  66.     end
  67.     else WriteM('Unable to Create SDL screen');
  68.   end
  69.   else WriteM('Unable to Initialize SDL');
  70. end;
  71.  
  72. procedure TForm1.FormDestroy(Sender: TObject);
  73. begin
  74.   SDL_QUIT;
  75. end;
  76.  
  77. procedure TForm1.WriteM(S: String);
  78. begin
  79.   memo1.Append(S);
  80. end;
  81.  
  82. end.
  83.  

Try and see if that works for you.
« Last Edit: May 23, 2017, 01:51:56 am by molly »

 

TinyPortal © 2005-2018