123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #include "IPS_graphics.h"
- // Implementation of putpixel. Thanks to code from,
- // https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-The-GdkPixbuf-Structure.html
- void put_pixel(GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue, guchar alpha)
- {
- guchar *pixels, *p;
- int rowstride, numchannels;
- numchannels = gdk_pixbuf_get_n_channels(pixbuf);
- rowstride = gdk_pixbuf_get_rowstride(pixbuf);
- pixels = gdk_pixbuf_get_pixels(pixbuf);
- p = pixels + y * rowstride + x * numchannels;
- p[0] = red; p[1] = green; p[2] = blue; p[3] = alpha;
- }
- // Pain a background function to make a pixel buffern and an image to display as default canvas
- void paint_a_background (gpointer data)
- {
- GdkPixbuf *p;
- p = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 0, 8, X_SIZE, Y_SIZE);
- /*Paint a background canvas for start up image*/
- int x,y;
- for (x = 0; x < X_SIZE; x++)
- for (y = 0; y < Y_SIZE; y++)
- put_pixel(p, (int)x, (int)y, (guchar)x, (guchar)y, (guchar)x+y, 255);
- gtk_image_set_from_pixbuf(GTK_IMAGE(data), GDK_PIXBUF(p));
- g_object_unref(p);
- }
- // Function to paint lattice DATA from the simulation into a pixbuffer
- void paint_lattice (gpointer data, IPSmodel *s)
- {
- GdkPixbuf *p;
- // set the colors for all the states (machine and tape)
- // default colors are defined as macros
- ColorMap colores =
- {
- // machine states
- VACANT, // vacancy
- OCCUPIED // occupied
- };
- int x,y;
- p = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 0, 8, X_SIZE, Y_SIZE);
- // What do we plot, Machine or Tape state?
- // Paint the lattice MACHINE configuration to a pixbuffer;
- for (x = 0; x < X_SIZE; x++)
- for (y = 0; y < Y_SIZE; y++)
- switch (s->lattice_configuration[x][y])
- {
- case 0: // empty (vacant) site (white)
- put_pixel (p, (int) x, (int) y,
- (guchar) colores.vacant[0], (guchar) colores.vacant[1], (guchar) colores.vacant[2], 255);
- break;
- case 1: // occupied site pointing North
- put_pixel (p, (int) x, (int) y,
- (guchar) colores.occupied[0], (guchar) colores.occupied[1], (guchar) colores.occupied[2], 255);
- break;
- }
- gtk_image_set_from_pixbuf(GTK_IMAGE(data), GDK_PIXBUF(p));
- g_object_unref(p);
- }
|