From ab9243ea38ea4969a47c8dba0ef44684f06c1aeb Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 17 Nov 2011 23:21:08 +0000 Subject: [PATCH] - got freetype-gl working under windows --- mk/windoze/glest_game.vcproj | 2 +- mk/windoze/shared_lib.vcproj | 48 ++++++++++++++++--- .../graphics/freetype-gl/vertex-buffer.h | 21 ++++---- .../graphics/freetype-gl/font-manager.c | 11 +++++ .../graphics/freetype-gl/texture-atlas.c | 40 +++++++++++++--- .../graphics/freetype-gl/texture-font.c | 14 +++++- .../graphics/freetype-gl/texture-glyph.c | 16 ++++++- .../sources/graphics/freetype-gl/vector.c | 23 +++++---- .../graphics/freetype-gl/vertex-buffer.c | 20 +++++++- .../graphics/gl/font_text_freetypegl.cpp | 27 ++++++----- 10 files changed, 171 insertions(+), 51 deletions(-) diff --git a/mk/windoze/glest_game.vcproj b/mk/windoze/glest_game.vcproj index 01f0b1ed..356cd895 100644 --- a/mk/windoze/glest_game.vcproj +++ b/mk/windoze/glest_game.vcproj @@ -163,7 +163,7 @@ /> + + + + + + @@ -376,6 +388,10 @@ RelativePath="..\..\source\shared_lib\sources\map\map_preview.cpp" > + + @@ -396,6 +412,26 @@ RelativePath="..\..\source\shared_lib\sources\util\string_utils.cpp" > + + + + + + + + + + @@ -500,11 +536,11 @@ > + //#include +#elif defined(_WIN32) + +#define WIN32_LEAN_AND_MEAN +#include +#include + #else - #include + //#include #endif #include "vector.h" #define MAX_VERTEX_ATTRIBUTE 64 -#ifdef __cplusplus -extern "C" { -#endif - - /** * Generic vertex attribute. */ @@ -133,8 +135,6 @@ typedef struct VertexAttribute *attributes[MAX_VERTEX_ATTRIBUTE]; } VertexBuffer; - - /** * Creates an empty vertex buffer. * @@ -470,8 +470,5 @@ vertex_attribute_new( GLenum target, char * GL_TYPE_STRING( GLenum gtype ); -#ifdef __cplusplus -} -#endif #endif /* __VERTEX_BUFFER_H__ */ diff --git a/source/shared_lib/sources/graphics/freetype-gl/font-manager.c b/source/shared_lib/sources/graphics/freetype-gl/font-manager.c index da934a1e..d28fcbc3 100644 --- a/source/shared_lib/sources/graphics/freetype-gl/font-manager.c +++ b/source/shared_lib/sources/graphics/freetype-gl/font-manager.c @@ -30,7 +30,10 @@ * those of the authors and should not be interpreted as representing official * policies, either expressed or implied, of Nicolas P. Rougier. * ========================================================================= */ +#ifdef HAVE_FONTCONFIG #include +#endif + #include #include #include @@ -122,6 +125,7 @@ font_manager_get_from_description( FontManager *self, { assert( self ); + { TextureFont *font; char *filename = font_manager_match_description( self, family, size, bold, italic ); // fprintf(stderr, "Matched filename for %s: %s\n", family, filename); @@ -132,6 +136,7 @@ font_manager_get_from_description( FontManager *self, font = font_manager_get_from_filename( self, filename, size ); free( filename ); return font; + } } TextureFont * @@ -140,10 +145,12 @@ font_manager_get_from_markup( FontManager *self, { assert( self ); assert( markup ); + { TextureFont *font = font_manager_get_from_description( self, markup->family, markup->size, markup->bold, markup->italic ); return font; + } } @@ -155,6 +162,8 @@ font_manager_match_description( FontManager *self, const int italic ) { char *filename = 0; + +#ifdef HAVE_FONTCONFIG int weight = FC_WEIGHT_REGULAR; int slant = FC_SLANT_ROMAN; if ( bold ) @@ -196,6 +205,8 @@ font_manager_match_description( FontManager *self, } } FcPatternDestroy( match ); +#endif + return filename; } diff --git a/source/shared_lib/sources/graphics/freetype-gl/texture-atlas.c b/source/shared_lib/sources/graphics/freetype-gl/texture-atlas.c index be7f3148..e4fee448 100644 --- a/source/shared_lib/sources/graphics/freetype-gl/texture-atlas.c +++ b/source/shared_lib/sources/graphics/freetype-gl/texture-atlas.c @@ -31,11 +31,19 @@ * policies, either expressed or implied, of Nicolas P. Rougier. * ========================================================================= */ #if defined(__APPLE__) - #include + //#include #else - #include + //#include #endif -#include +//#include + +#ifdef WIN32 +#include +#endif +#include +#include +//#include + #include #include #include @@ -55,6 +63,7 @@ texture_atlas_new( size_t width, size_t height, size_t depth ) { assert( (depth == 1) || (depth == 3) ); + { TextureAtlas *self = (TextureAtlas *) malloc( sizeof(TextureAtlas) ); if( !self ) { @@ -65,18 +74,22 @@ texture_atlas_new( size_t width, size_t height, size_t depth ) self->width = width; self->height = height; self->depth = depth; + + { Node node = {0,0,width}; vector_push_back( self->nodes, &node ); self->texid = 0; self->data = (unsigned char *) calloc( width*height*depth, sizeof(unsigned char) ); - + { // This is a special region that is used for background and underlined // decorations of glyphs int n = 4; - unsigned char buffer[n*n]; + //unsigned char buffer[n*n]; + unsigned char buffer[16]; memset(buffer, 255, n*n); + { Region r = texture_atlas_get_region( self, n, n ); texture_atlas_set_region( self, r.x, r.y, r.width, r.height, buffer, 1); self->black.x = r.x + 1; @@ -85,6 +98,10 @@ texture_atlas_new( size_t width, size_t height, size_t depth ) self->black.height= r.height - 2; return self; + } + } + } + } } @@ -149,6 +166,7 @@ texture_atlas_set_region( TextureAtlas *self, assert( (x + width) <= self->width); assert( y < self->height); assert( (y + height) <= self->height); + { size_t i; size_t depth = self->depth; size_t charsize = sizeof(char); @@ -157,6 +175,7 @@ texture_atlas_set_region( TextureAtlas *self, memcpy( self->data+((y+i)*self->width + x ) * charsize * depth, data + (i*stride) * charsize, width * charsize * depth ); } + } } @@ -224,6 +243,7 @@ texture_atlas_get_region( TextureAtlas *self, assert( width ); assert( height ); */ + { int y, best_height, best_width, best_index; Node *node, *prev; Region region = {0,0,width,height}; @@ -294,6 +314,7 @@ texture_atlas_get_region( TextureAtlas *self, texture_atlas_merge( self ); self->used += width * height; return region; + } } @@ -304,21 +325,28 @@ texture_atlas_clear( TextureAtlas *self ) vector_clear( self->nodes ); self->used = 0; + { Node node = {0,0,self->width}; vector_push_back( self->nodes, &node ); memset( self->data, 0, self->width*self->height*self->depth ); + { // This is a special region that is used for background and underlined // decorations of glyphs int n = 4; - unsigned char buffer[n*n]; + //unsigned char buffer[n*n]; + unsigned char buffer[16]; memset(buffer, 255, n*n); + { Region r = texture_atlas_get_region( self, n, n ); texture_atlas_set_region( self, r.x, r.y, r.width, r.height, buffer, 1); self->black.x = r.x + 1; self->black.y = r.y + 1; self->black.width = r.width - 2; self->black.height= r.height - 2; + } + } + } } diff --git a/source/shared_lib/sources/graphics/freetype-gl/texture-font.c b/source/shared_lib/sources/graphics/freetype-gl/texture-font.c index ef132e32..f12860d2 100644 --- a/source/shared_lib/sources/graphics/freetype-gl/texture-font.c +++ b/source/shared_lib/sources/graphics/freetype-gl/texture-font.c @@ -34,7 +34,7 @@ #include FT_FREETYPE_H // #include FT_ADVANCES_H #include FT_LCD_FILTER_H -#include +//#include #include #include #include @@ -83,6 +83,7 @@ texture_font_new( TextureAtlas *atlas, self->lcd_weights[3] = 0; self->lcd_weights[4] = 0; + { /* Get font metrics at high resolution */ FT_Library library; FT_Face face; @@ -91,6 +92,7 @@ texture_font_new( TextureAtlas *atlas, return self; } + { FT_Size_Metrics metrics = face->size->metrics; self->ascender = (metrics.ascender >> 6) / 100.0; self->descender = (metrics.descender >> 6) / 100.0; @@ -98,6 +100,8 @@ texture_font_new( TextureAtlas *atlas, self->linegap = self->height - self->ascender + self->descender; return self; + } + } } @@ -224,6 +228,7 @@ texture_font_cache_glyphs( TextureFont *self, { glyph_index = FT_Get_Char_Index( face, charcodes[i] ); + { // WARNING: We use texture-atlas depth to guess if user wants // LCD subpixel rendering FT_Int32 flags = FT_LOAD_RENDER; @@ -239,12 +244,16 @@ texture_font_cache_glyphs( TextureFont *self, if( depth == 3 ) { +#if ((FREETYPE_MAJOR <= 2) && ((FREETYPE_MINOR < 3))) +//#error "Need FreeType 2.3.0 or newer" +#else FT_Library_SetLcdFilter( library, FT_LCD_FILTER_LIGHT ); flags |= FT_LOAD_TARGET_LCD; if( self->lcd_filter ) { FT_Library_SetLcdFilterWeights( library, self->lcd_weights ); } +#endif } error = FT_Load_Glyph( face, glyph_index, flags ); @@ -308,6 +317,7 @@ texture_font_cache_glyphs( TextureFont *self, vector_push_back( self->glyphs, glyph ); texture_glyph_delete( glyph ); + } } FT_Done_Face( face ); FT_Done_FreeType( library ); @@ -350,8 +360,10 @@ texture_font_get_glyph( TextureFont *self, if( texture_font_cache_glyphs( self, buffer ) == 0 ) { + free(buffer); return (TextureGlyph *) vector_back( self->glyphs ); } + free(buffer); return NULL; } diff --git a/source/shared_lib/sources/graphics/freetype-gl/texture-glyph.c b/source/shared_lib/sources/graphics/freetype-gl/texture-glyph.c index c6dba7e3..96684b1d 100644 --- a/source/shared_lib/sources/graphics/freetype-gl/texture-glyph.c +++ b/source/shared_lib/sources/graphics/freetype-gl/texture-glyph.c @@ -31,10 +31,14 @@ * policies, either expressed or implied, of Nicolas P. Rougier. * ========================================================================= */ #if defined(__APPLE__) - #include + //#include #else - #include + +//#define WIN32_LEAN_AND_MEAN +//#include +#include #endif + #include #include #include "texture-font.h" @@ -84,6 +88,7 @@ texture_glyph_render( TextureGlyph *self, { assert( self ); + { int x = pen->x + self->offset_x; int y = pen->y + self->offset_y + markup->rise; int w = self->width; @@ -108,6 +113,7 @@ texture_glyph_render( TextureGlyph *self, pen->x += self->advance_x + markup->spacing; pen->y += self->advance_y; + } } @@ -150,6 +156,8 @@ texture_glyph_add_to_vertex_buffer( const TextureGlyph *self, g = markup->background_color.g; b = markup->background_color.b; a = markup->background_color.a; + + { GLuint index = buffer->vertices->size; GLuint indices[] = {index, index+1, index+2, index, index+2, index+3}; @@ -159,6 +167,7 @@ texture_glyph_add_to_vertex_buffer( const TextureGlyph *self, { x1,y0,0, u1,v0, r,g,b,a } }; vertex_buffer_push_back_indices( buffer, indices, 6 ); vertex_buffer_push_back_vertices( buffer, vertices, 4 ); + } } // Underline @@ -177,6 +186,8 @@ texture_glyph_add_to_vertex_buffer( const TextureGlyph *self, b = markup->foreground_color.b; a = markup->foreground_color.a; } + + { int x0 = (int)( pen->x + self->offset_x ); int y0 = (int)( pen->y + self->offset_y + rise ); int x1 = (int)( x0 + self->width ); @@ -197,6 +208,7 @@ texture_glyph_add_to_vertex_buffer( const TextureGlyph *self, pen->x += self->advance_x + spacing; pen->y += self->advance_y; + } } diff --git a/source/shared_lib/sources/graphics/freetype-gl/vector.c b/source/shared_lib/sources/graphics/freetype-gl/vector.c index 204d4416..4a36777f 100644 --- a/source/shared_lib/sources/graphics/freetype-gl/vector.c +++ b/source/shared_lib/sources/graphics/freetype-gl/vector.c @@ -44,6 +44,7 @@ vector_new( size_t item_size ) { assert( item_size ); + { Vector *self = (Vector *) malloc( sizeof(Vector) ); if( !self ) { @@ -54,6 +55,7 @@ vector_new( size_t item_size ) self->capacity = 1; self->items = malloc( self->item_size * self->capacity ); return self; + } } @@ -77,7 +79,8 @@ vector_get( const Vector *self, assert( self ); assert( self->size ); assert( index < self->size ); - return self->items + index * self->item_size; + + return (char *)self->items + index * self->item_size; } @@ -202,7 +205,7 @@ vector_set( Vector *self, assert( self ); assert( self->size ); assert( index < self->size ); - memcpy( self->items + index * self->item_size, + memcpy( (char *)self->items + index * self->item_size, item, self->item_size ); } @@ -223,8 +226,8 @@ vector_insert( Vector *self, } if( index < self->size ) { - memmove( self->items + (index + 1) * self->item_size, - self->items + (index + 0) * self->item_size, + memmove( (char *)self->items + (index + 1) * self->item_size, + (char *)self->items + (index + 0) * self->item_size, (self->size - index) * self->item_size); } self->size++; @@ -243,8 +246,8 @@ vector_erase_range( Vector *self, assert( first < self->size ); assert( last < self->size+1 ); assert( first < last ); - memmove( self->items + first * self->item_size, - self->items + last * self->item_size, + memmove( (char *)self->items + first * self->item_size, + (char *)self->items + last * self->item_size, (self->size - last) * self->item_size); self->size -= (last-first); } @@ -318,7 +321,7 @@ vector_push_back_data( Vector *self, { vector_reserve(self, self->size+count); } - memmove( self->items + self->size * self->item_size, data, + memmove( (char *)self->items + self->size * self->item_size, data, count*self->item_size ); self->size += count; } @@ -341,10 +344,10 @@ vector_insert_data( Vector *self, { vector_reserve(self, self->size+count); } - memmove( self->items + (index + count ) * self->item_size, - self->items + (index ) * self->item_size, + memmove( (char *)self->items + (index + count ) * self->item_size, + (char *)self->items + (index ) * self->item_size, count*self->item_size ); - memmove( self->items + index * self->item_size, data, + memmove( (char *)self->items + index * self->item_size, data, count*self->item_size ); self->size += count; } diff --git a/source/shared_lib/sources/graphics/freetype-gl/vertex-buffer.c b/source/shared_lib/sources/graphics/freetype-gl/vertex-buffer.c index 32055923..38607b9d 100644 --- a/source/shared_lib/sources/graphics/freetype-gl/vertex-buffer.c +++ b/source/shared_lib/sources/graphics/freetype-gl/vertex-buffer.c @@ -63,6 +63,7 @@ vertex_buffer_new( const char *format ) do { end = (char *) (strchr(start+1, ':')); + { char *desc = 0; if (end == NULL) { @@ -72,14 +73,18 @@ vertex_buffer_new( const char *format ) { desc = strndup(start, end-start); } + + { VertexAttribute *attribute = vertex_attribute_parse( desc ); start = end+1; free(desc); attribute->pointer = pointer; stride += attribute->size*GL_TYPE_SIZE( attribute->type ); - pointer+= attribute->size*GL_TYPE_SIZE( attribute->type ); + pointer = (char *)pointer + attribute->size*GL_TYPE_SIZE( attribute->type ); self->attributes[index] = attribute; index++; + } + } } while ( end && (index < MAX_VERTEX_ATTRIBUTE) ); for( i=0; ivertices_id ); + { size_t i; for( i=0; i @@ -22,10 +22,12 @@ #include #endif +#include "platform_common.h" #include "util.h" using namespace std; using namespace Shared::Util; +using namespace Shared::PlatformCommon; namespace Shared { namespace Graphics { namespace Gl { @@ -67,7 +69,7 @@ void TextFreetypeGL::init(string fontName, int fontSize) { this->buffer = vertex_buffer_new( "v3f:t2f:c4f" ); //font = texture_font_new( atlas, "Verdana", minsize ); - this->font = texture_font_new( atlas, fontFile, fontFaceSize ); + this->font = texture_font_new( atlas, fontFile, (float)fontFaceSize ); //font = texture_font_new( atlas, font_manager_match_description( 0, "Verdana", minsize, bold, italic ), minsize ); int missed = texture_font_cache_glyphs( font, cache ); @@ -95,10 +97,10 @@ void TextFreetypeGL::Render(const char* str, const int len) { vertex_buffer_clear( this->buffer ); - float currentColor[4]; + float currentColor[4] = { 0,0,0,1 }; glGetFloatv(GL_CURRENT_COLOR,currentColor); - Markup markup = { 0, this->fontFaceSize, 0, 0, 0.0, 0.0, + Markup markup = { 0, (float)this->fontFaceSize, 0, 0, 0.0, 0.0, {currentColor[0],currentColor[1],currentColor[2],currentColor[3]}, {0,0,0,0}, 0, {0,0,0,1}, 0, {0,0,0,1}, 0, {0,0,0,1}, 0, {0,0,0,1}, 0 }; @@ -118,10 +120,10 @@ void TextFreetypeGL::Render(const char* str, const int len) { TextureGlyph *glyph = texture_font_get_glyph( this->font, thisChar ); // Take kerning into account if necessary - int kx = texture_glyph_get_kerning( glyph, prevChar ); + float kx = texture_glyph_get_kerning( glyph, prevChar ); // Add glyph to the vertex buffer - texture_glyph_add_to_vertex_buffer( glyph, this->buffer, &markup, &pen, kx ); + texture_glyph_add_to_vertex_buffer( glyph, this->buffer, &markup, &pen, (int)kx ); } //glBindTexture( GL_TEXTURE_2D, manager->atlas->texid ); @@ -130,6 +132,7 @@ void TextFreetypeGL::Render(const char* str, const int len) { glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glEnable( GL_TEXTURE_2D ); + vertex_buffer_render( this->buffer, GL_TRIANGLES, "vtc" ); glDisable( GL_TEXTURE_2D ); @@ -149,10 +152,10 @@ void TextFreetypeGL::Render(const wchar_t* str, const int len) { vertex_buffer_clear( this->buffer ); - float currentColor[4]; + GLfloat currentColor[4] = { 0,0,0,1 }; glGetFloatv(GL_CURRENT_COLOR,currentColor); - Markup markup = { 0, this->fontFaceSize, 0, 0, 0.0, 0.0, + Markup markup = { 0, (float)this->fontFaceSize, 0, 0, 0.0, 0.0, {currentColor[0],currentColor[1],currentColor[2],currentColor[3]}, {0,0,0,0}, 0, {0,0,0,1}, 0, {0,0,0,1}, 0, {0,0,0,1}, 0, {0,0,0,1}, 0 }; @@ -163,10 +166,10 @@ void TextFreetypeGL::Render(const wchar_t* str, const int len) { TextureGlyph *glyph = texture_font_get_glyph( this->font, text[i] ); // Take kerning into account if necessary - int kx = texture_glyph_get_kerning( glyph, text[i-1] ); + float kx = texture_glyph_get_kerning( glyph, text[i-1] ); // Add glyph to the vertex buffer - texture_glyph_add_to_vertex_buffer( glyph, this->buffer, &markup, &pen, kx ); + texture_glyph_add_to_vertex_buffer( glyph, this->buffer, &markup, &pen, (int)kx ); } //glBindTexture( GL_TEXTURE_2D, manager->atlas->texid ); @@ -202,7 +205,7 @@ float TextFreetypeGL::LineHeight(const char* str, const int len) { float result = 0; if(strlen(str) > 0) { TextureGlyph *glyph = texture_font_get_glyph( font, str[0] ); - result = glyph->height; + result = (float)glyph->height; } return result; } @@ -211,7 +214,7 @@ float TextFreetypeGL::LineHeight(const wchar_t* str, const int len) { float result = 0; if(wcslen(str) > 0) { TextureGlyph *glyph = texture_font_get_glyph( font, str[0] ); - result = glyph->height; + result = (float)glyph->height; } return result; }