Skip to content

Commit f544a71

Browse files
committed
Refactored all code, added slider and menu buttons
1 parent a0af6d2 commit f544a71

File tree

8 files changed

+912
-0
lines changed

8 files changed

+912
-0
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
OBJ=display.o gui.o global.o main.o
2+
EXE=sort
3+
HEADERS=*.h
4+
LIBS=-lX11 -lXext
5+
ARGS=-c
6+
CC=g++
7+
8+
all: $(EXE)
9+
10+
%.o: %.cpp $(HEADERS)
11+
$(CC) $(ARGS) $< -o $@
12+
13+
$(EXE): $(OBJ)
14+
$(CC) $^ -o $@ $(LIBS)
15+
16+
clean:
17+
rm $(EXE) *.o ./*/*.o

display.cpp

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

display.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef DISPLAY_H
2+
#define DISPLAY_H
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <unistd.h>
7+
#include <X11/Xlib.h>
8+
#include <X11/keysym.h>
9+
#include <X11/extensions/Xdbe.h>
10+
11+
12+
class X11 {
13+
private:
14+
Display *dpy;
15+
Window win;
16+
GC gc;
17+
XdbeBackBuffer backBuffer;
18+
XdbeSwapInfo swapInfo;
19+
public:
20+
X11(int&,int&);
21+
~X11();
22+
23+
void swapBuffers();
24+
bool getPending();
25+
void getNextEvent(XEvent*);
26+
void setColor(int,int,int);
27+
void setWindowTitle(std::string);
28+
void clearScreen(int,int);
29+
void drawString(float,float,std::string);
30+
void drawPoint(float,float);
31+
void drawLine(float,float,float,float);
32+
void drawRectangle(float,float,float,float);
33+
void fillRectangle(float,float,float,float);
34+
void drawArc(int,int,int,int);
35+
void drawCircle(int,int,int);
36+
void fillArc(int,int,int,int);
37+
void fillCircle(int,int,int);
38+
};
39+
40+
#endif

global.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include "global.h"
2+
3+
Global::Global() {
4+
xres = 720;
5+
curOption = NULL;
6+
yres = 1280;
7+
sorting = rButtonDown = lButtonDown = false;
8+
dropMenu = true;
9+
listAmountSlider = new Slider(50, SLIDER_RADIUS, 10, 500, 10);
10+
list = new int[amount];
11+
srand((unsigned)time(NULL));
12+
for(int i=0; i<amount; i++)
13+
list[i] = rand() % yres;
14+
width = xres/amount;
15+
16+
int sx = listAmountSlider->getLastX() + SLIDER_RADIUS*3;
17+
float h = SLIDER_RADIUS * 2;
18+
float w = 100.0f;
19+
for(int i=0; i<NUMSORTS; i++) {
20+
options[i].init(sx,0,w,h,i);
21+
sx += 1.5*w;
22+
}
23+
}
24+
Global::~Global() {
25+
delete listAmountSlider;
26+
listAmountSlider = NULL;
27+
delete list;
28+
list = NULL;
29+
}
30+
void Global::init() {
31+
srand((unsigned)time(NULL));
32+
int prevAmount = amount;
33+
amount = listAmountSlider->getValue();
34+
if(amount == prevAmount) return;
35+
list = new int[amount];
36+
for(int i=0; i<amount; i++)
37+
list[i] = rand() % yres;
38+
width = (float)xres/(float)amount;
39+
}
40+
void Global::setMouseCoords(int mousex, int mousey) {
41+
this->mousex = mousex;
42+
this->mousey = mousey;
43+
}
44+
void Global::processSettings() {
45+
if(listAmountSlider->slide(mousex)) {
46+
init();
47+
}
48+
}
49+
void Global::renderMenu(X11& x11) {
50+
if(!sorting) {
51+
x11.setColor(150,150,150);
52+
x11.fillRectangle(0,0,xres, SLIDER_RADIUS*2);
53+
listAmountSlider->render(x11);
54+
Button<int>* cur;
55+
for(int i=0; i<NUMSORTS; i++) {
56+
cur = &options[i];
57+
x11.setColor(200,200,200);
58+
x11.fillRectangle(cur->getX(),cur->getY(),cur->getWidth(),cur->getHeight());
59+
}
60+
}
61+
else {
62+
63+
}
64+
}
65+
66+
void checkResize(XEvent *e, Global& g) {
67+
if (e->type != ConfigureNotify)
68+
return;
69+
XConfigureEvent xce = e->xconfigure;
70+
g.xres = xce.width;
71+
g.yres = xce.height;
72+
g.init();
73+
// x11.setWindowTitle();
74+
}
75+
76+
void checkMouse(XEvent *e, Global& g) {
77+
Slider* slider = g.listAmountSlider;
78+
if (e->type == ButtonRelease) {
79+
g.lButtonDown = g.rButtonDown = false;
80+
slider->setClicked(false);
81+
return;
82+
}
83+
if (e->type == ButtonPress) {
84+
if (e->xbutton.button==1 && !g.sorting) {
85+
//Left button is down
86+
g.lButtonDown = true;
87+
if(!g.sorting) {
88+
slider->setClicked(slider->checkMouse(g.mousex, g.mousey));
89+
Button<int>* cur;
90+
for(int i=0; i<NUMSORTS; i++) {
91+
cur = &g.options[i];
92+
if(cur->checkMouse(g.mousex, g.mousey)) {
93+
g.curOption = cur;
94+
break;
95+
}
96+
}
97+
}
98+
}
99+
if (e->xbutton.button==3) {
100+
//Right button is down
101+
}
102+
}
103+
if (g.mousex != e->xbutton.x || g.mousey != e->xbutton.y) {
104+
//Mouse moved
105+
g.setMouseCoords(e->xbutton.x, e->xbutton.y);
106+
}
107+
}
108+
int checkKeys(XEvent *e, Global& g) {
109+
if (e->type != KeyPress && e->type != KeyPress)
110+
return 0;
111+
int key = XLookupKeysym(&e->xkey, 0);
112+
switch(key) {
113+
case XK_Escape:
114+
return(1);
115+
}
116+
117+
return(0);
118+
}

global.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef GLOBAL_H
2+
#define GLOBAL_H
3+
4+
#include "gui.h"
5+
#include "display.h"
6+
#include <cstdlib>
7+
#include <ctime>
8+
9+
enum {SHUFFLE=0,BUBBLESORT,SELECTIONSORT,INSERTIONSORT,SHELLSORT,BISELECTIONSORT,NUMSORTS};
10+
11+
class Global {
12+
public:
13+
int xres, yres;
14+
int mousex, mousey;
15+
int BARWIDTH=30;
16+
int amount=100;
17+
Slider* listAmountSlider;
18+
Button<int> options[NUMSORTS];
19+
Button<int>* curOption;
20+
int* list;
21+
float width;
22+
bool sorting;
23+
bool dropMenu;
24+
bool lButtonDown, rButtonDown;
25+
26+
Global();
27+
~Global();
28+
void init();
29+
void setMouseCoords(int,int);
30+
void processSettings();
31+
void renderMenu(X11&);
32+
};
33+
34+
void checkResize(XEvent*, Global&);
35+
void checkMouse(XEvent*, Global&);
36+
int checkKeys(XEvent*, Global&);
37+
#endif

0 commit comments

Comments
 (0)