Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.81 KB | None | 0 0
  1.  
  2. #include <X11/Xlib.h>
  3. #include <X11/Xutil.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8.  
  9. #define TRUE 1
  10. #define FALSE 0
  11.  
  12. int main(int argc, char *argv[]) {
  13.     char text[] = "kliknij w okno";
  14.     char help[] = "klawisze: c - czyszczenie, m - tryb, q - koniec";
  15.     char icon_name[] = "Grafika";
  16.     char title[] = "Grafika komputerowa";
  17.     Display* display;
  18.     Window window;
  19.     GC gc;
  20.     XEvent event;
  21.     KeySym key;
  22.     XSizeHints info;
  23.     int screen_no;
  24.     unsigned long foreground;
  25.     unsigned long background;
  26.     char buffer[8];
  27.     int hm_keys;
  28.  
  29.     int to_end;
  30.     XPoint whole[] = {
  31.         {10, 10},
  32.         {40, 0},
  33.         {0, 40},
  34.         {-40, 0},
  35.         {0, -40}
  36.     };
  37.  
  38.     XPoint square[] = {
  39.         {0, 0},
  40.         {120, 0},
  41.         {0, 120},
  42.         {-120, 0},
  43.         {0, -120}
  44.     };
  45.     XPoint* poly1, *poly2, *poly3;
  46.     XPoint pts[1000];
  47.     int pos[1000];
  48.     int hm_pts = 0;
  49.     XPoint current_point;
  50.     int i;
  51.     const int r = 35;
  52.     int dx,dy;
  53.  
  54.     Colormap colormap;
  55.     int red, green, blue, yellow, white;
  56.  
  57.     display = XOpenDisplay("");
  58.     screen_no = DefaultScreen(display);
  59.     background = WhitePixel(display, screen_no);
  60.     foreground = BlackPixel(display, screen_no);
  61.  
  62.     colormap = DefaultColormap(display, screen_no);
  63.  
  64.     int AllocNamedColor(char *name) //funkcja przydzielania kolorow
  65.     {
  66.         XColor col;
  67.         XParseColor(display, colormap, name, &col);
  68.         XAllocColor(display, colormap, &col);
  69.         return col.pixel;
  70.     }
  71.  
  72.     red = AllocNamedColor("red");
  73.     green = AllocNamedColor("green");
  74.     blue = AllocNamedColor("blue");
  75.     yellow = AllocNamedColor("yellow");
  76.     white = AllocNamedColor("white");
  77.  
  78.     info.x = 100;
  79.     info.y = 150;
  80.     info.width = 640;
  81.     info.height = 480;
  82.     info.flags = PPosition | PSize;
  83.  
  84.     window = XCreateSimpleWindow(display, DefaultRootWindow(display),
  85.             info.x, info.y, info.width, info.height,
  86.             7/* grubosc ramki */, foreground, background);
  87.     XSetStandardProperties(display, window, title, icon_name, None,
  88.             argv, argc, &info);
  89.     gc = XCreateGC(display, window, 0, 0);
  90.  
  91.     XSetBackground(display, gc, background);
  92.     XSetForeground(display, gc, foreground);
  93.  
  94.     XSelectInput(display, window, (KeyPressMask | ExposureMask | ButtonPressMask |
  95.             ButtonReleaseMask | Button1MotionMask));
  96.     XMapRaised(display, window);
  97.  
  98.     to_end = FALSE;
  99.  
  100.     int idTrafionejFigury(XPoint p) {
  101.         for (i = hm_pts - 1; i >= 0; i--) {
  102.             if (sqrt ((pow((double)(p.x-pts[i].x), 2) + pow((double)(p.y-pts[i].y), 2))) - r < 0 &&
  103.                     sqrt ((pow((double)(p.x-pts[i].x), 2) + pow((double)(p.y-pts[i].y), 2))) - r*0.5 < 0) {
  104.                 return i;
  105.             }
  106.         }
  107.         return -1;
  108.     }
  109.  
  110.     void usunFigure(int id) {
  111.         for (i = id; i < hm_pts - 1; i++) {
  112.             pts[i] = pts[i + 1];
  113.             pos[i] = pos[i + 1];
  114.         }
  115.         hm_pts--;
  116.     }
  117.  
  118.     XPoint moving = {0, 0};
  119.     int przesuwanie = -1;
  120.  
  121.     while (to_end == FALSE) {
  122.         XNextEvent(display, &event);
  123.  
  124.         switch (event.type) {
  125.             case Expose:
  126.                 if (event.xexpose.count == 0) {
  127.                     XSetForeground(display, gc, background);
  128.                     XFillRectangle(event.xexpose.display, event.xexpose.window, gc, 0, 0, 1000, 1000);
  129.                     XSetForeground(display, gc, foreground);
  130.                     XDrawImageString(event.xexpose.display, event.xexpose.window, gc, 100, 70, text, strlen(text));
  131.  
  132.                     XSetForeground(display, gc, blue);
  133.                     XDrawImageString(event.xexpose.display, event.xexpose.window, gc, 10, 10, help, strlen(help));
  134.  
  135.                     for (i = 0; i < hm_pts; i++) // rysowanie kwadratow lub trojkatow
  136.                     {
  137.                         if(pos[i])
  138.                         {
  139.                         //poly1 = square;
  140.                         poly2 = whole;
  141.  
  142.                         //poly1[0].x = pts[i].x - 30;
  143.                         //poly1[0].y = pts[i].y -30;
  144.                         poly2[0].x = pts[i].x - 20;
  145.                         poly2[0].y = pts[i].y -20;
  146.  
  147.                         XSetForeground(display, gc, red); // wnetrze
  148.                         XFillPolygon(event.xexpose.display, event.xexpose.window, gc, poly2, 4 + pos[i], Nonconvex, CoordModePrevious);
  149.                         XSetForeground(display, gc, blue); // wnetrze
  150.                         XFillArc(event.xexpose.display, event.xexpose.window, gc, pts[i].x-(r*0.5), pts[i].y-(r*0.5), r, r, 0, 360*64);
  151.                         }
  152.                         else
  153.                         {
  154.                         poly2 = whole;
  155.  
  156.                         //poly1[0].x = pts[i].x - 30;
  157.                         //poly1[0].y = pts[i].y -30;
  158.                         poly2[0].x = pts[i].x - 20;
  159.                         poly2[0].y = pts[i].y -20;
  160.  
  161.                         //XFillArc(event.xexpose.display, event.xexpose.window, gc, pts[i].x-r, pts[i].y-r, 2*r, 2*r, 0, 360*64);
  162.                         XSetForeground(display, gc, blue); // wnetrze
  163.                         XFillPolygon(event.xexpose.display, event.xexpose.window, gc, poly2, 4 + pos[i], Nonconvex, CoordModePrevious);
  164.                         XSetForeground(display, gc, red); // wnetrze
  165.                         XFillArc(event.xexpose.display, event.xexpose.window, gc, pts[i].x-(r*0.5), pts[i].y-(r*0.5), r, r, 0, 360*64);
  166.                         }
  167.  
  168.                     }
  169.  
  170.                 }
  171.                 break;
  172.  
  173.             case MappingNotify:
  174.                 XRefreshKeyboardMapping(&event.xmapping);
  175.                 break;
  176.  
  177.             case MotionNotify:
  178.                 //printf("ruch.");
  179.                 if (przesuwanie >= 0) {
  180.                     current_point.x = event.xbutton.x + dx;
  181.                     current_point.y = event.xbutton.y + dy;
  182.  
  183.                     pts[przesuwanie] = current_point;
  184.                     //pos[hm_pts] = 0;
  185.                     //hm_pts++;
  186.  
  187.                     event.type = Expose; // wymuszenie odswiezenia okna
  188.                     event.xexpose.count = 0;
  189.                     XSendEvent(display, window, 0, ExposureMask, &event);
  190.                 }
  191.                 break;
  192.  
  193.             case ButtonPress:
  194.                 if (event.xbutton.button == Button1) // wstawienie nowego punktu lamanej
  195.                 {
  196.                     current_point.x = event.xbutton.x;
  197.                     current_point.y = event.xbutton.y;
  198.                     int id = idTrafionejFigury(current_point);
  199.                     if (id >= 0) {
  200.                         //usunFigure(id);
  201.                         przesuwanie = id;
  202.                         dx = pts[i].x - current_point.x;
  203.                         dy = pts[i].y - current_point.y;
  204.                         moving = current_point;
  205.                     } else {
  206.  
  207.                         przesuwanie = -1;
  208.                     }
  209.                     event.type = Expose; // wymuszenie odswiezenia okna
  210.                     event.xexpose.count = 0;
  211.                     XSendEvent(display, window, 0, ExposureMask, &event);
  212.                 }
  213.                 break;
  214.  
  215.             case ButtonRelease:
  216.                 if (event.xbutton.button == Button1) // wstawienie nowego punktu lamanej
  217.                 {
  218.                     current_point.x = event.xbutton.x;
  219.                     current_point.y = event.xbutton.y;
  220.                     int id = idTrafionejFigury(current_point);
  221.                     if (id >= 0) {
  222.                         if (przesuwanie < 0 ||
  223.                                 (moving.x == current_point.x &&
  224.                                 moving.y == current_point.y))
  225.                             usunFigure(id);
  226.                         przesuwanie = -1;
  227.                     } else
  228.                         if (przesuwanie < 0 ||
  229.                             (moving.x == current_point.x &&
  230.                             moving.y == current_point.y)) {
  231.                         pts[hm_pts] = current_point;
  232.                         pos[hm_pts] = 0;
  233.                         hm_pts++;
  234.                         przesuwanie = -1;
  235.  
  236.                     }
  237.                     event.type = Expose; // wymuszenie odswiezenia okna
  238.                     event.xexpose.count = 0;
  239.                     XSendEvent(display, window, 0, ExposureMask, &event);
  240.                 } else if (event.xbutton.button == Button3) // wstawienie nowego punktu lamanej
  241.                 {
  242.  
  243.                     current_point.x = event.xbutton.x;
  244.                     current_point.y = event.xbutton.y;
  245.                     int id = idTrafionejFigury(current_point);
  246.  
  247.                     if (id >= 0) {
  248.                         //usunFigure(id);
  249.                     } else {
  250.                         pts[hm_pts] = current_point;
  251.                         pos[hm_pts] = 1;
  252.                         hm_pts++;
  253.                     }
  254.                     event.type = Expose; // wymuszenie odswiezenia okna
  255.                     event.xexpose.count = 0;
  256.                     XSendEvent(display, window, 0, ExposureMask, &event);
  257.                 }
  258.                 break;
  259.  
  260.             case KeyPress:
  261.                 hm_keys = XLookupString(&event.xkey, buffer, 8, &key, 0);
  262.                 if (hm_keys == 1) {
  263.                     if (buffer[0] == 'q') to_end = TRUE; // koniec programu
  264.                     else if (buffer[0] == 'c') // czyszczenie okna
  265.                     {
  266.                         hm_pts = 0;
  267.                         XClearWindow(event.xexpose.display, event.xexpose.window);
  268.  
  269.                         event.type = Expose; // wymuszenie odswiezenia okna
  270.                         event.xexpose.count = 0;
  271.                         XSendEvent(display, window, 0, ExposureMask, &event);
  272.                     }
  273.                 }
  274.  
  275.             default:
  276.                 break;
  277.         }
  278.     }
  279.  
  280.     XFreeGC(display, gc);
  281.     XDestroyWindow(display, window);
  282.     XCloseDisplay(display);
  283.  
  284.     return 0;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement