From b2093e6113e0966e4d2fd7e33e58d213de6a8126 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sat, 19 Dec 2020 17:12:37 -0600 Subject: [PATCH] updates to newer version of Onyx --- build.sh | 2 +- src/font.onyx | 59 +++++++++++++++++++------------------- src/gfx/atlas.onyx | 10 +++---- src/gfx/font.onyx | 2 +- src/gfx/quad_renderer.onyx | 33 +++++++++------------ src/main.onyx | 14 ++++----- src/quad_tree.onyx | 8 +++--- src/utils/gl.onyx | 4 +-- 8 files changed, 64 insertions(+), 68 deletions(-) diff --git a/build.sh b/build.sh index 37baa37..a5a4875 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/sh -onyx src/main.onyx && onyx doc src/main.onyx +onyx -V src/main.onyx diff --git a/src/font.onyx b/src/font.onyx index 458a222..ea4c167 100644 --- a/src/font.onyx +++ b/src/font.onyx @@ -1,6 +1,7 @@ package ttf_font use package core +use package core.intrinsics.wasm { sqrt_f32 } use package vecmath // Very proud of how small this function can be using slices @@ -72,7 +73,7 @@ binary_reader_create :: proc (data: [] u8, initial_pos := 0) -> BinaryReader { return ~~val / cast(f32) (1 << 16); } -#private_file read_string :: proc (use br: ^BinaryReader, len: i32) -> string { +#private_file read_string :: proc (use br: ^BinaryReader, len: i32) -> str { ret := data.data[pos .. pos + len]; pos += len; return ret; @@ -98,7 +99,7 @@ TrueTypeFont :: struct { entry_selector : u16; range_shift : u16; - table_map : i32map.I32Map(TTTableInfo); + table_map : map.Map(i32, TTTableInfo); char_maps : [..] TTCmap; version : u32; @@ -167,7 +168,7 @@ TTGlyphPoint :: struct { ttf_create :: proc (ttf_data: [] u8) -> TrueTypeFont { ttf : TrueTypeFont; ttf.reader = binary_reader_create(ttf_data); - i32map.init(^ttf.table_map); + map.init(^ttf.table_map); array.init(^ttf.char_maps); ttf_read_offset_table(^ttf); ttf_read_head_table(^ttf); @@ -194,9 +195,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); + map.put(^table_map, tag_int, table_info); - if !str.equal(tag, "head") { + if !string.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 +210,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 := map.get(^table_map, string_to_beu32("head"), empty_table_hack); seek(^reader, head_table_info.offset); version = read_u32(^reader); @@ -233,8 +234,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 := map.get(^table_map, string_to_beu32("loca"), empty_table_hack); + glyf_table_info := map.get(^table_map, string_to_beu32("glyf"), empty_table_hack); old: u32; defer seek(^reader, old); @@ -259,7 +260,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 := map.get(^table_map, string_to_beu32("glyf"), empty_table_hack); if offset >= glyf_table_info.offset + glyf_table_info.length do return null; @@ -372,7 +373,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 := map.get(^table_map, string_to_beu32("maxp"), empty_table_hack); old := seek(^reader, maxp_table_info.offset + 4); defer seek(^reader, old); @@ -381,7 +382,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 := map.get(^table_map, string_to_beu32("cmap"), empty_table_hack); seek(^reader, cmap_table_info.offset); version := read_u16(^reader); @@ -419,7 +420,7 @@ TTCmap4 :: struct { range_shift : u16; segments : [..] TTSegment; - cache : i32map.I32Map(i32); + cache : map.Map(i32, i32); } TTSegment :: struct { @@ -467,22 +468,22 @@ ttf_read_cmap0 :: proc (use ttf: ^TrueTypeFont) { ttf_read_cmap4 :: proc (use ttf: ^TrueTypeFont) { cmap : TTCmap; cmap.cmap4.format = TTCmapFormat.Segmented; - map := ^cmap.cmap4; - i32map.init(^map.cache); + imap := ^cmap.cmap4; + map.init(^imap.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); + imap.seg_count = read_u16(^reader) >> ~~1; + imap.search_range = read_u16(^reader); + imap.entry_selector = read_u16(^reader); + imap.range_shift = read_u16(^reader); - array.init(^map.segments, ~~map.seg_count); - map.segments.count = cast(u32) map.seg_count; + array.init(^imap.segments, ~~imap.seg_count); + imap.segments.count = cast(u32) imap.seg_count; - for ^seg: map.segments do seg.end_code = read_u16(^reader); + for ^seg: imap.segments do seg.end_code = read_u16(^reader); read_u16(^reader); // Reserved and unused - for ^seg: map.segments do seg.start_code = read_u16(^reader); - for ^seg: map.segments do seg.id_delta = read_u16(^reader); - for ^seg: map.segments { + for ^seg: imap.segments do seg.start_code = read_u16(^reader); + for ^seg: imap.segments do seg.id_delta = read_u16(^reader); + for ^seg: imap.segments { seg.id_range_offset = read_u16(^reader); if seg.id_range_offset != ~~0 do seg.id_range_offset += ~~(tell(^reader) - 2); } @@ -513,7 +514,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 map.has(^cmap.cache, charcode) do return map.get(^cmap.cache, charcode); index := 0; for ^seg: cmap.segments { @@ -530,14 +531,14 @@ ttf_lookup_in_cmap4 :: proc (use ttf: ^TrueTypeFont, cmap: ^TTCmap4, charcode: u } } - i32map.put(^cmap.cache, charcode, index); + map.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 := map.get(^table_map, string_to_beu32("hhea"), empty_table_hack); seek(^reader, hhea_table_info.offset); hhea.version = read_u32(^reader); @@ -566,7 +567,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 := map.get(^table_map, string_to_beu32("hmtx"), empty_table_hack); offset := hmtx_table_info.offset; hmtx : TTHorizontalMetrics; @@ -784,7 +785,7 @@ ttf_calc_table_checksum :: proc (reader: ^BinaryReader, offset: u32, length: u32 } #private_file -string_to_beu32 :: proc (s: string) -> u32 { +string_to_beu32 :: proc (s: str) -> u32 { return (cast(u32) s[0] << 24) | (cast(u32) s[1] << 16) | (cast(u32) s[2] << 8) diff --git a/src/gfx/atlas.onyx b/src/gfx/atlas.onyx index d210fbf..2e59654 100644 --- a/src/gfx/atlas.onyx +++ b/src/gfx/atlas.onyx @@ -8,7 +8,7 @@ use package gfx.quad_renderer { Quad } Atlas :: struct { texture : ^texture.Texture; - map : i32map.I32Map(AtlasSprite); + texmap : map.Map(i32, AtlasSprite); } AtlasSprite :: struct { @@ -21,17 +21,17 @@ AtlasSprite :: struct { create :: proc (tex: ^texture.Texture) -> Atlas { atlas : Atlas; atlas.texture = tex; - i32map.init(^atlas.map); + map.init(^atlas.texmap); return atlas; } map :: proc (use atlas: ^Atlas, id: i32, sprite: AtlasSprite) { - i32map.put(^map, id, sprite); + map.put(^texmap, id, sprite); } lookup :: proc (use atlas: ^Atlas, id: i32, offset := 0) -> AtlasSprite { - sprite := i32map.get(^map, id, AtlasSprite.{}); + sprite := map.get(^texmap, id, AtlasSprite.{}); if offset != 0 { sprite.x += ~~offset * sprite.w; @@ -50,7 +50,7 @@ lookup :: proc (use atlas: ^Atlas, id: i32, offset := 0) -> AtlasSprite { } apply_to_quad :: proc (use atlas: ^Atlas, id: i32, quad: ^Quad) { - sprite := i32map.get(^map, id, AtlasSprite.{}); + sprite := map.get(^texmap, 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; diff --git a/src/gfx/font.onyx b/src/gfx/font.onyx index 704508a..403c140 100644 --- a/src/gfx/font.onyx +++ b/src/gfx/font.onyx @@ -19,7 +19,7 @@ char_to_tex_offset :: proc (ch: u8) -> u32 { return loc; } -draw_text :: proc (renderer: ^RenderContext, s: string, pos: V2f, scale := 1.0f) { +draw_text :: proc (renderer: ^RenderContext, s: str, pos: V2f, scale := 1.0f) { x := pos.x; y := pos.y; diff --git a/src/gfx/quad_renderer.onyx b/src/gfx/quad_renderer.onyx index 179e76a..7f1ea79 100644 --- a/src/gfx/quad_renderer.onyx +++ b/src/gfx/quad_renderer.onyx @@ -35,20 +35,21 @@ Quad :: struct { color : Color4f32 = Color4f32.{ 0f, 0f, 0f, 0f }; } -init :: proc (use qr: ^QuadRenderer, initial_quads := 10) { +init :: proc (use qr: ^QuadRenderer, initial_quads := 10) #export "QUAD_INIT" { vertexArray = gl.createVertexArray(); gl.bindVertexArray(vertexArray); // Set up vertex and index data - vertex_data : [4] V2f; - vertex_data[0] = V2f.{ 0.0f, 0.0f }; - vertex_data[1] = V2f.{ 0.0f, 1.0f }; - vertex_data[2] = V2f.{ 1.0f, 1.0f }; - vertex_data[3] = V2f.{ 1.0f, 0.0f }; + vertex_data := V2f.[ + V2f.{ 0.0f, 0.0f }, + V2f.{ 0.0f, 1.0f }, + V2f.{ 1.0f, 1.0f }, + V2f.{ 1.0f, 0.0f }, + ]; vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); - gl.bufferData(gl.ARRAY_BUFFER, Buffer.{ + gl.bufferData(gl.ARRAY_BUFFER, (#type [] void).{ count = sizeof [4] V2f, data = cast(^void) vertex_data }, gl.STATIC_DRAW); @@ -66,7 +67,7 @@ init :: proc (use qr: ^QuadRenderer, initial_quads := 10) { quadBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer); - gl.bufferData(gl.ARRAY_BUFFER, Buffer.{ + gl.bufferData(gl.ARRAY_BUFFER, (#type [] void).{ count = sizeof Quad * quad_data.count, data = cast(^void) quad_data.data, }, gl.DYNAMIC_DRAW); @@ -82,19 +83,11 @@ init :: proc (use qr: ^QuadRenderer, initial_quads := 10) { gl.vertexAttribPointer(5, 4, gl.FLOAT, false, sizeof Quad, 8 * sizeof f32); gl.bindBuffer(gl.ARRAY_BUFFER, -1); - index_data : [6] gl.GLubyte; - index_data[0] = cast(gl.GLubyte) 0; - index_data[1] = cast(gl.GLubyte) 1; - index_data[2] = cast(gl.GLubyte) 2; - index_data[3] = cast(gl.GLubyte) 0; - index_data[4] = cast(gl.GLubyte) 2; - index_data[5] = cast(gl.GLubyte) 3; - - // index_data := { 0, 1, 2, 0, 2, 3 }; + index_data := gl.GLubyte.[ 0, 1, 2, 0, 2, 3 ]; indexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); - gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, Buffer.{ + gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, (#type [] void).{ count = sizeof [6] gl.GLubyte, data = cast(^void) index_data, }, gl.STATIC_DRAW); @@ -172,8 +165,10 @@ update_at_index :: proc (use qr: ^QuadRenderer, idx: i32, quad: Quad) { rebuffer_data :: proc (use qr: ^QuadRenderer) { if !is_data_dirty do return; + println("Rebuffering DATA!!"); + gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer); - gl.bufferSubData(gl.ARRAY_BUFFER, 0, Buffer.{ + gl.bufferSubData(gl.ARRAY_BUFFER, 0, (#type [] void).{ count = quad_data.count * sizeof Quad, data = cast(^void) quad_data.data, }); diff --git a/src/main.onyx b/src/main.onyx index 5ecef09..c709f32 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -54,7 +54,7 @@ render_context_init :: proc (use rc: ^RenderContext, qr: ^QuadRenderer = null) { *rc = RenderContext.{ quads = aqr, - max_quad_idx = qr.quad_data.count, + max_quad_idx = aqr.quad_data.count, }; } @@ -159,14 +159,13 @@ update :: proc (dt: f32) { } if simulating { - quad_scratch : allocator.ScratchState; - quad_alloc : Allocator; - allocator.scratch_state_init(^quad_scratch, ~~quad_scratch_buffer, 512 * 1024); - allocator.scratch_alloc_init(^quad_alloc, ^quad_scratch); + quad_scratch := alloc.ring.make(~~quad_scratch_buffer, 512 * 1024); + quad_alloc := alloc.ring.make_allocator(^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); + for ^d: dudes do dude_update(d, dt, ^dude_tree); for ^d: dudes { if d.can_move_x do d.pos.x += d.vel.x * dt; @@ -196,6 +195,7 @@ draw :: proc () { // draw_quad_tree(^dude_tree); dude_sprite := atlas.lookup(^main_atlas, 0); + for ^d: dudes { aabb := dude_get_aabb(d); renderer.color = d.color; @@ -236,7 +236,7 @@ loop :: proc () #export { draw(); } -main :: proc (args: [] cstring) { +main :: proc (args: [] cstr) { println("Setting up WebGL2 canvas..."); time_now :: proc () -> u32 #foreign "time" "now" ---; @@ -408,7 +408,7 @@ Tile :: struct { tilemap_init :: proc (use tm: ^Tilemap, w := 10, h := 10) { width = w; height = h; - allocator.alloc_slice(^tiles, width * height); + alloc.alloc_slice(^tiles, width * height); printf("The tilemap takes up %i kibibytes.\n", (width * height * sizeof Tile) >> 10); diff --git a/src/quad_tree.onyx b/src/quad_tree.onyx index d21d4a6..b0cdb6f 100644 --- a/src/quad_tree.onyx +++ b/src/quad_tree.onyx @@ -49,10 +49,10 @@ quadtree_subdivide :: proc (use qt: ^QuadTree($T), a: Allocator) { hw := region.w / ~~ 2; hh := region.h / ~~ 2; - qt.nw = alloc(a, sizeof QuadTree(T)); - qt.ne = alloc(a, sizeof QuadTree(T)); - qt.sw = alloc(a, sizeof QuadTree(T)); - qt.se = alloc(a, sizeof QuadTree(T)); + qt.nw = raw_alloc(a, sizeof QuadTree(T)); + qt.ne = raw_alloc(a, sizeof QuadTree(T)); + qt.sw = raw_alloc(a, sizeof QuadTree(T)); + qt.se = raw_alloc(a, sizeof QuadTree(T)); quadtree_init(qt.nw, AABB.{ region.x, region.y, hw, hh }); quadtree_init(qt.ne, AABB.{ region.x + hw, region.y, hw, hh }); diff --git a/src/utils/gl.onyx b/src/utils/gl.onyx index 10d6177..75dd0e7 100644 --- a/src/utils/gl.onyx +++ b/src/utils/gl.onyx @@ -1,9 +1,9 @@ package gl_utils -use package core.str { make as string_make } +use package core.string { make as string_make } use package gl as gl -compile_shader :: proc (shader_type: gl.GLenum, source: string) -> gl.GLShader { +compile_shader :: proc (shader_type: gl.GLenum, source: str) -> gl.GLShader { shader := gl.createShader(shader_type); gl.shaderSource(shader, source); gl.compileShader(shader); -- 2.25.1