random addition and efficiency gains
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 20 Feb 2021 01:57:00 +0000 (19:57 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 20 Feb 2021 01:57:00 +0000 (19:57 -0600)
dist/prez.wasm
initial.prez
onyx.prez
src/show_parser.onyx
src/slides.onyx

index 598ac5a4ea5db3dd4ff0b49896bef2c830ecb6a5..5a1bf6436ba03e657c1e5cec7315bd273d2fd09d 100644 (file)
Binary files a/dist/prez.wasm and b/dist/prez.wasm differ
index 8fa408a04cc2996a97550326090a9c2a10917671..ddecafcefcabd12547eaeeee42d687c4eb57772e 100644 (file)
@@ -1,3 +1,5 @@
+[aspect_ratio 4 3]
+
 [slide] [background 60 60 60]
-[text_style undefind font_name "monospace" font_size 48 font_attr bold centered]
+[text_style inherit font_name "monospace" font_size 48 font_attr bold centered]
 [y 50] Drop a presentation file here!
index 8af1ebf0098a28acd7c60960f31dac8e851fe7b2..a5d7fac1b8fb796fb7498543c4a7c119719fad9b 100644 (file)
--- a/onyx.prez
+++ b/onyx.prez
@@ -60,7 +60,7 @@
 
 [y 30] It defines:
 [text_style inherit padding 15]
-▪ Virtual Instruction Set Architecture
+▪ Virtual Instruction Set Architecture
 ▪ Linear Memory Model
 ▪ Arbitrary imports
 ▪ Safe indirect function calls
index a3c5d27feb3d21b6a3dbade9dc1d2bcfa1ec7dad..310930e260bf03b8b13159283f355a06d9fa976c 100644 (file)
@@ -25,16 +25,22 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
 
     text_styles := map.make(str, Slide_Item_Text, default_text_style);
     defer map.free(^text_styles);
+    map.put(^text_styles, "inherit", default_text_style);
 
     variables := map.make(str, u32);
     defer map.free(^variables);
 
     parse_context := ParseContext.{ ^show_reader, ^variables };
 
-    current_slide: ^Slide;
-    current_item_idx := 0;
+    current_slide: ^Slide = null;
     current_text_style: Slide_Item_Text;
 
+    current_slide_items := array.make(#type ^Slide_Item, 8);
+    defer array.free(^current_slide_items);
+
+    aspect_ratio := 1.0f;
+    y_increment := 0.05f;
+
     while !stream_end_of_file(^show_stream) {
         skip_whitespace(^show_reader); 
         defer skip_whitespace(^show_reader); 
@@ -48,23 +54,39 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                 command_name := read_word(^show_reader, allocator=parse_alloc);
 
                 if command_name == "slide" {
-                    prev_slide := ^slideshow.slides[slideshow.slides.count - 1];
+                    background_color := Color.{ 0, 0, 0 };
+
+                    if current_slide != null {
+                        background_color = current_slide.background; 
+                        flush_items(current_slide, ^current_slide_items);
+                    }
+
                     current_slide = slideshow_insert_slide(slideshow);
-                    current_item_idx = 0;
 
-                    // @Robustness @Incomplete: Currently, there is a fixed number of items
-                    // on EVERY slide, which both wastes memory for small slides, and limits
-                    // how many things can be in a slide.
-                    slide_init(current_slide, background_color=prev_slide.background, item_count = 16);
+                    // @NOTE: item_count is 0 because the items will be allocated and inserted later
+                    slide_init(current_slide,
+                        item_count = 0,
+                        background_color = background_color,
+                        aspect = aspect_ratio);
                 }
                 elseif command_name == "background" {
                     r, g, b := parse_color(^parse_context);
                     current_slide.background = Color.{ r, g, b };
                 }
                 elseif command_name == "y" {
-                    y := read_u32(^show_reader);
+                    y := parse_numeric_value(^parse_context);
                     current_text_style.y_pos = cast(f32) y / 100;
                 }
+                elseif command_name == "y_inc" {
+                    y := parse_numeric_value(^parse_context);
+                    y_increment = cast(f32) y / 100;
+                }
+                elseif command_name == "aspect_ratio" {
+                    w := parse_numeric_value(^parse_context);
+                    h := parse_numeric_value(^parse_context);
+
+                    aspect_ratio = cast(f32) w / cast(f32) h;
+                }
                 elseif command_name == "text_style" {
                     text_style_name := read_word(^show_reader, numeric_allowed=true);
                     if !map.has(^text_styles, text_style_name) {
@@ -100,16 +122,14 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                     new_slide_image.kind = Slide_Item.Kind.Image;
                     parse_image_style(^parse_context, ^new_slide_image.image, parse_alloc);
 
-                    current_slide.items[current_item_idx] = new_slide_image;
-                    current_item_idx += 1;
+                    array.push(^current_slide_items, new_slide_image);
                 }
                 elseif command_name == "rect" {
                     new_slide_rect := slideshow_make_item(slideshow);
                     new_slide_rect.kind = Slide_Item.Kind.Rect;
                     parse_rect_style(^parse_context, ^new_slide_rect.rect, parse_alloc);
 
-                    current_slide.items[current_item_idx] = new_slide_rect;
-                    current_item_idx += 1;
+                    array.push(^current_slide_items, new_slide_rect);
                 }
                 elseif command_name == "var" {
                     var_name := read_word(^show_reader, numeric_allowed=true);
@@ -130,24 +150,32 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
 
             case #default {
                 // @Memory
-                text := read_line(^show_reader);
+                text_allocator := alloc.arena.make_allocator(^slideshow.arena);
+                text := read_line(^show_reader, text_allocator);
 
                 new_slide_text := slideshow_make_item(slideshow);
                 new_slide_text.text = current_text_style;
                 new_slide_text.text.text = text; // Oofta...
 
-                current_slide.items[current_item_idx] = new_slide_text;
-                current_item_idx += 1;
+                array.push(^current_slide_items, new_slide_text);
 
-                // @Robustness
-                // This feels like the right amount, but obviously this should change
-                // depending on font size, window height, etc...
-                current_text_style.y_pos += .05;
+                current_text_style.y_pos += y_increment;
             }
         }
     }
 
+    flush_items(current_slide, ^current_slide_items);
+
     printf("Total slide count: %i\n", slideshow.slides.count);
+
+    flush_items :: (slide: ^Slide, items: ^[..] ^Slide_Item) {
+        slide.items = memory.make_slice(#type ^Slide_Item, items.count);
+        for idx: items.count {
+            slide.items[idx] = items.data[idx];
+        }
+
+        array.clear(items);
+    }
 }
 
 #private_file
index 0af702f2bcd854d2a194d09c6a31261c5ee0d799..ce54db5805a4e830cfc8b465e966980098206d6c 100644 (file)
@@ -25,7 +25,8 @@ Slideshow :: struct {
 }
 
 Slide :: struct {
-    background: Color;
+    background   : Color;
+    aspect_ratio : f32 = 1;
 
     items : [] ^Slide_Item;
 }
@@ -51,7 +52,7 @@ Slide_Item_Base :: struct {
 Slide_Item_Text :: struct {
     use base := Slide_Item_Base.{ Slide_Item.Kind.Text };
 
-    color : Color = Color.{ 1, 1, 1 };
+    color := Color.{ 1, 1, 1 };
 
     font_name : str;
     font_size : u32;
@@ -163,11 +164,17 @@ slideshow_load_image :: (use s: ^Slideshow, image_name: str, image_path: str) ->
     return image;
 }
 
-slide_init :: (use slide: ^Slide, background_color := Color.{0, 0, 0, 1}, item_count := 4) {
+slide_init :: (use slide: ^Slide, background_color := Color.{0, 0, 0, 1}, item_count := 4, aspect := 1.0f) {
     background = background_color;
+    aspect_ratio = aspect;
 
-    items = memory.make_slice(#type ^Slide_Item, item_count);
-    memory.set(items.data, 0, items.count * sizeof ^Slide_Item);
+    if item_count > 0 {
+        items = memory.make_slice(#type ^Slide_Item, item_count);
+        memory.set(items.data, 0, items.count * sizeof ^Slide_Item);
+
+    } else {
+        items.count = 0;
+    }
 }
 
 slide_render :: (use slide: ^Slide) {