#!/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
--- /dev/null
+[lsp]
+mode=project
+onyxFiles=src/build.onyx
[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"]
# ---------------------------------------------
-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."
}
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);
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 {
/* 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" ---
+}
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
}
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);
}
}
-loop :: () -> void #export "loop" {
+#export "loop" loop
+loop :: () {
poll_events();
slideshow_update_animation(^the_slideshow);
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;
}
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.{
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);
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
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);
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);
}
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;
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);
}
}
-#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);
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);
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);
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);
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;
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);
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);
animation: ^Slide_Animation;
- items : [] ^Slide_Item;
+ items : [] ^Slide_Item = .{};
}
Slide_Item :: struct #union {
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=.{});
}
}
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];
}
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;
}
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;
}
}
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) {
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);
}
// @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;
+// }
}
Slide_Animation_Swipe :: struct {
- use base := Slide_Animation.{
+ use base: Slide_Animation = .{
init = init,
update = update,
render = render
Slide_Animation_Fade :: struct {
- use base := Slide_Animation.{
+ use base: Slide_Animation = .{
init = init,
update = update,
render = render
}
Slide_Animation_Wiper :: struct {
- use base := Slide_Animation.{
+ use base: Slide_Animation = .{
init = init,
update = update,
render = render