ntk-chtheme: Add new color scheme. Save/restore selection color.
[ntk.git] / test / cube.cxx
blob22206ad3be7ef38f13069460c7125e99a44b5884
1 //
2 // "$Id: cube.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $"
3 //
4 // Another forms test program for the Fast Light Tool Kit (FLTK).
5 //
6 // Modified to have 2 cubes to test multiple OpenGL contexts
7 //
8 // Copyright 1998-2010 by Bill Spitzak and others.
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA.
25 // Please report all bugs and problems on the following page:
27 // http://www.fltk.org/str.php
30 #include <config.h>
31 #include <FL/Fl.H>
32 #include <FL/Fl_Window.H>
33 #include <FL/Fl_Box.H>
34 #include <FL/Fl_Button.H>
35 #include <FL/Fl_Radio_Light_Button.H>
36 #include <FL/Fl_Slider.H>
37 #include <stdlib.h>
39 #if !HAVE_GL
40 class cube_box : public Fl_Box {
41 public:
42 double lasttime;
43 int wire;
44 double size;
45 double speed;
46 cube_box(int x,int y,int w,int h,const char *l=0)
47 :Fl_Box(FL_DOWN_BOX,x,y,w,h,l){
48 label("This demo does\nnot work without GL");
51 #else
52 #include <FL/Fl_Gl_Window.H>
53 #include <FL/gl.h>
55 class cube_box : public Fl_Gl_Window {
56 void draw();
57 int handle(int);
58 public:
59 double lasttime;
60 int wire;
61 double size;
62 double speed;
63 cube_box(int x,int y,int w,int h,const char *l=0)
64 : Fl_Gl_Window(x,y,w,h,l) {lasttime = 0.0;}
67 /* The cube definition */
68 float v0[3] = {0.0, 0.0, 0.0};
69 float v1[3] = {1.0, 0.0, 0.0};
70 float v2[3] = {1.0, 1.0, 0.0};
71 float v3[3] = {0.0, 1.0, 0.0};
72 float v4[3] = {0.0, 0.0, 1.0};
73 float v5[3] = {1.0, 0.0, 1.0};
74 float v6[3] = {1.0, 1.0, 1.0};
75 float v7[3] = {0.0, 1.0, 1.0};
77 #define v3f(x) glVertex3fv(x)
79 void drawcube(int wire) {
80 /* Draw a colored cube */
81 glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
82 glColor3ub(0,0,255);
83 v3f(v0); v3f(v1); v3f(v2); v3f(v3);
84 glEnd();
85 glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
86 glColor3ub(0,255,255); v3f(v4); v3f(v5); v3f(v6); v3f(v7);
87 glEnd();
88 glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
89 glColor3ub(255,0,255); v3f(v0); v3f(v1); v3f(v5); v3f(v4);
90 glEnd();
91 glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
92 glColor3ub(255,255,0); v3f(v2); v3f(v3); v3f(v7); v3f(v6);
93 glEnd();
94 glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
95 glColor3ub(0,255,0); v3f(v0); v3f(v4); v3f(v7); v3f(v3);
96 glEnd();
97 glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
98 glColor3ub(255,0,0); v3f(v1); v3f(v2); v3f(v6); v3f(v5);
99 glEnd();
102 void cube_box::draw() {
103 lasttime = lasttime+speed;
104 if (!valid()) {
105 glLoadIdentity();
106 glViewport(0,0,w(),h());
107 glEnable(GL_DEPTH_TEST);
108 glFrustum(-1,1,-1,1,2,10000);
109 glTranslatef(0,0,-10);
110 gl_font(FL_HELVETICA_BOLD, 16 );
112 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
113 glPushMatrix();
114 glRotatef(float(lasttime*1.6),0,0,1);
115 glRotatef(float(lasttime*4.2),1,0,0);
116 glRotatef(float(lasttime*2.3),0,1,0);
117 glTranslatef(-1.0, 1.2f, -1.5);
118 glScalef(float(size),float(size),float(size));
119 drawcube(wire);
120 glPopMatrix();
121 gl_color(FL_GRAY);
122 glDisable(GL_DEPTH_TEST);
123 gl_draw(wire ? "Cube: wire" : "Cube: flat", -4.5f, -4.5f );
124 glEnable(GL_DEPTH_TEST);
127 int cube_box::handle(int e) {
128 switch (e) {
129 case FL_ENTER: cursor(FL_CURSOR_CROSS); break;
130 case FL_LEAVE: cursor(FL_CURSOR_DEFAULT); break;
132 return Fl_Gl_Window::handle(e);
135 #endif
137 Fl_Window *form;
138 Fl_Slider *speed, *size;
139 Fl_Button *button, *wire, *flat;
140 cube_box *cube, *cube2;
142 void makeform(const char *name) {
143 form = new Fl_Window(510+390,390,name);
144 new Fl_Box(FL_DOWN_FRAME,20,20,350,350,"");
145 new Fl_Box(FL_DOWN_FRAME,510,20,350,350,"");
146 speed = new Fl_Slider(FL_VERT_SLIDER,390,90,40,220,"Speed");
147 size = new Fl_Slider(FL_VERT_SLIDER,450,90,40,220,"Size");
148 wire = new Fl_Radio_Light_Button(390,20,100,30,"Wire");
149 flat = new Fl_Radio_Light_Button(390,50,100,30,"Flat");
150 button = new Fl_Button(390,340,100,30,"Exit");
151 cube = new cube_box(23,23,344,344, 0);
152 cube2 = new cube_box(513,23,344,344, 0);
153 Fl_Box *b = new Fl_Box(FL_NO_BOX,cube->x(),size->y(),
154 cube->w(),size->h(),0);
155 form->resizable(b);
156 b->hide();
157 form->end();
160 // added to demo printing
161 #include <FL/Fl_Sys_Menu_Bar.H>
162 #include <FL/Fl_Printer.H>
164 void print_cb(Fl_Widget *w, void *data)
166 Fl_Printer printer;
167 Fl_Window *win = Fl::first_window();
168 if(!win) return;
169 if( printer.start_job(1) ) return;
170 if( printer.start_page() ) return;
171 printer.scale(0.5,0.5);
172 printer.print_widget( win );
173 printer.end_page();
174 printer.end_job();
176 // end of printing demo
178 int main(int argc, char **argv) {
179 makeform(argv[0]);
180 // added to demo printing
181 form->begin();
182 static Fl_Menu_Item items[] = {
183 { "Print", 0, 0, 0, FL_SUBMENU },
184 { "Print window", 0, print_cb, 0, 0 },
185 { 0 },
186 { 0 }
188 Fl_Sys_Menu_Bar *menubar_;
189 menubar_ = new Fl_Sys_Menu_Bar(0, 0, 60, 20);
190 menubar_->box(FL_FLAT_BOX);
191 menubar_->menu(items);
192 form->end();
193 // end of printing demo
194 speed->bounds(4,0);
195 speed->value(cube->speed = cube2->speed = 1.0);
196 size->bounds(4,0.01);
197 size->value(cube->size = cube2->size = 1.0);
198 flat->value(1); cube->wire = 0; cube2->wire = 1;
199 form->label("cube");
200 form->show(argc,argv);
201 cube->show();
202 cube2->show();
203 #if 0
204 // This demonstrates how to manipulate OpenGL contexts.
205 // In this case the same context is used by multiple windows (I'm not
206 // sure if this is allowed on Win32, can somebody check?).
207 // This fixes a bug on the XFree86 3.0 OpenGL where only one context
208 // per program seems to work, but there are probably better uses for
209 // this!
210 cube->make_current(); // causes context to be created
211 cube2->context(cube->context()); // share the contexts
212 #endif
213 for (;;) {
214 if (form->visible() && speed->value())
215 {if (!Fl::check()) break;} // returns immediately
216 else
217 {if (!Fl::wait()) break;} // waits until something happens
218 cube->wire = wire->value();
219 cube2->wire = !wire->value();
220 cube->size = cube2->size = size->value();
221 cube->speed = cube2->speed = speed->value();
222 cube->redraw();
223 cube2->redraw();
224 if (Fl::readqueue() == button) break;
226 return 0;
230 // End of "$Id: cube.cxx 8033 2010-12-15 12:11:16Z AlbrechtS $".