From 3aac7cdfbb4d444aa56f75af323e102189e33f88 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 22 Sep 2020 15:39:48 -0500 Subject: [PATCH] working on more font stuff --- src/font.onyx | 39 ++++++++++++++++++++------------------- src/gfx/texture.onyx | 8 ++------ src/main.onyx | 35 +++++++++++++++++------------------ src/vecmath.onyx | 5 +++-- tags | 3 +-- 5 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/font.onyx b/src/font.onyx index f0fc3ac..0ec7779 100644 --- a/src/font.onyx +++ b/src/font.onyx @@ -600,7 +600,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi // Black background for y: 0 .. height do for x: 0 .. width do data[x + y * width] = cast(u8) 0; - scale := cast(f32) height / ~~cast(i32) units_per_em; + // scale := ~~cast(i32) units_per_em / cast(f32) height; state := 0; count := 0; @@ -626,10 +626,6 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi } case 1 do if p.on_curve { - // draw_line(data, width, height, scale, - // curr_x, curr_y, - // ~~p.x, ~~p.y); - curr_x = cast(i32) p.x; curr_y = cast(i32) p.y; @@ -710,28 +706,33 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi count += 1; state = 0; - // for p: poly_points { - // print_vec(p); + for p: poly_points { + print_vec(p); - // tx := cast(i32) (~~p.x * scale); - // ty := cast(i32) (~~p.y * scale); + tx := cast(i32) (~~p.x * height / ~~units_per_em); + ty := cast(i32) (~~p.y * height / ~~units_per_em); - // if tx >= 0 && ty >= 0 && tx < width && ty < height { - // data[tx + (height - 1 - ty) * width] = cast(u8) 255; - // } - // } - - 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]; - if curr == ~~0 do data[x + (height - 1 - y) * width] = cast(u8) 255; - else do data[x + (height - 1 - y) * width] = cast(u8) 0; + if tx >= 0 && ty >= 0 && tx < width && ty < height { + data[tx + (height - 1 - ty) * width] += cast(u8) 100; } } + // for y: 0 .. height do for x: 0 .. width { + // sx := x * ~~units_per_em / width; + // sy := y * ~~units_per_em / height; + + // if is_inside_polygon(array_to_slice(^poly_points), V2i.{ sx, sy }) { + // curr := data[x + (height - 1 - y) * width]; + // if curr == ~~0 do data[x + (height - 1 - y) * width] = cast(u8) 255; + // else do data[x + (height - 1 - y) * width] = cast(u8) 0; + // } + // } + array_clear(^poly_points); } } + + println(cast(i32) glyph.contour_count); } #private_file diff --git a/src/gfx/texture.onyx b/src/gfx/texture.onyx index 74c601d..ed014a3 100644 --- a/src/gfx/texture.onyx +++ b/src/gfx/texture.onyx @@ -4,10 +4,6 @@ use package core use package gl as gl use package gl_utils as gl_utils -Color3 :: struct { r: u8; g: u8; b: u8; } - -#private_file current_tex_id := 0; - Texture :: struct { // NOTE: After a texture is loaded, we may not want to keep the // raw texture data in memory any longer. This would not be possible @@ -45,12 +41,12 @@ texture_prepare :: proc (use tex: ^Texture) { if data_mode == TextureDataMode.BRIGHTNESS do format = gl.LUMINANCE; texture_id = gl.createTexture(); - // gl.activeTexture(gl.TEXTURE0); + gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, texture_id); gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, gl.UNSIGNED_BYTE, data); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); - // gl.bindTexture(gl.TEXTURE_2D, -1); + gl.bindTexture(gl.TEXTURE_2D, -1); } texture_use :: proc (use tex: ^Texture) { diff --git a/src/main.onyx b/src/main.onyx index 1d82280..1d09552 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -126,7 +126,7 @@ update :: proc () { if input.key_down(^input_state, Key.ArrowRight) do player.x += player_speed; } -glyph_size :: 128 +glyph_size :: 64 glyph_data : [glyph_size * glyph_size] u8 draw :: proc () { @@ -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 }; + // 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 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 { @@ -166,15 +166,14 @@ draw :: proc () { // draw_rect(^renderer, ~~ cast(i32) p.x * rx + 100f, ~~ cast(i32) p.y * ry + 100f, 10f, 10f); // } + texture_use(^font_tex); + draw_rect(^renderer, 100f, 100f, 256f, 256f); + render_context_flush(^renderer); texture_use(^smile); - draw_rect(^renderer, ~~input_state.mouse.x, ~~input_state.mouse.y, 100f, 100f); + draw_rect(^renderer, ~~input_state.mouse.x, ~~input_state.mouse.y, 10f, 10f); draw_rect(^renderer, player.pos.x, player.pos.y, 100f, 100f); render_context_flush(^renderer); - - texture_use(^font_tex); - draw_rect(^renderer, 100f, 100f, 512f, 512f); - render_context_flush(^renderer); } // This procedure is called asynchronously from JS every frame. @@ -210,7 +209,7 @@ main :: proc (args: [] cstring) { ttf = ttf_create(ttf_data); glyph : ^TTGlyph = null; - glyph_index := ttf_lookup_glyph_by_char(^ttf, ~~ #char "S"); + glyph_index := ttf_lookup_glyph_by_char(^ttf, ~~ #char "~"); 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 a52bd9a..34c66c1 100644 --- a/src/vecmath.onyx +++ b/src/vecmath.onyx @@ -83,13 +83,14 @@ 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) 10000; // Needs be infinity... close enough + extreme.x = p.x + ~~1000000; // 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 v2_square_dist(polygon[i], polygon[next]) < ~~(20 * 20) do continue; + if v2_equal(polygon[i], polygon[next]) 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 3d80bac..cc29dd4 100644 --- a/tags +++ b/tags @@ -83,7 +83,6 @@ CURRENT_PROGRAM /usr/share/onyx/core/js/webgl.onyx /^CURRENT_PROGRAM CURRENT_QUERY /usr/share/onyx/core/js/webgl.onyx /^CURRENT_QUERY :: 0x8865$/ CURRENT_VERTEX_ATTRIB /usr/share/onyx/core/js/webgl.onyx /^CURRENT_VERTEX_ATTRIB :: 0x8626$/ CW /usr/share/onyx/core/js/webgl.onyx /^CW :: 0x0900$/ -Color3 src/gfx/texture.onyx /^Color3 :: struct { r: u8; g: u8; b: u8; }$/ Color4f32 src/gfx/quad_renderer.onyx /^Color4f32 :: struct { r: f32; g: f32; b: f32; a: f32; }$/ DECR /usr/share/onyx/core/js/webgl.onyx /^DECR :: 0x1E03$/ DECR_WRAP /usr/share/onyx/core/js/webgl.onyx /^DECR_WRAP :: 0x8508$/ @@ -765,7 +764,7 @@ getShaderParameter /usr/share/onyx/core/js/webgl.onyx /^getShaderParameter getUniformLocation /usr/share/onyx/core/js/webgl.onyx /^getUniformLocation :: proc (program: GLProgram, name: string) -> GLUniformLocation #foreign "gl" "getUniformLocation" ---$/ getVertexAttribOffset /usr/share/onyx/core/js/webgl.onyx /^getVertexAttribOffset :: proc (index: GLuint, pname: GLenum) #foreign "gl" "getVertexAttribOffset" ---$/ glyph_data src/main.onyx /^glyph_data : [glyph_size * glyph_size] u8$/ -glyph_size src/main.onyx /^glyph_size :: 128$/ +glyph_size src/main.onyx /^glyph_size :: 64$/ heap_alloc /usr/share/onyx/core/alloc.onyx /^heap_alloc :: proc (size_: u32, align: u32) -> rawptr {$/ heap_alloc_proc /usr/share/onyx/core/alloc.onyx /^heap_alloc_proc :: proc (data: rawptr, aa: AllocAction, size: u32, align: u32, oldptr: rawptr) -> rawptr {$/ heap_allocator /usr/share/onyx/core/alloc.onyx /^heap_allocator : Allocator;$/ -- 2.25.1