Page 1 of 1

C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 2 C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 2 Rate Topic: -----

#1 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Posted 07 September 2009 - 10:32 AM

C++ CLASS DERIVED FROM FLTK WIDGET TUTORIAL PART 2


YOU WILL LEARN HOW TO:

1. CODE A C++ CLASS DERIVED FROM A FLTK WIDGET.

2. INSTALL AND USE OPENGL WITH FLTK.

• I. INTRODUCTION

Hello; nice to meet you! Welcome to the “C++ CLASS DERIVED FROM A FLTK WIDGET TUTORIAL PART 2”

This tutorial is a very brief overview of information presented by Dr. Bjarne Stroustrup in his book “Programming Principles and Practices Using C++,” Addison-Wesley 2009, ISBN 978-0321543721.

The web site for his book and the appropriate FLTX header files is:

http://www.stroustrup.com/Programming/

• II. EXAMPLE 5

Please copy and paste Example 5 into your IDE then build and debug.


//************************************
//Exampe 5
//C++ CLASS DERIVED FROM FLTK WIDGET 
//************************************
#include <Fl/Fl.H>			//Required by all FLTK programs.
#include <Fl/Fl_window.H>	 //Required for window class.
#include <FL/FL_Button.H>	 //Required for button class.
#include <stdio.h>			//Required for display.
#include <stdlib.h>

class Passes_Userdata : public Fl_Window
{
   private:

	  static void MyCallback(Fl_Widget*w, void* userdata)
	  {
		  fprintf(stderr, "Button Clicked! Userdata passed is %d\n", (int)userdata);
	  }

   public:

	   int data;
	   Passes_Userdata(int w, int h, const char *name = 0) : Fl_Window(w, h, name)
	   {
		   data = 54321;
		   Fl_Button *but = new Fl_Button(5, 5, 75, 30, "Click Me");
		   but->callback(MyCallback, (void*)54321);

		   show();
		}
};

int main()
{
	Passes_Userdata passes_userdata(600,400,"FLTK is easy to learn and fun to use!");
	return(Fl::run());		//Returns value of FLTK command loop, this
							  //function returns only when all windows are closed.
}



Example 5 creates a C++ class derived from a FLTK Fl widget.

The Fl widget we used was Fl_window.

We made everything public to make it easier to understand.

When we coded the constructor we followed the FLTK parameter order of width, height, and label name.

We declared the window button inside of the class constructor.

We made the callback for the button a private member of the class.

Defining the class will automatically show the window.

• III. INSTALLING OPENGL

There are lots of free download websites you can use; this is one of the many for OpenGL95:

http://www.freedownl...L_Download.html

Download and use WinZip or a similar package to extract the OpenGL95.zip file to your C:\ drive.

• IV. USING OPENGL WITH FLTK IN MICROSOFT VISUAL STUDIO

Use the following steps to use OpenGL with FLTK in Microsoft Visual Studio:

1. Open Visual Studio

Use the same Win32 console application empty project you have been using for all of your non-OpenGL FLTK applications.

2. From the Visual Studio main top menu choose Project and from the drop-down menu choose Properties.

To expand a sub-menu click the Linker folder in the left menu of the Properties dialog box. In the sub-menu click Input. On the right, in the additional dependencies text field you should see the following entries we added for FLTK:

fltkd.lib wsock32.lib comctl32.lib fltkjpegd.lib fltkimagesd.lib

At the end of this list add the following four libraries:

opengl32.lib glu32.lib fltkgld.lib glaux.lib

In the “Ignore Specific Library” text field you should already have the following text if you do not have it add it now:

libcd.lib

In the left menu of the Properties window click C\C++ to expand a sub-menu. Click the “Code Generation” sub-menu item. On the right, if it has not already been changed, change the “Runtime Library” drop-down to:

Multi-threaded Debug DLL (\MDd)

Click OK to close the Properties window.

• V. EXAMPLE 6

