IPS_graphics.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "IPS_graphics.h"
  2. // Implementation of putpixel. Thanks to code from,
  3. // https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-The-GdkPixbuf-Structure.html
  4. void put_pixel(GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue, guchar alpha)
  5. {
  6. guchar *pixels, *p;
  7. int rowstride, numchannels;
  8. numchannels = gdk_pixbuf_get_n_channels(pixbuf);
  9. rowstride = gdk_pixbuf_get_rowstride(pixbuf);
  10. pixels = gdk_pixbuf_get_pixels(pixbuf);
  11. p = pixels + y * rowstride + x * numchannels;
  12. p[0] = red; p[1] = green; p[2] = blue; p[3] = alpha;
  13. }
  14. // Pain a background function to make a pixel buffern and an image to display as default canvas
  15. void paint_a_background (gpointer data)
  16. {
  17. GdkPixbuf *p;
  18. p = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 0, 8, X_SIZE, Y_SIZE);
  19. /*Paint a background canvas for start up image*/
  20. int x,y;
  21. for (x = 0; x < X_SIZE; x++)
  22. for (y = 0; y < Y_SIZE; y++)
  23. put_pixel(p, (int)x, (int)y, (guchar)x, (guchar)y, (guchar)x+y, 255);
  24. gtk_image_set_from_pixbuf(GTK_IMAGE(data), GDK_PIXBUF(p));
  25. g_object_unref(p);
  26. }
  27. // Function to paint lattice DATA from the simulation into a pixbuffer
  28. void paint_lattice (gpointer data, IPSmodel *s)
  29. {
  30. GdkPixbuf *p;
  31. // set the colors for all the states (machine and tape)
  32. // default colors are defined as macros
  33. ColorMap colores =
  34. {
  35. // machine states
  36. VACANT, // vacancy
  37. OCCUPIED // occupied
  38. };
  39. int x,y;
  40. p = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 0, 8, X_SIZE, Y_SIZE);
  41. // What do we plot, Machine or Tape state?
  42. // Paint the lattice MACHINE configuration to a pixbuffer;
  43. for (x = 0; x < X_SIZE; x++)
  44. for (y = 0; y < Y_SIZE; y++)
  45. switch (s->lattice_configuration[x][y])
  46. {
  47. case 0: // empty (vacant) site (white)
  48. put_pixel (p, (int) x, (int) y,
  49. (guchar) colores.vacant[0], (guchar) colores.vacant[1], (guchar) colores.vacant[2], 255);
  50. break;
  51. case 1: // occupied site pointing North
  52. put_pixel (p, (int) x, (int) y,
  53. (guchar) colores.occupied[0], (guchar) colores.occupied[1], (guchar) colores.occupied[2], 255);
  54. break;
  55. }
  56. gtk_image_set_from_pixbuf(GTK_IMAGE(data), GDK_PIXBUF(p));
  57. g_object_unref(p);
  58. }