alex

/

v Public
0 Issues 1 Contributor 0 Releases 4 Branches
Additions: 38 Deletions: 32 View patch
1 import sokol.sapp
2
3 fn main() {
4- gg.new_context(
5+ mut ctx := gg.new_context(
6 bg_color: gx.white
7 window_title: 'Cursor'
8 frame_fn: frame
9 init_fn: init
10- ).run()
11+ )
12+ ctx.run()
13 }
14
15 fn init(mut ctx gg.Context) {
16
1 color gx.Color
2 }
3
4-struct App {
5+pub struct App {
6 pub:
7 args simargs.ParallelArgs
8 request_chan chan &sim.SimRequest
9
1 ft: 0
2 ui_mode: cfg.ui_mode
3 native_rendering: cfg.native_rendering
4- }
5- if cfg.user_data == unsafe { nil } {
6- ctx.user_data = ctx
7+ window: sapp.Desc{
8+ init_userdata_cb: gg_init_sokol_window
9+ frame_userdata_cb: gg_frame_fn
10+ event_userdata_cb: gg_event_fn
11+ fail_userdata_cb: gg_fail_fn
12+ cleanup_userdata_cb: gg_cleanup_fn
13+ window_title: &char(cfg.window_title.str)
14+ html5_canvas_name: &char(cfg.window_title.str)
15+ width: cfg.width
16+ height: cfg.height
17+ sample_count: cfg.sample_count
18+ high_dpi: true
19+ fullscreen: cfg.fullscreen
20+ __v_native_render: cfg.native_rendering
21+ // drag&drop
22+ enable_dragndrop: cfg.enable_dragndrop
23+ max_dropped_files: cfg.max_dropped_files
24+ max_dropped_file_path_length: cfg.max_dropped_file_path_length
25+ swap_interval: cfg.swap_interval
26+ }
27 }
28 ctx.set_bg_color(cfg.bg_color)
29 // C.printf('new_context() %p\n', cfg.user_data)
30- window := sapp.Desc{
31- user_data: ctx
32- init_userdata_cb: gg_init_sokol_window
33- frame_userdata_cb: gg_frame_fn
34- event_userdata_cb: gg_event_fn
35- fail_userdata_cb: gg_fail_fn
36- cleanup_userdata_cb: gg_cleanup_fn
37- window_title: &char(cfg.window_title.str)
38- html5_canvas_name: &char(cfg.window_title.str)
39- width: cfg.width
40- height: cfg.height
41- sample_count: cfg.sample_count
42- high_dpi: true
43- fullscreen: cfg.fullscreen
44- __v_native_render: cfg.native_rendering
45- // drag&drop
46- enable_dragndrop: cfg.enable_dragndrop
47- max_dropped_files: cfg.max_dropped_files
48- max_dropped_file_path_length: cfg.max_dropped_file_path_length
49- swap_interval: cfg.swap_interval
50- }
51- ctx.window = window
52 return ctx
53 }
54
55 // run starts the main loop of the context.
56-pub fn (ctx &Context) run() {
57+pub fn (mut ctx Context) run() {
58+ // set context late, in case it changed (e.g., due to embedding)
59+ ctx.window = sapp.Desc{
60+ ...ctx.window
61+ user_data: ctx
62+ }
63+ if ctx.user_data == unsafe { nil } {
64+ ctx.user_data = ctx
65+ }
66 sapp.run(&ctx.window)
67 }
68
69
1 sz.height = g.height
2 sz.width = g.width
3 g.config = cfg
4- if isnil(cfg.user_data) {
5- g.user_data = g
6- }
7 g.window = dom.window()
8 document := dom.document
9 canvas_elem := document.getElementById(cfg.canvas.str) or {
10 }
11
12 pub fn (mut ctx Context) run() {
13+ // set context late, in case it changed (e.g., due to embedding)
14+ if isnil(ctx.user_data) {
15+ ctx.user_data = ctx
16+ }
17+
18 gg_animation_frame_fn(mut ctx)
19 }
20