Please copy and paste Example 6 into your IDE. You have to use the same Project file we just setup. It will not work in other Project files until you set them up the same way we did the one above. Then, build and debug Example 6 to test to see if you can now code in OpenGL using FLTK.

//************************************
//Example 6
//OpenGL example showing text on a
//rotating 3D object.
//************************************
#include <FL/Fl.H>
#include <FL/Fl_Gl_window.H>
#include <FL/gl.h>
#include <GL/glu.h>
#include <string.h>
#include <stdio.h>
//Tetrahedron points.
#define TOP	0,  1,  0
#define RIGHT  1, -1,  1
#define LEFT  -1, -1,  1
#define BACK   0, -1, -1
class MyGlWindow : public Fl_Gl_Window {
	float rotangle;
	void draw() {
		//Init viewport.
		if (!valid()) {
			valid(1);
			// Initialize GL.
			glClearColor(0.0, 0.0, 0.0, 0.0);
			glClearDepth(1.0);
			glDepthFunc(GL_LESS);
			glEnable(GL_DEPTH_TEST);
			glShadeModel(GL_FLAT);
		}
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//Position camera/viewport init.
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glViewport(0,0,w(),h());
		gluPerspective(45.0, (float)w()/(float)h(), 1.0, 10.0);
		glTranslatef(0.0, 0.0, -5.0);
		//Position object.
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glRotatef(rotangle, 1, 0, 1);
		glRotatef(rotangle, 0, 1, 0);
		glRotatef(rotangle, 1, 1, 1);
		//Draw tetrahedron.
		glColor3f(1.0, 0.0, 0.0); glBegin(GL_POLYGON); glVertex3f(TOP);   glVertex3f(RIGHT);  glVertex3f(LEFT);  glEnd();
		glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f(TOP);   glVertex3f(BACK);   glVertex3f(RIGHT); glEnd();
		glColor3f(0.0, 0.0, 1.0); glBegin(GL_POLYGON); glVertex3f(TOP);   glVertex3f(LEFT);   glVertex3f(BACK);  glEnd();
		glColor3f(0.5, 0.5, 0.5); glBegin(GL_POLYGON); glVertex3f(RIGHT); glVertex3f(BACK);   glVertex3f(LEFT);  glEnd();
		//Print tetrahedron's points on object.
		//Disable depth buffer while drawing text,
		//so text draws /over/ object.
		glDisable(GL_DEPTH_TEST);
		{
			const char *p;
			gl_font(1, 12);
			glColor3f(1.0, 1.0, 1.0);
			glRasterPos3f(TOP);   p = "+ top";   gl_draw(p, strlen(p));
			glRasterPos3f(LEFT);  p = "+ left";  gl_draw(p, strlen(p));
			glRasterPos3f(RIGHT); p = "+ right"; gl_draw(p, strlen(p));
			glRasterPos3f(BACK);  p = "+ back";  gl_draw(p, strlen(p));
		}
		glEnable(GL_DEPTH_TEST);
		//Print rotangle value at fixed position at lower left.
		char s[40];
		sprintf(s, "ROT=%.2f", rotangle);
		glLoadIdentity(); glRasterPos2f(-3,-2); gl_draw(s, strlen(s));
	}
	static void Timer_CB(void *userdata) {
		MyGlWindow *o = (MyGlWindow*)userdata;
		o->rotangle += 1.0;
		o->redraw();
		Fl::repeat_timeout(1.0/24.0, Timer_CB, userdata);	   //24fps
	}
public:
	//CONSTRUCTOR
	MyGlWindow(int X,int Y,int W,int H,const char*L=0) : Fl_Gl_Window(X,Y,W,H,L) {
		rotangle = 0;
		Fl::add_timeout(3.0, Timer_CB, (void*)this);	   //Wait 3 secs before animation begins.
	}
};
//MAIN
int main() {
	 Fl_Window win(500, 300);
	 MyGlWindow mygl(10, 10, win.w()-20, win.h()-20);
	 win.show();
	 return(Fl::run());
}
	



If Example 6 does not work post your questions in the DIC forum and either I or someone else will help you.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1