Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

displaycard_glx.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: displaycard_glx.cpp,v 1.4 2001/02/17 17:12:10 plasmoid Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #ifdef USE_OPENGL
00016 
00017 #include "displaycard_glx.h"
00018 #include "GL/Generic/blit_gl_generic.h"
00019 #include "API/Core/System/cl_assert.h"
00020 #include "API/Application/clanapp.h"
00021 #include "../../Core/System/Unix/init_linux.h"
00022 
00023 CL_GLX_DisplayCard::CL_GLX_DisplayCard(
00024         Display *dpy,
00025         Window root,
00026         int card_no) :
00027 CL_GL_DisplayCard_Generic(card_no), resolution(dpy, DefaultScreen(dpy))
00028 {
00029         this->root = root;
00030         this->dpy = dpy;
00031 
00032         if (CL_Force_DispTarget::get_name()=="glx")
00033                 m_uses_gl = true;
00034         else
00035                 m_uses_gl = false;
00036 
00037         initialized = false;
00038         max_texture_size = 0;
00039 
00040         // set reasonable default values
00041         memset(&gl_config, 0, sizeof(gl_config));
00042         gl_config.buffer_size = 16;
00043         gl_config.depth_size = 16;
00044         gl_config.rgba = 1;
00045         gl_config.doublebuffer = 1;
00046 }
00047 
00048 CL_GLX_DisplayCard::~CL_GLX_DisplayCard()
00049 {
00050         if (initialized)
00051         {
00052                 glXDestroyContext(dpy, ctx);
00053                 XDestroyWindow(dpy, win);
00054         }
00055 }
00056 
00057 void CL_GLX_DisplayCard::flip_display(bool sync)
00058 {
00059         glXSwapBuffers(get_display(), get_window());
00060 }
00061 
00062 void CL_GLX_DisplayCard::put_display(const class CL_Rect &rect)
00063 {
00064         cl_assert(false); // hmm... how do you do this under opengl? :)
00065 }
00066 
00067 void CL_GLX_DisplayCard::set_videomode(
00068         int width,
00069         int height,
00070         int bpp,
00071         bool fullscreen,
00072         bool allow_resize,
00073         bool video_memory)
00074 {       
00075         fullscreen = false;
00076         this->fullscreen = fullscreen;
00077         if (initialized) // clean up
00078         {
00079                 //end_2d();
00080                 glXDestroyContext(dpy, ctx);
00081                 XDestroyWindow(dpy, win);
00082                 initialized = false;
00083         }
00084 
00085         set_gfxmode(width, height, bpp, fullscreen, allow_resize);
00086 
00087         int dummy;
00088         if (!glXQueryExtension(dpy, &dummy, &dummy))
00089                 cl_assert(false);
00090 
00091         static int gl_attribs[20];
00092         memset(&gl_attribs, None, 20*sizeof(int));
00093 
00094         // todo: figure out what GLX_LEVEL is
00095 
00096         int i = 0;
00097         if (gl_config.use_gl)
00098                 gl_attribs[i++] = GLX_USE_GL;
00099         if (gl_config.rgba)
00100                 gl_attribs[i++] = GLX_RGBA;
00101         if (gl_config.doublebuffer)
00102                 gl_attribs[i++] = GLX_DOUBLEBUFFER;
00103         if (gl_config.stereo)
00104                 gl_attribs[i++] = GLX_STEREO;
00105         if (gl_config.aux_buffers)
00106                 gl_attribs[i++] = GLX_AUX_BUFFERS;
00107 
00108         if (gl_config.buffer_size)
00109         {
00110                 gl_attribs[i++] = GLX_BUFFER_SIZE;
00111                 gl_attribs[i++] = gl_config.buffer_size;
00112         }
00113         if (gl_config.red_size)
00114         {
00115                 gl_attribs[i++] = GLX_RED_SIZE;
00116                 gl_attribs[i++] = gl_config.red_size;
00117         }
00118         if (gl_config.green_size)
00119         {
00120                 gl_attribs[i++] = GLX_GREEN_SIZE;
00121                 gl_attribs[i++] = gl_config.green_size;
00122         }
00123         if (gl_config.blue_size)
00124         {
00125                 gl_attribs[i++] = GLX_BLUE_SIZE;
00126                 gl_attribs[i++] = gl_config.blue_size;
00127         }
00128         if (gl_config.alpha_size)
00129         {
00130                 gl_attribs[i++] = GLX_ALPHA_SIZE;
00131                 gl_attribs[i++] = gl_config.alpha_size;
00132         }
00133         if (gl_config.depth_size)
00134         {
00135                 gl_attribs[i++] = GLX_DEPTH_SIZE;
00136                 gl_attribs[i++] = gl_config.depth_size;
00137         }
00138         if (gl_config.stencil_size)
00139         {
00140                 gl_attribs[i++] = GLX_STENCIL_SIZE;
00141                 gl_attribs[i++] = gl_config.stencil_size;
00142         }
00143         if (gl_config.accum_red_size)
00144         {
00145                 gl_attribs[i++] = GLX_ACCUM_RED_SIZE;
00146                 gl_attribs[i++] = gl_config.accum_red_size;
00147         }
00148         if (gl_config.accum_green_size)
00149         {
00150                 gl_attribs[i++] = GLX_ACCUM_GREEN_SIZE;
00151                 gl_attribs[i++] = gl_config.accum_green_size;
00152         }
00153         if (gl_config.accum_blue_size)
00154         {
00155                 gl_attribs[i++] = GLX_ACCUM_BLUE_SIZE;
00156                 gl_attribs[i++] = gl_config.accum_blue_size;
00157         }
00158         if (gl_config.accum_alpha_size)
00159         {
00160                 gl_attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
00161                 gl_attribs[i++] = gl_config.accum_alpha_size;
00162         }
00163 
00164         XVisualInfo *visual_info = glXChooseVisual(
00165                 dpy,
00166                 DefaultScreen(dpy),
00167                 gl_attribs);
00168 
00169         cl_assert(visual_info != NULL);
00170 
00171         ctx = glXCreateContext(
00172                 dpy,
00173                 visual_info,
00174                 None,
00175                 GL_TRUE);
00176 
00177         cl_assert(ctx != NULL);
00178 
00179         Colormap cmap = XCreateColormap(
00180                 dpy,
00181                 RootWindow(dpy, visual_info->screen),
00182                 visual_info->visual,
00183                 AllocNone);
00184 
00185         XSetWindowAttributes attr;
00186         attr.colormap = cmap;
00187         attr.background_pixel = BlackPixel(dpy, DefaultScreen(dpy));
00188         attr.override_redirect = False;
00189         int attr_flags =
00190                 CWOverrideRedirect |
00191                 CWColormap |
00192                 CWBackPixel;
00193 
00194         win_width = width;
00195         win_height = height;
00196         if (fullscreen)
00197         {
00198                 resolution.set_mode(width, height);
00199                 win_width = resolution.get_width();
00200                 win_height = resolution.get_height();
00201                 attr.override_redirect = True;
00202         }
00203 
00204         win = XCreateWindow(
00205                 dpy,
00206                 RootWindow(dpy, visual_info->screen),
00207                 0,
00208                 0,
00209                 win_width,
00210                 win_height,
00211                 0,
00212                 visual_info->depth,
00213                 InputOutput,
00214                 visual_info->visual,
00215                 attr_flags,
00216                 &attr);
00217         
00218         cl_assert(win);
00219 
00220         char *title = CL_ClanApplication::app->get_title();
00221         XTextProperty text_property;
00222         XStringListToTextProperty(&title, 1, &text_property);
00223 
00224         XSizeHints size_hints;
00225         size_hints.x = 0;
00226         size_hints.y = 0;
00227         size_hints.width = win_width;
00228         size_hints.height = win_height;
00229         size_hints.flags = PSize | PMinSize | PMaxSize;
00230         size_hints.min_width = win_width;
00231         size_hints.min_height = win_height;
00232         size_hints.max_width = win_width;
00233         size_hints.max_height = win_height;
00234 
00235         XSetWMProperties(
00236                 dpy,
00237                 win,
00238                 &text_property,
00239                 &text_property,
00240                 0,
00241                 0,
00242                 &size_hints,
00243                 0,
00244                 0);
00245 
00246         glXMakeCurrent(dpy, win, ctx);
00247         XMapRaised(dpy, win);
00248 
00249         if (fullscreen)
00250         {
00251                 XSetInputFocus(dpy, win, RevertToParent, CurrentTime);
00252                 XGrabPointer(dpy, win, true, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
00253         }
00254 
00255         // Listen for keystrokes...
00256         // ------------------------
00257 
00258         XSelectInput(dpy, win, KeyPressMask|KeyReleaseMask);
00259 
00260         glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
00261         
00262 #ifdef DEBUG
00263         // Output OpenGL vendor & renderer
00264         // -------------------------------
00265         
00266         cout << endl;
00267         cout << "OpenGL status" << endl << endl;
00268         cout << "Vendor     : " << glGetString(GL_VENDOR) << endl;
00269         cout << "Renderer   : " << glGetString(GL_RENDERER) << endl;
00270         cout << "Version    : " << glGetString(GL_VERSION) << endl;
00271         cout << "Extensions : " << glGetString(GL_EXTENSIONS) << endl << endl;
00272 
00273         int bufferSize, level, renderType, doubleBuffer, stereo, auxBuffers, redSize, greenSize;
00274         int blueSize, alphaSize, depthSize, stencilSize, acRedSize, acGreenSize, acBlueSize, acAlphaSize;
00275 
00276         glXGetConfig(dpy, visual_info, GLX_BUFFER_SIZE, &bufferSize);
00277         glXGetConfig(dpy, visual_info, GLX_LEVEL, &level);
00278         glXGetConfig(dpy, visual_info, GLX_RGBA, &renderType);
00279         glXGetConfig(dpy, visual_info, GLX_DOUBLEBUFFER, &doubleBuffer);
00280         glXGetConfig(dpy, visual_info, GLX_STEREO, &stereo);
00281         glXGetConfig(dpy, visual_info, GLX_AUX_BUFFERS, &auxBuffers);
00282         glXGetConfig(dpy, visual_info, GLX_RED_SIZE, &redSize);
00283         glXGetConfig(dpy, visual_info, GLX_GREEN_SIZE, &greenSize);
00284         glXGetConfig(dpy, visual_info, GLX_BLUE_SIZE, &blueSize);
00285         glXGetConfig(dpy, visual_info, GLX_ALPHA_SIZE, &alphaSize);
00286         glXGetConfig(dpy, visual_info, GLX_DEPTH_SIZE, &depthSize);
00287         glXGetConfig(dpy, visual_info, GLX_STENCIL_SIZE, &stencilSize);
00288         glXGetConfig(dpy, visual_info, GLX_ACCUM_RED_SIZE, &acRedSize); 
00289         glXGetConfig(dpy, visual_info, GLX_ACCUM_GREEN_SIZE, &acGreenSize);
00290         glXGetConfig(dpy, visual_info, GLX_ACCUM_BLUE_SIZE, &acBlueSize);
00291         glXGetConfig(dpy, visual_info, GLX_ACCUM_ALPHA_SIZE, &acAlphaSize);
00292 
00293         cout << "GLX status" << endl << endl;
00294 
00295         cout << " visual class: ";
00296         switch (visual_info->c_class)
00297         {
00298                 case StaticGray  : { cout << "StaticGray" << endl; break; }
00299                 case GrayScale   : { cout << "GrayScale" << endl; break; }
00300                 case StaticColor : { cout << "StaticColor" << endl; break; }
00301                 case PseudoColor : { cout << "PseudoColor" << endl; break; }
00302                 case TrueColor   : { cout << "TrueColor" << endl; break; }
00303                 case DirectColor : { cout << "DirectColor" << endl; break; }
00304                 default          : { cout << "unknown " << endl; }
00305         }
00306 
00307         cout << "visual depth: " << visual_info->depth << endl << endl;
00308         if (glXIsDirect(dpy, ctx)) 
00309                 cout << "direct rendering" << endl;
00310         if (renderType)
00311                 cout << "RGBA" << endl;
00312         if (doubleBuffer)
00313                 cout << "double buffer" << endl;
00314         if (stereo)
00315                 cout << "stereo buffer" << endl;
00316         cout << endl;
00317 
00318         cout << "GLX_BUFFER_SIZE      : " << bufferSize << endl;
00319         cout << "GLX_LEVEL            : " <<  level << endl;
00320 //      cout << "GLX_RGBA             : " << renderType << endl;
00321 //      cout << "GLX_DOUBELBUFFER     : " << doubleBuffer << endl;
00322 //      cout << "GLX_STEREO           : " << stereo << endl;
00323         cout << "GLX_AUX_BUFFERS      : " << auxBuffers << endl;
00324         cout << "GLX_RED_SIZE         : " << redSize << endl;
00325         cout << "GLX_GREEN_SIZE       : " << greenSize << endl;
00326         cout << "GLX_BLUE_SIZE        : " << blueSize << endl;
00327         cout << "GLX_ALPHA_SIZE       : " << alphaSize << endl;
00328         cout << "GLX_DEPTH_SIZE       : " << depthSize << endl;
00329         cout << "GLX_STENCIL_SIZE     : " << stencilSize << endl;
00330         cout << "GLX_ACCUM_RED_SIZE   : " << acRedSize << endl; 
00331         cout << "GLX_ACCUM_GREEN_SIZE : " << acGreenSize << endl;
00332         cout << "GLX_ACCUM_BLUE_SIZE  : " << acBlueSize << endl;
00333         cout << "GLX_ACCUM_ALPHA_SIZE : " << acAlphaSize << endl;
00334         cout << endl << endl;
00335         cout << "GL_MAX_TEXTURE_SIZE  : " << max_texture_size << endl;
00336         cout << endl << endl;
00337 
00338 #endif
00339 
00340         // Setup OpenGL sanity:
00341         // --------------------
00342         if (!m_uses_gl) 
00343                 begin_2d();
00344 
00345         initialized = true;
00346         clear_display(0.0, 0.0, 0.0, 1.0);
00347 }
00348 
00349 #endif

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001