updated to latest version of Onyx
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 7 Dec 2020 00:42:35 +0000 (18:42 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 7 Dec 2020 00:42:35 +0000 (18:42 -0600)
src/font.onyx
src/gfx/atlas.onyx
src/gfx/font.onyx
src/gfx/quad_renderer.onyx
src/gfx/texture.onyx
src/globals.onyx
src/main.onyx
src/quad_tree.onyx
src/utils/gl.onyx
src/vecmath.onyx

index 72b96b597115fcd4264e339c12fa08f59721ad51..1ebb87248cca5686b9f8ed667fc731876ba8074b 100644 (file)
@@ -98,7 +98,7 @@ TrueTypeFont :: struct {
     entry_selector : u16;
     range_shift    : u16;
 
-    table_map : I32Map(TTTableInfo);
+    table_map : i32map.I32Map(TTTableInfo);
     char_maps : [..] TTCmap;
 
     version : u32;
@@ -167,8 +167,8 @@ TTGlyphPoint :: struct {
 ttf_create :: proc (ttf_data: [] u8) -> TrueTypeFont {
     ttf : TrueTypeFont;
     ttf.reader = binary_reader_create(ttf_data);
-    i32map_init(^ttf.table_map);
-    array_init(^ttf.char_maps);
+    i32map.init(^ttf.table_map);
+    array.init(^ttf.char_maps);
     ttf_read_offset_table(^ttf);
     ttf_read_head_table(^ttf);
     ttf_read_cmap_table(^ttf);
@@ -194,9 +194,9 @@ ttf_read_offset_table :: proc (use ttf: ^TrueTypeFont) {
         table_info.offset   = read_u32(^reader);
         table_info.length   = read_u32(^reader);
 
-        i32map_put(^table_map, tag_int, table_info);
+        i32map.put(^table_map, tag_int, table_info);
 
-        if !string_equal(tag, "head") {
+        if !str.equal(tag, "head") {
             csum := ttf_calc_table_checksum(^reader, table_info.offset, table_info.length);
             if table_info.checksum != csum {
                 print("WARNING: Checksum for table '");
@@ -209,7 +209,7 @@ ttf_read_offset_table :: proc (use ttf: ^TrueTypeFont) {
 
 ttf_read_head_table :: proc (use ttf: ^TrueTypeFont) {
     empty_table_hack := TTTableInfo.{};
-    head_table_info  := i32map_get(^table_map, string_to_beu32("head"), empty_table_hack);
+    head_table_info  := i32map.get(^table_map, string_to_beu32("head"), empty_table_hack);
     seek(^reader, head_table_info.offset);
 
     version = read_u32(^reader);
@@ -233,8 +233,8 @@ ttf_read_head_table :: proc (use ttf: ^TrueTypeFont) {
 
 ttf_lookup_glyph_offset :: proc (use ttf: ^TrueTypeFont, glyph_index: i32) -> u32 {
     empty_table_hack := TTTableInfo.{};
-    loca_table_info  := i32map_get(^table_map, string_to_beu32("loca"), empty_table_hack);
-    glyf_table_info  := i32map_get(^table_map, string_to_beu32("glyf"), empty_table_hack);
+    loca_table_info  := i32map.get(^table_map, string_to_beu32("loca"), empty_table_hack);
+    glyf_table_info  := i32map.get(^table_map, string_to_beu32("glyf"), empty_table_hack);
 
     old: u32;
     defer seek(^reader, old);
@@ -259,7 +259,7 @@ ttf_read_glyph :: proc (use ttf: ^TrueTypeFont, glyph_index: i32) -> ^TTGlyph {
     offset := ttf_lookup_glyph_offset(ttf, glyph_index);
 
     empty_table_hack := TTTableInfo.{};
-    glyf_table_info  := i32map_get(^table_map, string_to_beu32("glyf"), empty_table_hack);
+    glyf_table_info  := i32map.get(^table_map, string_to_beu32("glyf"), empty_table_hack);
 
     if offset >= glyf_table_info.offset + glyf_table_info.length do return null;
 
@@ -285,8 +285,8 @@ ttf_read_glyph :: proc (use ttf: ^TrueTypeFont, glyph_index: i32) -> ^TTGlyph {
 }
 
 ttf_glyph_destroy :: proc (glyph: ^TTGlyph) {
-    array_free(^glyph.contour_ends);
-    array_free(^glyph.points);
+    array.free(^glyph.contour_ends);
+    array.free(^glyph.points);
     cfree(glyph);
 }
 
@@ -302,34 +302,34 @@ TTGlyphFlags :: enum #flags {
 
 #private_file
 ttf_read_simple_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph) {
-    array_init(^glyph.contour_ends, ~~glyph.contour_count);
-    array_init(^glyph.points);
+    array.init(^glyph.contour_ends, ~~glyph.contour_count);
+    array.init(^glyph.points);
 
     for i: 0 .. ~~glyph.contour_count {
-        array_push(^glyph.contour_ends, read_u16(^reader));
+        array.push(^glyph.contour_ends, read_u16(^reader));
     }
 
     seek(^reader, ~~read_u16(^reader) + tell(^reader));
 
     if glyph.contour_count == ~~0 do return;
 
-    num_points := array_fold(^glyph.contour_ends, cast(u16) 0, max_poly) + ~~1;
+    num_points := array.fold(^glyph.contour_ends, cast(u16) 0, math.max) + ~~1;
 
     flags : [..] TTGlyphFlags;
-    array_init(^flags);
-    defer array_free(^flags);
+    array.init(^flags);
+    defer array.free(^flags);
 
     for i: 0 .. ~~num_points {
         flag := cast(TTGlyphFlags) read_u8(^reader);
-        array_push(^flags, flag);
-        array_push(^glyph.points, TTGlyphPoint.{ on_curve = (flag & TTGlyphFlags.On_Curve) != ~~0 });
+        array.push(^flags, flag);
+        array.push(^glyph.points, TTGlyphPoint.{ on_curve = (flag & TTGlyphFlags.On_Curve) != ~~0 });
 
         if (flag & TTGlyphFlags.Repeat) != ~~0 {
             rep_count := read_u8(^reader);
             i += ~~rep_count;
             for i: 0 .. ~~rep_count {
-                array_push(^flags, flag);
-                array_push(^glyph.points, TTGlyphPoint.{ on_curve = (flag & TTGlyphFlags.On_Curve) != ~~0 });
+                array.push(^flags, flag);
+                array.push(^glyph.points, TTGlyphPoint.{ on_curve = (flag & TTGlyphFlags.On_Curve) != ~~0 });
             }
         }
     }
@@ -372,7 +372,7 @@ ttf_read_simple_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph) {
 
 ttf_glyph_count :: proc (use ttf: ^TrueTypeFont) -> u32 {
     empty_table_hack := TTTableInfo.{};
-    maxp_table_info  := i32map_get(^table_map, string_to_beu32("maxp"), empty_table_hack);
+    maxp_table_info  := i32map.get(^table_map, string_to_beu32("maxp"), empty_table_hack);
     old := seek(^reader, maxp_table_info.offset + 4);
     defer seek(^reader, old);
 
@@ -381,7 +381,7 @@ ttf_glyph_count :: proc (use ttf: ^TrueTypeFont) -> u32 {
 
 ttf_read_cmap_table :: proc (use ttf: ^TrueTypeFont) {
     empty_table_hack := TTTableInfo.{};
-    cmap_table_info  := i32map_get(^table_map, string_to_beu32("cmap"), empty_table_hack);
+    cmap_table_info  := i32map.get(^table_map, string_to_beu32("cmap"), empty_table_hack);
     seek(^reader, cmap_table_info.offset);
 
     version := read_u16(^reader);
@@ -419,7 +419,7 @@ TTCmap4 :: struct {
     range_shift    : u16;
 
     segments : [..] TTSegment;
-    cache    : I32Map(i32);
+    cache    : i32map.I32Map(i32);
 }
 
 TTSegment :: struct {
@@ -456,26 +456,26 @@ ttf_read_cmap0 :: proc (use ttf: ^TrueTypeFont) {
     cmap.cmap0.format = TTCmapFormat.Simple;
 
     glyphs : [..] u8;
-    array_init(^glyphs, 256);
-    for i: 0 .. 256 do array_push(^glyphs, read_u8(^reader));
+    array.init(^glyphs, 256);
+    for i: 0 .. 256 do array.push(^glyphs, read_u8(^reader));
 
-    cmap.cmap0.glyph_indicies = array_to_slice(^glyphs);
+    cmap.cmap0.glyph_indicies = array.to_slice(^glyphs);
 
-    array_push(^char_maps, cmap);
+    array.push(^char_maps, cmap);
 }
 
 ttf_read_cmap4 :: proc (use ttf: ^TrueTypeFont) {
     cmap : TTCmap;
     cmap.cmap4.format = TTCmapFormat.Segmented;
     map := ^cmap.cmap4;
-    i32map_init(^map.cache);
+    i32map.init(^map.cache);
 
     map.seg_count = read_u16(^reader) >> ~~1;
     map.search_range = read_u16(^reader);
     map.entry_selector = read_u16(^reader);
     map.range_shift = read_u16(^reader);
 
-    array_init(^map.segments, ~~map.seg_count);
+    array.init(^map.segments, ~~map.seg_count);
     map.segments.count = cast(u32) map.seg_count;
 
     for ^seg: map.segments do seg.end_code        = read_u16(^reader);
@@ -487,7 +487,7 @@ ttf_read_cmap4 :: proc (use ttf: ^TrueTypeFont) {
         if seg.id_range_offset != ~~0 do seg.id_range_offset += ~~(tell(^reader) - 2);
     }
 
-    array_push(^char_maps, cmap);
+    array.push(^char_maps, cmap);
 }
 
 ttf_lookup_glyph_by_char :: proc (use ttf: ^TrueTypeFont, charcode: u32) -> u32 {
@@ -513,7 +513,7 @@ ttf_lookup_in_cmap0 :: proc (use ttf: ^TrueTypeFont, cmap: ^TTCmap0, charcode: u
 
 #private_file
 ttf_lookup_in_cmap4 :: proc (use ttf: ^TrueTypeFont, cmap: ^TTCmap4, charcode: u32) -> u32 {
-    if i32map_has(^cmap.cache, charcode) do return i32map_get(^cmap.cache, charcode);
+    if i32map.has(^cmap.cache, charcode) do return i32map.get(^cmap.cache, charcode);
 
     index := 0;
     for ^seg: cmap.segments {
@@ -530,14 +530,14 @@ ttf_lookup_in_cmap4 :: proc (use ttf: ^TrueTypeFont, cmap: ^TTCmap4, charcode: u
         }
     }
 
-    i32map_put(^cmap.cache, charcode, index);
+    i32map.put(^cmap.cache, charcode, index);
 
     return index;
 }
 
 ttf_read_hhea_table :: proc (use ttf: ^TrueTypeFont) {
     empty_table_hack := TTTableInfo.{};
-    hhea_table_info  := i32map_get(^table_map, string_to_beu32("hhea"), empty_table_hack);
+    hhea_table_info  := i32map.get(^table_map, string_to_beu32("hhea"), empty_table_hack);
     seek(^reader, hhea_table_info.offset);
 
     hhea.version = read_u32(^reader);
@@ -566,7 +566,7 @@ TTHorizontalMetrics :: struct {
 
 ttf_lookup_horizontal_metrics :: proc (use ttf: ^TrueTypeFont, glyph_index: u32) -> TTHorizontalMetrics {
     empty_table_hack := TTTableInfo.{};
-    hmtx_table_info  := i32map_get(^table_map, string_to_beu32("hmtx"), empty_table_hack);
+    hmtx_table_info  := i32map.get(^table_map, string_to_beu32("hmtx"), empty_table_hack);
     offset := hmtx_table_info.offset;
 
     hmtx : TTHorizontalMetrics;
@@ -610,8 +610,8 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
     curr_y := 0;
 
     poly_points : [..] V2i;
-    array_init(^poly_points, 16);
-    defer array_free(^poly_points);
+    array.init(^poly_points, 16);
+    defer array.free(^poly_points);
 
     for i: 0 .. glyph.points.count {
         p := glyph.points[i];
@@ -621,7 +621,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
                 curr_x = cast(i32) p.x;
                 curr_y = cast(i32) p.y;
 
-                array_push(^poly_points, V2i.{ ~~p.x, ~~p.y });
+                array.push(^poly_points, V2i.{ ~~p.x, ~~p.y });
                 state = 1;
             }
 
@@ -629,7 +629,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
                 curr_x = cast(i32) p.x;
                 curr_y = cast(i32) p.y;
 
-                array_push(^poly_points, V2i.{ ~~p.x, ~~p.y });
+                array.push(^poly_points, V2i.{ ~~p.x, ~~p.y });
 
             } else {
                 state = 2;
@@ -647,7 +647,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
 
                     for t: 1 .. 5 {
                         s := bezier_curve(~~t / 4.0f, bps);
-                        array_push(^poly_points, V2i.{ ~~s.x, ~~s.y });
+                        array.push(^poly_points, V2i.{ ~~s.x, ~~s.y });
                     }
 
                     curr_x = cast(i32) p.x;
@@ -664,7 +664,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
 
                     for t: 1 .. 5 {
                         s := bezier_curve(~~t / 4.0f, bps);
-                        array_push(^poly_points, V2i.{ ~~s.x, ~~s.y });
+                        array.push(^poly_points, V2i.{ ~~s.x, ~~s.y });
                     }
 
                     curr_x = cast(i32) bp[2].x;
@@ -686,7 +686,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
 
                 for t: 1 .. 5 {
                     s := bezier_curve(~~t / 4.0f, bps);
-                    array_push(^poly_points, V2i.{ ~~s.x, ~~s.y });
+                    array.push(^poly_points, V2i.{ ~~s.x, ~~s.y });
                 }
 
             } else {
@@ -698,7 +698,7 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
 
                 for t: 1 .. 5 {
                     s := bezier_curve(~~t / 4.0f, bps);
-                    array_push(^poly_points, V2i.{ ~~s.x, ~~s.y });
+                    array.push(^poly_points, V2i.{ ~~s.x, ~~s.y });
                 }
             }
 
@@ -721,14 +721,14 @@ ttf_render_glyph :: proc (use ttf: ^TrueTypeFont, glyph: ^TTGlyph, data: ^u8, wi
                 sx := x * ~~units_per_em / width;
                 sy := y * ~~units_per_em / height;
 
-                if is_inside_polygon(array_to_slice(^poly_points), V2i.{ sx, sy }) {
+                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);
+            array.clear(^poly_points);
         }
     }
 
index 03f227cbd2dd3b5231e1fdc657b64c051b5b53b0..d210fbf42e00325f2a998dc8612a23f63093bd2c 100644 (file)
@@ -1,12 +1,14 @@
-package gfx
+package gfx.atlas
 
 use package core
 use package gl as gl
 use package gl_utils as gl_utils
+use package gfx.texture as texture
+use package gfx.quad_renderer { Quad }
 
 Atlas :: struct {
-    texture : ^Texture;
-    map     : I32Map(AtlasSprite);
+    texture : ^texture.Texture;
+    map     : i32map.I32Map(AtlasSprite);
 }
 
 AtlasSprite :: struct {
@@ -16,20 +18,20 @@ AtlasSprite :: struct {
     h : f32 = 0.0f;
 }
 
-atlas_create :: proc (tex: ^Texture) -> Atlas {
+create :: proc (tex: ^texture.Texture) -> Atlas {
     atlas : Atlas;
     atlas.texture = tex;
-    i32map_init(^atlas.map);
+    i32map.init(^atlas.map);
 
     return atlas;
 }
 
-atlas_map :: proc (use atlas: ^Atlas, id: i32, sprite: AtlasSprite) {
-    i32map_put(^map, id, sprite);
+map :: proc (use atlas: ^Atlas, id: i32, sprite: AtlasSprite) {
+    i32map.put(^map, id, sprite);
 }
 
-atlas_lookup :: proc (use atlas: ^Atlas, id: i32, offset := 0) -> AtlasSprite {
-    sprite := i32map_get(^map, id, AtlasSprite.{});
+lookup :: proc (use atlas: ^Atlas, id: i32, offset := 0) -> AtlasSprite {
+    sprite := i32map.get(^map, id, AtlasSprite.{});
 
     if offset != 0 {
         sprite.x += ~~offset * sprite.w;
@@ -47,8 +49,8 @@ atlas_lookup :: proc (use atlas: ^Atlas, id: i32, offset := 0) -> AtlasSprite {
     return sprite;
 }
 
-atlas_apply_to_quad :: proc (use atlas: ^Atlas, id: i32, quad: ^Quad) {
-    sprite := i32map_get(^map, id, AtlasSprite.{});
+apply_to_quad :: proc (use atlas: ^Atlas, id: i32, quad: ^Quad) {
+    sprite := i32map.get(^map, id, AtlasSprite.{});
     quad.tex_size.x = sprite.w / ~~texture.width;
     quad.tex_size.y = sprite.h / ~~texture.height;
     quad.tex_pos.x = sprite.x / ~~texture.width;
index 84957c813a94ea40730d2d1a9f15a513fc9dd237..704508a419e2eea82b0ceaf0db8160df1221126a 100644 (file)
@@ -1,17 +1,18 @@
-package gfx
+package gfx.font
 
 use package core
 use package globals
 use package vecmath
 use package main { RenderContext, draw_textured_rect }
+use package gfx.atlas as atlas
 
 #private_file
-font_chars := "0123456789!\"#$% ABCDEFGHIJKLMNOPQRSTUVWXYZ'()*+,abcdefghijklmnopqrstuvwxyz,-./\\,^:;<=>?[]~|_";
+chars := "0123456789!\"#$% ABCDEFGHIJKLMNOPQRSTUVWXYZ'()*+,abcdefghijklmnopqrstuvwxyz,-./\\,^:;<=>?[]~|_";
 
 #private_file
 char_to_tex_offset :: proc (ch: u8) -> u32 {
        loc := 0;
-       for c: font_chars {
+       for c: chars {
                if c == ch do break;
                loc += 1;
        }
@@ -25,9 +26,9 @@ draw_text :: proc (renderer: ^RenderContext, s: string, pos: V2f, scale := 1.0f)
        font_size := scale * 16.0f;
 
        for ch: s {
-               char_sprite := atlas_lookup(^atlas, 2, char_to_tex_offset(ch));
+        char_sprite := atlas.lookup(^main_atlas, 2, char_to_tex_offset(ch));
                draw_textured_rect(renderer, x, y, font_size, font_size, char_sprite.x, char_sprite.y, char_sprite.w, char_sprite.h);
 
                x += font_size * 0.85f;
        }
-}
\ No newline at end of file
+}
index 258d83d1cd8291cd4577bc21d48f1df8defe2711..179e76a195e7119bc91806dc406deeb1ef515fd9 100644 (file)
@@ -1,4 +1,4 @@
-package gfx
+package gfx.quad_renderer
 
 use package core
 use package gl as gl
@@ -35,7 +35,7 @@ Quad :: struct {
     color : Color4f32 = Color4f32.{ 0f, 0f, 0f, 0f };
 }
 
-quad_renderer_init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {
+init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {
     vertexArray = gl.createVertexArray();
     gl.bindVertexArray(vertexArray);
 
@@ -57,11 +57,11 @@ quad_renderer_init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {
     gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 2 * sizeof gl.GLfloat, 0);
     gl.bindBuffer(gl.ARRAY_BUFFER, -1);
 
-    array_init(^quad_data, initial_quads);
+    array.init(^quad_data, initial_quads);
 
     default_quad := Quad.{};
     for i: 0 .. initial_quads {
-        array_push(^quad_data, default_quad);
+        array.push(^quad_data, default_quad);
     }
 
     quadBuffer = gl.createBuffer();
@@ -109,13 +109,13 @@ quad_renderer_init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {
     }
 
     u_proj_loc = gl.getUniformLocation(quad_program, "u_proj");
-    quad_renderer_update_view(qr);
+    update_view(qr);
 
     u_world_loc = gl.getUniformLocation(quad_program, "u_world");
-    quad_renderer_update_world(qr);
+    update_world(qr);
 }
 
-quad_renderer_draw :: proc (use qr: ^QuadRenderer, count := -1) {
+draw :: proc (use qr: ^QuadRenderer, count := -1) {
     c := count;
     if count == -1 do c = quad_data.count;
 
@@ -125,7 +125,7 @@ quad_renderer_draw :: proc (use qr: ^QuadRenderer, count := -1) {
     gl.bindVertexArray(-1);
 }
 
-quad_renderer_update_view :: proc (use qr: ^QuadRenderer) {
+update_view :: proc (use qr: ^QuadRenderer) {
     proj_mat : [4 * 4] gl.GLfloat;
     for ^it: proj_mat do *it = 0.0f;
 
@@ -148,7 +148,7 @@ quad_renderer_update_view :: proc (use qr: ^QuadRenderer) {
     gl.uniformMatrix4(u_proj_loc, true, proj_mat);
 }
 
-quad_renderer_update_world :: proc (use qr: ^QuadRenderer,
+update_world :: proc (use qr: ^QuadRenderer,
         scale_x := 1.0f, scale_y := 1.0f, trans_x := 0.0f, trans_y := 0.0f) {
     world_mat : [4 * 4] gl.GLfloat;
     for ^it: world_mat do *it = 0.0f;
@@ -164,12 +164,12 @@ quad_renderer_update_world :: proc (use qr: ^QuadRenderer,
     gl.uniformMatrix4(u_world_loc, false, world_mat);
 }
 
-quad_update_at_index :: proc (use qr: ^QuadRenderer, idx: i32, quad: Quad) {
+update_at_index :: proc (use qr: ^QuadRenderer, idx: i32, quad: Quad) {
     quad_data[idx] = quad;
     is_data_dirty = true;
 }
 
-quad_rebuffer_data :: proc (use qr: ^QuadRenderer) {
+rebuffer_data :: proc (use qr: ^QuadRenderer) {
     if !is_data_dirty do return;
 
     gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
index 6536f63f233acf33034bb85ec8c36e14e495e13c..40351dcbf894c48b4c95504c673f6c6203b48352 100644 (file)
@@ -1,4 +1,4 @@
-package gfx
+package gfx.texture
 
 use package core
 use package gl as gl
@@ -9,12 +9,12 @@ textures : struct {
     smile   : Texture;
 }
 
-textures_init :: proc () {
-    textures.tilemap = texture_create(#file_contents "res/tilemap.data", 256, 256);
-    textures.smile   = texture_create(#file_contents "res/smile.data", 32, 32);
+init :: proc () {
+    textures.tilemap = create(#file_contents "res/tilemap.data", 256, 256);
+    textures.smile   = create(#file_contents "res/smile.data", 32, 32);
 
-    texture_prepare(^textures.tilemap);
-    texture_prepare(^textures.smile);
+    prepare(^textures.tilemap);
+    prepare(^textures.smile);
 }
 
 
@@ -37,7 +37,7 @@ TextureDataMode :: enum {
 }
 
 // NOTE: This assumes that the data is raw u8 RGB packed color data.
-texture_create :: proc (texdata: [] u8, width: i32, height: i32, data_mode := TextureDataMode.RGB) -> Texture {
+create :: proc (texdata: [] u8, width: i32, height: i32, data_mode := TextureDataMode.RGB) -> Texture {
     tex : Texture;
     tex.data = texdata;
     tex.width = width;
@@ -48,7 +48,7 @@ texture_create :: proc (texdata: [] u8, width: i32, height: i32, data_mode := Te
     return tex;
 }
 
-texture_prepare :: proc (use tex: ^Texture) {
+prepare :: proc (use tex: ^Texture) {
     if texture_id != -1 do return;
 
     format := gl.RGB;
@@ -65,6 +65,6 @@ texture_prepare :: proc (use tex: ^Texture) {
     gl.bindTexture(gl.TEXTURE_2D, -1);
 }
 
-texture_use :: proc (use tex: ^Texture) {
+use_texture :: proc (use tex: ^Texture) {
     gl.bindTexture(gl.TEXTURE_2D, texture_id);
 }
index 2edc82aee702aee3a911d81ab40098d91b2df686..7359be2d4a3c0e0a250fe95bcdd00b9c9c75af05 100644 (file)
@@ -2,7 +2,8 @@ package globals
 
 use package input as input
 use package main
-use package gfx
+use package gfx as gfx
+use package gfx.atlas as atlas
 use package quad_tree
 
 window_width  := 0
@@ -12,7 +13,7 @@ half_window_height := 0
 
 renderer      : RenderContext
 input_state   : input.InputState
-atlas         : Atlas
+main_atlas    : atlas.Atlas
 
 dudes         : [..] Dude
 dude_tree     : QuadTree(Dude)
index b0cfcd689a4092545a5509b79e77be1e1e7dd376..5ecef094767c63abe13422bf02eb811f75d4c2cc 100644 (file)
@@ -19,6 +19,7 @@ package main
 
 use package core
 use package gfx
+use package gfx.quad_renderer { Color4f32, Quad, QuadRenderer }
 use package gl as gl
 use package event as event
 use package input as input { Key }
@@ -31,7 +32,7 @@ use package aabb
 NUM_QUADS :: 1 << 17
 
 RenderContext :: struct {
-    quad_renderer: ^QuadRenderer;
+    quads: ^QuadRenderer;
 
     curr_quad_idx: i32 = 0;
     max_quad_idx:  i32 = 0;
@@ -48,11 +49,11 @@ render_context_init :: proc (use rc: ^RenderContext, qr: ^QuadRenderer = null) {
 
     if aqr == null {
         aqr = calloc(sizeof QuadRenderer);
-        quad_renderer_init(aqr, NUM_QUADS);
+        quad_renderer.init(aqr, NUM_QUADS);
     }
 
     *rc = RenderContext.{
-        quad_renderer = aqr,
+        quads = aqr,
         max_quad_idx = qr.quad_data.count,
     };
 }
@@ -60,7 +61,7 @@ render_context_init :: proc (use rc: ^RenderContext, qr: ^QuadRenderer = null) {
 draw_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32) {
     if curr_quad_idx >= max_quad_idx do return;
 
-    quad_update_at_index(quad_renderer, curr_quad_idx, Quad.{
+    quad_renderer.update_at_index(quads, curr_quad_idx, Quad.{
         pos   = V2f.{ x, y },
         size  = V2f.{ w, h },
         color = color,
@@ -74,7 +75,7 @@ draw_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32) {
 draw_textured_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f32, tx: f32, ty: f32, tw: f32, th: f32) {
     if curr_quad_idx >= max_quad_idx do return;
 
-    quad_update_at_index(quad_renderer, curr_quad_idx, Quad.{
+    quad_renderer.update_at_index(quads, curr_quad_idx, Quad.{
         pos   = V2f.{ x, y },
         size  = V2f.{ w, h },
         color = color,
@@ -88,22 +89,22 @@ draw_textured_rect :: proc (use rc: ^RenderContext, x: f32, y: f32, w: f32, h: f
 
 draw_quad :: proc (use rc: ^RenderContext, quad: ^Quad) {
     if curr_quad_idx >= max_quad_idx do return;
-    quad_update_at_index(quad_renderer, curr_quad_idx, *quad);
+    quad_renderer.update_at_index(quads, curr_quad_idx, *quad);
     curr_quad_idx += 1;
 }
 
 render_context_ui :: proc (use rc: ^RenderContext) {
-    quad_renderer_update_world(quad_renderer, 1.0f, 1.0f, ~~-half_window_width, ~~-half_window_height);
-    quad_rebuffer_data(quad_renderer);
-    quad_renderer_draw(quad_renderer, curr_quad_idx);
+    quad_renderer.update_world(quads, 1.0f, 1.0f, ~~-half_window_width, ~~-half_window_height);
+    quad_renderer.rebuffer_data(quads);
+    quad_renderer.draw(quads, curr_quad_idx);
 
     curr_quad_idx = 0;
 }
 
 render_context_transformation :: proc (use rc: ^RenderContext) {
-    quad_renderer_update_world(quad_renderer, scale, scale, trans_x, trans_y);
-    quad_rebuffer_data(quad_renderer);
-    quad_renderer_draw(quad_renderer, curr_quad_idx);
+    quad_renderer.update_world(quads, scale, scale, trans_x, trans_y);
+    quad_renderer.rebuffer_data(quads);
+    quad_renderer.draw(quads, curr_quad_idx);
 
     curr_quad_idx = 0;
 }
@@ -127,7 +128,7 @@ poll_events :: proc () {
             gl.canvasSize(window_width, window_height);
             gl.viewport(0, 0, window_width, window_height);
 
-            quad_renderer_update_view(renderer.quad_renderer);
+            quad_renderer.update_view(renderer.quads);
         }
 
         case #default do input.process_event(^input_state, ^ev);
@@ -158,10 +159,10 @@ update :: proc (dt: f32) {
     }
 
     if simulating {
-        quad_scratch : ScratchState;
+        quad_scratch : allocator.ScratchState;
         quad_alloc : Allocator;
-        scratch_state_init(^quad_scratch, ~~quad_scratch_buffer, 512 * 1024);
-        scratch_alloc_init(^quad_alloc, ^quad_scratch);
+        allocator.scratch_state_init(^quad_scratch, ~~quad_scratch_buffer, 512 * 1024);
+        allocator.scratch_alloc_init(^quad_alloc, ^quad_scratch);
 
         quadtree_init(^dude_tree, AABB.{ 0.0f, 0.0f, ~~tilemap.width * TILE_SIZE, ~~tilemap.height * TILE_SIZE});
         for ^d: dudes do quadtree_insert(^dude_tree, d, quad_alloc);
@@ -188,13 +189,13 @@ draw :: proc () {
     gl.clear(gl.COLOR_BUFFER_BIT);
 
     // World Rendering
-    texture_use(^textures.tilemap);
+    texture.use_texture(^texture.textures.tilemap);
 
     tilemap_draw(^tilemap, ^renderer);
 
     // draw_quad_tree(^dude_tree);
 
-    dude_sprite := atlas_lookup(^atlas, 0);
+    dude_sprite := atlas.lookup(^main_atlas, 0);
     for ^d: dudes {
         aabb := dude_get_aabb(d);
         renderer.color = d.color;
@@ -209,7 +210,7 @@ draw :: proc () {
     draw_rect(^renderer, 10.0f, 10.0f, 1000.0f, 50.0f);
 
     renderer.color = Color4f32.{ 0.0f, 0.0f, 0.0f, 1.0f };
-    draw_text(^renderer, "Hello. Test(12486)", V2f.{ 10.0f, 10.0f }, 2.0f);
+    font.draw_text(^renderer, "Hello. Test(12486)", V2f.{ 10.0f, 10.0f }, 2.0f);
 
     draw_rect(^renderer, ~~input_state.mouse.x, ~~input_state.mouse.y, 10f, 10f);
     render_context_ui(^renderer);
@@ -239,7 +240,7 @@ main :: proc (args: [] cstring) {
     println("Setting up WebGL2 canvas...");
 
     time_now :: proc () -> u32 #foreign "time" "now" ---;
-    random_seed(time_now());
+    random.set_seed(time_now());
 
     if !gl.init("gamecanvas") {
         print("Failed to initialize GL canvas.");
@@ -253,13 +254,13 @@ main :: proc (args: [] cstring) {
     gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
 
     render_context_init(^renderer, null);
-    textures_init();
+    texture.init();
 
-    atlas = atlas_create(^textures.tilemap);
-    atlas_map(^atlas, 0, AtlasSprite.{ x = 16.0f, y = 0.0f, w = 16.0f, h = 16.0f });
-    atlas_map(^atlas, 1, AtlasSprite.{ x = 32.0f, y = 0.0f, w = 16.0f, h = 16.0f });
+    main_atlas = atlas.create(^texture.textures.tilemap);
+    atlas.map(^main_atlas, 0, atlas.AtlasSprite.{ x = 16.0f, y = 0.0f, w = 16.0f, h = 16.0f });
+    atlas.map(^main_atlas, 1, atlas.AtlasSprite.{ x = 32.0f, y = 0.0f, w = 16.0f, h = 16.0f });
 
-    atlas_map(^atlas, 2, AtlasSprite.{ x = 0.0f, y = 160.0f, w = 16.0f, h = 16.0f });
+    atlas.map(^main_atlas, 2, atlas.AtlasSprite.{ x = 0.0f, y = 160.0f, w = 16.0f, h = 16.0f });
 
     event.init();
     input.init(^input_state);
@@ -268,9 +269,9 @@ main :: proc (args: [] cstring) {
     Dude_Color_Table[1] = Color4f32.{ 0.2f, 1.0f, 0.2f, 1.0f };
     Dude_Color_Table[2] = Color4f32.{ 0.2f, 0.2f, 1.0f, 1.0f };
 
-    array_init(^dudes);
+    array.init(^dudes);
     for i: 0 .. 2000 {
-        array_push(^dudes, dude_create_random());
+        array.push(^dudes, dude_create_random());
     }
 
     tilemap_init(^tilemap, 250, 140);
@@ -300,10 +301,10 @@ Dude :: struct {
 
 dude_create_random :: proc () -> Dude {
     return Dude.{
-        pos = V2f.{ random_float(0.0f, 1200.0f), random_float(0.0f, 1200.0f) },
-        vel = V2f.{ random_float(-3.0f, 3.0f), random_float(-3.0f, 3.0f) },
-        size = random_float(4.0f, 6.0f),
-        color = Dude_Color_Table[random() % 3],
+        pos = V2f.{ random.float(0.0f, 1200.0f), random.float(0.0f, 1200.0f) },
+        vel = V2f.{ random.float(-3.0f, 3.0f), random.float(-3.0f, 3.0f) },
+        size = random.float(4.0f, 6.0f),
+        color = Dude_Color_Table[random.int() % 3],
 
         // BUG: These should not have to be here but because of the "use entity" in the defintion of Dude, they do.
         // I presume this is because entity counts as toward the number of needed elements, even though it shouldn't.
@@ -314,9 +315,9 @@ dude_create_random :: proc () -> Dude {
 }
 
 dude_update :: proc (use dude: ^Dude, dt: f32, other_dudes: ^QuadTree(Dude)) {
-    if random_between(0, 100) < 1 || couldnt_move_count >= ~~2 {
-        vel.x = random_float(-300.0f, 300.0f);
-        vel.y = random_float(-300.0f, 300.0f);
+    if random.between(0, 100) < 1 || couldnt_move_count >= ~~2 {
+        vel.x = random.float(-300.0f, 300.0f);
+        vel.y = random.float(-300.0f, 300.0f);
         couldnt_move_count = ~~0;
     }
 
@@ -341,8 +342,8 @@ dude_try_move :: proc (use dude: ^Dude, dt: f32, other_dudes: ^QuadTree(Dude)) {
     old_pos := pos;
 
     potential_dudes : [..] ^Dude;
-    array_init(^potential_dudes);
-    defer array_free(^potential_dudes);
+    array.init(^potential_dudes);
+    defer array.free(^potential_dudes);
 
     around := AABB.{ x = pos.x - size * 5.0f, y = pos.y - size * 5.0f, w = size * 10.0f, h = size * 10.0f };
     quadtree_query(other_dudes, around, ^potential_dudes);
@@ -407,7 +408,7 @@ Tile :: struct {
 tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) {
     width = w;
     height = h;
-    alloc_slice(^tiles, width * height);
+    allocator.alloc_slice(^tiles, width * height);
 
     printf("The tilemap takes up %i kibibytes.\n", (width * height * sizeof Tile) >> 10);
 
index 7c3f72dbce50c0b9bd3e2be1d22ffe0dc1b0fac0..d21d4a67f5a0730c8774e11e2de7941080291242 100644 (file)
@@ -84,9 +84,13 @@ quadtree_insert :: proc (use qt: ^QuadTree($T), t: ^T, alloc := context.allocato
 quadtree_query :: proc (use qt: ^QuadTree($T), r: AABB, p: ^[..] ^T) {
     if !aabb_intersects(region, r) do return;
 
-    while i := 0; i < points_count {
-        if aabb_contains(r, points[i].pos) do array_push(p, points[i]);
-        i += 1;
+    // while i := 0; i < points_count {
+    //     if aabb_contains(r, points[i].pos) do array_push(p, points[i]);
+    //     i += 1;
+    // }
+
+    for i: 0 .. points_count {
+        if aabb_contains(r, points[i].pos) do array.push(p, points[i]);
     }
 
     if nw == null do return;
index 36238a860c3c1151e65960bc86aef4f7bdee0d43..10d61779a8db4638c080c8df44f35a233f82c95f 100644 (file)
@@ -1,6 +1,6 @@
 package gl_utils
 
-use package core { string_make }
+use package core.str { make as string_make }
 use package gl as gl
 
 compile_shader :: proc (shader_type: gl.GLenum, source: string) -> gl.GLShader {
index 34c66c1548ca277142fae05fa7e694ae4bffeb1d..b1bac2222422ee866e9f81b62f2d244f171d063f 100644 (file)
@@ -56,10 +56,10 @@ v2_orientation :: proc (a: V2($T), b: V2(T), c: V2(T)) -> i32 {
 
 // assumes a b and c are colinear
 v2_on_segment :: proc (a: V2($T), b: V2(T), c: V2(T)) -> bool {
-    return b.x <= max_poly(a.x, c.x)
-        && b.x >= min_poly(a.x, c.x)
-        && b.y <= max_poly(a.y, c.y)
-        && b.y >= min_poly(a.y, c.y);
+    return b.x <= math.max(a.x, c.x)
+        && b.x >= math.min(a.x, c.x)
+        && b.y <= math.max(a.y, c.y)
+        && b.y >= math.min(a.y, c.y);
 }