| 1 | // Copyright(C) 2019 Lars Pontoppidan. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license file distributed with this software package |
| 3 | module main |
| 4 | |
| 5 | import time |
| 6 | import sokol.sapp |
| 7 | import sokol.gfx |
| 8 | import sokol.sgl |
| 9 | import particle |
| 10 | |
| 11 | fn main() { |
| 12 | mut app := &App{ |
| 13 | width: 800 |
| 14 | height: 400 |
| 15 | pass_action: gfx.create_clear_pass_action(0.1, 0.1, 0.1, 1.0) |
| 16 | } |
| 17 | app.init() |
| 18 | app.run() |
| 19 | } |
| 20 | |
| 21 | struct App { |
| 22 | pass_action gfx.PassAction |
| 23 | mut: |
| 24 | width int |
| 25 | height int |
| 26 | frame i64 |
| 27 | last i64 |
| 28 | ps particle.System |
| 29 | alpha_pip sgl.Pipeline |
| 30 | } |
| 31 | |
| 32 | fn (mut a App) init() { |
| 33 | a.frame = 0 |
| 34 | a.last = time.ticks() |
| 35 | a.ps = particle.System{ |
| 36 | width: a.width |
| 37 | height: a.height |
| 38 | } |
| 39 | a.ps.init(particle.SystemConfig{ |
| 40 | pool: 20000 |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | fn (mut a App) cleanup() { |
| 45 | unsafe { |
| 46 | a.ps.free() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn (mut a App) run() { |
| 51 | title := 'V Particle Example' |
| 52 | desc := sapp.Desc{ |
| 53 | width: a.width |
| 54 | height: a.height |
| 55 | user_data: a |
| 56 | init_userdata_cb: init |
| 57 | frame_userdata_cb: frame |
| 58 | event_userdata_cb: event |
| 59 | window_title: &char(title.str) |
| 60 | cleanup_userdata_cb: cleanup |
| 61 | } |
| 62 | sapp.run(&desc) |
| 63 | } |
| 64 | |
| 65 | fn (a App) draw() { |
| 66 | sgl.load_pipeline(a.alpha_pip) |
| 67 | a.ps.draw() |
| 68 | } |
| 69 | |
| 70 | fn init(mut app App) { |
| 71 | desc := sapp.create_desc() |
| 72 | gfx.setup(&desc) |
| 73 | sgl_desc := sgl.Desc{ |
| 74 | max_vertices: 50 * 65536 |
| 75 | } |
| 76 | sgl.setup(&sgl_desc) |
| 77 | mut pipdesc := gfx.PipelineDesc{} |
| 78 | unsafe { vmemset(&pipdesc, 0, int(sizeof(pipdesc))) } |
| 79 | |
| 80 | color_state := gfx.ColorTargetState{ |
| 81 | blend: gfx.BlendState{ |
| 82 | enabled: true |
| 83 | src_factor_rgb: .src_alpha |
| 84 | dst_factor_rgb: .one_minus_src_alpha |
| 85 | } |
| 86 | } |
| 87 | pipdesc.colors[0] = color_state |
| 88 | |
| 89 | app.alpha_pip = sgl.make_pipeline(&pipdesc) |
| 90 | } |
| 91 | |
| 92 | fn cleanup(mut app App) { |
| 93 | app.cleanup() |
| 94 | gfx.shutdown() |
| 95 | } |
| 96 | |
| 97 | fn frame(mut app App) { |
| 98 | app.width = sapp.width() |
| 99 | app.height = sapp.height() |
| 100 | t := time.ticks() |
| 101 | dt := f64(t - app.last) / 1000.0 |
| 102 | app.ps.update(dt) |
| 103 | draw(app) |
| 104 | pass := sapp.create_default_pass(app.pass_action) |
| 105 | gfx.begin_pass(&pass) |
| 106 | sgl.default_pipeline() |
| 107 | sgl.draw() |
| 108 | gfx.end_pass() |
| 109 | gfx.commit() |
| 110 | app.frame++ |
| 111 | app.last = t |
| 112 | } |
| 113 | |
| 114 | fn event(ev &sapp.Event, mut app App) { |
| 115 | if ev.type == .mouse_move { |
| 116 | app.ps.explode(ev.mouse_x, ev.mouse_y) |
| 117 | } |
| 118 | if ev.type == .mouse_up || ev.type == .mouse_down { |
| 119 | if ev.mouse_button == .left { |
| 120 | is_pressed := ev.type == .mouse_down |
| 121 | if is_pressed { |
| 122 | app.ps.explode(ev.mouse_x, ev.mouse_y) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | if ev.type == .key_up || ev.type == .key_down { |
| 127 | if ev.key_code == .r { |
| 128 | is_pressed := ev.type == .key_down |
| 129 | if is_pressed { |
| 130 | app.ps.reset() |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | if ev.type == .touches_began || ev.type == .touches_moved { |
| 135 | if ev.num_touches > 0 { |
| 136 | touch_point := ev.touches[0] |
| 137 | app.ps.explode(touch_point.pos_x, touch_point.pos_y) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | fn draw(a &App) { |
| 143 | sgl.defaults() |
| 144 | sgl.matrix_mode_projection() |
| 145 | sgl.ortho(0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0) |
| 146 | sgl.push_matrix() |
| 147 | a.draw() |
| 148 | sgl.pop_matrix() |
| 149 | } |
| 150 | |