Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Lesson 3

Lesson3: Using Animation.
In this lesson you will learn how to create animation in your window. We will modify code from the Lesson 2, to add animation to the triangle.
See also:
sjgui::CWnd::OnAnimate()
// For this lesson, you will learn how to make animation,
// we will animate the triangle from lesson2. Triangle will be
// rotating around Y axis. For that we have a special event OnAnimate().
class CSpcWnd : public sjgui::CWnd
{
    // This variable will hold the current rotation angle
    float   m_fRotation;
public:
    // Now we need a constructor, because m_fRotation should be reset
    // when class is initialized. We also should not forget to call
    // base class constructor
    CSpcWnd():CWnd(){m_fRotation=0.0f;}
    // New handler for animation
    virtual void OnAnimate()
    {
        // change rotation by 1 degree
        m_fRotation+=1;
        // we do not want to have rotation angle more then 360 degrees
        if(m_fRotation>360)m_fRotation-=360;
    }
    // Drawing is as it was in lesson2, we just need to add command for animations.
    virtual void OnDraw()
    {
        // For simplicity let's us call Animation function, when
        // we draw the scene, even though in a big program, it could
        // happen that you would like to animate everything in a separate thread.
        Animate();
        SET_GL_SIMPLE_PROSPECTIVE; // The same as it was in lesson2
        glTranslatef(0.0f,0.0f,-10.0f); // Move triangle down
        // Now we rotate our triangle around Y axis
        glRotatef(m_fRotation,0.0f,1.0f,0.0f);
        // Let us set color to a blue
        glColor3f(0.0f,0.0f,1.0f);
        // this is our triangle in the center of the screen.
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
    }
}; // end of CSpcWnd class
Back to Lesson 2. Forward to Lesson 4.
The following is full source code for this lesson.
// This header includes all resources of the sjgui library
#include <sjgui/sjgui.h>

// For this lesson, you will learn how to make animation,
// we will animate the triangle from lesson2. Triangle will be
// rotating around Y axis. For that we have a special event OnAnimate().
class CSpcWnd : public sjgui::CWnd
{
    // This variable will hold the current rotation angle
    float   m_fRotation;
public:
    // Now we need a constructor, because m_fRotation should be reset
    // when class is initialized. We also should not forget to call
    // base class constructor
    CSpcWnd():CWnd(){m_fRotation=0.0f;}
    // New handler for animation
    virtual void OnAnimate()
    {
        // change rotation by 1 degree
        m_fRotation+=1;
        // we do not want to have rotation angle more then 360 degrees
        if(m_fRotation>360)m_fRotation-=360;
    }
    // Drawing is as it was in lesson2, we just need to add command for animations.
    virtual void OnDraw()
    {
        // For simplicity let's us call Animation function, when
        // we draw the scene, even though in a big program, it could
        // happen that you would like to animate everything in a separate thread.
        Animate();
        SET_GL_SIMPLE_PROSPECTIVE; // The same as it was in lesson2
        glTranslatef(0.0f,0.0f,-10.0f); // Move triangle down
        // Now we rotate our triangle around Y axis
        glRotatef(m_fRotation,0.0f,1.0f,0.0f);
        // Let us set color to a blue
        glColor3f(0.0f,0.0f,1.0f);
        // this is our triangle in the center of the screen.
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
    }
}; // end of CSpcWnd class

// Now we need to setup our window, we do it in the function main.
int main(int argc, char* argv[])
{
    // Events receiver
    CSpcWnd SpcWnd;
    // Position the window
    SpcWnd.PosWnd(20,50,320,240);
    // Create window
    if(sjgui::Create("Animating",&SpcWnd))
    {
        // Run it. Now all events are transferred to our class.
        sjgui::GlobalMainLoop();
    }
    else
        // Print error message if something was wrong.
        printf("Could not create opengl window.\n");
    return 0;
} // the end of main
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:05 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo