-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.cpp
149 lines (144 loc) · 3.87 KB
/
display.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "display.h"
X11::X11(int& xres, int& yres) {
XSetWindowAttributes attributes;
XdbeBackBufferAttributes *backAttr;
dpy = XOpenDisplay(NULL);
attributes.event_mask = ExposureMask | StructureNotifyMask |
PointerMotionMask | ButtonPressMask |
ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
attributes.backing_store = Always;
attributes.save_under = True;
attributes.override_redirect = False;
attributes.background_pixel = 0x00000000;
Window root = DefaultRootWindow(dpy);
XWindowAttributes main;
XGetWindowAttributes(dpy, root, &main);
xres = main.width;
yres = main.height;
// g.init();
//Create a window
win = XCreateWindow(dpy, root, 0, 0, xres, yres, 0,
CopyFromParent, InputOutput, CopyFromParent,
CWBackingStore | CWOverrideRedirect | CWEventMask |
CWSaveUnder | CWBackPixel, &attributes);
//Create gc
gc = XCreateGC(dpy, win, 0, NULL);
//Get back buffer and attributes
backBuffer = XdbeAllocateBackBufferName(dpy, win, XdbeUndefined);
backAttr = XdbeGetBackBufferAttributes(dpy, backBuffer);
swapInfo.swap_window = backAttr->window;
swapInfo.swap_action = XdbeUndefined;
XFree(backAttr);
//Map and raise window
setWindowTitle("VISUAL SORT");
XMapWindow(dpy, win);
XRaiseWindow(dpy, win);
}
X11::~X11() {
if(!XdbeDeallocateBackBufferName(dpy, backBuffer)) {
fprintf(stderr,"Error : unable to deallocate back buffer.\n");
}
XFreeGC(dpy, gc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void X11::swapBuffers() {
XdbeSwapBuffers(dpy, &swapInfo, 1);
usleep(4000);
}
bool X11::getPending() {
return XPending(dpy);
}
void X11::getNextEvent(XEvent *e) {
XNextEvent(dpy, e);
}
void X11::setColor(int r, int g, int b) {
unsigned long cref = 0L;
cref += r;
cref <<= 8;
cref += g;
cref <<= 8;
cref += b;
XSetForeground(dpy, gc, cref);
}
void X11::setWindowTitle(std::string title) {
XStoreName(dpy, win, title.c_str());
}
void X11::clearScreen(int xres, int yres) {
//XClearWindow(dpy, backBuffer);
XSetForeground(dpy, gc, 0x00050505);
XFillRectangle(dpy, backBuffer, gc, 0, 0, xres, yres);
}
void X11::drawString(float x, float y, std::string word) {
XDrawString(dpy, backBuffer, gc, x, y, word.c_str(), word.length());
}
void X11::drawPoint(float x, float y) {
XDrawPoint(dpy, backBuffer, gc, x, y);
}
void X11::drawLine(float x0, float y0, float x1, float y1) {
XDrawLine(dpy, backBuffer, gc, x0, y0, x1, y1);
}
void X11::drawRectangle(float x, float y, float w, float h) {
//x,y is upper-left corner
XDrawRectangle(dpy, backBuffer, gc, x, y, w, h);
}
void X11::fillRectangle(float x, float y, float w, float h) {
//x,y is upper-left corner
XFillRectangle(dpy, backBuffer, gc, x, y, w, h);
}
void X11::drawArc(int xc, int yc, int x, int y) {
drawPoint(xc+x, yc+y);
drawPoint(xc-x, yc+y);
drawPoint(xc+x, yc-y);
drawPoint(xc-x, yc-y);
drawPoint(xc+y, yc+x);
drawPoint(xc-y, yc+x);
drawPoint(xc+y, yc-x);
drawPoint(xc-y, yc-x);
}
void X11::drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
drawArc(xc, yc, x, y);
while (y >= x) {
// for each pixel we will
// draw all eight pixels
x++;
// check for decision parameter
// and correspondingly
// update d, x, y
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawArc(xc, yc, x, y);
}
}
void X11::fillArc(int xc, int yc, int x, int y) {
drawLine(xc+x, yc+y, xc-x, yc+y);
drawLine(xc+x, yc-y, xc-x, yc-y);
drawLine(xc+y, yc+x, xc-y, yc+x);
drawLine(xc+y, yc-x, xc-y, yc-x);
}
void X11::fillCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
fillArc(xc, yc, x, y);
while (y >= x) {
// for each pixel we will
// draw all eight pixels
x++;
// check for decision parameter
// and correspondingly
// update d, x, y
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
fillArc(xc, yc, x, y);
}
}