How to do simple animation?



Hello,

I want to do a simple animation but does not know how I tell gtk to redraw my 
drawing-area. The animation works fine, when I constantly resizing the window 
with the mouse.

I tryed to sumit the expose_event in an loop, but I don't even know if I put 
the submit command at the right place in code, and so on.

So here comes my little program it would be nice if you could add the missing 
code, so I can see where I have to put it.

Thanks
Julian Gude

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <math.h>

gboolean expose_event( GtkWidget *widget,
		       GdkEventExpose *event,
		       gpointer data)
{
  int x , x0=100 , y0=100; 
  int y;
  double phi;
  double w = M_PI/100;
  static int t = 0;
  int r = 100;
  int A = M_PI_2;
  GdkDrawable *flaeche = widget->window;
  GdkGC       *gc      = widget->style->fg_gc[widget->state];
  
  gdk_window_clear_area( flaeche,
			 event->area.x,
			 event->area.y,
			 event->area.width,
			 event->area.height);

  gdk_gc_set_clip_rectangle( gc , &event->area );

  phi = A * cos( w*t );
  t++;
  x = (int) (r * sin ( phi ) + x0);
  y = (int) (r * cos ( phi ) + y0);

  gdk_draw_line( flaeche , gc , x0 , y0 , x , y );

  gdk_gc_set_clip_rectangle( gc , NULL );

  return TRUE;
}

int main( int argc , char *argv[] )
{
  int i;
  GtkWidget *fenster;
  GtkWidget *drawing_area;

  gtk_init( &argc , &argv );

  fenster = gtk_window_new( GTK_WINDOW_TOPLEVEL );
  gtk_signal_connect( GTK_OBJECT( fenster ), "delete_event" ,
		      GTK_SIGNAL_FUNC( gtk_main_quit ) , NULL );

  drawing_area = gtk_drawing_area_new();
  gtk_drawing_area_size( GTK_DRAWING_AREA( drawing_area ) , 0 , 0 );
  gtk_signal_connect( GTK_OBJECT( drawing_area ), "expose_event",
		      GTK_SIGNAL_FUNC( expose_event ) , NULL );

  gtk_container_add( GTK_CONTAINER( fenster ) , drawing_area );

  gtk_widget_show( drawing_area );
  gtk_widget_show( fenster );

  gtk_main();
  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]