working on more font stuff
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Sep 2020 20:39:48 +0000 (15:39 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 22 Sep 2020 20:39:48 +0000 (15:39 -0500)
src/font.onyx
src/gfx/texture.onyx
src/main.onyx
src/vecmath.onyx
tags

index f0fc3ac47e14432d63c4d552da1bc26647c57e9d..0ec7779c56d0e372e509b94c8c49aec884e16762 100644 (file)
@@ -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
index 74c601d4648bdeea05625a44fc252c9438b93dfc..ed014a36712a485eb8fe0efe46758643afbd1c7e 100644 (file)
@@ -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) {
index 1d82280afb757e6d71cadba1eac46f7bf1cd63c0..1d095525204f50141858349b2929961a6d0e0c63 100644 (file)
@@ -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);
index a52bd9a04227923baa9577ad2117d015ec628441..34c66c1548ca277142fae05fa7e694ae4bffeb1d 100644 (file)
@@ -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 3d80bac0115702e12ac2764e00a873fed1e1a81e..cc29dd4fe8e4d9ed9cafd8db98b8c67b9595d058 100644 (file)
--- 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;$/