themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / gl_start.cxx
bloba52a45766232f73b7c4425ec9d5459fd9ad2b1c9
1 //
2 // "$Id: gl_start.cxx 8368 2011-02-04 23:32:53Z manolo $"
3 //
4 // OpenGL context routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // You MUST use gl_visual() to select the default visual before doing
29 // show() of any windows. Mesa will crash if you try to use a visual
30 // not returned by glxChooseVisual.
32 // This does not work with Fl_Double_Window's! It will try to draw
33 // into the front buffer. Depending on the system this will either
34 // crash or do nothing (when pixmaps are being used as back buffer
35 // and GL is being done by hardware), work correctly (when GL is done
36 // with software, such as Mesa), or draw into the front buffer and
37 // be erased when the buffers are swapped (when double buffer hardware
38 // is being used)
40 #include <config.h>
41 #if HAVE_GL
43 #include <FL/Fl.H>
44 #include <FL/Fl_Window.H>
45 #include <FL/x.H>
46 #include <FL/fl_draw.H>
47 #include "Fl_Gl_Choice.H"
49 static GLContext context;
50 static int clip_state_number=-1;
51 static int pw, ph;
53 #ifdef WIN32
54 static Fl_Gl_Choice* gl_choice;
55 #endif
57 #ifdef __APPLE__
58 static Fl_Gl_Choice* gl_choice;
59 #endif
61 Fl_Region XRectangleRegion(int x, int y, int w, int h); // in fl_rect.cxx
63 /** Creates an OpenGL context */
64 void gl_start() {
65 if (!context) {
66 #if defined(USE_X11)
67 context = fl_create_gl_context(fl_visual);
68 #elif defined(WIN32)
69 if (!gl_choice) Fl::gl_visual(0);
70 context = fl_create_gl_context(Fl_Window::current(), gl_choice);
71 #elif defined(__APPLE_QUARTZ__)
72 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
73 context = fl_create_gl_context(Fl_Window::current(), gl_choice);
74 #else
75 # error Unsupported platform
76 #endif
78 fl_set_gl_context(Fl_Window::current(), context);
79 #if !defined(WIN32) && !defined(__APPLE__)
80 glXWaitX();
81 #endif
82 if (pw != Fl_Window::current()->w() || ph != Fl_Window::current()->h()) {
83 pw = Fl_Window::current()->w();
84 ph = Fl_Window::current()->h();
85 glLoadIdentity();
86 glViewport(0, 0, pw, ph);
87 glOrtho(0, pw, 0, ph, -1, 1);
88 glDrawBuffer(GL_FRONT);
90 if (clip_state_number != fl_graphics_driver->fl_clip_state_number) {
91 clip_state_number = fl_graphics_driver->fl_clip_state_number;
92 int x, y, w, h;
93 if (fl_clip_box(0, 0, Fl_Window::current()->w(), Fl_Window::current()->h(),
94 x, y, w, h)) {
95 fl_clip_region(XRectangleRegion(x,y,w,h));
96 glScissor(x, Fl_Window::current()->h()-(y+h), w, h);
97 glEnable(GL_SCISSOR_TEST);
98 } else {
99 glDisable(GL_SCISSOR_TEST);
104 /** Releases an OpenGL context */
105 void gl_finish() {
106 glFlush();
107 #if !defined(WIN32) && !defined(__APPLE__)
108 glXWaitGL();
109 #endif
112 int Fl::gl_visual(int mode, int *alist) {
113 Fl_Gl_Choice *c = Fl_Gl_Choice::find(mode,alist);
114 if (!c) return 0;
115 #if defined(USE_X11)
116 fl_visual = c->vis;
117 fl_colormap = c->colormap;
118 #elif defined(WIN32)
119 gl_choice = c;
120 #elif defined(__APPLE_QUARTZ__)
121 // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
122 gl_choice = c;
123 #else
124 # error Unsupported platform
125 #endif
126 return 1;
128 #endif
131 // End of "$Id: gl_start.cxx 8368 2011-02-04 23:32:53Z manolo $".