glut and (Visual)C++

  • Follow


Hello,

I've got a C program where display, reshape and idle functions are defined
as :
    void Reshape(int w, int h) {...}
    void Display(void) {...}
    void Idle(void) {...}
The use of these functions is set in an Init() function, by :
    glutReshapeFunc(Reshape);
    glutDisplayFunc(Display);
    glutIdleFunc(Idle);
and everything works well, compiled with VisualC++ 6.0.

I try to convert the C into C++.
Display, Reshape, Idle and Init are then defined in the same way, but as
member functions of an "Application" class. My problem is that I can't
compile anymore because I get the following error messages :
    'glutReshapeFunc' : cannot convert parameter 1 from 'void (int,int)' to
'void (__cdecl *)(int,int)'        'glutDisplayFunc' : cannot convert
parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'
    'glutIdleFunc' : cannot convert parameter 1 from 'void (void)' to 'void
(__cdecl *)(void)'

Does anyone met this problem ? Do you know what to do to fix it ?
Thanks,
J.No.


0
Reply Jean 6/6/2004 12:10:03 AM

"Jean-No�l M�goz" <nospam_jnmegoz@infonie.fr> wrote in message
news:40c26055$0$7939$626a14ce@news.free.fr...

> I try to convert the C into C++.
> Display, Reshape, Idle and Init are then defined in the same way, but as
> member functions of an "Application" class. My problem is that I can't
> compile anymore because I get the following error messages :
>     'glutReshapeFunc' : cannot convert parameter 1 from 'void (int,int)'
to
> 'void (__cdecl *)(int,int)'        'glutDisplayFunc' : cannot convert
> parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'
>     'glutIdleFunc' : cannot convert parameter 1 from 'void (void)' to
'void
> (__cdecl *)(void)'
>
> Does anyone met this problem ? Do you know what to do to fix it ?

Nonstatic class member functions are not "C-style" functions.
Each such function has an implicit first parameter, the "this"
pointer.  Something like
  void Application::MyReshapeFunc(int,int);
has a signature effectively like
  void (_cdecl*)(Application*,int,int);
The compiler cannot match this to void (_cdecl*)(int,int).

A standard work-around is to make your class function
static, then have a static data member that stores "this".
The idea is to globally access "this" rather than implicitly
access it via the first parameter of the function.
  static void Application::MyReshapeFunc (int w, int h);
  static Application* ms_theApp;
  int m_width, m_height;

Application::Application ()
{
    ms_theApp = this;
}

void Application::MyReshapeFunc (int w, int h)
{
    ms_theApp->m_width = w;
    ms_theApp->m_height = h;
    // whatever else...
}

--
Dave Eberly
http://www.magic-software.com


0
Reply Dave 6/6/2004 1:09:04 AM


"Dave Eberly" <dNOSPAMeberly@usemydomain.com> a �crit dans le message de
news:Q8uwc.14501$Yd3.12885@newsread3.news.atl.earthlink.net...
> "Jean-No�l M�goz" <nospam_jnmegoz@infonie.fr> wrote in message
> news:40c26055$0$7939$626a14ce@news.free.fr...
>
>
> A standard work-around is to make your class function
> static, then have a static data member that stores "this".
> The idea is to globally access "this" rather than implicitly
> access it via the first parameter of the function.
>   static void Application::MyReshapeFunc (int w, int h);
>   static Application* ms_theApp;
>   int m_width, m_height;
>
> Application::Application ()
> {
>     ms_theApp = this;
> }
>
> void Application::MyReshapeFunc (int w, int h)
> {
>     ms_theApp->m_width = w;
>     ms_theApp->m_height = h;
>     // whatever else...
> }
>

I've made what you wrote, and made my functions static, and added a static
member to store "this" to access non static members.
But I still have the same compilation errors !
Do you understand anything ?


0
Reply Jean 6/6/2004 2:01:48 AM

"Jean-No�l M�goz" <nospam_jnmegoz@infonie.fr> wrote in message
news:40c27a87$0$7953$626a14ce@news.free.fr...

> I've made what you wrote, and made my functions static, and added a static
> member to store "this" to access non static members.
> But I still have the same compilation errors !

I have an application wrapper of this type at my web site
(listed in my signature).  Go to the site, select the
"Source Code" tab.  In the next column of tabs near the
bottom, select "Applications".  The abstract application
interface is in the files WmlApplication.{h,inl,cpp}.  The
file WmlGlutApplication.cpp shows a variation on the
mechanism I described to you.  The callback functions
are static C-style functions, but they each get a pointer
to the application by a call to a static class member
function, then thell that application object to do something.

In the 'main', you see
    glutReshapeFunc(ReshapeCallback);
In the file you see
    static void ReshapeCallback (int iWidth, int iHeight)
    {
        Application* pkTheApp = Application::GetApplication();
        if ( pkTheApp )
            pkTheApp->OnReshape(iWidth,iHeight);
    }
Equivalently, you can declare this function as a static
member of Application (in the Application.h file):
    static void Application::ReshapeCallback (int iWidth, int iHeight);
