Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- #define TRUE 1
- #define FALSE 0
- int main(int argc, char *argv[]) {
- char text[] = "kliknij w okno";
- char help[] = "klawisze: c - czyszczenie, m - tryb, q - koniec";
- char icon_name[] = "Grafika";
- char title[] = "Grafika komputerowa";
- Display* display;
- Window window;
- GC gc;
- XEvent event;
- KeySym key;
- XSizeHints info;
- int screen_no;
- unsigned long foreground;
- unsigned long background;
- char buffer[8];
- int hm_keys;
- int to_end;
- XPoint whole[] = {
- {10, 10},
- {40, 0},
- {0, 40},
- {-40, 0},
- {0, -40}
- };
- XPoint square[] = {
- {0, 0},
- {120, 0},
- {0, 120},
- {-120, 0},
- {0, -120}
- };
- XPoint* poly1, *poly2, *poly3;
- XPoint pts[1000];
- int pos[1000];
- int hm_pts = 0;
- XPoint current_point;
- int i;
- const int r = 35;
- int dx,dy;
- Colormap colormap;
- int red, green, blue, yellow, white;
- display = XOpenDisplay("");
- screen_no = DefaultScreen(display);
- background = WhitePixel(display, screen_no);
- foreground = BlackPixel(display, screen_no);
- colormap = DefaultColormap(display, screen_no);
- int AllocNamedColor(char *name) //funkcja przydzielania kolorow
- {
- XColor col;
- XParseColor(display, colormap, name, &col);
- XAllocColor(display, colormap, &col);
- return col.pixel;
- }
- red = AllocNamedColor("red");
- green = AllocNamedColor("green");
- blue = AllocNamedColor("blue");
- yellow = AllocNamedColor("yellow");
- white = AllocNamedColor("white");
- info.x = 100;
- info.y = 150;
- info.width = 640;
- info.height = 480;
- info.flags = PPosition | PSize;
- window = XCreateSimpleWindow(display, DefaultRootWindow(display),
- info.x, info.y, info.width, info.height,
- 7/* grubosc ramki */, foreground, background);
- XSetStandardProperties(display, window, title, icon_name, None,
- argv, argc, &info);
- gc = XCreateGC(display, window, 0, 0);
- XSetBackground(display, gc, background);
- XSetForeground(display, gc, foreground);
- XSelectInput(display, window, (KeyPressMask | ExposureMask | ButtonPressMask |
- ButtonReleaseMask | Button1MotionMask));
- XMapRaised(display, window);
- to_end = FALSE;
- int idTrafionejFigury(XPoint p) {
- for (i = hm_pts - 1; i >= 0; i--) {
- if (sqrt ((pow((double)(p.x-pts[i].x), 2) + pow((double)(p.y-pts[i].y), 2))) - r < 0 &&
- sqrt ((pow((double)(p.x-pts[i].x), 2) + pow((double)(p.y-pts[i].y), 2))) - r*0.5 < 0) {
- return i;
- }
- }
- return -1;
- }
- void usunFigure(int id) {
- for (i = id; i < hm_pts - 1; i++) {
- pts[i] = pts[i + 1];
- pos[i] = pos[i + 1];
- }
- hm_pts--;
- }
- XPoint moving = {0, 0};
- int przesuwanie = -1;
- while (to_end == FALSE) {
- XNextEvent(display, &event);
- switch (event.type) {
- case Expose:
- if (event.xexpose.count == 0) {
- XSetForeground(display, gc, background);
- XFillRectangle(event.xexpose.display, event.xexpose.window, gc, 0, 0, 1000, 1000);
- XSetForeground(display, gc, foreground);
- XDrawImageString(event.xexpose.display, event.xexpose.window, gc, 100, 70, text, strlen(text));
- XSetForeground(display, gc, blue);
- XDrawImageString(event.xexpose.display, event.xexpose.window, gc, 10, 10, help, strlen(help));
- for (i = 0; i < hm_pts; i++) // rysowanie kwadratow lub trojkatow
- {
- if(pos[i])
- {
- //poly1 = square;
- poly2 = whole;
- //poly1[0].x = pts[i].x - 30;
- //poly1[0].y = pts[i].y -30;
- poly2[0].x = pts[i].x - 20;
- poly2[0].y = pts[i].y -20;
- XSetForeground(display, gc, red); // wnetrze
- XFillPolygon(event.xexpose.display, event.xexpose.window, gc, poly2, 4 + pos[i], Nonconvex, CoordModePrevious);
- XSetForeground(display, gc, blue); // wnetrze
- 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);
- }
- else
- {
- poly2 = whole;
- //poly1[0].x = pts[i].x - 30;
- //poly1[0].y = pts[i].y -30;
- poly2[0].x = pts[i].x - 20;
- poly2[0].y = pts[i].y -20;
- //XFillArc(event.xexpose.display, event.xexpose.window, gc, pts[i].x-r, pts[i].y-r, 2*r, 2*r, 0, 360*64);
- XSetForeground(display, gc, blue); // wnetrze
- XFillPolygon(event.xexpose.display, event.xexpose.window, gc, poly2, 4 + pos[i], Nonconvex, CoordModePrevious);
- XSetForeground(display, gc, red); // wnetrze
- 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);
- }
- }
- }
- break;
- case MappingNotify:
- XRefreshKeyboardMapping(&event.xmapping);
- break;
- case MotionNotify:
- //printf("ruch.");
- if (przesuwanie >= 0) {
- current_point.x = event.xbutton.x + dx;
- current_point.y = event.xbutton.y + dy;
- pts[przesuwanie] = current_point;
- //pos[hm_pts] = 0;
- //hm_pts++;
- event.type = Expose; // wymuszenie odswiezenia okna
- event.xexpose.count = 0;
- XSendEvent(display, window, 0, ExposureMask, &event);
- }
- break;
- case ButtonPress:
- if (event.xbutton.button == Button1) // wstawienie nowego punktu lamanej
- {
- current_point.x = event.xbutton.x;
- current_point.y = event.xbutton.y;
- int id = idTrafionejFigury(current_point);
- if (id >= 0) {
- //usunFigure(id);
- przesuwanie = id;
- dx = pts[i].x - current_point.x;
- dy = pts[i].y - current_point.y;
- moving = current_point;
- } else {
- przesuwanie = -1;
- }
- event.type = Expose; // wymuszenie odswiezenia okna
- event.xexpose.count = 0;
- XSendEvent(display, window, 0, ExposureMask, &event);
- }
- break;
- case ButtonRelease:
- if (event.xbutton.button == Button1) // wstawienie nowego punktu lamanej
- {
- current_point.x = event.xbutton.x;
- current_point.y = event.xbutton.y;
- int id = idTrafionejFigury(current_point);
- if (id >= 0) {
- if (przesuwanie < 0 ||
- (moving.x == current_point.x &&
- moving.y == current_point.y))
- usunFigure(id);
- przesuwanie = -1;
- } else
- if (przesuwanie < 0 ||
- (moving.x == current_point.x &&
- moving.y == current_point.y)) {
- pts[hm_pts] = current_point;
- pos[hm_pts] = 0;
- hm_pts++;
- przesuwanie = -1;
- }
- event.type = Expose; // wymuszenie odswiezenia okna
- event.xexpose.count = 0;
- XSendEvent(display, window, 0, ExposureMask, &event);
- } else if (event.xbutton.button == Button3) // wstawienie nowego punktu lamanej
- {
- current_point.x = event.xbutton.x;
- current_point.y = event.xbutton.y;
- int id = idTrafionejFigury(current_point);
- if (id >= 0) {
- //usunFigure(id);
- } else {
- pts[hm_pts] = current_point;
- pos[hm_pts] = 1;
- hm_pts++;
- }
- event.type = Expose; // wymuszenie odswiezenia okna
- event.xexpose.count = 0;
- XSendEvent(display, window, 0, ExposureMask, &event);
- }
- break;
- case KeyPress:
- hm_keys = XLookupString(&event.xkey, buffer, 8, &key, 0);
- if (hm_keys == 1) {
- if (buffer[0] == 'q') to_end = TRUE; // koniec programu
- else if (buffer[0] == 'c') // czyszczenie okna
- {
- hm_pts = 0;
- XClearWindow(event.xexpose.display, event.xexpose.window);
- event.type = Expose; // wymuszenie odswiezenia okna
- event.xexpose.count = 0;
- XSendEvent(display, window, 0, ExposureMask, &event);
- }
- }
- default:
- break;
- }
- }
- XFreeGC(display, gc);
- XDestroyWindow(display, window);
- XCloseDisplay(display);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement