diff --git a/CHANGELOG.md b/CHANGELOG.md index f4cb116..db9b908 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# next: v0.6.0 + +- Add image cache layer to SpatialTree.gd (#47, #48) + # v0.5.1 - Raise the camera in TheWoods (#46) diff --git a/ImageCache.gd b/ImageCache.gd new file mode 100644 index 0000000..f6c4727 --- /dev/null +++ b/ImageCache.gd @@ -0,0 +1,4 @@ +extends Node + +# hash(TreeParams.id()) -> Image +var cache = {} diff --git a/SpatialTree.gd b/SpatialTree.gd index 4b1098b..20f1735 100644 --- a/SpatialTree.gd +++ b/SpatialTree.gd @@ -6,20 +6,14 @@ export var axiom = "F" export var n = 4 export var delta = 22.5 export var rules = "F:FF-[-F+F+F]+[+F-F-F]" +export var image_cache_path: NodePath + +const TreeParams = preload("res://TreeParams.gd") func _ready(): - $NativeHelp.set("base/rules", rules) - $NativeHelp.set("base/axiom", axiom) - $NativeHelp.set("base/n", n) - $NativeHelp.set("base/delta", delta) - $NativeHelp.set("base/stroke_width", stroke_width) - $NativeHelp.set("base/stroke_length", stroke_length) - - var img_with_blank_space:Image = $NativeHelp.make_image() - if img_with_blank_space == null: - printerr("image wasn't created") - return - var img = img_with_blank_space.get_rect(img_with_blank_space.get_used_rect()) + var make_image_start_time = OS.get_ticks_msec() + var img = _cached_make_image() + print("make image took %d" % (OS.get_ticks_msec() - make_image_start_time)) var translate_x = 0.5 - _guess_center_along_bottom(img) @@ -79,3 +73,35 @@ func _guess_center_along_bottom(img: Image) -> float: img.unlock() return found_col * 1.0 / w * 1.0 + +func _cached_make_image(): + var params = TreeParams.new() + params.stroke_length = stroke_length + params.stroke_width = stroke_width + params.n = n + params.delta = delta + params.axiom = axiom + params.rules = rules + + var params_id = params.id() + var cache_node = get_node_or_null(image_cache_path) + if cache_node and cache_node.cache and cache_node.cache.has(params_id): + return cache_node.cache[params_id] + else: + $NativeHelp.set("base/rules", rules) + $NativeHelp.set("base/axiom", axiom) + $NativeHelp.set("base/n", n) + $NativeHelp.set("base/delta", delta) + $NativeHelp.set("base/stroke_width", stroke_width) + $NativeHelp.set("base/stroke_length", stroke_length) + + var make_image_start_time = OS.get_ticks_msec() + + var img_with_blank_space:Image = $NativeHelp.make_image() + if img_with_blank_space == null: + printerr("image wasn't created") + return + var final_img = img_with_blank_space.get_rect(img_with_blank_space.get_used_rect()) + if cache_node: + cache_node.cache[params_id] = final_img + return final_img diff --git a/TheWoods.gd b/TheWoods.gd index fa0ce6a..9af993d 100644 --- a/TheWoods.gd +++ b/TheWoods.gd @@ -1,19 +1,13 @@ extends Spatial const SpatialTree = preload("res://SpatialTree.tscn") +const TreeParams = preload("res://TreeParams.gd") +const ImageCache = preload("res://ImageCache.gd") -const ROWS = 3 +const ROWS = 10 const COLS = 10 const SPACING = 4 -class TreeParams: - var stroke_width = 2.0 - var stroke_length = 4.0 - var axiom = "F" - var n = 4 - var delta = 22.5 - var rules = "F:FF-[-F+F+F]+[+F-F-F]" - var _samples = [] func _make_samples(): @@ -61,10 +55,15 @@ func _make_samples(): func _ready(): _make_samples() + var image_cache = ImageCache.new() + image_cache.name = "ImageCache" + add_child(image_cache) + var tree_image_cache_path = NodePath("%s/%s" % [get_path() , "/ImageCache"]) for i in range(0,ROWS): for j in range(0, COLS): var tree = SpatialTree.instance() + tree.image_cache_path = tree_image_cache_path var sample = _samples[randi()%_samples.size()] tree.n = sample.n tree.rules = sample.rules diff --git a/TreeParams.gd b/TreeParams.gd new file mode 100644 index 0000000..d5bce34 --- /dev/null +++ b/TreeParams.gd @@ -0,0 +1,11 @@ +extends Resource + +var stroke_width = 2.0 +var stroke_length = 4.0 +var axiom = "F" +var n = 4 +var delta = 22.5 +var rules = "F:FF-[-F+F+F]+[+F-F-F]" + +func id() -> String: + return "SW:%f_SL:%f_N:%d_D:%f_A:%s_R:%s" % [stroke_width, stroke_length, n, delta, axiom, rules]