Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members Related Pages
Lesson4: Using widgets. - In this lesson you will learn how to use widget to interact with the application. We will modify code from the Lesson 3. We will add a checkbox, which will control the animation. When it is checked animation is on, when unchecked triangle stops spinning.
- See also:
- sjgui, sjgui::CWnd::RegisterChild(), sjgui::CCheckBox
class CSpcWnd : public sjgui::CWnd
{
float m_fRotation;
sjgui::CCheckBox m_chbOn;
public:
CSpcWnd():CWnd()
{
m_fRotation=0.0f;
RegisterChild(&m_chbOn);
m_chbOn.SetLabel("Animation On/Off");
m_chbOn.PosWnd(0,0,320,m_chbOn.GetHeight());
}
virtual void OnAnimate()
{
if(m_chbOn.IsChecked())
m_fRotation+=1;
if(m_fRotation>360)m_fRotation-=360;
}
virtual void OnDraw()
{
Animate();
SET_GL_SIMPLE_PROSPECTIVE;
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(m_fRotation,0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
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();
SET_GL_FOR_GUI_DRAW;
}
};
Back to Lesson 3. Forward to Lesson 5.
- The following is full source code for this lesson.
#include <sjgui/sjgui.h>
class CSpcWnd : public sjgui::CWnd
{
float m_fRotation;
sjgui::CCheckBox m_chbOn;
public:
CSpcWnd():CWnd()
{
m_fRotation=0.0f;
RegisterChild(&m_chbOn);
m_chbOn.SetLabel("Animation On/Off");
m_chbOn.PosWnd(0,0,320,m_chbOn.GetHeight());
}
virtual void OnAnimate()
{
if(m_chbOn.IsChecked())
m_fRotation+=1;
if(m_fRotation>360)m_fRotation-=360;
}
virtual void OnDraw()
{
Animate();
SET_GL_SIMPLE_PROSPECTIVE;
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(m_fRotation,0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
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();
SET_GL_FOR_GUI_DRAW;
}
};
int main(int argc, char* argv[])
{
CSpcWnd SpcWnd;
SpcWnd.PosWnd(20,50,320,240);
if(sjgui::Create("Using widgets",&SpcWnd))
{
sjgui::GlobalMainLoop();
}
else
printf("Could not create opengl window.\n");
return 0;
}
|
|