Showing posts with label FLTK. Show all posts
Showing posts with label FLTK. Show all posts

Sunday, September 25, 2011

C++ FLTK OPENGL 3D ANIMATION TUTORIAL









REVISED: Friday, March 22, 2013



CONTENTS:
I.     INTRODUCTION
II.    INSTALLING OPENGL
III.   USING FLTK IN MICROSOFT VISUAL STUDIO
IV.   3D ANIMATION USING FLTK AND OPENGL

YOU WILL LEARN HOW TO:
1. INSTALL AND USE OPENGL WITH FLTK.
2. CODE 3D ANIMATION WITH FLTK OPENGL.

I. INTRODUCTION


Welcome to the “C++ FLTK OPENGL 3D ANIMATION TUTORIAL"

II. INSTALLING OPENGL


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

http://download-opengl-graphics.com/


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

III. USING 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.

IV. 3D ANIMATION USING FLTK AND OPENGL


Please copy and paste the following example 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 the example program to test to see if you can now code in OpenGL using FLTK.

//***********************************
//Texture Mapped Cube
//erco/loic 03/17/09
//***********************************
#include <cmath>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#include <stdio.h>
#include <math.h>
#include <FL/Fl.H>
#include <FL/Fl_window.H>
#include <FL/Fl_Gl_window.H>
#include <FL/gl.h>

//***********************************
//OpenGL spinning cube with
//checker texturemap
//***********************************
#define WIN_W 400      
//window width
#define WIN_H 400       
//window height
#define TEX_W 64        
//texturemap width
#define TEX_H 64        
//texturemap height
#define FPS (1.0/24.0)  
//frames per second playback

//***********************************
//OpenGL class to show
//texturemapped cube
//***********************************
class MyGlWindow : public Fl_Gl_Window {
    double spin;
    GLuint TexID;
//***********************************
//TIMER CALLBACK
//Handles rotation the object
//***********************************
    static void Timer_CB(void *userdata) {
        MyGlWindow *mygl = (MyGlWindow*)userdata;
        mygl->spin += 2.0;      
//spin
        mygl->redraw();
        Fl::repeat_timeout(FPS, Timer_CB, userdata);
    }
public:
//CTOR
    MyGlWindow(int x,int y,int w,int h,const char *l=0) : Fl_Gl_Window(x,y,w,h,l) {
        spin = 0.0;
        Fl::add_timeout(FPS, Timer_CB, (void*)this);       
//24fps timer
    }
//***********************************
//PERSPECTIVE VIEW
//Same as gluPerspective()..
//***********************************
    void Perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
        GLdouble xmin, xmax, ymin, ymax;
        ymax = zNear * tan(fovy * M_PI / 360.0);
        ymin = -ymax;
        xmin = ymin * aspect;
        xmax = ymax * aspect;
        glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
    }
//***********************************
//RESHAPED VIEWPORT
//OpenGL
//window changes size
//***********************************
    void ReshapeViewport() {
//***********************************
//Viewport
//***********************************
        glViewport(0, 0, w(), h());
//***********************************
//Projection
//***********************************
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GLfloat ratio = w() / h();
        Perspective(30.0, 1.0*ratio, 1.0, 30.0);
        glTranslatef(0.0, 0.0, -8.0);
//***********************************
//Model view
//***********************************
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
//***********************************
//OPENGL INITIALIZATION
//OpenGL do *only once* on startup.
//***********************************
    void GlInit() {
//***********************************
//Make sure we only do this once
//***********************************
        static int first_time = 1;
        if ( first_time ) {
            first_time = 0;
//***********************************
//Texture Map Init
//***********************************
            GLubyte img[TEX_W][TEX_H][3]; //after glTexImage2D(), array is no longer needed
            glGenTextures(1, &TexID);
            glBindTexture(GL_TEXTURE_2D, TexID);
//***********************************
//Texture Mapping Mode
//Uncomment one of the following lines:
//GL_DECAL or GL_MODULATE
//glTexEnvf(GL_TEXTURE_ENV,
//GL_TEXTURE_ENV_MODE,
//GL_DECAL);   
//use actual texture colors
//***********************************
            glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
//texture colors affected by poly's color

            for (int x=0; x<TEX_W; x++) {
                for (int y=0; y<TEX_H; y++) {
//***********************************
//Texture Pattern
//Uncomment one of the following lines:
//checkboard or basketweave
//GLubyte c = ((x&16)^(y&16)) ? ((x%16)
//<<4) : (((x%16)^15)<<4); //basket weave
//***********************************
                    GLubyte c = ((x&16)^(y&16)) ? 255 : 0;                        
//checkerboard
                    img[x][y][0] = c;
                    img[x][y][1] = c;
                    img[x][y][2] = c;
                }
            }
            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEX_W, TEX_H, 0, GL_RGB, GL_UNSIGNED_BYTE, &img[0][0][0]);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glEnable(GL_TEXTURE_2D);
//***********************************
//OpenGL settings
//***********************************
            glShadeModel(GL_FLAT);
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_LEQUAL);
        }
    }
//***********************************
//FLTK DRAW
//Called by FLTK to draw the scene.
//***********************************
    void draw() {
//***********************************
//Initialize/handle reshaped viewport
//***********************************
        if ( !valid() ) {
            valid(1);
            GlInit();
            ReshapeViewport();
        }
//***********************************
//Clear
//***********************************
        glClearColor(.5,.5,.5, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//***********************************
//Setup model matrix
//***********************************
        glLoadIdentity();
        glRotatef(spin, 0.5, 1.0, 0.0); 
//show all sides of cube

//***********************************
//Draw cube with texture assigned
//to each face
//***********************************
        glBegin(GL_QUADS);
//***********************************
//Front Face
//***********************************
        glColor3f(1.0, 0.0, 0.0); 
//red
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Back Face
//***********************************
        glColor3f(0.0, 1.0, 1.0); 
//cyn
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Top Face
//***********************************
        glColor3f(0.0, 1.0, 0.0); 
//grn
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0,  1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,  1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Bottom Face
//***********************************
        glColor3f(1.0, 0.0, 1.0); 
//mag
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Right face
//***********************************
        glColor3f(0.0, 0.0, 1.0); 
//blu
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0,  1.0,  1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0, -1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0,  1.0); 
//Bottom Left Of The Texture and Quad
//***********************************
//Left Face
//***********************************
        glColor3f(1.0, 1.0, 0.0); 
//yel
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0, -1.0); 
//Top Left Of The Texture and Quad
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0,  1.0,  1.0); 
//Top Right Of The Texture and Quad
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0,  1.0); 
//Bottom Right Of The Texture and Quad
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); 
//Bottom Left Of The Texture and Quad
        glEnd();
//***********************************
//DEBUG: CHECK FOR ERRORS
//***********************************
        GLenum err = glGetError();
        if ( err != GL_NO_ERROR ) {
            fprintf(stderr, "GLGETERROR=%d\n", (int)err);
        }
    }
};
int main(int argc, char *argv[]) {
    MyGlWindow* mygl = new MyGlWindow(10, 10, WIN_W-20, WIN_H-20, "Texture Test");
    mygl->end();
    mygl->resizable(mygl);
    mygl->show();
    return(Fl::run());
}

YOU HAVE  LEARNED HOW TO:
1. INSTALL AND USE OPENGL WITH FLTK.
2. CODE 3D ANIMATION WITH FLTK OPENGL.

Elcric Otto Circle







-->

-->

-->




How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




C++ FLTK INSTALL OPENGL TUTORIAL









REVISED: Friday, March 22, 2013




CONTENTS:
I.     INTRODUCTION
II.   EXAMPLE 1 - C++ CLASS DERIVED FROM FLTK WIDGET
III. INSTALLING OPENGL
IV. USING OPENGL WITH FLTK IN MICROSOFT VISUAL STUDIO
V.  EXAMPLE 2 - OPENGL TEXT ON A ROTATING 3D OBJECT

YOU WILL LEARN HOW TO:
1. CODE A C++ CLASS DERIVED FROM A FLTK WIDGET.
2. INSTALL AND USE OPENGL WITH FLTK.

I. INTRODUCTION


Welcome to the “C++ FLTK INSTALL OPENGL TUTORIAL”

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 1 - C++ CLASS DERIVED FROM FLTK WIDGET


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

//***********************************
//Exampe 1
//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 1 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://download-opengl-graphics.com/


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 2 - OPENGL TEXT ON A ROTATING 3D OBJECT.


Please copy and paste Example 2 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 2 to test to see if you can now code in OpenGL using FLTK.

//***********************************
//Example 2
//OPENGL 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());
}

YOU HAVE LEARNED HOW TO:
1. CODE A C++ CLASS DERIVED FROM A FLTK WIDGET.
2. INSTALL AND USE OPENGL WITH FLTK.

Elcric Otto Circle




-->

-->

-->




How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"




C++ FLTK ADDING BUTTONS TO A WINDOW TUTORIAL









REVISED: Friday, March 22, 2013




CONTENTS:
I.    INTRODUCTION
II.  EXAMPLE 1 - BLANK WINDOW
III. EXAMPLE 2 - ADDING A BUTTON
IV. EXAMPLE 3 - ADDING A BUTTON CALLBACK
V. EXAMPLE 4 - PASSING USER DATA

YOU WILL LEARN HOW TO CODE A C++ FLTK:
1. Blank window.
2. Window button.
3. userdata.

I. INTRODUCTION


Hello; nice to meet you! Welcome to the “C++ FLTK ADDING BUTTONS TO A WINDOW TUTORIAL.”

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 1 - BLANK WINDOW


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

