Big code cleanup

74 views
Skip to first unread message

Nicolas Rougier

unread,
May 7, 2012, 7:12:40 AM5/7/12
to freet...@googlegroups.com
I just committed a big code cleanup and added API documentation.

Hope everthing's ok and did not break things.


Nicolas

Yongcong Wang

unread,
Jun 13, 2012, 3:59:49 AM6/13/12
to freet...@googlegroups.com
Finally I got time to check out the latest code and walk through them. There are a lot issues under Windows & VS,  which I had commented before but seem to be ignored...

Environment: Windows, VS2010 Express, r152

1,  On Windows, you must include <Windows.h> before OpenGL headers. I recommend in opengl.h :
 
#if defined(__APPLE__)
#  ifdef GL_ES_VERSION_2_0
#    include <OpenGLES/ES2/gl.h>
#  else
#    include <OpenGL/gl.h>
#    include <Glut/glut.h>
#  endif
#elif defined(_WIN32) || defined(_WIN64)
    #include <Windows.h>
    #include <GL/glew.h>
    #include <GL/wglew.h>
    #include <GLUT/glut.h> // changed according to your path of glut.h 
#else
    #include <GL/gl.h>
    #include <GL/glut.h>
#endif

2, texture-atlas.c should removed the following code and instead include opengl.h:

#if defined(__APPLE__)
    #include <OpenGL/gl.h>
#else
    #include <GL/gl.h>
#endif

 All files that need OpenGL API should follow this rule. The same problem also occurs in shader.c.

3, texture-font.c should include platform.h, because it use function round()

4, vector.c, line 84

return self->items + index * self->item_size;

On VC, you could not add an offset to void*, instead you should:

 return (char*)(self->items) + index * self->item_size;

 The same problems occurred in line 210, 230, 250, 322, 344... (please search for them)

5, In vertex-buffer.c: strndup() is not available on Windows, too. Add the following code:

// strndup() was only added in OSX lion
#ifdef __APPLE__
char *
strndup( const char *s1, size_t n)
{
    char *copy = calloc( n+1, sizeof(char) );
    memcpy( copy, s1, n );
    return copy;
};
#elif defined(_WIN32) || defined(_WIN64) 
// strndup() is not available on Windows, too
char *
strndup( const char *s1, size_t n)
{
char *copy= (char*)malloc( n+1 );
memcpy( copy, s1, n );
copy[n] = 0;
return copy;
};
#endif

6, text-buffer.c, the following code is illegality in VC:

        vertices[vcount+0] =
            (glyph_vertex_t) { (int)x0,y0,0,  s0,t0,  r,g,b,a,  x0-((int)x0), gamma };

instead you should:

vertices[vcount+0].x = x0;
vertices[vcount+0].y = y0;
// etc..

I recommend the use of macro:

#define SET_GLYPH_VERTEX(value,x0,y0,z0,s0,t0,r,g,b,a,sh,gm) {\
glyph_vertex_t *gv=&value;\
gv->x=x0;gv->y=y0;gv->z=z0;\
gv->u=s0;gv->v=t0;\
gv->r=r;gv->g=g;gv->b=b;gv->a=a;\
gv->shift=sh;\
gv->gamma=gm;}
SET_GLYPH_VERTEX(vertices[vcount+0], (int)x0,y0,0,  s0,t0,  r,g,b,a,  x0-((int)x0), gamma);

 7, demo-console.c, added the following:

#define wcpncpy wcsncpy
#define wcpcpy wcscpy

 Because wcpncpy() and wcpcpy() is not usable in VC

8, demo-console.c, line 116, following code is not allowed in VC:

    markup_t normal = {
        .family  = "fonts/VeraMono.ttf",
        .size    = 13.0,
        .bold    = 0,
        .italic  = 0,
        .rise    = 0.0,
        .spacing = 0.0,
        .gamma   = 1.0,
        .foreground_color    = black,
        .background_color    = none,
        .underline           = 0,
        .underline_color     = white,
        .overline            = 0,
        .overline_color      = white,
        .strikethrough       = 0,
        .strikethrough_color = white,
        .font = 0,
    };

Instead you should: 

    markup_t normal;
    normal.family  = "fonts/VeraMono.ttf",
    normal.size    = 13.0,
    normal.bold    = 0,
    normal.italic  = 0,
    normal.rise    = 0.0,
    normal.spacing = 0.0,
    normal.gamma   = 1.0,
    normal.foreground_color    = black,
    normal.background_color    = none,
    normal.underline           = 0,
    normal.underline_color     = white,
    normal.overline            = 0,
    normal.overline_color      = white,
    normal.strikethrough       = 0,
    normal.strikethrough_color = white,
    normal.font = 0,


在 2012年5月7日星期一UTC+8下午7时12分40秒,Nicolas Rougier写道:
Message has been deleted

Yongcong Wang

unread,
Jun 13, 2012, 4:09:45 AM6/13/12
to freet...@googlegroups.com
I also found that in VC, I must change the default compiler option /TC (Compile as C Code) to /TP (Compile as C++ Code) because of the following compiler error: 

typedef struct {
     // ...
} texture_atlas_t;
texture_atlas_t *atlas = (texture_atlas_t*)malloc(sizeof(texture_atlas_t)); // Error C2143 

Maybe it's caused by the poor support of C98 in VC. 


在 2012年5月7日星期一UTC+8下午7时12分40秒,Nicolas Rougier写道:

Nicolas Rougier

unread,
Jun 13, 2012, 4:23:54 AM6/13/12
to freet...@googlegroups.com

Thanks a lot for the nice and detailed report. I've just committed all the suggested changes.

Nicolas

Nicolas Rougier

unread,
Jun 13, 2012, 4:25:54 AM6/13/12
to freet...@googlegroups.com

I think this is the explanation, VC seems to only support C89. Do you know by any chance how to add the default /TP to VC in the CMakeLists.txt ?

Nicolas

quarnster

unread,
Nov 18, 2012, 3:58:33 AM11/18/12
to freet...@googlegroups.com
Old topic, but something like the following (untested) should work:

if(MSVC)
  foreach(flag_var
          CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    set(${flag_var} "${${flag_var}} /TP")
  endforeach(flag_var)
endif()

/f

Nicolas Rougier

unread,
Nov 20, 2012, 1:09:33 PM11/20/12
to freet...@googlegroups.com

Great I'll try to integrate it in the gl-3.0 branch with the newly proposed cmake patch (see issues page).
Thanks.
Reply all
Reply to author
Forward
0 new messages