From CS559 Computer Graphics Fall 2007

Main: Tutorial3

Adapted from Stephen J. Chenney's Tutorial
Modified by Yu-Chi Lai at 2005
Modified by cx in 2007 (updated for VS05)

On this page... (hide)

  1. 1. Step 1: Add a Window Class
  2. 2. Step 2: Modify Main
  3. 3. Step 3: Add Include Paths
  4. 4. Step 4: Add Library Paths
  5. 5. Step 5: Project Settings
  6. 6. Step 6: Build and Run the Program
  7. 7. Appendix: Set up the FLTK at home

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

Lets get started.

1.  Step 1: Add a Window Class

#include <iostream>
using namespace std;
int main(int argc, char** args)
{
	// Notice I start from i=1 not 0 because the args[0] is reserved 
    //        for the name of this program.
	for(int i = 1; i < argc; i++)
	{
	     cerr << i << "th argument is " << args[i] << "\n";
	}
}
#ifndef MY_WINDOW_H
#define MY_WINDOW_H

#include <Fl/Fl_Window.h>

class MyWindow : public Fl_Window
{
	public:
		MyWindow(int width, int height, char* title);
		virtual ~MyWindow();
};

#endif
#include "MyWindow.h"

MyWindow::MyWindow(int width, int height, char* title) 
         : Fl_Window(width, height, title)

{}

MyWindow::~MyWindow()

{}

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

2.  Step 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 <Fl/Fl.h>
#include "MyWindow.h"

 int main(int argc, char** args)
 {
	MyWindow myWindow(400, 400, "CS559 Tutorial");
	myWindow.show();

	Fl::run();

	return 0;
}

This new version of main creates an instance of our window class with a size of 400x400 pixels and a title of "CS559 Tutorial". 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 MyWindow inherited from the base class Fl_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.  Step 3: Add Include Paths

4.  Step 4: Add Library Paths

5.  Step 5: Project Settings

6.  Step 6: 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 Note). 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.

7.  Appendix: Set up the FLTK at home

Note: Warning Normally, 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".

Source Code

Retrieved from http://www.cs.wisc.edu/graphics/Courses/559-f2007/Main/Tutorial3
Page last modified on October 10, 2007, at 05:04 PM