关闭

GTK+程序跨平台移植:把【GTK弹球小游戏"uunubt"!】从ubuntu10.10移植到Windows XP下。

665人阅读 评论(0) 收藏 举报

下面我把它移到XP下编译运行:

1,首先在XP上需要安装依赖环境(详情可参考http://blog.csdn.net/uunubt/archive/2011/01/07/6122373.aspx);

     我发现网上好多链接都失效了,所以建议到我开源邮箱下载:uunubt@yahoo.cn  00000000

 

2,这里是以前在ubuntu10.10下的【GTK弹球小游戏"uunubt"!】文章:  http://blog.csdn.net/uunubt/archive/2010/11/12/6005174.aspx    

 

3,拷入源码编译运行:

 

 

#include <gtk/gtk.h>
#include <stdio.h>

gint sx=0;

gint sy=0;

gint ex=1;

gint ey=1;

gint barx=250;

gint diameter=50;

GtkWidget *window;

GtkWidget *da;

gint single =0;

 

static void destroy(GtkWidget *widget,gpointer data)

{

gtk_main_quit();

}

 

void press(GtkWidget *widget,GdkEventKey *event)

{

g_print ("press: %x/n",event->keyval);

switch(event->keyval)

{

case 65361: //左键

//g_print ("Rigth/n"); //use debug

if (barx>0 )

barx = barx - 10;

else

barx = 0;

break ;

case 65363: //右键

//g_print ("Left/n"); //use debug

if (barx<500 )

barx = barx + 10;

else

barx = 500;

break ;

case 32: //空格键

single = 1;

//g_print ("Stop/n"); //use debug

break;

case 65293: //回车键

single =0;

//g_print ("Begin/n"); //use debug

 

break;

default:

break;

}

}

 

 

gboolean my_expose(GtkWidget *da,GdkEventExpose *event,gpointer data )

{

GdkGC *gc1,*gc2,*gc;

GdkColor color;

gc1 = gdk_gc_new(da->window);

color.red = 0;

color.green = 65535;

color.blue = 0;

gdk_gc_set_rgb_fg_color(gc1,&color);

gc2 = gdk_gc_new(da->window);

color.red = 65535;

color.green = 0;

color.blue = 0;

gdk_gc_set_rgb_fg_color(gc2,&color);

gc = gc1;

gdk_draw_rectangle(da->window,gc,TRUE,barx,480,100,20);

gc = gc2;

gdk_draw_arc(da->window,gc,TRUE,ex,ey,diameter,diameter,0,64*360);

g_object_unref(G_OBJECT(gc1));

g_object_unref(G_OBJECT(gc2));

 

return TRUE;

}

 

void leave_notify_event(GtkWidget *widget, GdkEventMotion *event)

{

gint x, y;

GdkModifierType state;

GdkRectangle rect;

if (event->is_hint)

gdk_window_get_pointer(event->window,&x,&y,&state);

else{

x = (gint)event->x;

y = (gint)event->y;

state = (GdkModifierType)event->state;

}

barx = x;

rect.x = 0;

rect.y = 470;

rect.width = 600;

rect.height = 30;

gdk_window_invalidate_rect(da->window,&rect,FALSE);

 

}

 

gboolean inBar()

{

if(ex >=barx && ex <= (barx+100))

return TRUE;

return FALSE;

}

 

gboolean chPosition()

{

gint tempx=ex;

gint tempy=ey;

 

if ( single == 1)

{

return TRUE;

}

if(((ex == 0) && (ey > sy)) ||( (ey == 0) && (ex>sx)))//daiwei+()

{

ex++;

ey++;

}//右下

else if(((ey == (480-diameter)) && (ex < sx) && (inBar())) || ((ex == (600-diameter)) && (ey < sy) ))//daiwei+()

{

ex--;

ey--;

}//左上

else if(((ey == 0) && (ex <sx)) || ((ex == (600-diameter)) && (ey > sy)))//daiwei+()

{

ex--;

ey++;

}//左下

else if(((ey == (480-diameter)) && (inBar()) && (ex > sx)) || ((ex == 0) && (ey < sy)))//daiwei+()

{

ex++;

ey--;

}//右上

else if(ey == (500-diameter) )

{

return FALSE;

}

else

{

ex = 2*ex - sx;

ey = 2*ey - sy;

}

sx = tempx;

sy = tempy;

gdk_window_invalidate_rect(da->window, NULL, FALSE);

return TRUE;

}

 

int main(int argc, char *argv[])

{

 

gtk_init(&argc,&argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

gtk_widget_set_events ( GTK_WIDGET(window), GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK|GDK_BUTTON_PRESS_MASK );

g_signal_connect(G_OBJECT(window),"delete_event", G_CALLBACK(gtk_main_quit),NULL);

g_signal_connect(G_OBJECT(window),"key_press_event", G_CALLBACK(press),NULL);

gtk_widget_add_events( GTK_WIDGET(window), GDK_KEY_PRESS_MASK |GDK_KEY_RELEASE_MASK|GDK_BUTTON_PRESS_MASK);

g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(destroy),NULL);

gtk_signal_connect(GTK_OBJECT(window),"leave_notify_event", GTK_SIGNAL_FUNC(leave_notify_event),NULL);

da = gtk_drawing_area_new();

gtk_widget_set_size_request(da,600,500);

gtk_container_add(GTK_CONTAINER(window),da);

g_signal_connect(da,"expose_event",G_CALLBACK(my_expose),NULL);

gtk_widget_show_all(window);

g_timeout_add(10,(GSourceFunc)chPosition,NULL);

gtk_main();

return 0;

}

 

4,发现和ubuntu下有点区别:键盘可用,鼠标移动无效。后台多个DOS命令行很烦,可以看到终端信息,呵呵。

  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:309887次
    • 积分:3984
    • 等级:
    • 排名:第8191名
    • 原创:42篇
    • 转载:389篇
    • 译文:0篇
    • 评论:12条
    文章分类
    最新评论