From: Brendan Hansen Date: Fri, 10 Feb 2023 20:19:31 +0000 (-0600) Subject: updated to latest version of onyx X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=8dcb5de6a206b46a2457c4296f85cad38a3e7e79;p=onyx-prez.git updated to latest version of onyx --- diff --git a/build.sh b/build.sh index f967216..77aba6e 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/sh -onyx -V -r js src/build.onyx -o dist/prez.wasm --use-post-mvp-features +onyx -V -r js src/build.onyx -o dist/prez.wasm diff --git a/onyx-lsp.ini b/onyx-lsp.ini new file mode 100644 index 0000000..9666a1e --- /dev/null +++ b/onyx-lsp.ini @@ -0,0 +1,3 @@ +[lsp] +mode=project +onyxFiles=src/build.onyx diff --git a/onyx.prez b/onyx.prez index 3e93444..f3d9864 100644 --- a/onyx.prez +++ b/onyx.prez @@ -24,7 +24,8 @@ [define_text_style hyperlink color 100 100 255 font_attr italic font_size 40 font_name "Arial" left padding 10] [define_text_style code color 255 255 255 font_size 40 font_name "monospace" left padding 20] -[load_image onyx_example "https://brendanfh.com/files/onyx_prez/onyx_example.png"] +# [load_image onyx_example "https://brendanfh.com/files/onyx_prez/onyx_example.png"] +[load_image onyx_example "https://webassembly.org/css/webassembly.svg"] [load_image wasm_logo "https://webassembly.org/css/webassembly.svg"] # --------------------------------------------- diff --git a/src/build.onyx b/src/build.onyx index 4892f68..e5450f8 100644 --- a/src/build.onyx +++ b/src/build.onyx @@ -1,6 +1,6 @@ -use package build_opts as build_opts -#if build_opts.Runtime != build_opts.Runtime_Js { +#local runtime :: package runtime +#if runtime.runtime != .Js { #error "This software is only compatible with a JavaScript runtime. Use '-r js' to specify this on the command line." } diff --git a/src/canvas.onyx b/src/canvas.onyx index f06b128..442dcb2 100644 --- a/src/canvas.onyx +++ b/src/canvas.onyx @@ -50,7 +50,7 @@ setup_canvas :: () { use Canvas canvas = init("prez_canvas"); - assert(canvas != -1, "Failed to set up canvas."); + assert(canvas != 0xffffffff, "Failed to set up canvas."); clear(canvas, 0.1, 0.1, 0.1); diff --git a/src/events.onyx b/src/events.onyx index 820076d..763d974 100644 --- a/src/events.onyx +++ b/src/events.onyx @@ -1,6 +1,6 @@ package event -#private_file Num_Buffered_Events :: 16 +#local Num_Buffered_Events :: 16 // NOTE: These need to match exactly what is in the corresponding javascript DomEventKind :: enum { @@ -98,11 +98,13 @@ poll :: (ev: ^Event) -> bool { /* Private members */ -#private_file EventStorage :: struct { - event_count : u32; - max_events : u32; - event_buffer : [Num_Buffered_Events] Event; -} +#local { + EventStorage :: struct { + event_count : u32; + max_events : u32; + event_buffer : [Num_Buffered_Events] Event; + } -#private_file event_storage : EventStorage; -#private_file event_setup :: (event_storage: ^EventStorage, event_size: u32) -> void #foreign "event" "setup" --- + event_storage : EventStorage; + event_setup :: (event_storage: ^EventStorage, event_size: u32) -> void #foreign "event" "setup" --- +} diff --git a/src/prez.onyx b/src/prez.onyx index 18d755a..e273b1c 100644 --- a/src/prez.onyx +++ b/src/prez.onyx @@ -1,5 +1,5 @@ use package core -use package event as event +event :: package event // This is an integer, not a boolean, because I was noticing an issue // where the slide would redraw, but the screen would still be blank @@ -37,7 +37,7 @@ poll_events :: () { } case FileDropped { - printf("New slideshow was dropped! %i bytes in size\n", ev.file.file_size); + printf("New slideshow was dropped! {} bytes in size\n", ev.file.file_size); source := memory.make_slice(u8, ev.file.file_size); defer cfree(source.data); @@ -52,7 +52,8 @@ poll_events :: () { } } -loop :: () -> void #export "loop" { +#export "loop" loop +loop :: () { poll_events(); slideshow_update_animation(^the_slideshow); @@ -68,7 +69,7 @@ loop :: () -> void #export "loop" { Canvas.clear(canvas, 0, 0, 0, 1); current_slide := slideshow_get_current_slide(^the_slideshow); - slide_render(current_slide); + if current_slide do slide_render(current_slide); redraw -= 1; } diff --git a/src/show_parser.onyx b/src/show_parser.onyx index c065bf2..f319f1f 100644 --- a/src/show_parser.onyx +++ b/src/show_parser.onyx @@ -13,7 +13,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { parse_alloc := alloc.arena.make_allocator(^parse_arena); - show_stream := string_stream_make(source); + show_stream := buffer_stream_make(source, fixed=true, write_enabled=false); show_reader := reader_make(^show_stream); default_text_style := Slide_Item_Text.{ @@ -30,7 +30,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { variables := map.make(str, u32); defer map.free(^variables); - animations := map.make(str, #type (Allocator) -> ^Slide_Animation, default=null_proc, hash_count=4); + animations := map.make(str, #type (Allocator) -> ^Slide_Animation, default=null_proc); defer map.free(^animations); map.put(^animations, "swipe", Slide_Animation_Swipe.make); map.put(^animations, "fade", Slide_Animation_Fade.make); @@ -47,11 +47,11 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { aspect_ratio := 1.0f; y_increment := 0.05f; - while !stream_end_of_file(^show_stream) { + while !reader_empty(^show_reader) { skip_whitespace(^show_reader); defer skip_whitespace(^show_reader); - _, next_byte := stream_peek_byte(^show_stream); + next_byte, _ := peek_byte(^show_reader); switch next_byte { case #char "[" { // Command @@ -96,7 +96,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { elseif command_name == "text_style" { text_style_name := read_word(^show_reader, numeric_allowed=true); if !map.has(^text_styles, text_style_name) { - printf("Text style '%s' was never defined.\n", text_style_name); + printf("Text style '{}' was never defined.\n", text_style_name); } text_style := map.get(^text_styles, text_style_name); @@ -150,15 +150,15 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { allocator := alloc.arena.make_allocator(^slideshow.arena); current_slide.animation = (map.get(^animations, anim_name))(allocator); } else { - printf("Unknown animation: '%s'\n", anim_name); + printf("Unknown animation: '{}'\n", anim_name); current_slide.animation = null; } } else { - printf("******** Unknown command: '%s'.\n", command_name); + printf("******** Unknown command: '{}'.\n", command_name); } - + read_until(^show_reader, #char "]", allocator=parse_alloc, consume_end=true); } @@ -169,7 +169,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { case #default { // @Memory text_allocator := alloc.arena.make_allocator(^slideshow.arena); - text := read_line(^show_reader, text_allocator); + text := read_line(^show_reader, allocator=text_allocator); new_slide_text := slideshow_make_item(slideshow); new_slide_text.text = current_text_style; @@ -184,7 +184,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { flush_items(current_slide, ^current_slide_items, slideshow); - printf("Total slide count: %i\n", slideshow.slides.count); + printf("Total slide count: {}\n", slideshow.slides.count); flush_items :: (slide: ^Slide, items: ^[..] ^Slide_Item, slideshow: ^Slideshow) { allocator := alloc.arena.make_allocator(^slideshow.arena); @@ -198,11 +198,13 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) { } } -#private_file +#local parse_text_style :: (use pc: ^ParseContext, text_style: ^Slide_Item_Text, parse_alloc := context.allocator) { use io - while !stream_end_of_file(reader.stream) && peek_byte(reader) != #char "]" { + while !reader_empty(reader) { + if b, err := peek_byte(reader); b == #char "]" do break; + skip_whitespace(reader); defer skip_whitespace(reader); style_name := read_word(reader, allocator=parse_alloc); @@ -231,21 +233,23 @@ parse_text_style :: (use pc: ^ParseContext, text_style: ^Slide_Item_Text, parse_ elseif style_name == "centered" do text_style.justify = Slide_Item_Text.Justify.Center; elseif style_name == "right" do text_style.justify = Slide_Item_Text.Justify.Right; else { - printf("Unknown style option: '%s'\n", style_name); + printf("Unknown style option: '{}'\n", style_name); advance_line(reader); break; } } } -#private_file +#local parse_image_style :: (use pc: ^ParseContext, image_style: ^Slide_Item_Image, parse_alloc := context.allocator) { use io image_name := read_word(reader, numeric_allowed=true); image_style.name = image_name; - while !stream_end_of_file(reader.stream) && peek_byte(reader) != #char "]" { + while !reader_empty(reader) { + if b, err := peek_byte(reader); b == #char "]" do break; + skip_whitespace(reader); defer skip_whitespace(reader); style_name := read_word(reader, allocator=parse_alloc); @@ -271,18 +275,20 @@ parse_image_style :: (use pc: ^ParseContext, image_style: ^Slide_Item_Image, par image_style.border_color = .{ r, g, b }; } else { - printf("Unknown style option: '%s'\n", style_name); + printf("Unknown style option: '{}'\n", style_name); advance_line(reader); break; } } } -#private_file +#local parse_rect_style :: (use pc: ^ParseContext, rect_style: ^Slide_Item_Rect, parse_alloc := context.allocator) { use io - while !stream_end_of_file(reader.stream) && peek_byte(reader) != #char "]" { + while !reader_empty(reader) { + if b, err := peek_byte(reader); b == #char "]" do break; + skip_whitespace(reader); defer skip_whitespace(reader); style_name := read_word(reader, allocator=parse_alloc); @@ -308,19 +314,19 @@ parse_rect_style :: (use pc: ^ParseContext, rect_style: ^Slide_Item_Rect, parse_ rect_style.color = .{ r, g, b }; } else { - printf("Unknown style option: '%s'\n", style_name); + printf("Unknown style option: '{}'\n", style_name); advance_line(reader); break; } } } -#private_file +#local parse_numeric_value :: (use pc: ^ParseContext) -> u32 { use io skip_whitespace(reader); - _, next_byte := stream_peek_byte(reader.stream); + next_byte := peek_byte(reader); if next_byte == #char "$" { io.read_byte(reader); var_name := read_word(reader, numeric_allowed=true); @@ -330,7 +336,7 @@ parse_numeric_value :: (use pc: ^ParseContext) -> u32 { if map.has(variables, var_name) { value = map.get(variables, var_name); } else { - printf("Variable '%s' was never defined!\n", var_name); + printf("Variable '{}' was never defined!\n", var_name); } return value; @@ -339,7 +345,7 @@ parse_numeric_value :: (use pc: ^ParseContext) -> u32 { return read_u32(reader); } -#private_file +#local parse_color :: (use pc: ^ParseContext) -> (f32, f32, f32) { r := parse_numeric_value(pc); g := parse_numeric_value(pc); @@ -352,12 +358,12 @@ parse_color :: (use pc: ^ParseContext) -> (f32, f32, f32) { return fr, fg, fb; } -#private_file +#local parse_string :: (use pc: ^ParseContext, allocator := context.allocator) -> str { use io // @Cleanup - dummy := read_until(reader, #char "\"", context.temp_allocator); + dummy := read_until(reader, #char "\"", allocator=context.temp_allocator); read_byte(reader); str_contents := read_until(reader, #char "\"", allocator=allocator); diff --git a/src/slides.onyx b/src/slides.onyx index 72136c2..4109c89 100644 --- a/src/slides.onyx +++ b/src/slides.onyx @@ -32,7 +32,7 @@ Slide :: struct { animation: ^Slide_Animation; - items : [] ^Slide_Item; + items : [] ^Slide_Item = .{}; } Slide_Item :: struct #union { @@ -113,8 +113,8 @@ slideshow_make :: (allocator := context.allocator) -> Slideshow { slideshow_init :: (use s: ^Slideshow, allocator := context.allocator) { title = "Untitled Slideshow"; - arena = alloc.arena.make(allocator, arena_size = 16 * 1024); - array.init(^slides, 4); + arena = alloc.arena.make(allocator, arena_size = 32 * 1024); + array.init(^slides, 4, allocator=allocator); map.init(^image_map, default=.{}); } @@ -129,8 +129,9 @@ slideshow_reset :: (use s: ^Slideshow) { } slideshow_get_current_slide :: (use s: ^Slideshow) -> ^Slide { - current_slide = math.clamp(current_slide, 0, slides.count - 1); + if slides.count == 0 do return null; + current_slide = math.clamp(current_slide, 0, slides.count - 1); return ^slides[current_slide]; } @@ -159,24 +160,23 @@ slideshow_insert_slide :: (use s: ^Slideshow, at := -1) -> ^Slide { return ^slides[at]; } - array.ensure_capacity(^slides, slides.count + 1); - defer slides.count += 1; - return ^slides[slides.count]; + slide := array.alloc_one(^slides); + return slide; } slideshow_make_item :: (use s: ^Slideshow) -> ^Slide_Item { allocator := alloc.arena.make_allocator(^arena); - ret := new(Slide_Item, allocator=allocator, initialize=false); + ret := make(Slide_Item, allocator=allocator); memory.set(ret, 0, sizeof Slide_Item); return ret; } // @Cleanup -use package core.intrinsics.wasm { __initialize } +use package core.intrinsics.onyx { __initialize } slideshow_load_image :: (use s: ^Slideshow, image_name: str, image_path: str) -> HTML_Image { if map.has(^image_map, image_name) { - printf("Warning: the image '%s' was already defined.", image_name); + printf("Warning: the image '{}' was already defined.", image_name); return image_map.default_value; } @@ -212,11 +212,12 @@ slide_init :: (use slide: ^Slide, if item_count > 0 { // @GlobalVariable allocator := alloc.arena.make_allocator(^the_slideshow.arena); - items = memory.make_slice(#type ^Slide_Item, item_count, allocator=allocator); + items = make([] ^Slide_Item, item_count, allocator=allocator); memory.set(items.data, 0, items.count * sizeof ^Slide_Item); } else { items.count = 0; + items.data = null; } } @@ -248,7 +249,11 @@ slide_render :: (use slide: ^Slide) { defer Canvas.set_transform(canvas, old_transform); Canvas.fill_rect(canvas, 0, 0, width, height, background.r, background.g, background.b, background.a); - for item: items do if item != null do slide_item_render(item, slide, width, height); + for item: items { + if item != null { + slide_item_render(item, slide, width, height); + } + } } slide_item_render :: (use slide_item: ^Slide_Item, slide: ^Slide, width: f32, height: f32) { @@ -335,8 +340,7 @@ slide_item_render :: (use slide_item: ^Slide_Item, slide: ^Slide, width: f32, he if text.font_attr & Underline != ~~ 0 do underline_str = "underline"; formatted_name := aprintf( - context.allocator, - "%s %s %s %ipx %s", + "{} {} {} {}px {}", bold_str, italic_str, underline_str, text.font_size, text.font_name); defer cfree(formatted_name.data); @@ -346,14 +350,14 @@ slide_item_render :: (use slide_item: ^Slide_Item, slide: ^Slide, width: f32, he } // @Cleanup: this should be part of the standard library. -aprintf :: (allocator: Allocator, format: str, va: ...) -> str { - // @Robustness: Same potential overflow bug. - buffer: [2048] u8; - formatted := conv.str_format_va(format, ~~buffer, va); - - output := string.alloc_copy(formatted, allocator = allocator); - return output; -} +// aprintf :: (allocator: Allocator, format: str, va: ..any) -> str { +// // @Robustness: Same potential overflow bug. +// buffer: [2048] u8; +// formatted := conv.format_va(buffer, format, va); +// +// output := string.alloc_copy(formatted, allocator = allocator); +// return output; +// } @@ -373,7 +377,7 @@ Slide_Animation :: struct { } Slide_Animation_Swipe :: struct { - use base := Slide_Animation.{ + use base: Slide_Animation = .{ init = init, update = update, render = render @@ -420,7 +424,7 @@ Slide_Animation_Swipe :: struct { Slide_Animation_Fade :: struct { - use base := Slide_Animation.{ + use base: Slide_Animation = .{ init = init, update = update, render = render @@ -467,7 +471,7 @@ Slide_Animation_Fade :: struct { } Slide_Animation_Wiper :: struct { - use base := Slide_Animation.{ + use base: Slide_Animation = .{ init = init, update = update, render = render