-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Interference * Show info, cycle module & resolution, interact Toggle FPS limit New update function signature includes tick * Game of life and wave sim * Sort * Readme update
- Loading branch information
1 parent
5adda5d
commit c18b7d9
Showing
12 changed files
with
298 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
.import/ | ||
.godot/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
extends Node | ||
|
||
const sizes: PoolVector2Array = PoolVector2Array([Vector2(50, 50), Vector2(100, 100), Vector2(200, 200), Vector2(400, 400), Vector2(800, 800), Vector2(1600, 1600)]) | ||
const modules: PoolStringArray = PoolStringArray(["interference", "mandelbrot", "life", "wave", "sort"]) | ||
|
||
var texture: ImageTexture = ImageTexture.new() | ||
var image: Image = Image.new() | ||
var module: String = modules[0] | ||
var size: Vector2 = sizes[2] | ||
var ticks: int = 0 | ||
onready var wasm: Wasm = Wasm.new() | ||
|
||
func _ready(): | ||
$TextureRect.texture = texture | ||
seed(OS.get_system_time_msecs()) | ||
_change_module() | ||
_change_size() | ||
_show_info() | ||
|
||
func _input(event): | ||
if event.is_action_pressed("ui_up"): _change("size", 1) | ||
elif event.is_action_pressed("ui_down"): _change("size", -1) | ||
elif event.is_action_pressed("ui_left"): _change("module", -1) | ||
elif event.is_action_pressed("ui_right"): _change("module", 1) | ||
elif event.is_action_pressed("toggle_fps_cap"): _change("fps_cap") | ||
|
||
func _gui_input(event): | ||
if !(event is InputEventMouseButton) or !event.pressed: return | ||
if get_node_or_null("Intro"): $Intro.queue_free() | ||
if !("interact" in wasm.inspect().export_functions): return | ||
var p = _localize_aspect_fit(event.position, $TextureRect.rect_size, size) | ||
if p.x < 0.0 or p.x > 1.0 or p.y < 0.0 or p.y > 1.0: return | ||
wasm.function("interact", [p.x, p.y, 1.0 if event.button_index == BUTTON_LEFT else 0.0 ]) | ||
|
||
func _process(_delta): | ||
$"%LabelFPS".text = "%d FPS" % Performance.get_monitor(Performance.TIME_FPS) | ||
wasm.function("update", [ticks, OS.get_ticks_msec() / 1000.0]) | ||
ticks += 1 | ||
|
||
func _load_wasm(path: String): | ||
var file = File.new() | ||
file.open(path, File.READ) | ||
var buffer = file.get_buffer(file.get_len()) | ||
var imports = { "functions": { | ||
"env.abort": [self, "_abort"], | ||
"env.draw_image": [self, "_draw_image"], | ||
"env.draw_pixel": [self, "_draw_pixel"], | ||
"env.seed": [self, "_seed"], | ||
} } | ||
wasm.load(buffer, imports) | ||
file.close() | ||
|
||
func _change(property: String, increment: int = 0): | ||
if get_node_or_null("Intro"): $Intro.queue_free() | ||
call_deferred("_change_%s" % property, increment) | ||
call_deferred("_show_info") | ||
|
||
func _change_module(increment: int = 0): | ||
var index = modules.find(module) + increment | ||
if index < 0 or index >= len(modules): return | ||
module = modules[index] | ||
_load_wasm("Modules/%s.wasm" % module) | ||
wasm.function("resize", [int(size.x), int(size.y)]) | ||
ticks = 0 | ||
|
||
func _change_size(increment: int = 0): | ||
var index = sizes.find(size) + increment | ||
if index < 0 or index >= len(sizes): return | ||
size = sizes[index] | ||
wasm.function("resize", [int(size.x), int(size.y)]) | ||
image.create(int(size.x), int(size.y), false, Image.FORMAT_RGBA8) | ||
ticks = 0 | ||
|
||
func _change_fps_cap(_increment: int = 0): | ||
Engine.target_fps = 0 if Engine.target_fps else 60 | ||
|
||
func _show_info(): | ||
$"%LabelModule".text = module | ||
$"%LabelSize".text = "%d X %d" % [size.x, size.y] | ||
$Tween.remove_all() | ||
$Tween.interpolate_property($Info, "modulate:a", $Info.modulate.a, 1.0, 0.2, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT, 0.0) | ||
$Tween.interpolate_property($Info, "rect_position:y", $Info.rect_position.y, 12, 0.2, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT, 0.0) | ||
$Tween.interpolate_property($Info, "modulate:a", 1.0, 0.0, 0.6, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT, 6.0) | ||
$Tween.interpolate_property($Info, "rect_position:y", 12, -98, 0.6, Tween.TRANS_CUBIC, Tween.EASE_IN_OUT, 6.0) | ||
$Tween.start() | ||
|
||
func _localize_aspect_fit(p: Vector2, outer: Vector2, inner: Vector2): | ||
var scale = outer.y / inner.y if outer.aspect() > inner.aspect() else outer.x / inner.x | ||
var scaled = inner * scale | ||
var offset = (outer - scaled) / 2 | ||
return (p - offset) / scaled | ||
|
||
# Wasm module imports | ||
|
||
func _abort(a: int, b: int, c: int, d: int) -> void: # Throw error from Wasm module | ||
push_error("Abort from Wasm module: %d %d %d %d" % [a, b, c, d]) | ||
|
||
func _draw_image(p: int, s: int) -> void: # Draw the entire image from Wasm memory | ||
image.lock() | ||
image.data.data = wasm.stream.seek(p).get_data(s)[1] | ||
image.unlock() | ||
texture.create_from_image(image, 0) | ||
|
||
func _draw_pixel(x: int, y: int, r: int, g: int, b: int, a: int) -> void: # Draw a single pixel with color component values | ||
image.lock() | ||
image.set_pixel(x, y, Color8(r, g, b, a)) | ||
image.unlock() | ||
texture.create_from_image(image, 0) | ||
|
||
func _seed() -> float: # Provide a random seed to the Wasm module | ||
return randf() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
[gd_scene load_steps=5 format=2] | ||
|
||
[ext_resource path="res://Main.gd" type="Script" id=1] | ||
[ext_resource path="res://Thaleah.ttf" type="DynamicFontData" id=2] | ||
|
||
[sub_resource type="StyleBoxFlat" id=4] | ||
content_margin_top = 12.0 | ||
content_margin_bottom = 12.0 | ||
bg_color = Color( 0, 0, 0, 1 ) | ||
corner_radius_top_left = 12 | ||
corner_radius_top_right = 12 | ||
corner_radius_bottom_right = 12 | ||
corner_radius_bottom_left = 12 | ||
corner_detail = 5 | ||
|
||
[sub_resource type="DynamicFont" id=5] | ||
size = 32 | ||
extra_spacing_top = -4 | ||
extra_spacing_bottom = -4 | ||
font_data = ExtResource( 2 ) | ||
|
||
[node name="Main" type="Control"] | ||
anchor_right = 1.0 | ||
anchor_bottom = 1.0 | ||
script = ExtResource( 1 ) | ||
|
||
[node name="Tween" type="Tween" parent="."] | ||
|
||
[node name="TextureRect" type="TextureRect" parent="."] | ||
anchor_right = 1.0 | ||
anchor_bottom = 1.0 | ||
expand = true | ||
stretch_mode = 6 | ||
|
||
[node name="Info" type="PanelContainer" parent="."] | ||
modulate = Color( 1, 1, 1, 0 ) | ||
anchor_left = 0.5 | ||
anchor_right = 0.5 | ||
margin_left = -128.0 | ||
margin_top = -98.0 | ||
margin_right = 128.0 | ||
custom_styles/panel = SubResource( 4 ) | ||
__meta__ = { | ||
"_edit_group_": true | ||
} | ||
|
||
[node name="VBoxContainer" type="VBoxContainer" parent="Info"] | ||
margin_top = 12.0 | ||
margin_right = 256.0 | ||
margin_bottom = 86.0 | ||
|
||
[node name="LabelModule" type="Label" parent="Info/VBoxContainer"] | ||
unique_name_in_owner = true | ||
margin_right = 256.0 | ||
margin_bottom = 22.0 | ||
custom_fonts/font = SubResource( 5 ) | ||
text = "Module" | ||
align = 1 | ||
|
||
[node name="LabelSize" type="Label" parent="Info/VBoxContainer"] | ||
unique_name_in_owner = true | ||
margin_top = 26.0 | ||
margin_right = 256.0 | ||
margin_bottom = 48.0 | ||
custom_fonts/font = SubResource( 5 ) | ||
text = "0 X 0" | ||
align = 1 | ||
|
||
[node name="LabelFPS" type="Label" parent="Info/VBoxContainer"] | ||
unique_name_in_owner = true | ||
margin_top = 52.0 | ||
margin_right = 256.0 | ||
margin_bottom = 74.0 | ||
custom_fonts/font = SubResource( 5 ) | ||
text = "FPS" | ||
align = 1 | ||
|
||
[node name="Intro" type="PanelContainer" parent="."] | ||
anchor_left = 0.5 | ||
anchor_top = 1.0 | ||
anchor_right = 0.5 | ||
anchor_bottom = 1.0 | ||
margin_left = -148.0 | ||
margin_top = -110.0 | ||
margin_right = 148.0 | ||
margin_bottom = -12.0 | ||
custom_styles/panel = SubResource( 4 ) | ||
__meta__ = { | ||
"_edit_group_": true | ||
} | ||
|
||
[node name="VBoxContainer" type="VBoxContainer" parent="Intro"] | ||
margin_top = 12.0 | ||
margin_right = 296.0 | ||
margin_bottom = 86.0 | ||
|
||
[node name="Label1" type="Label" parent="Intro/VBoxContainer"] | ||
margin_right = 296.0 | ||
margin_bottom = 22.0 | ||
custom_fonts/font = SubResource( 5 ) | ||
text = "Left/right module" | ||
align = 1 | ||
|
||
[node name="Label2" type="Label" parent="Intro/VBoxContainer"] | ||
margin_top = 26.0 | ||
margin_right = 296.0 | ||
margin_bottom = 48.0 | ||
custom_fonts/font = SubResource( 5 ) | ||
text = "Up/down resolution" | ||
align = 1 | ||
|
||
[node name="Label3" type="Label" parent="Intro/VBoxContainer"] | ||
margin_top = 52.0 | ||
margin_right = 296.0 | ||
margin_bottom = 74.0 | ||
custom_fonts/font = SubResource( 5 ) | ||
text = "C Toggle FPS limit" | ||
align = 1 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../addons |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
; Engine configuration file. | ||
; It's best edited using the editor UI and not directly, | ||
; since the parameters that go here are not all obvious. | ||
; | ||
; Format: | ||
; [section] ; section goes between [] | ||
; param=value ; assign values to parameters | ||
|
||
config_version=4 | ||
|
||
_global_script_classes=[ { | ||
"base": "", | ||
"class": "Wasm", | ||
"language": "NativeScript", | ||
"path": "res://addons/godot-wasm/Wasm.gdns" | ||
} ] | ||
_global_script_class_icons={ | ||
"Wasm": "" | ||
} | ||
|
||
[application] | ||
|
||
config/name="Wasm Visualizations" | ||
run/main_scene="res://Main.tscn" | ||
|
||
[debug] | ||
|
||
settings/fps/force_fps=60 | ||
|
||
[display] | ||
|
||
window/size/width=400 | ||
window/size/height=400 | ||
window/vsync/use_vsync=false | ||
|
||
[gui] | ||
|
||
common/drop_mouse_on_gui_input_disabled=true | ||
|
||
[input] | ||
|
||
toggle_fps_cap={ | ||
"deadzone": 0.5, | ||
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":67,"physical_scancode":0,"unicode":0,"echo":false,"script":null) | ||
] | ||
} | ||
|
||
[physics] | ||
|
||
common/enable_pause_aware_picking=true | ||
|
||
[rendering] | ||
|
||
environment/default_clear_color=Color( 1, 1, 1, 1 ) |