| 1 | module anim |
| 2 | |
| 3 | import gg |
| 4 | import sim |
| 5 | import sim.args as simargs |
| 6 | |
| 7 | const bg_color = gg.white |
| 8 | |
| 9 | struct Pixel { |
| 10 | x f32 |
| 11 | y f32 |
| 12 | color gg.Color |
| 13 | } |
| 14 | |
| 15 | pub struct App { |
| 16 | pub: |
| 17 | args simargs.ParallelArgs |
| 18 | request_chan chan &sim.SimRequest |
| 19 | result_chan chan &sim.SimResult |
| 20 | pub mut: |
| 21 | gg &gg.Context = unsafe { nil } |
| 22 | iidx int |
| 23 | pixels []u32 |
| 24 | } |
| 25 | |
| 26 | pub fn new_app(args simargs.ParallelArgs) &App { |
| 27 | total_pixels := args.grid.height * args.grid.width |
| 28 | |
| 29 | mut app := &App{ |
| 30 | args: args |
| 31 | pixels: []u32{len: total_pixels} |
| 32 | request_chan: chan &sim.SimRequest{cap: args.grid.width} |
| 33 | } |
| 34 | app.gg = gg.new_context( |
| 35 | width: args.grid.width |
| 36 | height: args.grid.height |
| 37 | create_window: true |
| 38 | window_title: 'V Pendulum Simulation' |
| 39 | user_data: app |
| 40 | bg_color: bg_color |
| 41 | frame_fn: frame |
| 42 | init_fn: init |
| 43 | ) |
| 44 | return app |
| 45 | } |
| 46 | |
| 47 | fn init(mut app App) { |
| 48 | app.iidx = app.gg.new_streaming_image(app.args.grid.width, app.args.grid.height, 4, |
| 49 | pixel_format: .rgba8 |
| 50 | ) |
| 51 | spawn pixels_worker(mut app) |
| 52 | } |
| 53 | |
| 54 | fn frame(mut app App) { |
| 55 | app.gg.begin() |
| 56 | app.draw() |
| 57 | app.gg.end() |
| 58 | } |
| 59 | |
| 60 | fn (mut app App) draw() { |
| 61 | mut istream_image := app.gg.get_cached_image_by_idx(app.iidx) |
| 62 | istream_image.update_pixel_data(unsafe { &u8(&app.pixels[0]) }) |
| 63 | app.gg.draw_image(0, 0, app.args.grid.width, app.args.grid.height, istream_image) |
| 64 | } |
| 65 | |