|
| 1 | +const c = @cImport({ |
| 2 | + @cInclude("SDL3/SDL.h"); |
| 3 | +}); |
1 | 4 |
|
| 5 | +const std = @import("std"); |
| 6 | +const main_app = @import("../../../app.zig"); |
| 7 | +const debug = @import("../../../debug.zig"); |
| 8 | +// const gfx = @import("graphics.zig"); |
| 9 | +const input = @import("../../input.zig"); |
| 10 | +// const modules = @import("../modules.zig"); |
| 11 | + |
| 12 | +const sokol = @import("sokol"); |
| 13 | +const slog = sokol.log; |
| 14 | +const sg = sokol.gfx; |
| 15 | +const sapp = sokol.app; |
| 16 | +const sglue = sokol.glue; |
| 17 | +const simgui = sokol.imgui; |
| 18 | + |
| 19 | +var app_config: main_app.AppConfig = undefined; |
| 20 | + |
| 21 | +pub const SokolAppConfig = struct { |
| 22 | + on_init_fn: *const fn () void, |
| 23 | + on_frame_fn: *const fn () void, |
| 24 | + on_cleanup_fn: *const fn () void, |
| 25 | + // maybe an on_event fn? |
| 26 | +}; |
| 27 | + |
| 28 | +// keep a static version of the app around |
| 29 | +var app: App = undefined; |
| 30 | + |
| 31 | +pub const App = struct { |
| 32 | + on_init_fn: *const fn () void, |
| 33 | + on_frame_fn: *const fn () void, |
| 34 | + on_cleanup_fn: *const fn () void, |
| 35 | + |
| 36 | + pub fn init(cfg: SokolAppConfig) void { |
| 37 | + debug.log("Creating SDL App backend", .{}); |
| 38 | + |
| 39 | + app = App{ |
| 40 | + .on_init_fn = cfg.on_init_fn, |
| 41 | + .on_frame_fn = cfg.on_frame_fn, |
| 42 | + .on_cleanup_fn = cfg.on_cleanup_fn, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + pub fn deinit() void { |
| 47 | + debug.log("SDL App Backend stopping", .{}); |
| 48 | + } |
| 49 | + |
| 50 | + export fn sokol_init() void { |
| 51 | + debug.log("Sokol app context initializing", .{}); |
| 52 | + |
| 53 | + // TODO: Put the buffer pool size and the shader pool size into a config |
| 54 | + sg.setup(.{ |
| 55 | + .environment = sglue.environment(), |
| 56 | + .logger = .{ .func = slog.func }, |
| 57 | + .buffer_pool_size = app_config.buffer_pool_size, // sokol default is 128 |
| 58 | + .shader_pool_size = app_config.shader_pool_size, // sokol default is 64 |
| 59 | + .image_pool_size = app_config.image_pool_size, // sokol default is 128 |
| 60 | + .pipeline_pool_size = app_config.pipeline_pool_size, // sokol default is 64 |
| 61 | + .sampler_pool_size = app_config.sampler_pool_size, // sokol default is 64 |
| 62 | + .attachments_pool_size = app_config.pass_pool_size, // sokol default is 16, |
| 63 | + }); |
| 64 | + |
| 65 | + simgui.setup(.{ |
| 66 | + .logger = .{ .func = slog.func }, |
| 67 | + }); |
| 68 | + |
| 69 | + debug.log("Sokol setup backend: {}", .{sg.queryBackend()}); |
| 70 | + |
| 71 | + // call the callback that will tell everything else to start up |
| 72 | + app.on_init_fn(); |
| 73 | + } |
| 74 | + |
| 75 | + export fn sokol_cleanup() void { |
| 76 | + app.on_cleanup_fn(); |
| 77 | + sg.shutdown(); |
| 78 | + } |
| 79 | + |
| 80 | + export fn sokol_frame() void { |
| 81 | + app.on_frame_fn(); |
| 82 | + } |
| 83 | + |
| 84 | + export fn sokol_input(event: ?*const sapp.Event) void { |
| 85 | + const ev = event.?; |
| 86 | + |
| 87 | + const imgui_did_handle = simgui.handleEvent(ev.*); |
| 88 | + if (imgui_did_handle) |
| 89 | + return; |
| 90 | + |
| 91 | + if (ev.type == .MOUSE_DOWN) { |
| 92 | + input.onMouseDown(@intFromEnum(ev.mouse_button)); |
| 93 | + } else if (ev.type == .MOUSE_UP) { |
| 94 | + input.onMouseUp(@intFromEnum(ev.mouse_button)); |
| 95 | + } else if (ev.type == .MOUSE_MOVE) { |
| 96 | + input.onMouseMoved(ev.mouse_x, ev.mouse_y, ev.mouse_dx, ev.mouse_dy); |
| 97 | + } else if (ev.type == .KEY_DOWN) { |
| 98 | + if (!ev.key_repeat) |
| 99 | + input.onKeyDown(@intFromEnum(ev.key_code)); |
| 100 | + } else if (ev.type == .KEY_UP) { |
| 101 | + input.onKeyUp(@intFromEnum(ev.key_code)); |
| 102 | + } else if (ev.type == .CHAR) { |
| 103 | + input.onKeyChar(ev.char_code); |
| 104 | + } else if (ev.type == .TOUCHES_BEGAN) { |
| 105 | + for (ev.touches) |touch| { |
| 106 | + if (touch.changed) |
| 107 | + input.onTouchBegin(touch.pos_x, touch.pos_y, touch.identifier); |
| 108 | + } |
| 109 | + } else if (ev.type == .TOUCHES_MOVED) { |
| 110 | + for (ev.touches) |touch| { |
| 111 | + if (touch.changed) |
| 112 | + input.onTouchMoved(touch.pos_x, touch.pos_y, touch.identifier); |
| 113 | + } |
| 114 | + } else if (ev.type == .TOUCHES_ENDED) { |
| 115 | + for (ev.touches) |touch| { |
| 116 | + if (touch.changed) |
| 117 | + input.onTouchEnded(touch.pos_x, touch.pos_y, touch.identifier); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + pub fn startMainLoop(config: main_app.AppConfig) void { |
| 123 | + app_config = config; |
| 124 | + |
| 125 | + debug.log("Sokol app starting main loop", .{}); |
| 126 | + |
| 127 | + sapp.run(.{ |
| 128 | + .init_cb = sokol_init, |
| 129 | + .frame_cb = sokol_frame, |
| 130 | + .cleanup_cb = sokol_cleanup, |
| 131 | + .event_cb = sokol_input, |
| 132 | + .width = config.width, |
| 133 | + .height = config.height, |
| 134 | + .icon = .{ |
| 135 | + .sokol_default = true, |
| 136 | + }, |
| 137 | + .window_title = config.title, |
| 138 | + .logger = .{ |
| 139 | + .func = slog.func, |
| 140 | + }, |
| 141 | + // .win32_console_attach = true, |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + pub fn getWidth() i32 { |
| 146 | + return sapp.width(); |
| 147 | + } |
| 148 | + |
| 149 | + pub fn getHeight() i32 { |
| 150 | + return sapp.height(); |
| 151 | + } |
| 152 | + |
| 153 | + pub fn captureMouse(captured: bool) void { |
| 154 | + sapp.lockMouse(captured); |
| 155 | + } |
| 156 | + |
| 157 | + pub fn startImguiFrame() void { |
| 158 | + simgui.newFrame(.{ |
| 159 | + .width = sapp.width(), |
| 160 | + .height = sapp.height(), |
| 161 | + .delta_time = sapp.frameDuration(), |
| 162 | + .dpi_scale = sapp.dpiScale(), |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + pub fn renderImgui() void { |
| 167 | + simgui.render(); |
| 168 | + } |
| 169 | + |
| 170 | + pub fn exit() void { |
| 171 | + sapp.quit(); |
| 172 | + } |
| 173 | +}; |
0 commit comments