#include
#include
#include
/*
* Hildon SDL Window reparenting test
*
* Compile with:
* cc -g -o hildon-sdl-test hildon-sdl-test.c `pkg-config –cflags –libs hildon-1 gtk+-2.0 sdl`
*
* Original sample:
* http://www.mail-archive.com/gtkmm-list@gnome.org/msg08601.html
*/
gboolean do_sdl_stuff( gpointer data ) {
SDL_Event event;
SDL_Surface *display = data;
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() % 240;
rect.w = rand() % 100 + 10;
rect.h = rand() % 100 + 10;
printf( “%ux%u+%u+%un”, rect.w, rect.h, rect.x, rect.y );
SDL_FillRect( display, &rect, SDL_MapRGB( display->format, rand()%255, rand()%255, rand()%255 ) );
SDL_Flip( display );
}
}
return TRUE;
}
void button_clicked( GtkButton *button, gpointer data ) {
/* Put draw rect userevent on queue */
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = 1;
SDL_PushEvent( &event );
}
void clear(void) {
/* Put clear userevent on queue */
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = 0;
SDL_PushEvent( &event );
}
int main( int argc, char **argv ) {
HildonProgram *program;
HildonWindow *window;
GtkWidget *sock, *box, *button;
char winhack[1024];
SDL_Surface *display;
gtk_init( &argc, &argv );
/* Create widgets */
program = HILDON_PROGRAM(hildon_program_get_instance());
g_set_application_name(“Hildon SDL sample”);
window = HILDON_WINDOW(hildon_window_new());
hildon_program_add_window(program, window);
sock = gtk_socket_new();
box = gtk_vbox_new( FALSE, 0 );
button = gtk_button_new_with_label( “Press me!” );
/* Setup socket widget */
gtk_widget_set_size_request( sock, 320, 240 );
gtk_box_pack_start( GTK_BOX(box), sock, TRUE, FALSE, 0 );
/* Setup button */
gtk_container_set_border_width( GTK_CONTAINER(button), 5 );
g_signal_connect( G_OBJECT(button), “clicked”, G_CALLBACK(button_clicked), display );
gtk_box_pack_start( GTK_BOX(box), button, FALSE, FALSE, 0 );
/* Setup window */
gtk_container_add( GTK_CONTAINER(window), box );
g_signal_connect( G_OBJECT(window), “delete-event”, G_CALLBACK(gtk_main_quit), NULL );
gtk_widget_show_all( GTK_WIDGET(window) );
/* Make SDL windows appear inside socket window */
snprintf( winhack, sizeof winhack, “SDL_WINDOWID=%i”, gtk_socket_get_id( GTK_SOCKET(sock) ) );
SDL_putenv( winhack );
/* Init SDL */
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
fprintf( stderr, “%s.n”, SDL_GetError() );
return EXIT_FAILURE;
}
/* Create SDL window */
if ( !( display = SDL_SetVideoMode( 320, 240, 0, 0 ) ) ) {
fprintf( stderr, “%s.n”, SDL_GetError() );
return EXIT_FAILURE;
}
/* Clear the SDL window */
clear();
/* Setup SDL event handlig timer */
g_timeout_add( 100, do_sdl_stuff, display );
printf(“%s”, winhack);
/* Start main loop */
gtk_main();
return 0;
}
这个例子采用了GTK的SOCKET控件来完成,将SDL窗口装入SOCKET这个控件中,达到HILDON和SDL混合编程的目的。
(linuxlearn)