home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / qube / xgfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-07  |  6.0 KB  |  231 lines

  1. /***
  2. ****  QuBE --- X-Windows graphical display routines.  These need work.
  3. ***/
  4.  
  5. #include "qube.h"
  6. #include "xgfx.h"
  7. #include <stdarg.h>
  8.  
  9. #include <X11/Xlib.h>
  10. #include <X11/Xutil.h>
  11. #include <X11/Xos.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/cursorfont.h>
  14. #include <X11/Xmu/Drawing.h>
  15. #include <X11/keysym.h>
  16.  
  17. #define STDEVENTS ExposureMask|KeyPressMask|ButtonPressMask|ButtonReleaseMask|FocusChangeMask| \
  18.                   StructureNotifyMask|PointerMotionMask|EnterWindowMask|LeaveWindowMask
  19.  
  20. char Sans8N[] = { "6x9" };
  21.  
  22. int MouseX;
  23. int MouseY;
  24.  
  25. static Display *display;
  26. static int screennum;
  27. static char *progname;
  28. static GC gc;
  29. static XGCValues gcvalues;
  30. static unsigned long gcvaluemask;
  31. static Cursor Arrow;
  32. static int FontCount;
  33. static XFontStruct *MainFont;
  34. static int displaywidth, displayheight;
  35. static Atom wm_delete_window, wm_protocols;
  36. static XEvent report;
  37. static Region region;
  38. static XRectangle rectangle;
  39. static Window qubewin;
  40. static Colormap colormap;
  41.  
  42. static XColor colorarray[16];
  43.  
  44. static unsigned int Reds[16] = { 0x0000, 0x0000, 0x0000, 0x0000, 0x7FFF, 0x7FFF, 0x7FFF, 0xAAAA,
  45.                 0x5555, 0x0000, 0x0000, 0x0000, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF };
  46. static unsigned int Grns[16] = { 0x0000, 0x0000, 0x7FFF, 0x7FFF, 0x0000, 0x0000, 0x3FFF, 0xAAAA,
  47.                 0x5555, 0x0000, 0xFFFF, 0xFFFF, 0x0000, 0x0000, 0xFFFF, 0xFFFF };
  48. static unsigned int Blus[16] = { 0x0000, 0x7FFF, 0x0000, 0x7FFF, 0x0000, 0x7FFF, 0x0000, 0xAAAA,
  49.                 0x5555, 0xFFFF, 0x0000, 0xFFFF, 0x0000, 0xFFFF, 0x0000, 0xFFFF };
  50.  
  51. static int KeyBuffer[128];
  52. static int KeyRead;
  53. static int KeyWrite;
  54.  
  55. int InitScrn(int mode)
  56. {
  57.         XWindowAttributes windinfo;
  58.         Screen *screenptr;
  59.         char *displayname = NULL;
  60.     int i;
  61.  
  62.     if (mode == M80x25x16) {
  63.         XDestroyWindow(display, qubewin);
  64.         XCloseDisplay(display);
  65.         return;
  66.     }
  67.  
  68.     if ((display = XOpenDisplay(displayname)) == NULL)
  69.         Error("Unable to connect to X server.");
  70.  
  71.     screennum = DefaultScreen(display);
  72.     screenptr = DefaultScreenOfDisplay(display);
  73.  
  74.     if (XGetWindowAttributes(display, RootWindow(display, screennum), &windinfo) == 0)
  75.                 Error("Could not obtain root window attributes.");
  76.     displaywidth = windinfo.width;
  77.     displayheight = windinfo.height;
  78.  
  79.     if ((MainFont = XLoadQueryFont(display, Sans8N)) == NULL)
  80.         Error("Error loading font \"6x9\".");
  81.  
  82.     gc = XCreateGC(display, RootWindow(display, screennum), GCForeground|GCBackground, &gcvalues);
  83.     XSetLineAttributes(display, gc, 1, LineSolid, CapRound, JoinRound);
  84.     XSetFont(display, gc, MainFont->fid);
  85.     XSetFunction(display, gc, GXxor);
  86.  
  87.     wm_protocols = XInternAtom(display, "WM_PROTOCOLS", 0);
  88.     wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", 0);
  89.  
  90.     qubewin = XCreateSimpleWindow(display, RootWindow(display, screennum), 0, 0,
  91.         640, 480, 1, BLACK, BLACK);
  92.  
  93.     region = XCreateRegion();
  94.  
  95.     Arrow = XCreateFontCursor(display, XC_left_ptr);
  96.  
  97.     XDefineCursor(display, qubewin, Arrow);
  98.     XSelectInput(display, qubewin, STDEVENTS);
  99.     XMapWindow(display, qubewin);
  100.  
  101.     colormap = XCreateColormap(display, qubewin, DefaultVisual(display, screennum), AllocAll);
  102.  
  103.     for (i = 0; i < 16; i++) {
  104.         colorarray[i].red = Reds[i];
  105.         colorarray[i].green = Grns[i];
  106.         colorarray[i].blue = Blus[i];
  107.         colorarray[i].flags = DoRed | DoGreen | DoBlue;
  108.         colorarray[i].pixel = i;
  109.     }
  110.  
  111.     XStoreColors(display, colormap, colorarray, 16);
  112.     XSetWindowColormap(display, qubewin, colormap);
  113.     XSync(display, 1);
  114.  
  115.     KeyRead = 0;
  116.     KeyWrite = 0;
  117.  
  118.     XFlush(display);
  119. }
  120.  
  121. int LineXOR(int x1, int y1, int x2, int y2, int color)
  122. {
  123.     XSetForeground(display, gc, color);
  124.     XDrawLine(display, qubewin, gc, x1, y1, x2, y2);
  125. }
  126.  
  127. int Gprintf(int x, int y, char *font, int color, char *format, ...)
  128. {
  129.     char string[1024];
  130.     va_list arg_ptr;
  131.     
  132.     va_start(arg_ptr, format);
  133.     XSetForeground(display, gc, color);
  134.     vsprintf(string, format, arg_ptr);
  135.  
  136.     XSetFunction(display, gc, GXcopy);
  137.         XDrawImageString(display, qubewin, gc, x, y, string, strlen(string));
  138.     XSetFunction(display, gc, GXxor);
  139. }
  140.  
  141. int BoxOutlnXOR(int x1, int y1, int x2, int y2, int color)
  142. {
  143.     XSetForeground(display, gc, color);
  144.     XDrawRectangle(display, qubewin, gc, x1, y1, x2-x1+1, y2-y1+1);
  145. }
  146.  
  147. int BoxFillXOR(int x1, int y1, int x2, int y2, int color)
  148. {
  149.     XSetForeground(display, gc, color);
  150.     XFillRectangle(display, qubewin, gc, x1, y1, x2-x1+1, y2-y1+1);
  151. }
  152.  
  153. int InitMouse(int width, int height, int xres, int yres)
  154. {
  155.  
  156. }
  157.  
  158. void StdCursor(int cursor)
  159. {
  160.  
  161. }
  162.  
  163. void KillMouse(void)
  164. {
  165.  
  166. }
  167.  
  168. void MouseOn(void)
  169. {
  170.  
  171. }
  172.  
  173. void MouseOff(void)
  174. {
  175.  
  176. }
  177.  
  178. void ReadMouse(void)
  179. {
  180.     KeySym keysym;
  181.  
  182.     XNextEvent(display, &report);
  183.  
  184.     switch(report.type) {
  185.     case Expose:
  186.         KeyBuffer[KeyWrite] = 0x7F;
  187.         KeyWrite = (KeyWrite + 1) & 0x7F;
  188.         break;
  189.     case KeyPress:
  190.         XLookupString(&report, NULL, 0, &keysym, NULL);
  191.  
  192.         switch (keysym) {
  193.         case XK_Left:    KeyBuffer[KeyWrite] = 0x4B; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  194.         case XK_Right:    KeyBuffer[KeyWrite] = 0x4D; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  195.         case XK_Up:    KeyBuffer[KeyWrite] = 0x48; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  196.         case XK_Down:    KeyBuffer[KeyWrite] = 0x50; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  197.         case XK_Escape:    KeyBuffer[KeyWrite] = 0x01; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  198.         case XK_equal:
  199.         case XK_plus:    KeyBuffer[KeyWrite] = 0x0D; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  200.         case XK_underscore:
  201.         case XK_minus:    KeyBuffer[KeyWrite] = 0x0C; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  202.         case XK_N:
  203.         case XK_n:    KeyBuffer[KeyWrite] = 0x31; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  204.         case XK_P:
  205.         case XK_p:    KeyBuffer[KeyWrite] = 0x19; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  206.         case XK_G:
  207.         case XK_g:    KeyBuffer[KeyWrite] = 0x22; KeyWrite = (KeyWrite + 1) & 0x7F; break;
  208.         }
  209.         break;
  210.     case MotionNotify:
  211.         MouseX = report.xmotion.x;
  212.         MouseY = report.xmotion.y;
  213.         break;
  214.     default:
  215.         break;
  216.     }
  217. }
  218.  
  219. int KeyStatus(void)
  220. {
  221.     return(KeyRead != KeyWrite);
  222. }
  223.  
  224. int ReadKeyScan(void)
  225. {
  226.     int c = KeyBuffer[KeyRead];
  227.     KeyRead = (KeyRead + 1) & 0x7F;
  228.     return(c);
  229. }
  230.  
  231.