//***********************************
//Exampe 1
//Blank Window
//***********************************
#include <Fl/Fl.H>            
//Required by all FLTK programs.
#include <Fl/Fl_window.H>     
//Required for window class.

int main()
{
    Fl_Window win(600,400);   
//Creates window width 600 height 400.
    win.show();                            
//Tells FLTK to show the window.
    return(Fl::run());                    
//Returns value of FLTK command loop, //this function returns only when all
//windows are closed.
}


Example 1 creates an empty window with no window title and no buttons.

There is one include file for every class in FLTK. For example:

#include <Fl/Fl_window.H>

Is the include file for the Fl_Window class.

In the following statement:

Fl_Window win(600,400);


win is the object name instantiated by the class window.

When an object of the class is instantiated the parentheses following the object name enclose the arguments required by the class constructor. In this case the first argument is the width and the second argument is the height. The third argument is an optional title character string.

return(Fl::run());


Returns the value of FLTK command loop, this function returns only when all windows are closed.

III. EXAMPLE 2 - ADDING A BUTTON


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

//***********************************
//Exampe 2
//Adding a button
//***********************************
#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.

int main()
{
    Fl_Window win(600,400,"FLTK is easy to learn and fun to use.");   
//Creates window width 600 height 400.
    Fl_Button button(5,5,75,30,"Click Me");  
//Button arguments starting
//point(x,y),width,heigth,"label."
    win.show();                                                  //Tells FLTK to show the window.
    return(Fl::run());                                          //Returns value of FLTK
//command loop, this
//function returns only when all
//windows are closed.
}


Below the window definition we added a call for a button.

Fl_Button button(5,5,75,30,"Click Me");


The button is a rectangle. A rectangle is defined by a (x,y) starting point for its top left corner of the rectangle and its width and its height. The first two arguments are the top left corner (x,y) starting point inside of the window. The third and fourth arguments are the width and height respectively. The fifth argument is a character string title for the button.

Since we are using the Button class we added an include file for button:

#include <FL/FL_Button.H>

And, we added a title to the window.

Fl_Window win(600,400,"FLTK is easy to learn and fun to use.");

IV. EXAMPLE 3 - ADDING A BUTTON CALLBACK


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

//***********************************
//Exampe 3
//Adding a button callback.
//***********************************
#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.

void My_Callback(Fl_Widget*w, void* userdata)
{
     fprintf(stderr, "Button works!\n");
}

int main()
{
    Fl_Window win(600,400,"FLTK is fun to learn and easy to use.");   
//Creates window width 600 height 400.
    Fl_Button button(5,5,75,30,"Click Me");  
//Button arguments starting
//point(x,y),width,heigth,"label."
    button.callback(My_Callback);
    win.show();                
//Tells FLTK to show the window.
    return(Fl::run());        
//Returns value of FLTK
//command loop, this
//function returns only when
//all windows are closed.
}


We want the button to do something. To make the button do something we have to add a callback.

void My_Callback(Fl_Widget*w, void* userdata)
{
     fprintf(stderr, "Button works!\n");
}


Callbacks in FLTK always return a void. 

We declared the callback named My_Callback. The first two arguments are a widget pointer, which is a pointer to the widget that invoked the callback, and a void pointer to userdata which can be any kind of data that you want.


Below the button definition we attach the callback to the button.

button.callback(My_Callback);


Now when the button is clicked the callback will display on the black background window, "FLTK is fun to learn and easy to use."

To get the display to work we added:

#include <stdio.h>

V. EXAMPLE 4 - PASSING USER DATA


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

//***********************************
//Exampe 4
//Passing userdata
//***********************************
#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.

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

int main()
{
    Fl_Window win(600,400,"FLTK is fun to learn and easy to use.");   
//Creates window width 600 height 400.
    Fl_Button button(5,5,75,30,"Click Me");  
//Button arguments starting
//point(x,y),width,heigth,"label."
    button.callback(My_Callback, (void*)54321);
    win.show();                                                  //Tells FLTK to show the window.
    return(Fl::run());                                          //Returns value of FLTK
//command loop, this
//function returns only when all
//windows are closed.
}


We want to pass userdata. userdata is passed as an optional second argument to the callback. The userdata can be any type of data you want as long as it is one piece of data that can be cast to a void pointer.

button.callback(My_Callback, (void*)54321);


When you push the button, it calls the callback function, and passes the 54321 data as the userdata and the function can do with that 54321 userdata whatever it wants.

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


In our example the 54321 userdata is printed to the black background window when the button is clicked.

You might have to click and drag the grey foreground window out of the way to see the black background window. Remember the display will be on the black background window not the grey foreground window.

YOU HAVE LEARNED HOW TO CODE A C++ FLTK:
1. Blank window.
2. Window button.
3. Userdata.

Elcric Otto Circle




-->

-->

-->




How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"