fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

gl_start.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: gl_start.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $"
00003 //
00004 // OpenGL context routines for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 // You MUST use gl_visual() to select the default visual before doing
00029 // show() of any windows.  Mesa will crash if you try to use a visual
00030 // not returned by glxChooseVisual.
00031 
00032 // This does not work with Fl_Double_Window's!  It will try to draw
00033 // into the front buffer.  Depending on the system this will either
00034 // crash or do nothing (when pixmaps are being used as back buffer
00035 // and GL is being done by hardware), work correctly (when GL is done
00036 // with software, such as Mesa), or draw into the front buffer and
00037 // be erased when the buffers are swapped (when double buffer hardware
00038 // is being used)
00039 
00040 #include <config.h>
00041 #if HAVE_GL
00042 
00043 #include <FL/Fl.H>
00044 #include <FL/Fl_Window.H>
00045 #include <FL/x.H>
00046 #include <FL/fl_draw.H>
00047 #include "Fl_Gl_Choice.H"
00048 
00049 extern int fl_clip_state_number; // in fl_rect.cxx
00050 
00051 static GLContext context;
00052 static int clip_state_number=-1;
00053 static int pw, ph;
00054 
00055 #ifdef WIN32
00056 static Fl_Gl_Choice* gl_choice;
00057 #endif
00058 
00059 #ifdef __APPLE__
00060 static Fl_Gl_Choice* gl_choice;
00061 #endif
00062 
00063 Fl_Region XRectangleRegion(int x, int y, int w, int h); // in fl_rect.cxx
00064 
00066 void gl_start() {
00067   if (!context) {
00068 #if defined(USE_X11)
00069     context = fl_create_gl_context(fl_visual);
00070 #elif defined(WIN32)
00071     if (!gl_choice) Fl::gl_visual(0);
00072     context = fl_create_gl_context(Fl_Window::current(), gl_choice);
00073 #elif defined(__APPLE_QUARTZ__)
00074     // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
00075     context = fl_create_gl_context(Fl_Window::current(), gl_choice);
00076 #else
00077 #  error Unsupported platform
00078 #endif
00079   }
00080   fl_set_gl_context(Fl_Window::current(), context);
00081 #if !defined(WIN32) && !defined(__APPLE__)
00082   glXWaitX();
00083 #endif
00084   if (pw != Fl_Window::current()->w() || ph != Fl_Window::current()->h()) {
00085     pw = Fl_Window::current()->w();
00086     ph = Fl_Window::current()->h();
00087     glLoadIdentity();
00088     glViewport(0, 0, pw, ph);
00089     glOrtho(0, pw, 0, ph, -1, 1);
00090     glDrawBuffer(GL_FRONT);
00091   }
00092   if (clip_state_number != fl_clip_state_number) {
00093     clip_state_number = fl_clip_state_number;
00094     int x, y, w, h;
00095     if (fl_clip_box(0, 0, Fl_Window::current()->w(), Fl_Window::current()->h(),
00096                     x, y, w, h)) {
00097       fl_clip_region(XRectangleRegion(x,y,w,h));
00098       glScissor(x, Fl_Window::current()->h()-(y+h), w, h);
00099       glEnable(GL_SCISSOR_TEST);
00100     } else {
00101       glDisable(GL_SCISSOR_TEST);
00102     }
00103   }
00104 }
00105 
00107 void gl_finish() {
00108   glFlush();
00109 #if !defined(WIN32) && !defined(__APPLE__)
00110   glXWaitGL();
00111 #endif
00112 }
00113 
00114 int Fl::gl_visual(int mode, int *alist) {
00115   Fl_Gl_Choice *c = Fl_Gl_Choice::find(mode,alist);
00116   if (!c) return 0;
00117 #if defined(USE_X11)
00118   fl_visual = c->vis;
00119   fl_colormap = c->colormap;
00120 #elif defined(WIN32)
00121   gl_choice = c;
00122 #elif defined(__APPLE_QUARTZ__)
00123   // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
00124   gl_choice = c;
00125 #else
00126 #  error Unsupported platform
00127 #endif
00128   return 1;
00129 }
00130 #endif
00131 
00132 //
00133 // End of "$Id: gl_start.cxx 7913 2010-11-29 18:18:27Z greg.ercolano $".
00134 //