mosra / magnum-bootstrap

Bootstrap projects for the Magnum C++11 graphics engine

Home Page:https://magnum.graphics/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add bootstrap example using FLTK

pdhahn opened this issue · comments

Similar to boostraps using wxWidgets or Qt, please add a case using FLTK 1.4.

Unfortunately I have no experience with FLTK whatsoever, but if you would have a minimal code that sets up a window with raw OpenGL, I could continue from there.

In general, in FLTK (1.4), the approach is to subclass the Fl_Gl_Window class for your own OpenGL window class code. If you download/install FLTK from github, it has a test application called test/cube.cxx that is not exactly trivial, but straightforward. It implements a class called cube_box that inherits Fl_Gl_Window which displays some moving cubes. Here's a skeletal snippet of the class declaration...

#include <FL/Fl_Gl_Window.H>
#include <FL/gl.h>
class cube_box : public Fl_Gl_Window {
  void draw();
  int handle(int);
public:
  double lasttime;
  int wire;
  double size;
  double speed;
  cube_box(int x,int y,int w,int h,const char *l=0) : Fl_Gl_Window(x,y,w,h,l) {
    lasttime = 0.0;
    box(FL_DOWN_FRAME);
  }
};

In a nutshell, you must override the inherited, virtual draw() method to include your own OpenGL drawing code (be sure to call the base class draw()!). The required OpenGL initialization is performed when(ever) the Fl_Gl_Window class's valid() method returns false. You can override the virtual handle() method to respond to events (mouse,keyboard) as desired (again, be sure to call the base class's handle() before returning).

As for the main(), in general, you create an instance of your derived window class, add any other FLTK widgets needed, call the window's inherited show() method, then finally run by calling the FLTK function Fl::run() (which only returns upon exit, which can happen with the Escape char press). Or (alternatively), as in this test code's case, repeatedly calling Fl::check() or Fl::wait() in an infinite loop to make FLTK incrementally update the display, process events, etc. These methods return immediately after doing their work, with a return code of 0 meaning "exit", else keep looping.

However, note in this test code FYI, it's a bit more complex because they create a container window (instance of Fl_Window) into which is added various widgets including two instances of the cube_box class (as sub-windows under the container window). That's all in accordance with the flexibility provided by FLTK, but of course you do not need all that stuff for your bootstrap.