Advertisement
shadvoll

cairo program alpha

Oct 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. //gcc example.c -o example `pkg-config --cflags --libs gtk+-3.0`
  2. //export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig"
  3.  
  4. #include <cairo.h>
  5. #include <gtk/gtk.h>
  6. #include <time.h>
  7. const int MAX_WIDTH = 512;
  8. const int MAX_HEIGHT = 512;
  9. const int MAX_SIZE = 512;
  10. const int GRID = 16;
  11. const double ADJ_COEF = MAX_SIZE/GRID;
  12.  
  13. struct vl_pixel{
  14.     int hor;
  15.     int ver;
  16.     int r;
  17.     int g;
  18.     int b;
  19. };
  20.  
  21. static void do_drawing(cairo_t *);
  22.  
  23. static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
  24.     gpointer user_data)
  25. {      
  26.   do_drawing(cr);
  27.  
  28.   return FALSE;
  29. }
  30.  
  31. void vl_pixel(cairo_t *cr,struct vl_pixel pixel){
  32.     cairo_set_source_rgb(cr,pixel.r/255.0,pixel.g/255.0,pixel.b/255.0);
  33.     cairo_rectangle(cr,ADJ_COEF*pixel.hor,ADJ_COEF*pixel.ver,ADJ_COEF,ADJ_COEF);
  34.     cairo_fill(cr);
  35.  
  36. }
  37. // struct vl_pixel *vl_pixel_read(FILE *file){
  38. //  char ch = getchar(FILE);
  39. //  while(ch != EOF){
  40.  
  41. //  }
  42. // }
  43. void vl_pixel_save(FILE *file, struct vl_pixel pixel){
  44.     fprintf(file, "%d %d %d %d %d\n", pixel.ver,pixel.hor,pixel.r,pixel.g,pixel.b);
  45. }
  46. static void do_drawing(cairo_t *cr)
  47. {  
  48.     cairo_set_line_width (cr, 0.1);
  49.     cairo_set_source_rgb (cr, 1, 0 , 0);
  50.     int hor=0,ver=0;
  51.     FILE *file;
  52.     file = fopen("pattern1.txt","w+");
  53.     for (hor=0;hor*ADJ_COEF<MAX_SIZE;hor++){
  54.         for (ver=0;ver*ADJ_COEF<MAX_SIZE;ver++){
  55.             struct vl_pixel pixel = {0};
  56.             pixel.hor=hor;
  57.             pixel.ver = ver;
  58.             pixel.r = rand()%255;
  59.             pixel.g = 0;
  60.             pixel.b = 0;
  61.             vl_pixel_save(file,pixel);
  62.             vl_pixel(cr,pixel);
  63.             // vl_pixel(cr,hor,ver,r,g,b);
  64.         }      
  65.     }
  66.    
  67. }
  68.  
  69.  
  70. int main(int argc, char *argv[])
  71. {
  72.   GtkWidget *window;
  73.   GtkWidget *darea;
  74.   srand(time(NULL));
  75.   gtk_init(&argc, &argv);
  76.  
  77.   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  78.  
  79.   darea = gtk_drawing_area_new();
  80.   gtk_container_add(GTK_CONTAINER(window), darea);
  81.  
  82.   g_signal_connect(G_OBJECT(darea), "draw",
  83.       G_CALLBACK(on_draw_event), NULL);
  84.   g_signal_connect(window, "destroy",
  85.       G_CALLBACK(gtk_main_quit), NULL);
  86.  
  87.   gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  88.   gtk_window_set_default_size(GTK_WINDOW(window), MAX_SIZE, MAX_SIZE);
  89.   gtk_window_set_title(GTK_WINDOW(window), "GTK window");
  90.  
  91.   gtk_widget_show_all(window);
  92.  
  93.   gtk_main();
  94.  
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement