Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.09 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.         {60, 0},
  41.         {0, 60},
  42.         {-60, 0},
  43.         {0, -60}
  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.                     (p.x < pts[i].x - 20 || p.x > pts[i].x + 20 ||
  104.                     p.y < pts[i].y - 20 || p.y > pts[i].y + 20)) {
  105.                 return i;
  106.             }
  107.         }
  108.         return -1;
  109.     }
  110.  
  111.     void usunFigure(int id) {
  112.         for (i = id; i < hm_pts - 1; i++) {
  113.             pts[i] = pts[i + 1];
  114.             pos[i] = pos[i + 1];
  115.         }
  116.         hm_pts--;
  117.     }
  118.  
  119.     XPoint moving = {0, 0};
  120.     int przesuwanie = -1;
  121.  
  122.     while (to_end == FALSE) {
  123.         XNextEvent(display, &event);
  124.  
  125.         switch (event.type) {
  126.             case Expose:
  127.                 if (event.xexpose.count == 0) {
  128.                     XSetForeground(display, gc, background);
  129.                     XFillRectangle(event.xexpose.display, event.xexpose.window, gc, 0, 0, 1000, 1000);
  130.                     XSetForeground(display, gc, foreground);
  131.                     XDrawImageString(event.xexpose.display, event.xexpose.window, gc, 100, 70, text, strlen(text));
  132.  
  133.                     XSetForeground(display, gc, blue);
  134.                     XDrawImageString(event.xexpose.display, event.xexpose.window, gc, 10, 10, help, strlen(help));
  135.  
  136.                     for (i = 0; i < hm_pts; i++) // rysowanie kwadratow lub trojkatow
  137.                     {
  138.                         if(pos[i])
  139.                         {
  140.                         //poly1 = square;
  141.                         poly2 = whole;
  142.  
  143.                         //poly1[0].x = pts[i].x - 30;
  144.                         //poly1[0].y = pts[i].y -30;
  145.                         poly2[0].x = pts[i].x - 20;
  146.                         poly2[0].y = pts[i].y -20;
  147.                         XSetForeground(display, gc, background); // wnetrze
  148.  
  149.                         XFillArc(event.xexpose.display, event.xexpose.window, gc, pts[i].x-r, pts[i].y-r, 2*r, 2*r, 0, 360*64);
  150.                         XSetForeground(display, gc, red); // wnetrze
  151.                         XFillPolygon(event.xexpose.display, event.xexpose.window, gc, poly2, 4 + pos[i], Nonconvex, CoordModePrevious);
  152.                         XSetForeground(display, gc, blue); // wnetrze
  153.                         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);
  154.                         }
  155.                         else
  156.                         {
  157.                         poly2 = whole;
  158.  
  159.                         //poly1[0].x = pts[i].x - 30;
  160.                         //poly1[0].y = pts[i].y -30;
  161.                         poly2[0].x = pts[i].x - 20;
  162.                         poly2[0].y = pts[i].y -20;
  163.                         XSetForeground(display, gc, background); // wnetrze
  164.  
  165.                         XFillArc(event.xexpose.display, event.xexpose.window, gc, pts[i].x-r, pts[i].y-r, 2*r, 2*r, 0, 360*64);
  166.                         XSetForeground(display, gc, blue); // wnetrze
  167.                         XFillPolygon(event.xexpose.display, event.xexpose.window, gc, poly2, 4 + pos[i], Nonconvex, CoordModePrevious);
  168.                         XSetForeground(display, gc, red); // wnetrze
  169.                         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);
  170.                         }
  171.  
  172.                     }
  173.  
  174.                 }
  175.                 break;
  176.  
  177.             case MappingNotify:
  178.                 XRefreshKeyboardMapping(&event.xmapping);
  179.                 break;
  180.  
  181.             case MotionNotify:
  182.                 //printf("ruch.");
  183.                 if (przesuwanie >= 0) {
  184.                     current_point.x = event.xbutton.x + dx;
  185.                     current_point.y = event.xbutton.y + dy;
  186.  
  187.                     pts[przesuwanie] = current_point;
  188.                     //pos[hm_pts] = 0;
  189.                     //hm_pts++;
  190.  
  191.                     event.type = Expose; // wymuszenie odswiezenia okna
  192.                     event.xexpose.count = 0;
  193.                     XSendEvent(display, window, 0, ExposureMask, &event);
  194.                 }
  195.                 break;
  196.  
  197.             case ButtonPress:
  198.                 if (event.xbutton.button == Button1) // wstawienie nowego punktu lamanej
  199.                 {
  200.                     current_point.x = event.xbutton.x;
  201.                     current_point.y = event.xbutton.y;
  202.                     int id = idTrafionejFigury(current_point);
  203.                     if (id >= 0) {
  204.                         //usunFigure(id);
  205.                         przesuwanie = id;
  206.                         dx = pts[i].x - current_point.x;
  207.                         dy = pts[i].y - current_point.y;
  208.                         moving = current_point;
  209.                     } else {
  210.  
  211.                         przesuwanie = -1;
  212.                     }
  213.                     event.type = Expose; // wymuszenie odswiezenia okna
  214.                     event.xexpose.count = 0;
  215.                     XSendEvent(display, window, 0, ExposureMask, &event);
  216.                 }
  217.                 break;
  218.  
  219.             case ButtonRelease:
  220.                 if (event.xbutton.button == Button1) // wstawienie nowego punktu lamanej
  221.                 {
  222.                     current_point.x = event.xbutton.x;
  223.                     current_point.y = event.xbutton.y;
  224.                     int id = idTrafionejFigury(current_point);
  225.                     if (id >= 0) {
  226.                         if (przesuwanie < 0 ||
  227.                                 (moving.x == current_point.x &&
  228.                                 moving.y == current_point.y))
  229.                             usunFigure(id);
  230.                         przesuwanie = -1;
  231.                     } else
  232.                         if (przesuwanie < 0 ||
  233.                             (moving.x == current_point.x &&
  234.                             moving.y == current_point.y)) {
  235.                         pts[hm_pts] = current_point;
  236.                         pos[hm_pts] = 0;
  237.                         hm_pts++;
  238.                         przesuwanie = -1;
  239.  
  240.                     }
  241.                     event.type = Expose; // wymuszenie odswiezenia okna
  242.                     event.xexpose.count = 0;
  243.                     XSendEvent(display, window, 0, ExposureMask, &event);
  244.                 } else if (event.xbutton.button == Button3) // wstawienie nowego punktu lamanej
  245.                 {
  246.  
  247.                     current_point.x = event.xbutton.x;
  248.                     current_point.y = event.xbutton.y;
  249.                     int id = idTrafionejFigury(current_point);
  250.  
  251.                     if (id >= 0) {
  252.                         //usunFigure(id);
  253.                     } else {
  254.                         pts[hm_pts] = current_point;
  255.                         pos[hm_pts] = 1;
  256.                         hm_pts++;
  257.                     }
  258.                     event.type = Expose; // wymuszenie odswiezenia okna
  259.                     event.xexpose.count = 0;
  260.                     XSendEvent(display, window, 0, ExposureMask, &event);
  261.                 }
  262.                 break;
  263.  
  264.             case KeyPress:
  265.                 hm_keys = XLookupString(&event.xkey, buffer, 8, &key, 0);
  266.                 if (hm_keys == 1) {
  267.                     if (buffer[0] == 'q') to_end = TRUE; // koniec programu
  268.                     else if (buffer[0] == 'c') // czyszczenie okna
  269.                     {
  270.                         hm_pts = 0;
  271.                         XClearWindow(event.xexpose.display, event.xexpose.window);
  272.  
  273.                         event.type = Expose; // wymuszenie odswiezenia okna
  274.                         event.xexpose.count = 0;
  275.                         XSendEvent(display, window, 0, ExposureMask, &event);
  276.                     }
  277.                 }
  278.  
  279.             default:
  280.                 break;
  281.         }
  282.     }
  283.  
  284.     XFreeGC(display, gc);
  285.     XDestroyWindow(display, window);
  286.     XCloseDisplay(display);
  287.  
  288.     return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement