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)
-
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).
-
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.
-
If you use the do_sdl_stuff function, you must put events in the SDL
event queue to refresh graphics.
-
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.