Advertisement
xerpi

sftd crappy atlas

May 24th, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. #define ATLAS_W 512
  2. #define ATLAS_H 512
  3.  
  4. typedef struct {
  5.     int x, y, w, h, left, top, v;
  6.     int advance_x, advance_y;
  7. } atlas_position;
  8.  
  9. struct sftd_font {
  10.     sftd_font_load_from from;
  11.     union {
  12.         char *filename;
  13.         struct {
  14.             const void *font_buffer;
  15.             unsigned int buffer_size;
  16.         };
  17.     };
  18.     FTC_CMapCache cmapcache;
  19.     FTC_ImageCache imagecache;
  20.  
  21.     // Atlas
  22.     sf2d_texture *atlas;
  23.     atlas_position atlas_pos[65536];
  24.     int atlas_x;
  25.     int atlas_y;
  26. };
  27.  
  28.  
  29. sftd_font *sftd_load_font_mem(const void *buffer, unsigned int size)
  30. {
  31.     sftd_font *font = malloc(sizeof(*font));
  32.     font->font_buffer = buffer;
  33.     font->buffer_size = size;
  34.  
  35.     FTC_CMapCache_New(ftcmanager, &font->cmapcache);
  36.     FTC_ImageCache_New(ftcmanager, &font->imagecache);
  37.  
  38.     // Atlas
  39.     font->atlas = sf2d_create_texture(ATLAS_W, ATLAS_H, TEXFMT_RGBA8, SF2D_PLACE_RAM);
  40.     memset(font->atlas_pos, 0, sizeof(*font->atlas_pos) * 65536);
  41.     font->atlas->tiled = 1;
  42.     font->atlas_x = 0;
  43.     font->atlas_y = 0;
  44.  
  45.     font->from = SFTD_LOAD_FROM_MEM;
  46.  
  47.     return font;
  48. }
  49.  
  50.  
  51. void sftd_draw_text(sftd_font *font, int x, int y, unsigned int color, unsigned int size, const char *text)
  52. {
  53.     FTC_FaceID face_id = (FTC_FaceID)font;
  54.     FT_Face face;
  55.     FTC_Manager_LookupFace(ftcmanager, face_id, &face);
  56.  
  57.     FT_Int charmap_index;
  58.     charmap_index = FT_Get_Charmap_Index(face->charmap);
  59.  
  60.     FT_Glyph glyph;
  61.     FT_Bool use_kerning = FT_HAS_KERNING(face);
  62.     FT_UInt glyph_index, previous = 0;
  63.     int pen_x = x;
  64.     int pen_y = y;
  65.  
  66.     FTC_ScalerRec scaler;
  67.     scaler.face_id = face_id;
  68.     scaler.width = size;
  69.     scaler.height = size;
  70.     scaler.pixel = 1;
  71.  
  72.     FT_ULong flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL;
  73.  
  74.     while (*text) {
  75.         glyph_index = FTC_CMapCache_Lookup(font->cmapcache, (FTC_FaceID)font, charmap_index, *text);
  76.  
  77.         if (use_kerning && previous && glyph_index) {
  78.             FT_Vector delta;
  79.             FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
  80.             pen_x += delta.x >> 6;
  81.         }
  82.  
  83.         if (font->atlas_pos[glyph_index].v) {
  84.             sf2d_draw_texture_part(font->atlas,
  85.                 pen_x + font->atlas_pos[glyph_index].left + x,
  86.                 pen_y - font->atlas_pos[glyph_index].top + y,
  87.                 font->atlas_pos[glyph_index].x,
  88.                 font->atlas_pos[glyph_index].y,
  89.                 font->atlas_pos[glyph_index].w,
  90.                 font->atlas_pos[glyph_index].h);
  91.  
  92.             pen_x += font->atlas_pos[glyph_index].advance_x >> 16;
  93.             pen_y += font->atlas_pos[glyph_index].advance_y >> 16;
  94.  
  95.         } else {
  96.             FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL);
  97.             if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
  98.                 FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
  99.  
  100.                 FT_Bitmap *bitmap = &bitmap_glyph->bitmap;
  101.  
  102.                 // Get next atlas pos;
  103.                 if (font->atlas_x + size > ATLAS_W) {
  104.                     font->atlas_x = 0;
  105.                     font->atlas_y += size;
  106.                 }
  107.  
  108.                 //printf("atlas_x: %i  atlas_y: %i\n", font->atlas_x, font->atlas_y);
  109.                 //printf("bwidth: %i  brows: %i\n", bitmap->width, bitmap->rows);
  110.  
  111.                 //draw_bitmap(&bitmap_glyph->bitmap, pen_x + bitmap_glyph->left + x, pen_y - bitmap_glyph->top + y, color);
  112.                 int j, k;
  113.                 for (j = 0; j < bitmap->rows; j++) {
  114.                     for (k = 0; k < bitmap->width; k++) {
  115.                         sf2d_set_pixel(font->atlas,
  116.                             (font->atlas_x + k),
  117.                             (font->atlas_y + j),
  118.                             __builtin_bswap32((color & ~0xFF) | bitmap->buffer[j*bitmap->width + k]));
  119.                     }
  120.                 }
  121.  
  122.                 font->atlas_pos[glyph_index].left = bitmap_glyph->left;
  123.                 font->atlas_pos[glyph_index].top = bitmap_glyph->top;
  124.                 font->atlas_pos[glyph_index].x = font->atlas_x;
  125.                 font->atlas_pos[glyph_index].y = font->atlas_y;
  126.                 font->atlas_pos[glyph_index].w = bitmap->width;
  127.                 font->atlas_pos[glyph_index].h = bitmap->rows;
  128.                 font->atlas_pos[glyph_index].advance_x = bitmap_glyph->root.advance.x;
  129.                 font->atlas_pos[glyph_index].advance_y = bitmap_glyph->root.advance.y;
  130.                 font->atlas_pos[glyph_index].v = 1;
  131.  
  132.                 sf2d_draw_texture_part(font->atlas,
  133.                     pen_x + font->atlas_pos[glyph_index].left + x,
  134.                     pen_y - font->atlas_pos[glyph_index].top + y,
  135.                     font->atlas_pos[glyph_index].x,
  136.                     font->atlas_pos[glyph_index].y,
  137.                     font->atlas_pos[glyph_index].w,
  138.                     font->atlas_pos[glyph_index].h);
  139.  
  140.                 font->atlas_x += size;
  141.  
  142.                 pen_x += bitmap_glyph->root.advance.x >> 16;
  143.                 pen_y += bitmap_glyph->root.advance.y >> 16;
  144.             }
  145.  
  146.         }
  147.  
  148.         previous = glyph_index;
  149.         text++;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement