# Bunnymark - a simple benchmark # Copyright (C) 2023 Duane Robertson # mark.gd # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . extends Node2D const COLOR_BATCH = 25 var bunny_image = preload('art/bunny2.png') var ang_vels:Array[float] = [ ] var bounds:Rect2 var bunnies:Array[Sprite2D] var fpsl := [ ] var lab:Label var speeds:Array[Vector2] = [ ] var start_pos := Vector2.ZERO var tim:Timer var trail:Node2D # Called when the node enters the scene tree for the first time. func _ready(): randomize() for i in 10: fpsl.push_back(60) trail = Node2D.new() add_child(trail) lab = Label.new() lab.position = Vector2i(100, 100) lab.text = 'Click to add 500 bunnies.\nRight-click to remove 500.' add_child(lab) lab.set('theme_override_fonts/font', preload('res://art/Roboto-Regular.ttf')) lab.set('theme_override_font_sizes/font_size', 40) var sty = StyleBoxFlat.new() sty.bg_color = Color.GRAY sty.border_color = Color.GRAY sty.border_width_top = 20 sty.border_width_bottom = 20 sty.border_width_right = 20 sty.border_width_left = 20 lab.add_theme_stylebox_override('normal', sty) lab.add_theme_color_override('font_color', Color.BLACK) tim = Timer.new() add_child(tim) tim.connect('timeout', _on_Timer_timeout) bounds = Rect2(Vector2i.ZERO, get_window().size).grow(-50) func _on_Timer_timeout() -> void: var fps := 0.0 fpsl.push_back(Engine.get_frames_per_second()) while len(fpsl) > 10: fpsl.pop_front() for i in fpsl: fps += i fps = round(fps / len(fpsl)) lab.set_text("%.0ffps %d" % [fps, len(bunnies)]) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta:float): for i in len(bunnies): var bun:Sprite2D = bunnies[i] var speed:Vector2 = speeds[i] bun.position += speed * delta bun.rotation += ang_vels[i] * delta if not bounds.has_point(bun.position): if bun.position.x > bounds.size.x: speed.x = abs(speed.x) * -1.0 bun.position.x = bounds.end.x elif bun.position.x < bounds.position.x: speed.x *= -1.0 bun.position.x = bounds.position.x if bun.position.y > bounds.size.y: speed.y = abs(speed.y) * -1.0 bun.position.y = bounds.end.y elif bun.position.y < bounds.position.y: speed.y *= -1.0 bun.position.y = bounds.position.y if randf() > 0.5: speed += Vector2(randf() - 0.5, randf() - 0.5) * 100.0 speeds[i] = speed func _unhandled_input(event): if event.is_action_pressed('click'): start_pos = get_global_mouse_position() if not bounds.has_point(start_pos): return if tim.is_stopped(): tim.start() var color:Color for i in 500: var speed = Vector2(randf() - 0.5, randf() - 0.5) speed = speed.normalized() * (randf() * 300.0 + 100.0) # speed = 250 * Vector2.ONE var ang_vel = PI * (randf() - 0.5) # ang_vel = 0.0 var spr = Sprite2D.new() if i % COLOR_BATCH == 0: color = Color(randf() * 0.3 + 0.7, randf() * 0.3 + 0.7, randf() * 0.3 + 0.7) spr.modulate = color spr.position = start_pos spr.rotation = 2.0 * PI * randf() # spr.rotation = 0.0 spr.scale = Vector2.ONE * randf() * 2.0 + Vector2(0.5, 0.5) # spr.scale = Vector2.ONE spr.texture = bunny_image trail.add_child(spr) bunnies.append(spr) speeds.append(speed) ang_vels.append(ang_vel) elif event.is_action_pressed('rclick'): for i in 500: var bun = bunnies.pop_back() speeds.pop_back() ang_vels.pop_back() if not bun: break trail.remove_child(bun) bun.queue_free()