and implement as
    void Application::ReshapeCallback (int iWidth, int iHeight)
    {
        if ( ms_pkApplication )
            ms_pkApplication ->OnReshape(iWidth,iHeight);
    }
Notice that the constructor sets ms_pkApplication to 'this'.  My
variation on this is designed to let me implement Application using
WGL, GLUT, AGL, DX, or any other platform I choose.

> Do you understand anything ?

Of course.

--
Dave Eberly
http://www.magic-software.com


0
Reply Dave 6/6/2004 2:14:03 AM

"Dave Eberly" <dNOSPAMeberly@usemydomain.com> a �crit dans le message de
news:L5vwc.14757$Yd3.2964@newsread3.news.atl.earthlink.net...
>
> The callback functions
> are static C-style functions, but they each get a pointer
> to the application by a call to a static class member
> function, then thell that application object to do something.
>
[What does mean "thell" ? Is it a type mismatch or anything else ?]

Well, it's me again... Thanks for your patience, Dave, but I still need some
help! I've had a very interessed look at your site, and I think I wrote my
own code on the same model, but I kept the callback functions error. If you
don't mind, could you have a look a it? You'll find underneath all about
the Reshape function only, to keep this message not too long, but
everything's done in the same way for Display or Idle...

My base class (which is named "World" in fact) is defined (in world.h) as :
----------------------------------------------------------------------
  class World
  {
  private:
    static World* m_this; // set to "this" in constuctor
    [...]
  public:
    World();
    virtual ~World();
    static World* GetThis() {return m_this;}
    void Init();
    void DoReshape(int w, int h);
    [...]
  };
----------------------------------------------------------------------
In file world.cpp, I wrote the static C-style function :
----------------------------------------------------------------------
  static void Reshape(int w, int h)
  {
    World::GetThis()->DoReshape(w,h);
  }
----------------------------------------------------------------------
The Init() function then is :
----------------------------------------------------------------------
  void World::Init()
  {
    glutInitDisplayMode( GLUT_RGB
                                  | GLUT_DOUBLE
                                  | GLUT_DEPTH );
    glutInitWindowSize(640,640);
    glutCreateWindow("Glut alors !");

    glutReshapeFunc(Reshape);
    [...]
  }
----------------------------------------------------------------------

What's going wrong with that ?

J.No.


0
Reply Jean 6/7/2004 12:01:04 PM

>   class World
>   {
>   private:
>     static World* m_this; // set to "this" in constuctor
>     [...]
>   public:
>     World();
>     virtual ~World();
>     static World* GetThis() {return m_this;}
>     void Init();

static void DoReshape(int, int);


>     void DoReshape(int w, int h);
>     [...]
>   };

Here the static keyword can be left out. It's the prototype that gives
the linking, I think!?

> In file world.cpp, I wrote the static C-style function :
>   static void Reshape(int w, int h)


0
Reply Gernot 6/7/2004 12:51:28 PM

"Gernot Frisch" <Me@Privacy.net> a �crit dans le message de
news:2ij6kpFnqig0U1@uni-berlin.de...
> >   class World
> >   {
> >   private:
> >     static World* m_this; // set to "this" in constuctor
> >     [...]
> >   public:
> >     World();
> >     virtual ~World();
> >     static World* GetThis() {return m_this;}
> >     void Init();
>
> static void DoReshape(int, int);
>
>
> >     void DoReshape(int w, int h);
> >     [...]
> >   };
>
> Here the static keyword can be left out. It's the prototype that gives
> the linking, I think!?
>

The DoReshape function isn't static (it's not meant to be, is  it ? Its
equivalent isn't in Dave Eberly's code...).
Be careful there are 2 functions :
- member function DoReshape, not static
- non-member function Reshape, static, which calls DoReshape from the
GetThis() pointer.

> > In file world.cpp, I wrote the static C-style function :
> >   static void Reshape(int w, int h)
>
>


0
Reply Jean 6/7/2004 1:01:05 PM

"Jean-No�l M�goz" <nospam_jnmegoz@infonie.fr> wrote in message
news:40c45876$0$27474$626a14ce@news.free.fr...
>
> "Dave Eberly" <dNOSPAMeberly@usemydomain.com> a �crit dans le message de
> news:L5vwc.14757$Yd3.2964@newsread3.news.atl.earthlink.net...
> >
> > The callback functions
> > are static C-style functions, but they each get a pointer
> > to the application by a call to a static class member
> > function, then thell that application object to do something.
> >
> [What does mean "thell" ? Is it a type mismatch or anything else ?]

I mistyped "tell" :)

> What's going wrong with that ?

I implemented a test project using your posted code.  I
have no compiler complaints.  See
http://www.magic-software.com/Temp/TestGLUT.zip
The compiler complaints must occur due to some other
problem.  Sometimes compiler complaints are side effects
of other errors, but the messages are not about the actual
cause.

--
Dave Eberly
http://www.magic-software.com


0
Reply Dave 6/7/2004 3:11:29 PM

7 Replies
300 Views

(page loaded in 0.065 seconds)

Similiar Articles:













7/24/2012 4:05:05 PM


Reply: