main-tutorialfltk

Adapted from Stephen J. Chenney’s Tutorial
Modified by Yu-Chi Lai at 2005
Modified by cx in 2007 (updated for VS05)
Modified by bfield in 2008 (combined old tutorials, new version of FlTK)
Modified by eilbert in 2009 (update for VS08)

This tutorial will show you how to open an FLTK window for doing OpenGL drawing. We will build on the project from the previous tutorial so please complete it first if you haven’t already.

Lets get started.

1.@ Add a Window Class

  • We need to add a class to project which will control our FLTK window. We begin by adding two new files to the project. We want to add a new source file or C++ File (.cpp) in VS08 called MyOpenGLWindow.cpp and a new header file or Header File(.h) in VS08 called MyOpenGLWindow.h. These files can be added just like main.cpp was in the previous tutorial.
  • We now need to add the code for this class. First the header file MyOpenGLWindow.h:
#ifndef MY_OPENGL_WINDOW_H
#define MY_OPENGL_WINDOW_H

#include 

// Disable the warning between the block of push and pop
#pragma warning (push)           
// The list of numbers of warning we will like to disable 
#pragma warning(disable : 4204)
#pragma warning(disable : 4244)
#pragma warning(disable : 4312)
#pragma warning(disable : 4311)
#pragma warning(disable : 4083)
#pragma warning(disable : 4193)
// List of all fltk include files 
#include 
#include 
#pragma warning (pop) // Enable the warning again

class MyOpenGLWindow : public Fl_Gl_Window {
public:
	MyOpenGLWindow();
	virtual void draw();
	virtual ~MyOpenGLWindow();
};

#endif
  • Lets look at this for a second. It defines a class called MyOpenWindow which is derived from the FLTK base window class Fl_Gl_Window. The file Fl_Gl_Window.h was included to get the definition of this class. Notice how the Fl directory is prepended to this file. All FLTK files should be prepended with Fl when they are included. Our window class has only a constructor (which takes three arguments) and a destructor. The underlying FLTK window class is going to do all the rest for us.
  • The code for the source file MyOpenGLWindow.cpp contains the implementations of the constructor and destructor.
#include "MyOpenGLWindow.h"
#include 
#include 

MyOpenGLWindow::MyOpenGLWindow() : Fl_Gl_Window(100,100,600,600,"My OpenGL program") {
	mode(FL_RGB | FL_ALPHA | FL_DEPTH | FL_DOUBLE);
}

MyOpenGLWindow::~MyOpenGLWindow()
{}

void MyOpenGLWindow::draw() {
	// clears the background of the drawing area	
	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT);

	// technically we should set up the drawing transform here
	// but for now, we'll assume its the identity, and will stay
	// that way since this is the only drawing code in the program

	// draws a red line
	glColor3f(1,0,0);
	glBegin(GL_LINES);
	glVertex3f(-0.5, 1, 0);
	glVertex3f(-0.5, -1, 0);
	glEnd();

	// draws a blue triangle
	glColor3f(0,0,1);
	glBegin(GL_TRIANGLES);
	glVertex3f(1, 1, 0);
	glVertex3f(-1, 0, 0);
	glVertex3f(1, -1, 0);
	glEnd();

}

The constructor just passes its arguments to the constructor of the base class, the FLTK class Fl_Gl_window. The destructor doesn’t do anything yet but we’ll need it later so we might as well make it now.

The draw function is called by FlTk whenever the window needs to be redrawn, and that is where we put our OpenGL calls. For now, the given setup code should be just fine, what you are interested in changing is the drawing of primitives such as lines and triangles (such as shown in the example).

2.@ Modify Main

Now that we have a window class we need to modify our main function to make use of it. Here’s the updated contents of main.cpp.

#include "MyOpenGLWindow.h"

int main(int argc, char** args)
{
	MyOpenGLWindow myWindow;
	myWindow.show();

	Fl::run();

	return 0;
}

This new version of main creates an instance of our window class. FLTK windows are not visible until they are shown. This allows you to create a window, set various attributes and then display the window. If it was visible initially the user might see the attributes of the window changing as you initialized them. So to make our window visible we call its show() method. This is one of the many method MyOpenGLWindow inherited from the base class Fl_Gl_Window.

Now we tell our window to show itself we have only one step remaining. We must tell the FLTK system to start processing events. FLTK is an event driven system, meaning it responds to user inputs. FLTK will not do anything until we tell it to start the event processing loop. We do this my calling Fl::run() which was included from the file Fl.h.

