www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Complex C typedef decl replacement

reply "Andrey" <andr-sar yandex.ru> writes:
Hello everyone!

What a proper replacement is for C declarations like this?

typedef void (APIENTRYP PFNGLGETCOMPRESSEDTEXIMAGEPROC) (GLenum 
target, GLint level, GLvoid *img);

typedef GLvoid (APIENTRY * PFNGLMULTIDRAWARRAYSPROC) (GLenum 
mode, const GLint *first, const GLsizei *count, GLsizei 
primcount);

And this:

void glAlphaFunc( GLenum func, GLclampf ref ); //ref is not 
acceptable as var name in D

alias void (* _GLUfuncptr)(); //how to make a proper aliasing of 
a function pointer?
Nov 23 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-11-23 14:26, Andrey wrote:
 Hello everyone!

 What a proper replacement is for C declarations like this?

 typedef void (APIENTRYP PFNGLGETCOMPRESSEDTEXIMAGEPROC) (GLenum target,
 GLint level, GLvoid *img);
I would guess that APIENTRYP and PFNGLGETCOMPRESSEDTEXIMAGEPROC are macros. You first have to look up what those evaluate to. This is probably a function pointer.
 typedef GLvoid (APIENTRY * PFNGLMULTIDRAWARRAYSPROC) (GLenum mode, const
 GLint *first, const GLsizei *count, GLsizei primcount);
Same as above.
 And this:

 void glAlphaFunc( GLenum func, GLclampf ref ); //ref is not acceptable
 as var name in D
Just use a difference name, like ref_, the parameter names doesn't matter when creating bindings.
 alias void (* _GLUfuncptr)(); //how to make a proper aliasing of a
 function pointer?
A function pointer in D looks like: void function () foo; So to make an alias to the above function pointer, it would look like this: extern (C) void function () _GLUfuncptr; Take a look at DStep, a tool for automatically converting C headers to D modules: https://github.com/jacob-carlborg/dstep -- /Jacob Carlborg
Nov 23 2012
next sibling parent reply "Mike Parker" <aldacron gmail.com> writes:
On Friday, 23 November 2012 at 14:22:11 UTC, Jacob Carlborg wrote:
 On 2012-11-23 14:26, Andrey wrote:
 Hello everyone!

 What a proper replacement is for C declarations like this?

 typedef void (APIENTRYP PFNGLGETCOMPRESSEDTEXIMAGEPROC) 
 (GLenum target,
 GLint level, GLvoid *img);
I would guess that APIENTRYP and PFNGLGETCOMPRESSEDTEXIMAGEPROC are macros. You first have to look up what those evaluate to. This is probably a function pointer.
 typedef GLvoid (APIENTRY * PFNGLMULTIDRAWARRAYSPROC) (GLenum 
 mode, const
 GLint *first, const GLsizei *count, GLsizei primcount);
extern(System) alias void function(GLenum, GLint, GLvoid*) PFNGLGETCOMPRESSEDTEXIMAGEPROC; extern(System) alias void function(GLenum, const(GLint)*, const(GLsizei)*, GLsizei) PFNGLMULTIDRAWARRAYSPROC;
 So to make an alias to the above function pointer, it would 
 look like this:

 extern (C) void function () _GLUfuncptr;
Should likely be extern(System), as OpenGL & GLU are extern(Windows) on Windows and extern(C) everywhere else.
Nov 23 2012
next sibling parent reply "Andrey" <andr-sar yandex.ru> writes:
Thank you much!
Nov 23 2012
parent "Mike Parker" <aldacron gmail.com> writes:
On Friday, 23 November 2012 at 15:07:05 UTC, Andrey wrote:
 Thank you much!
If you're doing this to use OpenGL in D, rather than as a learning exercise, you can save yourself a lot of pain by using Derelict. https://github.com/aldacron/Derelict3/
Nov 23 2012
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-11-23 16:01, Mike Parker wrote:

 Should likely be extern(System), as OpenGL & GLU are extern(Windows) on
 Windows and extern(C) everywhere else.
Right. -- /Jacob Carlborg
Nov 25 2012
prev sibling parent "Andrey" <andr-sar yandex.ru> writes:
Thank you for the reply. I guess, I should have added this before.


#if !defined(OPENSTEP) && (defined(__WIN32__) && 
!defined(__CYGWIN__))

defined(BUILD_GL32)

/* tag specify we're building mesa as a DLL */



defined(_DLL)

/* tag specifying we're building for DLL runtime support */



only */



defined(UNDER_CE)

/* The generated DLLs by MingW with STDCALL are not compatible 
with the ones done by Microsoft's compilers */





#elif defined(__CYGWIN__) && defined(USE_OPENGL32) /* use native 
windows opengl32 */


#elif (defined(__GNUC__) && __GNUC__ >= 4) || 
(defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))


#endif /* WIN32 && !CYGWIN */

#if (defined(__BEOS__) && defined(__POWERPC__)) || 
defined(__QUICKDRAW__)

#endif

/*
  * WINDOWS: Include windows.h here to define APIENTRY.
  * It is also useful when applications include this file by
  * including only glut.h, since glut.h depends on windows.h.
  * Applications needing to include windows.h with parms other
  * than "WIN32_LEAN_AND_MEAN" may include windows.h before
  * glut.h or gl.h.
  */
#if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#endif

#if defined(macintosh) && PRAGMA_IMPORT_SUPPORTED
#pragma import on
#endif

#ifndef GLAPI
#define GLAPI extern
#endif

#ifndef GLAPIENTRY
#define GLAPIENTRY
#endif

#ifndef APIENTRY
#define APIENTRY GLAPIENTRY
#endif

/* "P" suffix to be used for a pointer to a function */
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif

Anyway, I don't understand C macro magic much and can't figure 
out, what exactly they do.

Even if I process the macros, what should I expect from those 
typedefs? Simple functions?
Nov 23 2012