SDL_WINDOWID: embedding SDL into gtkmm

Hello. I’m trying to embed an SDL_Window into my own application.

My way to (try) to do it is like this.

(window is a GtkWindow)

void onGtkWindowRealized()
{
unsigned long wid = GDK_WINDOW_XWINDOW(window_.gobj()->window);
std::stringstream sdlhack << “SDL_WINDOWID=” << wid;

putenv(const_cast<const char *>(sdlhack.str().c_str());

if (SDL_Init(SDL_INIT_VIDEO) != 0)

else
SDL_SetVideoMode(…)

}

I think that this way SDL should draw into my own application window, like
described in a lot of mailing lists I googled.
But I get an SDL toplevel window instead. So I have my app’s window and an
SDL one. I checked if the variable is
correctly set up, and it is. So… could anyone help on this, please.
Thank you very much in advance.

I’m not sure but i think the window has to be a GtkSocket, atleast that
works for me, see the attached test program. It works but putting the
draw rect stuff inside the button callback didn’t seem to work, thats
why it’s an event.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: gtk-sdl-test.c
Type: text/x-csrc
Size: 2964 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070915/9e8313f1/attachment.cOn Fri, Sep 14, 2007 at 11:21:28AM +0200, Germ?n Diago wrote:

Hello. I’m trying to embed an SDL_Window into my own application.
I think that this way SDL should draw into my own application window, like
described in a lot of mailing lists I googled.
But I get an SDL toplevel window instead. So I have my app’s window and an
SDL one. I checked if the variable is
correctly set up, and it is. So… could anyone help on this, please.
Thank you very much in advance.

2007/9/15, Mikael Eriksson <mikael_miffe_eriksson at yahoo.se>:

Hello. I’m trying to embed an SDL_Window into my own application.
I think that this way SDL should draw into my own application window,
like
described in a lot of mailing lists I googled.
But I get an SDL toplevel window instead. So I have my app’s window and
an
SDL one. I checked if the variable is
correctly set up, and it is. So… could anyone help on this, please.
Thank you very much in advance.

I’m not sure but i think the window has to be a GtkSocket, atleast that
works for me, see the attached test program. It works but putting the
draw rect stuff inside the button callback didn’t seem to work, thats
why it’s an event.

I thought of this way of doing that. But the problem is that I have two
different processes instead of
one. I’d like to have one process and two threads to manage the whole
program. Anyway, if there’s not
an optimal solution, I’ll try with the two processes one. Thanks for your
help.> On Fri, Sep 14, 2007 at 11:21:28AM +0200, Germ?n Diago wrote:


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

2007/9/15, Mikael Eriksson <mikael_miffe_eriksson at yahoo.se>:

Hello. I’m trying to embed an SDL_Window into my own application.
I think that this way SDL should draw into my own application window,
like
described in a lot of mailing lists I googled.
But I get an SDL toplevel window instead. So I have my app’s window and
an
SDL one. I checked if the variable is
correctly set up, and it is. So… could anyone help on this, please.
Thank you very much in advance.

I’m not sure but i think the window has to be a GtkSocket, atleast that
works for me, see the attached test program. It works but putting the
draw rect stuff inside the button callback didn’t seem to work, thats
why it’s an event.

I thought of this way of doing that. But the problem is that I have two
different processes instead of
one. I’d like to have one process and two threads to manage the whole
program. Anyway, if there’s not
an optimal solution, I’ll try with the two processes one. Thanks for your
help.

You don’t have to put them into separate processes, GtkSocket works just
fine with everything in one process. Anyway, doing it like you did in
the first mail, using SDL_WINDOWID=GDK_WINDOW_XWINDOW(window->window)
works for me. But i’m not using gtkmm, can it be that that is
interfering?On Sat, Sep 15, 2007 at 11:04:57AM +0200, Germ?n Diago wrote:

On Fri, Sep 14, 2007 at 11:21:28AM +0200, Germ?n Diago wrote:

2007/9/15, Mikael Eriksson <mikael_miffe_eriksson at yahoo.se>:

2007/9/15, Mikael Eriksson <mikael_miffe_eriksson at yahoo.se>:

Hello. I’m trying to embed an SDL_Window into my own application.
I think that this way SDL should draw into my own application
window,

like

described in a lot of mailing lists I googled.
But I get an SDL toplevel window instead. So I have my app’s window
and

an

SDL one. I checked if the variable is
correctly set up, and it is. So… could anyone help on this,
please.

Thank you very much in advance.

I’m not sure but i think the window has to be a GtkSocket, atleast
that

works for me, see the attached test program. It works but putting the
draw rect stuff inside the button callback didn’t seem to work, thats
why it’s an event.

I thought of this way of doing that. But the problem is that I have
two
different processes instead of
one. I’d like to have one process and two threads to manage the whole
program. Anyway, if there’s not
an optimal solution, I’ll try with the two processes one. Thanks for
your
help.

You don’t have to put them into separate processes, GtkSocket works just
fine with everything in one process. Anyway, doing it like you did in
the first mail, using SDL_WINDOWID=GDK_WINDOW_XWINDOW(window->window)
works for me. But i’m not using gtkmm, can it be that that is
interfering?

Maybe. I don’t know. What I did is to configure the window like you said
when
the main window is realized. Maybe it’s a gtkmm issue. I don’t know.
I looked at the documentation an it says that GtkSocket embeds from other
processes.
So if you use it, is it necessary to create a new process? Because I
don’t know what to do.
http://library.gnome.org/devel/gtk/2.11/GtkSocket.html
If it works for you, it should for me.> On Sat, Sep 15, 2007 at 11:04:57AM +0200, Germ?n Diago wrote:

On Fri, Sep 14, 2007 at 11:21:28AM +0200, Germ?n Diago wrote:


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

2007/9/15, Mikael Eriksson <mikael_miffe_eriksson at yahoo.se>:

2007/9/15, Mikael Eriksson <mikael_miffe_eriksson at yahoo.se>:

Hello. I’m trying to embed an SDL_Window into my own application.
I think that this way SDL should draw into my own application
window,

like

described in a lot of mailing lists I googled.
But I get an SDL toplevel window instead. So I have my app’s window
and

an

SDL one. I checked if the variable is
correctly set up, and it is. So… could anyone help on this,
please.

Thank you very much in advance.

I’m not sure but i think the window has to be a GtkSocket, atleast
that

works for me, see the attached test program. It works but putting the
draw rect stuff inside the button callback didn’t seem to work, thats
why it’s an event.

I thought of this way of doing that. But the problem is that I have
two
different processes instead of
one. I’d like to have one process and two threads to manage the whole
program. Anyway, if there’s not
an optimal solution, I’ll try with the two processes one. Thanks for
your
help.

You don’t have to put them into separate processes, GtkSocket works just
fine with everything in one process. Anyway, doing it like you did in
the first mail, using SDL_WINDOWID=GDK_WINDOW_XWINDOW(window->window)
works for me. But i’m not using gtkmm, can it be that that is
interfering?

This code doesn’t want to work, but don’t know why. Can anyone see anything
I can’t?> On Sat, Sep 15, 2007 at 11:04:57AM +0200, Germ?n Diago wrote:

On Fri, Sep 14, 2007 at 11:21:28AM +0200, Germ?n Diago wrote:


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

-------------- next part --------------
A non-text attachment was scrubbed…
Name: VentanaSDL.cpp
Type: text/x-c++src
Size: 2326 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070917/d80f8c9a/attachment.cpp

This code doesn’t want to work, but don’t know why. Can anyone see
anything I can’t?

Your code is almost working, but you forget some things:
(First of all, sorry to everyone for my bad english!, I’m spanish)

  1. SDL_putenv seems not to work with the string returned by c_str().
    A new char* type string must be created. Then, c_str() string must be
    copied to it (ie with sprintf).

  2. You don’t call to do_sdl_stuff for refresh SDL. Graphics will not be
    dispayed.
    The black SDL screen will not show when it embeded into GTK window.

  3. If you use the do_sdl_stuff function, you must put events in the SDL
    event queue to refresh graphics.

  4. You not need to call g_assert in member “ejecutar”. You can remove it.

Here is my working code based on yours:

#include <gtkmm.h>
#include <gtkmm/socket.h>
#include <glib.h>
#include <SDL.h>

#include
#include

using namespace std;

class VentanaSDL {
private:
Gtk::Main main_;
Gtk::Socket socket_;
Gtk::Window window_;
Gtk::VBox vbox_;
SDL_Surface * superficie_;

public:

VentanaSDL(int argc, char ** argv) : main_(&argc, &argv), superficie_(0)
{
window_.set_title(“Ventana SDL”);
socket_.set_size_request(320,200);
window_.set_border_width(6);
}

bool bucleSDL(){
SDL_Event event;
SDL_Surface *display = this->superficie_;
while ( SDL_PollEvent( &event ) ) {
// Handle quit event, not sure if this will ever appear
if ( event.type == SDL_QUIT ) return FALSE;
// Handle clear userevent
if ( event.type == SDL_USEREVENT && event.user.code == 0 ) {
SDL_FillRect( display, NULL, 0 );
SDL_Flip( display );
}

  // Handle draw rect userevent
  if ( event.type == SDL_USEREVENT && event.user.code == 1 ) {
    SDL_Rect rect;

    rect.x = rand() % 320;
    rect.y = rand() % 200;
    rect.w = rand() % 100 + 10;
    rect.h = rand() % 100 + 10;

    SDL_FillRect( display, &rect, SDL_MapRGB( display->format,

rand()%255, rand()%255, rand()%255 ) );
SDL_Flip( display );
}
}
// Forzar a que se dibuje un cuadrado nuevo
event.type = SDL_USEREVENT;
event.user.code = 1;
SDL_PushEvent( &event );

return true;

}

void clear(void) {
/* Put clear userevent on queue */
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = 0;
SDL_PushEvent( &event );
}

void ejecutar()
{
vbox_.pack_start(socket_, false, false);

window_.add(vbox_);
window_.show_all();

std::stringstream sdlhack;
sdlhack << "SDL_WINDOWID=" << socket_.get_id() << std::ends;

char* winhack=new char[32];
sprintf(winhack,sdlhack.str().c_str());
SDL_putenv(winhack);

if (SDL_Init(SDL_INIT_VIDEO))
  throw std::runtime_error("Error al inicializar la SDL");
else{
  superficie_ = SDL_SetVideoMode(320, 200, 0, 0);
  clear();
}

Glib::signal_timeout().connect(sigc::mem_fun(*this,&VentanaSDL::bucleSDL),
5);
// Use this for global (non class) functions:
// Glib::signal_timeout().connect(sigc::ptr_fun(&bucleSDL), 100);

main_.run(window_);

}

~VentanaSDL()
{
if (superficie_) SDL_FreeSurface(superficie_);
}

SDL_Surface* obtSuperficie(){return superficie_;}

};

int main(int argc, char ** argv)
{
VentanaSDL app(argc, argv);

app.ejecutar();

}–
View this message in context: http://www.nabble.com/SDL_WINDOWID%3A-embedding-SDL-into-gtkmm-tf4441255.html#a12938077
Sent from the SDL mailing list archive at Nabble.com.

2007/10/3, Daniel Ponsoda :>

This code doesn’t want to work, but don’t know why. Can anyone see
anything I can’t?

Your code is almost working, but you forget some things:
(First of all, sorry to everyone for my bad english!, I’m spanish)

  1. SDL_putenv seems not to work with the string returned by c_str().
    A new char* type string must be created. Then, c_str() string must be
    copied to it (ie with sprintf).

  2. You don’t call to do_sdl_stuff for refresh SDL. Graphics will not be
    dispayed.
    The black SDL screen will not show when it embeded into GTK window.

  3. If you use the do_sdl_stuff function, you must put events in the SDL
    event queue to refresh graphics.

  4. You not need to call g_assert in member “ejecutar”. You can remove it.

Here is my working code based on yours:

#include <gtkmm.h>
#include <gtkmm/socket.h>
#include <glib.h>
#include <SDL.h>

#include
#include

using namespace std;

class VentanaSDL {
private:
Gtk::Main main_;
Gtk::Socket socket_;
Gtk::Window window_;
Gtk::VBox vbox_;
SDL_Surface * superficie_;

public:

VentanaSDL(int argc, char ** argv) : main_(&argc, &argv), superficie_(0)
{
window_.set_title(“Ventana SDL”);
socket_.set_size_request(320,200);
window_.set_border_width(6);
}

bool bucleSDL(){
SDL_Event event;
SDL_Surface *display = this->superficie_;
while ( SDL_PollEvent( &event ) ) {
// Handle quit event, not sure if this will ever appear
if ( event.type == SDL_QUIT ) return FALSE;
// Handle clear userevent
if ( event.type == SDL_USEREVENT && event.user.code == 0 ) {
SDL_FillRect( display, NULL, 0 );
SDL_Flip( display );
}

  // Handle draw rect userevent
  if ( event.type == SDL_USEREVENT && event.user.code == 1 ) {
    SDL_Rect rect;

    rect.x = rand() % 320;
    rect.y = rand() % 200;
    rect.w = rand() % 100 + 10;
    rect.h = rand() % 100 + 10;

    SDL_FillRect( display, &rect, SDL_MapRGB( display->format,

rand()%255, rand()%255, rand()%255 ) );
SDL_Flip( display );
}
}
// Forzar a que se dibuje un cuadrado nuevo
event.type = SDL_USEREVENT;
event.user.code = 1;
SDL_PushEvent( &event );

return true;

}

void clear(void) {
/* Put clear userevent on queue */
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = 0;
SDL_PushEvent( &event );
}

void ejecutar()
{
vbox_.pack_start(socket_, false, false);

window_.add(vbox_);
window_.show_all();

std::stringstream sdlhack;
sdlhack << "SDL_WINDOWID=" << socket_.get_id() << std::ends;

char* winhack=new char[32];
sprintf(winhack,sdlhack.str().c_str());
SDL_putenv(winhack);

if (SDL_Init(SDL_INIT_VIDEO))
  throw std::runtime_error("Error al inicializar la SDL");
else{
  superficie_ = SDL_SetVideoMode(320, 200, 0, 0);
  clear();
}

Glib::signal_timeout().connect(sigc::mem_fun(*this,&VentanaSDL::bucleSDL),
5);
// Use this for global (non class) functions:
// Glib::signal_timeout().connect(sigc::ptr_fun(&bucleSDL), 100);

main_.run(window_);

}

~VentanaSDL()
{
if (superficie_) SDL_FreeSurface(superficie_);
}

SDL_Surface* obtSuperficie(){return superficie_;}

};

int main(int argc, char ** argv)
{
VentanaSDL app(argc, argv);

app.ejecutar();

}

View this message in context:
http://www.nabble.com/SDL_WINDOWID%3A-embedding-SDL-into-gtkmm-tf4441255.html#a12938077
Sent from the SDL mailing list archive at Nabble.com.

_Thank you very much for your answer. Yo tambi?n soy espa?ol :-). Pleasant
to see spanish people interested in programming.


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