Advertisement
Tkap1

Untitled

Oct 15th, 2023
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. func void draw_text(const char* text, s_v2 in_pos, int layer, float font_size, s_v4 color, b8 centered, s_font* font, s_render_data render_data = zero, s_transform t = zero)
  2. {
  3.     if(!render_data.framebuffer) {
  4.         render_data.framebuffer = &g_r->framebuffers[0];
  5.     }
  6.  
  7.     t.layer = layer;
  8.  
  9.     float scale = font->scale * (font_size / font->size);
  10.  
  11.     int len = (int)strlen(text);
  12.     assert(len > 0);
  13.     s_v2 pos = in_pos;
  14.     s_v2 text_size = get_text_size(text, font, font_size);
  15.     if(centered) {
  16.         pos.x -= text_size.x / 2;
  17.         pos.y -= text_size.y / 2;
  18.     }
  19.     s_v2 arr[2] = zero;
  20.     // pos.y += font->ascent * scale;
  21.     for(int char_i = 0; char_i < len; char_i++) {
  22.         int c = text[char_i];
  23.         if(c <= 0 || c >= 128) { continue; }
  24.  
  25.         s_glyph glyph = font->glyph_arr[c];
  26.         t.draw_size = v2((glyph.x1 - glyph.x0) * scale, (glyph.y1 - glyph.y0) * scale);
  27.  
  28.  
  29.         s_v2 glyph_pos = pos;
  30.         glyph_pos.x += glyph.x0 * scale;
  31.         glyph_pos.y += -glyph.y0 * scale;
  32.         s_v2 glyph_center = glyph_pos;
  33.         glyph_center += t.draw_size / 2 * v2(1, 1);
  34.         // glyph_pos = v2_rotate_around(glyph_center, in_pos, t.rotation) - t.draw_size / 2;
  35.  
  36.         glyph_pos.y += text_size.y;
  37.         t.flags |= e_render_flag_use_texture;
  38.         t.pos = glyph_pos;
  39.         float r = (float)g_r->total_time;
  40.         // t.pos = in_pos;
  41.         // t.pos = v2_rotate_around(t.pos, in_pos, rotation);
  42.  
  43.         s_v2 center = t.pos + t.draw_size / 2 * v2(1, -1);
  44.         s_v2 topleft = t.pos + t.draw_size * v2(0, -1);
  45.         s_v2 bottomleft = t.pos;
  46.  
  47.         draw_rect(t.pos, 1, t.draw_size, make_color(0.4f, 0,0), zero, {.origin_offset = c_origin_bottomleft});
  48.         draw_rect(center, 75, v2(4), make_color(0, 1,0), zero);
  49.         draw_rect(topleft, 75, v2(4), make_color(0, 0,1), zero, {.origin_offset = c_origin_topleft});
  50.         draw_rect(bottomleft, 75, v2(4), make_color(1, 1,0), zero, {.origin_offset = c_origin_bottomleft});
  51.         draw_rect(in_pos, 77, v2(4), make_color(0, 1,1), zero, {.origin_offset = c_origin_topleft});
  52.  
  53.         // float a = t.pos.x - in_pos.x;
  54.         t.pos = v2_rotate_around(topleft, in_pos, r);
  55.         t.pos.y += t.draw_size.y;
  56.         if(char_i <= 1) {arr[char_i] = t.pos;}
  57.  
  58.         // draw_rect(glyph_center, 76, v2(2), make_color(1, 1,0), zero);
  59.         // draw_rect(in_pos, 0, text_size, make_color(0, 0.3f,0), zero);
  60.         // t.draw_size = v2(glyph.width, glyph.height);
  61.         t.color = color;
  62.         // t.texture_size = texture.size;
  63.         t.uv_min = glyph.uv_min;
  64.         t.uv_max = glyph.uv_max;
  65.         t.origin_offset = c_origin_bottomleft;
  66.         t.rotation = r;
  67.  
  68.         bucket_add(
  69.             &render_data.framebuffer->transforms[get_render_offset(font->texture.game_id, render_data.blend_mode)], t,
  70.             &g_r->arenas[g_r->arena_index], &g_r->did_we_alloc
  71.         );
  72.  
  73.         pos.x += glyph.advance_width * scale;
  74.  
  75.     }
  76.  
  77.     printf("%f\n", v2_distance(arr[0], arr[1]));
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement