1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <gtkmm.h>
#include <gtkmm/socket.h>
#include <glib.h>
#include <SDL/SDL.h>
#include <sstream>
#include <iostream>
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 on_delete_event()
// {
// printf("test");
//}
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();
return 0;
} |
Partager