Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,64 @@
|
||||
module anim
|
||||
|
||||
import gg
|
||||
import sim
|
||||
import sim.args as simargs
|
||||
|
||||
const bg_color = gg.white
|
||||
|
||||
struct Pixel {
|
||||
x f32
|
||||
y f32
|
||||
color gg.Color
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
pub:
|
||||
args simargs.ParallelArgs
|
||||
request_chan chan &sim.SimRequest
|
||||
result_chan chan &sim.SimResult
|
||||
pub mut:
|
||||
gg &gg.Context = unsafe { nil }
|
||||
iidx int
|
||||
pixels []u32
|
||||
}
|
||||
|
||||
pub fn new_app(args simargs.ParallelArgs) &App {
|
||||
total_pixels := args.grid.height * args.grid.width
|
||||
|
||||
mut app := &App{
|
||||
args: args
|
||||
pixels: []u32{len: total_pixels}
|
||||
request_chan: chan &sim.SimRequest{cap: args.grid.width}
|
||||
}
|
||||
app.gg = gg.new_context(
|
||||
width: args.grid.width
|
||||
height: args.grid.height
|
||||
create_window: true
|
||||
window_title: 'V Pendulum Simulation'
|
||||
user_data: app
|
||||
bg_color: bg_color
|
||||
frame_fn: frame
|
||||
init_fn: init
|
||||
)
|
||||
return app
|
||||
}
|
||||
|
||||
fn init(mut app App) {
|
||||
app.iidx = app.gg.new_streaming_image(app.args.grid.width, app.args.grid.height, 4,
|
||||
pixel_format: .rgba8
|
||||
)
|
||||
spawn pixels_worker(mut app)
|
||||
}
|
||||
|
||||
fn frame(mut app App) {
|
||||
app.gg.begin()
|
||||
app.draw()
|
||||
app.gg.end()
|
||||
}
|
||||
|
||||
fn (mut app App) draw() {
|
||||
mut istream_image := app.gg.get_cached_image_by_idx(app.iidx)
|
||||
istream_image.update_pixel_data(unsafe { &u8(&app.pixels[0]) })
|
||||
app.gg.draw_image(0, 0, app.args.grid.width, app.args.grid.height, istream_image)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
module anim
|
||||
|
||||
import benchmark
|
||||
import sim.img
|
||||
|
||||
fn pixels_worker(mut app App) {
|
||||
mut bmark := benchmark.new_benchmark()
|
||||
for {
|
||||
result := <-app.result_chan or { break }
|
||||
bmark.step()
|
||||
// find the closest magnet
|
||||
pixel_color := img.compute_pixel(result)
|
||||
app.pixels[result.id] = u32(pixel_color.abgr8())
|
||||
bmark.ok()
|
||||
}
|
||||
bmark.stop()
|
||||
println(bmark.total_message(@FN))
|
||||
}
|
||||
Reference in new issue
Block a user