Anonymous user
Vibrating rectangles: Difference between revisions
→{{header|Nim}}
(Added Wren) |
|||
Line 394:
end
</lang>
=={{header|Nim}}==
{{libheader|gintro}}
Should work on any platform where Gtk is available. Tested on Linux.
<lang Nim>import sugar
import gintro/[gobject, gdk, gtk, gio, cairo]
import gintro/glib except Pi
const
Width = 640
Height = 480
X0 = Width div 2 - 1
Y0 = Height div 2 - 1
Colors = [(0.0, 0.0, 0.0), (255.0, 0.0, 0.0), (0.0, 255.0, 0.0), (0.0, 0.0, 255.0),
(255.0, 0.0, 255.0), (0.0, 255.0, 255.0), (255.0, 255.0, 0.0), (255.0, 255.0, 255.0)]
Sizes = collect(newSeq, for size in countdown(Width, 4, 16): float(size))
type
# Description of the simulation.
Simulation = ref object
area: DrawingArea # Drawing area.
isize: Natural # Size index.
icolor: Natural # Color index.
#---------------------------------------------------------------------------------------------------
proc newSimulation(area: DrawingArea): Simulation =
## Allocate and initialize the simulation object.
new(result)
result.area = area
result.isize = 0
#---------------------------------------------------------------------------------------------------
proc draw(sim: Simulation; context: cairo.Context) =
## Draw the rectangles.
context.setSource(Colors[sim.icolor])
let width = Sizes[sim.isize]
let height = width * 3 / 4
context.rectangle(X0 - width * 0.5, Y0 - height * 0.5, width, height)
context.stroke()
#---------------------------------------------------------------------------------------------------
proc update(sim: Simulation): gboolean =
## Update the simulation state.
sim.isize = (sim.isize + 1) mod Sizes.len
if sim.isize == 0:
# Change color for next cycle.
sim.icolor = (sim.icolor + 1) mod Colors.len
sim.draw(sim.area.window.cairoCreate())
result = gboolean(1)
#---------------------------------------------------------------------------------------------------
proc activate(app: Application) =
## Activate the application.
let window = app.newApplicationWindow()
window.setSizeRequest(Width, Height)
window.setTitle("Vibrating rectangles")
let area = newDrawingArea()
window.add(area)
let sim = newSimulation(area)
timeoutAdd(40, update, sim)
window.showAll()
#———————————————————————————————————————————————————————————————————————————————————————————————————
let app = newApplication(Application, "Rosetta.vibrating_rectangles")
discard app.connect("activate", activate)
discard app.run()</lang>
=={{header|Objeck}}==
|