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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
//Framework for graphics
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
//--------------------
//XWindows variables
Display *dpy;
Window win;
GC gc;
//--------------------
struct Global {
int xres, yres;
int circleType;
int done;
Global() {
//constructor
xres=640;
yres=480;
circleType=0;
done=0;
}
} g;
//-------------------
//function prototypes
void initXwindows();
void cleanupXwindows();
void checkResize(XEvent *e);
void checkMouse(XEvent *e);
void checkKeys(XEvent *e);
void physics();
void render();
void clearScreen();
int main()
{
srand((unsigned)time(NULL));
initXwindows();
while (!g.done) {
//Check the event queue
while (XPending(dpy)) {
//Handle evemts one-by-one
XEvent e;
XNextEvent(dpy, &e);
checkResize(&e);
checkMouse(&e);
checkKeys(&e);
render();
}
}
cleanupXwindows();
return 0;
}
void cleanupXwindows(void)
{
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void initXwindows(void)
{
if (!(dpy=XOpenDisplay(NULL))) {
fprintf(stderr, "ERROR: could not open display\n");
exit(EXIT_FAILURE);
}
int scr = DefaultScreen(dpy);
win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 1, 1,
g.xres, g.yres, 0, 0x00ffffff, 0x00000000);
gc = XCreateGC(dpy, win, 0, NULL);
XMapWindow(dpy, win);
XSelectInput(dpy, win, ExposureMask | StructureNotifyMask |
PointerMotionMask | ButtonPressMask |
ButtonReleaseMask | KeyPressMask |
KeyReleaseMask);
XStoreName(dpy, win, "CMPS-3480 Lab1 Circles");
}
void checkResize(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 clearScreen(void)
{
XClearWindow(dpy, win);
}
void checkMouse(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) {
//Log("ButtonPress %i %i\n", 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;
}
}
void checkKeys(XEvent *e)
{
if (e->type != KeyPress)
return;
//A key was pressed
int key = XLookupKeysym(&e->xkey, 0);
switch (key) {
case XK_a:
break;
case XK_equal:
case XK_minus:
break;
case XK_Delete:
break;
case XK_Escape:
//quitting the program
g.done=1;
break;
}
clearScreen();
}
void physics(void)
{
}
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);
}
void showCircle(void)
{
//Draw a yellow pixel
//
setColor3i(255, 255, 0);
switch (g.circleType) {
case 0:
XDrawPoint(dpy, win, gc, 10, 10);
break;
}
}
void render(void)
{
showCircle();
}
|