3.@ Set Directories

We’ve now got all the code we need and we almost ready to build our project but first we must tell VS08 where to find the FLTK files that we are using. We must tell it where to find the header files (*.h) that we’ve included in our project, such as Fl.h. It also need to know where to find the FLTK library that we’ve built for you.

3.1@ Specify Relative Pathname

We could hardcode the location of each of the FLTK files. However, if we set up an environmental variable for the entire system, we can then just use a relative pathname when specifying the location of FLTK files. This will make it easier to transfer a project to another system.

  1. Right-click on My Computer. (My Computer should be located on the Desktop or in the Start menu.)
  2. Select Properties.
  3. Select the Advanced tab.
  4. Click the Environmental Variables button.
  5. In the User variables section, set the variable FLTKHOME to the value S:\fltk-1.1.9\i386_winxp\vs9. You will probably need to click New and then enter this information.

Please not that adding the environmental variable should be done when VS08 is not running. So if you have it open, close VS08, add the new variable and then restart VS08.

In case nothing works, as mentioned before use the hardcoded location (and skip the previous 5 steps in 3.1). So what you need to do is replace “$(fltkhome)” in 3.2 and 3.3 with the full path - S:\fltk-1.1.9\i386_winxp\vs9 But this is highly discouraged.

3.2@ Add Include Paths

Now, we’ll tell VS08 where to find the FLTK files.

  • Open the Class View window, right click on our project (FLTKHelloWorld), and choose Properties. Select C/C++ -> General from the left hand pane and enter $(fltkhome)\include for Additional Include Directories.

3.3@ Add Library Paths

  • We now need to tell VS08 where to find the pre-built fltk libraries. Select Linker -> General from the left hand pane and add $(fltkhome)\lib to Additional Library Directories.

  • Click OK.

4.@ Project Settings

  • Now that VS08 knows where to find the FLTK library we need to tell it which libraries we’re interested in. Unlike the include and library paths this must be done each time you create a project that needs them. Open the Class View window, right click on our project (FLTKHelloWorld), and choose Properties again.
  • Make sure Configuration at the top left of the Properties window is set to Debug (or Active (Debug)).
  • Choose Linker -> Input from the left hand pane. Add fltkd.lib comctl32.lib wsock32.lib opengl32.lib glu32.lib fltkgld.lib to the Additional Dependencies. Make sure the libraries are separated by whitespace. fltkd.lib is the debug library for FLTK. We always use debug builds for this course for simplicity. comctl32.lib and wsock32.lib are Microsoft Windows libraries which FLTK depends on.

  • If you want to build your project in “Release” mode, you should set Configuration to Release in the previous step, and link with the release version of the libraries by replacing fltkgld.lib and fltkd.lib with fltkgl and fltk.lib respectively. The step below about changing the C runtime should also be skipped.
  • One final setting for the FLTK library. The FLTK library uses a debug version of the C runtime from a dll (dynamically linked library). What does that mean? It means the FLTK library loads the basic C library in a different way they the default VS08 project so we need to change our project to match FLTK.
  • Choose C/C++ -> Code Generation from the left hand pane and set the Runtime Library to Multi-threaded Debug DLL (/MDd).

  • Click OK.

5.@ Build and Run the Program

We build our program just like before. Choose Build -> Build Solution. Your project should build with no errors but a lot of warnings from FLTK(see BAD LINK). If you have some errors see where they are and find the difference between your code and the code supplied with this tutorial. Fix the errors and try again.

One its built you can run it via Debug -> Start Without Debugging. You should get a gray window like this one. What’s that? All this for a gray window? Don’t worry we’ll actually start doing something with our window in the next tutorial.

6.@ Appendix: Set up the FLTK at home (old version…)

(:comment Note: WarningNormally, if you would like to write a good and robust program, you should pay attention to the warnings because we have ever got into a big trouble when we neglected some of the warnings. However, all the warnings in this program come from FLTK library and we cannot remove it unless we modify the FLTK library. It is very disturbing for us to get other important warnings. Here I provide a solution to remove these FLTK warning and let other warnings pass through. Everytime when you need to include the FLTK header file you need to put these set of “#pragma …” to enclose those “includes”. They tell the compiler not to generate the warnings which is listed in the number list in these “includes”.:)

(Download source file here)

Page last modified on September 08, 2010, at 12:11 PM