/****************************************************************************** * FILE: animation_menu.cc * A simple animation in a window with a menu. * * Author: Dana Vrajitoru, IUSB * Class: C481 B581 Computer Graphics ******************************************************************************/ #include #include #include #include "menu_handle.h" static struct ballStruct { double x, y; double xVelocity, yVelocity; } ball[5]; /* a callback for the buttons */ static void a_callback(GtkWidget *button, gpointer data) { /*just print a string so that we know we got there*/ g_print("Inside Callback\n"); } /* GnomeUIInfo file_menu[] = { GNOMEUIINFO_MENU_EXIT_ITEM(gtk_main_quit,NULL), GNOMEUIINFO_END }; GnomeUIInfo some_menu[] = { GNOMEUIINFO_ITEM_NONE("_Menuitem","Just a menuitem", a_callback), GNOMEUIINFO_SEPARATOR, GNOMEUIINFO_ITEM_NONE("M_enuitem2","Just a menuitem", a_callback), GNOMEUIINFO_END }; GnomeUIInfo menubar[] = { GNOMEUIINFO_MENU_FILE_TREE(file_menu), GNOMEUIINFO_SUBTREE("_Some menu",some_menu), GNOMEUIINFO_END }; */ GnomeUIInfo file_menu[2], some_menu[4], menubar[3]; void makeMenus() { Menu_add_exit(file_menu, 0); Menu_end(file_menu, 1); Menu_add_item_simple(some_menu, 0, "_Menuitem","Just a menuitem", a_callback); Menu_add_separator(some_menu, 1); Menu_add_item_simple(some_menu, 2, "M_enuitem2","Just a menuitem", a_callback); Menu_end(some_menu, 3); Menu_add_file(menubar, file_menu); Menu_add_subtree(menubar, 1, some_menu, "_Some menu"); Menu_end(menubar, 2); } gint eventDelete(GtkWidget *widget, GdkEvent *event, gpointer data); gint eventDestroy(GtkWidget *widget, GdkEvent *event, gpointer data); gint nextFrame(gpointer data); void newBall(ballStruct &b); void nextBall(ballStruct &b); const int WIDTH=400, HEIGHT=300, GRAVITY=0.8, RADIUS=5, DIAMETER=RADIUS*2, INTERVAL=30; int main(int argc, char **argv) { int i; GtkWidget *app; GtkWidget *area; gnome_init("animation", "1.0", argc, argv); makeMenus(); app = gnome_app_new("animation", "Drawing Area Animation"); gtk_container_set_border_width(GTK_CONTAINER(app), 3); gtk_signal_connect(GTK_OBJECT(app), "delete_event", GTK_SIGNAL_FUNC(eventDelete), NULL); gtk_signal_connect(GTK_OBJECT(app), "destroy", GTK_SIGNAL_FUNC(eventDestroy), NULL); area = gtk_drawing_area_new(); gtk_widget_set_usize(area, WIDTH, HEIGHT); gnome_app_set_contents(GNOME_APP(app),area); /*create the menus for the application*/ gnome_app_create_menus (GNOME_APP (app), menubar); gtk_timeout_add(INTERVAL, nextFrame, area); for (i=0; i<5; i++) newBall(ball[i]); gtk_widget_show_all(app); gtk_main(); exit(0); } gint nextFrame(gpointer data) { int i; static GdkPixmap *pixmap = NULL; GtkWidget *widget = (GtkWidget *)data; if (pixmap == NULL) { pixmap = gdk_pixmap_new(widget->window, WIDTH, HEIGHT, -1); } gdk_draw_rectangle(pixmap, widget->style->white_gc, 1, 0, 0, WIDTH, HEIGHT); for (i=0; i<5; i++) { nextBall(ball[i]); gdk_draw_arc(pixmap, widget->style->black_gc, 1, (int)ball[i].x, (int)ball[i].y, DIAMETER, DIAMETER, 0, 360*64); } gdk_draw_pixmap(widget->window, widget->style->black_gc, pixmap, 0, 0, 0, 0, WIDTH, HEIGHT); return(1); } void newBall(ballStruct &b) { b.x = ((double)rand()*WIDTH)/RAND_MAX; b.y = (((double)rand()*HEIGHT)/RAND_MAX) - HEIGHT; do { b.xVelocity = ((double)rand()*10)/RAND_MAX; b.yVelocity = ((double)rand()*10)/RAND_MAX; } while (fabs(b.xVelocity) < 0.5); } void nextBall(ballStruct &b) { if ((b.x < -DIAMETER) || (b.x > WIDTH)) { newBall(b); return; } if ((b.y + DIAMETER) >= HEIGHT) { if (b.yVelocity > 0) b.yVelocity = -b.yVelocity; b.yVelocity *= 0.9; } else { b.yVelocity += GRAVITY; } b.x += b.xVelocity; b.y += b.yVelocity; } gint eventDelete(GtkWidget *widget, GdkEvent *event, gpointer data) { gtk_main_quit(); return(0); } gint eventDestroy(GtkWidget *widget, GdkEvent *event, gpointer data) { gtk_main_quit(); return(0); }