linux下sdl多窗口,HILDON窗口嵌入SDL窗口的方法

该博客展示了如何利用GTK的SOCKET控件将SDL窗口嵌入到HILDON环境中,实现HILDON和SDL的混合编程。通过创建一个HILDON Window,并在其中嵌入SDL显示区域,结合SDL事件处理,实现了在HILDON应用中显示和更新SDL内容的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值