1 /*
2 * glxwindow.cc
3 * jcorso@__STOPSPAMMINGME__.cs.jhu.edu
4 * original xwindow code from darius burschka
5 *
6 */
7
8 #include <iostream.h>
9 #include <math.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "glxwindow.h"
14
15
16 static char *color_names[]=
17 {
18 "white","green","red","yellow","goldenrod"
19 };
20
21
22 void glxwindow::drawtest(void)
23 {
24 glClearColor(0.0,0.0,0.0,0.0);
25 glClear(GL_COLOR_BUFFER_BIT);
26
27 glPointSize(5);
28 glBegin(GL_POINTS);
29
30 glColor3f(1.0,1.0,1.0);
31 glVertex2i(0,0);
32
33 glColor3f(0.0,1.0,1.0);
34 glVertex2i(lin/2,col/2);
35
36 glColor3f(1.0,0.0,0.0);
37 glVertex2i(100,100);
38 glVertex2i(100,200);
39 glVertex2i(200,100);
40
41 glEnd();
42
43 glXSwapBuffers(dpy,window);
44 glFlush();
45 }
46
47 void glxwindow::get_colors(void)
48 {
49 XColor xcolor,near_color;
50 int i;
51
52 // allocate draw colors
53 for(i=0;i<DRAW_COLORS;i++)
54 {
55 XAllocNamedColor(dpy,DefaultColormap(dpy,DefaultScreen(dpy)),
56 ^I^I^I color_names[i],&near_color,&xcolor);
57 draw_color[i]=near_color.pixel;
58 }
59 }
60
61
62 void glxwindow :: map() {
63 if(window) {
64 XMapWindow(dpy,window);XFlush(dpy);
65 XResizeWindow(dpy,window,lin,col);
66 XFlush(dpy);
67
68 glXMakeCurrent(dpy,window,glxc);
69 glMatrixMode(GL_PROJECTION);
70 glOrtho(0,col,0,lin,0,2000);
71 glViewport(0,0,col,lin);
72 glMatrixMode(GL_MODELVIEW);
73 glLoadIdentity();
74 glFlush();
75 }
76 }
77
78 void glxwindow::print_string(char *string,int x, int y,int col_index,
79 int font_index)
80 {
81 //XTextItem text={string,strlen(string),0,font[font_index]};
82 XTextItem text={string,strlen(string),0,0};
83 XDrawText(dpy,window,(GC)gc_window[col_index],x,y,&text,1);
84 }
85
86
87
88 glxwindow::glxwindow(WinSpec *spec)
89 {
90 XSizeHints^I^Ihints;
91 Visual^I^Ivisual;
92 XSetWindowAttributes^Ixswa;
93 int^I^I^Ibg_pix,fg_pix;
94
95 // defaults
96 window=0;
97 col=MAX_WIDTH;
98 lin=MAX_HEIGTH;
99
100 cout << "Xwindow created with (rows,cols) " << lin << "," << col << endl;
101
102 // open window
103 if (!(dpy = XOpenDisplay(spec->display)))
104 {
105 perror("Cannot open display\n");
106 return;
107 }
108
109 bg_pix = WhitePixel(dpy, DefaultScreen(dpy));
110 fg_pix = BlackPixel(dpy, DefaultScreen(dpy));
111 xswa.backing_store = NotUseful;
112 xswa.background_pixel = bg_pix;
113 xswa.border_pixel = fg_pix;
114 xswa.colormap = DefaultColormap(dpy,DefaultScreen(dpy));
115 visual.visualid = CopyFromParent;
116 window=XCreateWindow(dpy,
117 ^IRootWindow(dpy, DefaultScreen(dpy)),
118 ^Ispec->posx,spec->posy,col,lin,1,
119 ^IDefaultDepth(dpy,DefaultScreen(dpy)), InputOutput,&visual,
120 ^ICWBackingStore|CWBorderPixel|CWBackPixel|CWColormap,
121 ^I&xswa);
122 hints.flags=USPosition|PMinSize|PMaxSize;
123 hints.x=spec->posx;
124 hints.y=spec->posy;
125 hints.min_width=MIN_WIDTH;
126 hints.min_height=MIN_HEIGTH;
127 hints.max_width=MAX_WIDTH;
128 hints.max_height=MAX_HEIGTH;
129 XSetNormalHints(dpy,window,&hints);
130 XChangeProperty(dpy,window,XA_WM_NAME, XA_STRING,8,
131 PropModeReplace, (unsigned char *)spec->window_title,
132 strlen(spec->window_title));
133 XSelectInput(dpy,window,ButtonPressMask);
134 get_colors();
135 xgcv.background = bg_pix;
136 xgcv.function=GXcopy;
137 xgcv.line_width=0;
138 for(int i=0;i<DRAW_COLORS;i++)
139 {
140 xgcv.foreground = draw_color[i];
141 gc_window[i]=(GContext)XCreateGC(dpy,window,
142 ^IGCFunction|GCForeground | GCBackground,&xgcv);
143
144 }
145 xgcv.foreground = bg_pix;
146 gc_clear=(GContext)XCreateGC(dpy,window,
147 ^IGCFunction|GCForeground | GCBackground,&xgcv);
148
149 int attrlist[] = {
150 GLX_RGBA,
151 GLX_RED_SIZE,8,
152 GLX_GREEN_SIZE,8,
153 GLX_BLUE_SIZE,8,
154 GLX_DEPTH_SIZE,8,
155 GLX_DOUBLEBUFFER,
156 None
157 };
158
159 char *str;
160 str = (char*)glXGetClientString(dpy,GLX_VERSION);
161 printf("glxwindow: found glx %s\n",str);
162 str = (char*)glXGetClientString(dpy,GLX_VENDOR);
163 printf("glxwindow: glx vendor %s\n",str);
164 str = (char*)glXGetClientString(dpy,GLX_EXTENSIONS);
165 printf("glxwindow: glx extensions %s\n\n",str);
166
167 xvi = glXChooseVisual(dpy,0,attrlist);
168 if (!xvi)
169 printf("glxwindow: could not create the XVisualInfo for glx\n");
170 glxc = glXCreateContext(dpy,xvi,NULL,true);
171 if (!glxc)
172 printf("glxwindow: could not create the glx rendering context\n");
173
174 }
175
176 glxwindow::~glxwindow(void)
177 {
178 if(window)
179 {
180 glXDestroyContext(dpy,glxc);
181 XDestroyWindow(dpy,window);
182 }
183 }