
global F_State f_state;

root_function F_State *
F_get_state() 
{
  return &f_state;
}

root_function Arena * 
F_get_arena()
{
  return f_state.arena;
}

root_function F_InitReceipt
F_init() {
  //String8 font_path = str8_lit("D:\\dev\\app_codebase\\data\\LiberationMono-Regular.ttf");
  String8 font_path = str8_lit("D:\\dev\\app_codebase\\data\\arial.ttf");
  
  // Load the ttf data into a simple buffer which is just temporary
  ArenaTemp scratch = scratch_get(0, 0);
  OS_Handle font_file = OS_file_open(OS_AccessFlag_Read, font_path); 
  OS_FileAttributes attrs = OS_attributes_from_file(font_file);
  String8 ttf_temp = OS_file_read(scratch.arena, font_file, 0, attrs.size);  

  f_state.arena = m_make_arena_reserve(Megabytes(FONT_ARENA_MEMORY_SIZE_MB));
  Arena* arena = f_state.arena;

  U64 pixel_height = 16;
  // ASCII 32..126, 96 glyphs
  U64 num_glyphs = 96;
  U64 start_glyph = 32;
  U64 atlas_size_x = 512;
  U64 atlas_size_y = atlas_size_x;

  stbtt_bakedchar *cdata = PushArrayZero(arena, stbtt_bakedchar, num_glyphs);
  U8 *baked_atlas = PushArrayZero(arena, U8, atlas_size_x*atlas_size_y);

  stbtt_BakeFontBitmap(ttf_temp.str, /* loaded font data from file */ 
                       0, /* offset */
                       (float)pixel_height, /* pixel height */
                       baked_atlas, /* pixel bitmap */
                       atlas_size_x, atlas_size_y, /* pixel width and height */
                       start_glyph, num_glyphs,
                       cdata);
  
  f_state.cdata = cdata;
  f_state.baked_atlas = baked_atlas;
  
  //U8 color = 0;
  //for(U32 i = 0; i < atlas_size_x; i++)
  //{
    //for(U32 j = 0; j < atlas_size_y; j++)
    //{
      //U32 index = j+i*atlas_size_x;
      //if(index % 32 == 0) {
        //if(color != 0) {
          //color = 0;
        //} else {
          //color = 255;
        //}
      //}
      //baked_atlas[index] = Max(index, 255);
      //
    //}
  //}
  
  Vec2_S64 atlas_size = vec2_S64(atlas_size_x, atlas_size_y);
  f_state.atlas.texture = R_tex2d_font_atlas(atlas_size, baked_atlas);
  f_state.atlas.size = atlas_size;
  f_state.atlas.start_glyph = start_glyph;
  f_state.atlas.last_glyph = start_glyph + num_glyphs;
  
  F_InitReceipt result = {0};
  scratch_release(scratch);
  return result;
}

root_function U8 *
F_get_baked_atlas()
{
  return f_state.baked_atlas;
}

root_function R_Handle 
F_atlas_texture_handle()
{
  return f_state.atlas.texture;
}

root_function F_Run 
F_run_from_string(Arena *arena, String8 string, Vec2_F32 pos)
{
  F_Run run = {0};
  U64 start_glyph = f_state.atlas.start_glyph;
  U64 end_glyph = f_state.atlas.last_glyph;
  U64 width = f_state.atlas.size.x;
  U64 height = f_state.atlas.size.y;
  
  float xpos = pos.x;
  float ypos = pos.y;
  int fill_rule = 1;
  for(U64 i = 0; i < string.size; i++)
  {
    char c = (char)string.str[i];
    if(c < start_glyph || c >= end_glyph) {
      continue;
    }
    stbtt_aligned_quad q;
    int glyph_index = c-start_glyph;
    
    stbtt_GetBakedQuad(f_state.cdata, width, height, glyph_index,
                       &xpos, &ypos, &q, fill_rule);
    Vec2_F32 src_p0 = vec2_F32(width*q.s0, height*q.t0);
    Vec2_F32 src_p1 = vec2_F32(width*q.s1, height*q.t1);
    F_Piece *piece = PushArrayZero(arena, F_Piece, 1);
    QueuePush(run.first_piece, run.last_piece, piece);
    
    piece->texture  = f_state.atlas.texture;
    piece->src_rect = rng2_F32(src_p0, src_p1);
    piece->advance  = xpos;
    piece->offset   = vec2_F32(0.0f, ypos);

    
    piece->dst_p0 = vec2_F32(q.x0, q.y0);
    piece->dst_p1 = vec2_F32(q.x1, q.y1);
    run.piece_count += 1;
    run.advance += piece->advance;
                       
                       
  }
  
  return run;
}
