FLTK logo

Re: GL windows in Fl_Tile

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.opengl  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: GL windows in Fl_Tile Greg Ercolano Dec 15, 2011  
 
	I wouldn't derive from Fl_Window, as that implies your
	coordinate space origin gets reset to zero. Changed that
	to Fl_Group.

	Also, fixed your yellow borders a bit; your code wasn't
	consistent with the +1/-1 offsets to keep the border onscreen.

	Revised code; see CHANGED lines:

---------------------------------------------------------------------------

/**
  Test by Robert Strickland to tile four OpenGL windows.
  Based on Erco's cheat:
  http://seriss.com/people/erco/fltk/#GL4PortResizable
  but without the code to do the proportional resizing
*/
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Tile.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/gl.h>

/**
  A Fl_Gl_Window that draws an X in a box
*/
class FLGL : public Fl_Gl_Window {
public:
  FLGL(int x, int y, int w, int h, const char* l=0) :
      Fl_Gl_Window(x, y, w, h, l) {
    end();
  }
protected:
  void draw() {
    if ( !valid() ) {
      // First time? init viewport, etc.
      valid(1);
      glLoadIdentity();
      glViewport(0, 0, w(), h());
      glOrtho(-w(), w(), -h(), h(), -1, 1);
    }
    // Clear screen
    glClear(GL_COLOR_BUFFER_BIT);
    // Draw white 'X'
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    glVertex2f( w(),  h());
    glVertex2f(-w(), -h());
    glVertex2f( w(), -h());
    glVertex2f(-w(),  h());
    glEnd();
    // Draw yellow border last, around the outer edge
    glColor3f(1.0, 1.0, 0.0);
    glBegin(GL_LINE_LOOP);
    glVertex2f(-w()+1, -h()+1);         // CHANGED
    glVertex2f( w()-1, -h()+1);         // CHANGED
    glVertex2f( w()-1,  h()-1);         // CHANGED
    glVertex2f(-w()+1,  h()-1);         // CHANGED
    glEnd();
  }
};

/**
  A container for a FLGL window that leaves room for a border
*/
class Viewport : public Fl_Group {              // CHANGED: Window -> Group
protected:
  FLGL *gl;
public:
  Viewport(int x, int y, int w, int h, const char* l=0) :
        Fl_Group(x, y, w, h, l) {               // CHANGED: Window -> Group
    box(FL_DOWN_FRAME);
    gl = new FLGL( x+10, y+10, w-20, h-20 );    // CHANGED: x,y,w,h
    resizable(gl);
    end();
  }
};

/**
  A 2x2 tile of OpenGL windows in Viewport containers with borders
*/
class Layout:public Fl_Tile {
private:
  Viewport *view[4];
public:
  Layout(int x, int y, int w, int h) : Fl_Tile(x, y, w, h) {
    box(FL_DOWN_BOX);
    color(FL_RED);          // (shouldn't be seen)
    // Create the 4 Viewports
    int k = 0;
    for (int j = 0; j < 2; j++) {
      for (int i = 0; i < 2; i++) {
        view[k] = new Viewport(i*200, j*200, 200, 200);
        view[k]->box(FL_DOWN_BOX);
        view[k]->color(9+k);
        view[k]->align(FL_ALIGN_CLIP);
        k++;
      }
    }
    end();
  }
};

int main() {
  Fl_Double_Window *win = new Fl_Double_Window(400, 400, "gltileb");
  Layout *layout = new Layout(0, 0, win->w(), win->h());
  win->resizable(layout);
  win->end();
  win->show();
  return(Fl::run());
}

// End of test code
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.