From: Brendan Hansen Date: Tue, 22 Sep 2020 12:32:20 +0000 (-0500) Subject: working on font code X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=404f646991d136b65c058ab1cabfc24dd6b7209a;p=onyx-game.git working on font code --- diff --git a/src/font.onyx b/src/font.onyx index 855cae7..f0fc3ac 100644 --- a/src/font.onyx +++ b/src/font.onyx @@ -616,11 +616,10 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi for i: 0 .. glyph.points.count { p := glyph.points[i]; - switch state { case 0 { - // curr_x = cast(i32) p.x; - // curr_y = cast(i32) p.y; + curr_x = cast(i32) p.x; + curr_y = cast(i32) p.y; array_push(^poly_points, V2i.{ ~~p.x, ~~p.y }); state = 1; @@ -631,8 +630,9 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi // curr_x, curr_y, // ~~p.x, ~~p.y); - // curr_x = cast(i32) p.x; - // curr_y = cast(i32) p.y; + curr_x = cast(i32) p.x; + curr_y = cast(i32) p.y; + array_push(^poly_points, V2i.{ ~~p.x, ~~p.y }); } else { @@ -642,27 +642,37 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi case #default { prev := glyph.points[i - 1]; if p.on_curve { - // draw_quadratic_line(data, width, height, scale, - // curr_x, curr_y, - // ~~prev.x, ~~prev.y, - // ~~p.x, ~~p.y); - - // curr_x = cast(i32) p.x; - // curr_y = cast(i32) p.y; - // array_push(^poly_points, V2i.{ ~~prev.x, ~~prev.y }); - array_push(^poly_points, V2i.{ ~~p.x, ~~p.y }); + + bp : [3] V2f; + bp[0] = V2f.{ ~~curr_x, ~~curr_y }; + bp[1] = V2f.{ ~~cast(i32) prev.x, ~~cast(i32) prev.y }; + bp[2] = V2f.{ ~~cast(i32) p.x, ~~cast(i32) p.y }; + bps := bp[0 .. 3]; + + for t: 1 .. 5 { + s := bezier_curve(~~t / 4.0f, bps); + array_push(^poly_points, V2i.{ ~~s.x, ~~s.y }); + } + + curr_x = cast(i32) p.x; + curr_y = cast(i32) p.y; + state = 1; } else { - //draw_quadratic_line(data, width, height, scale, - // curr_x, curr_y, - // ~~prev.x, ~~prev.y, - // ~~((prev.x + p.x) / ~~2), ~~((prev.y + p.y) / ~~2)); - - //curr_x = cast(i32) ((prev.x + p.x) / ~~2); - //curr_y = cast(i32) ((prev.y + p.y) / ~~2); - array_push(^poly_points, V2i.{ ~~prev.x, ~~prev.y }); - array_push(^poly_points, V2i.{ ~~((prev.x + p.x) / ~~2), ~~((prev.y + p.y) / ~~2) }); + bp : [3] V2f; + bp[0] = V2f.{ ~~curr_x, ~~curr_y }; + bp[1] = V2f.{ ~~cast(i32) prev.x, ~~cast(i32) prev.y }; + bp[2] = V2f.{ ~~(~~(prev.x + p.x) / 2), ~~(~~(prev.y + p.y) / 2) }; + bps := bp[0 .. 3]; + + for t: 1 .. 5 { + s := bezier_curve(~~t / 4.0f, bps); + array_push(^poly_points, V2i.{ ~~s.x, ~~s.y }); + } + + curr_x = cast(i32) bp[2].x; + curr_y = cast(i32) bp[2].y; } } } @@ -672,36 +682,45 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi p = glyph.points[contour_start]; if p.on_curve { - // draw_quadratic_line(data, width, height, scale, - // curr_x, curr_y, - // ~~prev.x, ~~prev.y, - // ~~p.x, ~~p.y); - - // curr_x = cast(i32) p.x; - // curr_y = cast(i32) p.y; - array_push(^poly_points, V2i.{ ~~prev.x, ~~prev.y }); + bp : [3] V2f; + bp[0] = V2f.{ ~~curr_x, ~~curr_y }; + bp[1] = V2f.{ ~~cast(i32) prev.x, ~~cast(i32) prev.y }; + bp[2] = V2f.{ ~~cast(i32) p.x, ~~cast(i32) p.y }; + bps := bp[0 .. 3]; + + for t: 1 .. 5 { + s := bezier_curve(~~t / 4.0f, bps); + array_push(^poly_points, V2i.{ ~~s.x, ~~s.y }); + } } else { - // draw_quadratic_line(data, width, height, scale, - // curr_x, curr_y, - // ~~prev.x, ~~prev.y, - // ~~((prev.x + p.x) / ~~2), ~~((prev.y + p.y) / ~~2)); - - // curr_x = cast(i32) ((prev.x + p.x) / ~~2); - // curr_y = cast(i32) ((prev.y + p.y) / ~~2); - array_push(^poly_points, V2i.{ ~~prev.x, ~~prev.y }); - array_push(^poly_points, V2i.{ ~~((prev.x + p.x) / ~~2), ~~((prev.y + p.y) / ~~2) }); + bp : [3] V2f; + bp[0] = V2f.{ ~~curr_x, ~~curr_y }; + bp[1] = V2f.{ ~~cast(i32) prev.x, ~~cast(i32) prev.y }; + bp[2] = V2f.{ ~~(~~(prev.x + p.x) / 2), ~~(~~(prev.y + p.y) / 2) }; + bps := bp[0 .. 3]; + + for t: 1 .. 5 { + s := bezier_curve(~~t / 4.0f, bps); + array_push(^poly_points, V2i.{ ~~s.x, ~~s.y }); + } } contour_start = i + 1; count += 1; state = 0; + // for p: poly_points { + // print_vec(p); + + // tx := cast(i32) (~~p.x * scale); + // ty := cast(i32) (~~p.y * scale); - // col := cast(u8) 255; - // if v2_orientation(poly_points[0], poly_points[1], poly_points[2]) < 0 do col = cast(u8) 0; + // if tx >= 0 && ty >= 0 && tx < width && ty < height { + // data[tx + (height - 1 - ty) * width] = cast(u8) 255; + // } + // } - for p: poly_points do print_vec(p); for y: 0 .. height do for x: 0 .. width { if is_inside_polygon(array_to_slice(^poly_points), V2i.{ ~~(~~x / scale), ~~(~~y / scale) }) { curr := data[x + (height - 1 - y) * width]; diff --git a/src/main.onyx b/src/main.onyx index 72c3ceb..1d82280 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -137,18 +137,18 @@ draw :: proc () { // renderer.color = Color4f32.{ 1f, 0f, 0f, 1f }; - // points : [6] V2f; - // points[0] = V2f.{ 500.0f, 700.0f }; - // points[1] = V2f.{ 600.0f, 200.0f }; - // points[2] = V2f.{ 700.0f, 400.0f }; - // points[3] = V2f.{ 1700.0f, 800.0f }; - // points[4] = V2f.{ 1800.0f, 400.0f }; - // points[5] = V2f.{ 1000.0f, 200.0f }; - - // for i: 0 .. 200 { - // pos := bezier_curve(~~i / 200.0f, points[0 .. 6]); - // draw_rect(^renderer, pos.x, pos.y, 16f, 16f); - // } + points : [6] V2f; + points[0] = V2f.{ 500.0f, 700.0f }; + points[1] = V2f.{ 600.0f, 200.0f }; + points[2] = V2f.{ 700.0f, 400.0f }; + points[3] = V2f.{ 1700.0f, 800.0f }; + points[4] = V2f.{ 1800.0f, 400.0f }; + points[5] = V2f.{ 1000.0f, 200.0f }; + + for i: 0 .. 200 { + pos := bezier_curve(~~i / 200.0f, points[0 .. 6]); + draw_rect(^renderer, pos.x, pos.y, 16f, 16f); + } // for y: 0 .. glyph_size do for x: 0 .. glyph_size { // if glyph_data[x + y * glyph_size] != ~~0 { @@ -173,7 +173,7 @@ draw :: proc () { render_context_flush(^renderer); texture_use(^font_tex); - draw_rect(^renderer, 100f, 100f, 64f, 64f); + draw_rect(^renderer, 100f, 100f, 512f, 512f); render_context_flush(^renderer); } @@ -210,7 +210,7 @@ main :: proc (args: [] cstring) { ttf = ttf_create(ttf_data); glyph : ^TTGlyph = null; - glyph_index := ttf_lookup_glyph_by_char(^ttf, ~~ #char "%"); + glyph_index := ttf_lookup_glyph_by_char(^ttf, ~~ #char "S"); glyph = ttf_read_glyph(^ttf, glyph_index); if glyph == null do return; defer ttf_glyph_destroy(glyph); diff --git a/src/vecmath.onyx b/src/vecmath.onyx index 034a48e..a52bd9a 100644 --- a/src/vecmath.onyx +++ b/src/vecmath.onyx @@ -40,6 +40,10 @@ v2_square_dist :: proc (a: V2($T), b: V2(T)) -> T { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } +v2_equal :: proc (a: V2($T), b: V2(T)) -> bool { + return a.x == b.x && a.y == b.y; +} + // -1 for counter-clockwise // 0 for colinear // 1 for clockwise @@ -79,12 +83,13 @@ is_inside_polygon :: proc (polygon: [] V2($T), p: V2(T)) -> bool { if polygon.count < 3 do return false; extreme : V2(T); - extreme.x = cast(T) 1000000; // Needs be infinity... close enough + extreme.x = cast(T) 10000; // Needs be infinity... close enough extreme.y = p.y; count := 0; for i: 0 .. polygon.count { next := (i + 1) % polygon.count; + // if v2_square_dist(polygon[i], polygon[next]) < ~~1000 do continue; if lines_intersect(polygon[i], polygon[next], p, extreme) { if v2_orientation(polygon[i], p, polygon[next]) == 0 { diff --git a/tags b/tags index 96bdeda..3d80bac 100644 --- a/tags +++ b/tags @@ -657,7 +657,7 @@ array_fold /usr/share/onyx/core/array.onyx /^array_fold :: proc (arr: ^[..] $T, array_free /usr/share/onyx/core/array.onyx /^array_free :: proc (arr: ^[..] $T) {$/ array_init /usr/share/onyx/core/array.onyx /^array_init :: proc (arr: ^[..] $T, initial_cap := 4) {$/ array_insert /usr/share/onyx/core/array.onyx /^array_insert :: proc (arr: ^[..] $T, idx: u32, x: T) {$/ -array_map /usr/share/onyx/core/array.onyx /^array_map :: proc (arr: ^[..] $T, f: proc (T) -> T) {$/ +array_map /usr/share/onyx/core/array.onyx /^array_map :: proc (arr: ^[..] $T, data: $R, f: proc (T, R) -> T) {$/ array_pop /usr/share/onyx/core/array.onyx /^array_pop :: proc (arr: ^[..] $T) -> T {$/ array_push /usr/share/onyx/core/array.onyx /^array_push :: proc (arr: ^[..] $T, x: T) {$/ array_remove /usr/share/onyx/core/array.onyx /^array_remove :: proc (arr: ^[..] $T, elem: T) {$/ @@ -956,6 +956,7 @@ uniformMatrix4 /usr/share/onyx/core/js/webgl.onyx /^uniformMatrix4 update src/main.onyx /^update :: proc () {$/ useProgram /usr/share/onyx/core/js/webgl.onyx /^useProgram :: proc (program: GLProgram) #foreign "gl" "useProgram" ---$/ v2_add src/vecmath.onyx /^v2_add :: proc (a: V2($T), b: V2(T)) -> V2(T) {$/ +v2_equal src/vecmath.onyx /^v2_equal :: proc (a: V2($T), b: V2(T)) -> bool {$/ v2_lerp src/vecmath.onyx /^v2_lerp :: proc (t: f32, start: V2($T), end: V2(T)) -> V2(T) {$/ v2_mul src/vecmath.onyx /^v2_mul :: proc (a: V2($T), scalar: T) -> V2(T) {$/ v2_on_segment src/vecmath.onyx /^v2_on_segment :: proc (a: V2($T), b: V2(T), c: V2(T)) -> bool {$/