programming a scroll



-- Hi

I develop the graphical interface of my study project with GTK, but it's
the first time I use it.

I want to scroll a window to display real time data. 

But my solution is too slow for me ; I use gdk_draw_rgb_image with my
scroll algorithm of my bitmap (that do not copy pixels, just move
pointers).
'seems redisplaying the image is not a good idear.

Do u have a better solution, or know how to optimise my program ?
Should I use GTK curves instead, GTK drawing area with drawing
primitives ? ; I thowght it wouldn't be faster than a direct memory
manipulation. 
what is Pixbuf (not GDK Reference Manual)?
Another idear ?

thanks 


-- 
++

Bruno Herbelin
http://www.cicv.fr/~bruno

/*gcc `gtk-config --cflags` gtkrgb.c -o gtkrgb `gtk-config --libs`*/

#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>

#define IMAGE_WIDTH      260
#define IMAGE_HEIGHT     300

gboolean on_darea_expose (GtkWidget *widget,
                          GdkEventExpose *event,
                          gpointer user_data);

int
main (int argc, char *argv[])
{
  
  
  GtkWidget *window, *darea;
  guchar *rgbbuf;
  guchar *image;
  guchar *currentline;
  guchar *middlebuffer;
  int x,y,z;
  int value;
  guchar *pos, *tmp;


  
  gtk_init (&argc, &argv);
  gdk_rgb_init();
  gtk_widget_set_default_colormap (gdk_rgb_get_cmap());
   gtk_widget_set_default_visual (gdk_rgb_get_visual());

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  darea = gtk_drawing_area_new();
  gtk_drawing_area_size (GTK_DRAWING_AREA (darea), IMAGE_WIDTH, IMAGE_HEIGHT);
  gtk_container_add (GTK_CONTAINER (window), darea);
  gtk_signal_connect (GTK_OBJECT (darea), "expose-event",
		      GTK_SIGNAL_FUNC (on_darea_expose), NULL);
  gtk_widget_show_all (window);
 
  /* double buffer ; I start at the begining, and progressively display */
  /* the line bellow to scroll vertically */
  rgbbuf = (guchar *) malloc ( 2* 3* IMAGE_WIDTH * IMAGE_HEIGHT * sizeof(guchar));
  image = rgbbuf;
  pos = rgbbuf;

/*initialize image with empty grid*/ 
  for (y = 0; y < IMAGE_HEIGHT; y++)
    for (x = 0; x < IMAGE_WIDTH; x++){
      *pos++ = x % (IMAGE_WIDTH/10) ? y % (IMAGE_HEIGHT/10) ? 180 : 200 : 200;
      *pos++ = x % (IMAGE_WIDTH/10) ? y % (IMAGE_HEIGHT/10) ? 180 : 200 : 200;
      *pos++ = x % (IMAGE_WIDTH/10) ? y % (IMAGE_HEIGHT/10) ? 180 : 200 : 200;
  }	

  /* remember where is the middle of the buffer, so I can restart at the begining */
  /* when I reach it */
  middlebuffer = pos;
  currentline = pos;
  z=0;

  
  //gtk_main();
  
  for(;;){
	
	/**** my stuff ****/
	value= z + 50;   /*normaly from my computations*/
	/**** .....    ****/

  
  pos = currentline;      	
  tmp = image;		
 /* draw the new line under the image, and copy it at the top for R G & B*/	
  for (x = 0; x < IMAGE_WIDTH; x++){
    	*pos = (x==value) ? 255 : ( z != 0 ? x % (IMAGE_WIDTH/10) ? 180 : 200 : 200 );
 	*tmp++ = *pos++;
 
    	*pos =  (x==value) ? 0 : ( z != 0 ? x % (IMAGE_WIDTH/10) ? 180 : 200 : 200 );
 	*tmp++ = *pos++;
 
    	*pos =  (x==value) ? 0 : ( z != 0 ? x % (IMAGE_WIDTH/10) ? 180 : 200 : 200 );
 	*tmp++ = *pos++;
    }

  if (image==middlebuffer){
	/* I've reached the middle ; restart at the begining with the copy at the top */
    image =  rgbbuf;
    currentline = middlebuffer;
	/* TO DO ; there is an error ; I forget 1 line */
  }
  else {
	/* scroll 1 line bellow */
    image =  tmp;
    currentline = pos;
  }

  z+= z<IMAGE_HEIGHT/10 ? 1 : -IMAGE_HEIGHT/10;

 /* gdk_draw_rgb_image (darea->window, darea->style->fg_gc[GTK_STATE_NORMAL],
		       0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
		       GDK_RGB_DITHER_NONE, image, IMAGE_WIDTH*3);
 */

gdk_draw_rgb_image_dithalign (darea->window, darea->style->fg_gc[GTK_STATE_NORMAL],
		       0, 0, IMAGE_WIDTH, IMAGE_HEIGHT,
		       GDK_RGB_DITHER_NORMAL, image, IMAGE_WIDTH*3,
			0, 1);
 
    if(gtk_events_pending())
      gtk_main_iteration();
  }
  
  free(rgbbuf);

  return 0;
}


gboolean
on_darea_expose (GtkWidget *widget,
                 GdkEventExpose *event,
                 gpointer user_data)
{

}


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