Question: purpose: Framework for computer graphics Using X11 (Xlib) for drawing practice program Modify the below framework: add member functions to the X11 class... draw a
purpose: Framework for computer graphics Using X11 (Xlib) for drawing practice program Modify the below framework: add member functions to the X11 class... draw a point draw a rectangle draw a circle or elipse fill a rectangle draw some text demonstrate your functions: click the mouse press the keys draw stuff display some text with instructions #include#include #include #include #include #include #include class Global { public: int xres, yres; Global() { srand((unsigned)time(NULL)); xres = 640; yres = 480; } } g; class X11 { private: Display *dpy; Window win; GC gc; public: X11() { //constructor if (!(dpy=XOpenDisplay(NULL))) { fprintf(stderr, "ERROR: could not open display "); fflush(stderr); exit(EXIT_FAILURE); } int scr = DefaultScreen(dpy); win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 1, 1, g.xres, g.yres, 0, 0x00ffffff, 0x00000000); XStoreName(dpy, win, "X11 sample program"); gc = XCreateGC(dpy, win, 0, NULL); XMapWindow(dpy, win); XSelectInput(dpy, win, ExposureMask | StructureNotifyMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask); } ~X11() { XDestroyWindow(dpy, win); XCloseDisplay(dpy); } void check_resize(XEvent *e) { //ConfigureNotify is sent when window size changes. if (e->type != ConfigureNotify) return; XConfigureEvent xce = e->xconfigure; g.xres = xce.width; g.yres = xce.height; } void clear_screen() { XClearWindow(dpy, win); } void setColor3i(int r, int g, int b) { unsigned long cref = 0L; cref += r; cref <<= 8; cref += g; cref <<= 8; cref += b; XSetForeground(dpy, gc, cref); } bool getXPending() { return (XPending(dpy)); } void getXNextEvent(XEvent *e) { XNextEvent(dpy, e); } void drawLine(int x0, int y0, int x1, int y1) { XDrawLine(dpy, win, gc, x0, y0, x1, y1); } } x11; //function prototypes void init(); void init_xwindows(); void cleanup_xwindows(); void check_resize(XEvent *e); void check_mouse(XEvent *e); int check_keys(XEvent *e); void physics(); void render(); void clear_screen(); int main(void) { int done = 0; while (!done) { //Check the event queue while (x11.getXPending()) { //Handle them one-by-one XEvent e; //XNextEvent(dpy, &e); x11.getXNextEvent(&e); x11.check_resize(&e); check_mouse(&e); done = check_keys(&e); //Render when any event happens render(); } } return 0; } void check_mouse(XEvent *e) { static int savex = 0; static int savey = 0; int mx = e->xbutton.x; int my = e->xbutton.y; // if (e->type == ButtonRelease) { return; } if (e->type == ButtonPress) { //A mouse button was pressed. //Log("ButtonPress %i %i ", e->xbutton.x, e->xbutton.y); if (e->xbutton.button==1) { //Left button pressed } if (e->xbutton.button==3) { //Right button pressed } } if (savex != mx || savey != my) { //mouse moved savex = mx; savey = my; } } int check_keys(XEvent *e) { if (e->type != KeyPress && e->type != KeyRelease) return 0; int key = XLookupKeysym(&e->xkey, 0); switch (key) { case XK_Escape: //program is ending return 1; } return 0; } void physics(void) { //no physics yet. } void render(void) { //render function is always at the bottom of program. x11.setColor3i(255, 255, 0); x11.drawLine(100, 100, 200, 200); }
The Makefile:
all: xwin xwin: xwin.cpp g++ xwin.cpp -Wall -oxwin -lX11 -lm clean: rm -f xwin
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
