| 1 | @[has_globals] |
| 2 | module sapp |
| 3 | |
| 4 | import sokol.gfx |
| 5 | import sokol.memory |
| 6 | |
| 7 | // Android needs a global reference to `g_desc` |
| 8 | __global g_desc C.sapp_desc |
| 9 | |
| 10 | pub fn create_desc() gfx.Desc { |
| 11 | return gfx.Desc{ |
| 12 | environment: glue_environment() |
| 13 | image_pool_size: 1000 |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | // create_default_pass creates a default `gfx.Pass` compatible with `sapp` and `sokol.gfx.begin_pass/1`. |
| 18 | pub fn create_default_pass(action gfx.PassAction) gfx.Pass { |
| 19 | return gfx.Pass{ |
| 20 | action: action |
| 21 | swapchain: glue_swapchain() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // sapp_to_gfx_pixelformat converts an sapp_pixel_format int to a gfx.PixelFormat. |
| 26 | // The sapp and gfx pixel format enums have different integer values, so a direct |
| 27 | // cast is incorrect. |
| 28 | fn sapp_to_gfx_pixelformat(sapp_fmt int) gfx.PixelFormat { |
| 29 | // sapp_pixel_format: _DEFAULT=0, NONE=1, RGBA8=2, SRGB8A8=3, BGRA8=4, SBGRA8=5, DEPTH=6, DEPTH_STENCIL=7 |
| 30 | return match sapp_fmt { |
| 31 | 1 { gfx.PixelFormat.none } |
| 32 | 2 { gfx.PixelFormat.rgba8 } |
| 33 | 3 { gfx.PixelFormat.srgb8a8 } |
| 34 | 4 { gfx.PixelFormat.bgra8 } |
| 35 | 5 { gfx.PixelFormat.bgra8 } // sbgra8 has no gfx equivalent, use bgra8 |
| 36 | 6 { gfx.PixelFormat.depth } |
| 37 | 7 { gfx.PixelFormat.depth_stencil } |
| 38 | else { gfx.PixelFormat.none } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // glue_environment returns a `gfx.Environment` compatible for use with `sapp` specific `gfx.Pass`es. |
| 43 | // The retuned `gfx.Environment` can be used when rendering via `sapp`. |
| 44 | // See also: documentation at the top of thirdparty/sokol/sokol_gfx.h |
| 45 | pub fn glue_environment() gfx.Environment { |
| 46 | sapp_env := C.sapp_get_environment() |
| 47 | mut env := gfx.Environment{} |
| 48 | unsafe { vmemset(&env, 0, int(sizeof(env))) } |
| 49 | env.defaults.color_format = sapp_to_gfx_pixelformat(sapp_env.defaults.color_format) |
| 50 | env.defaults.depth_format = sapp_to_gfx_pixelformat(sapp_env.defaults.depth_format) |
| 51 | env.defaults.sample_count = sapp_env.defaults.sample_count |
| 52 | $if macos && !darwin_sokol_glcore33 ? { |
| 53 | env.metal.device = sapp_env.metal.device |
| 54 | } |
| 55 | return env |
| 56 | } |
| 57 | |
| 58 | // glue_swapchain returns a `gfx.Swapchain` compatible for use with `sapp` specific display/rendering `gfx.Pass`es. |
| 59 | // The retuned `gfx.Swapchain` can be used when rendering via `sapp`. |
| 60 | // See also: documentation at the top of thirdparty/sokol/sokol_gfx.h |
| 61 | pub fn glue_swapchain() gfx.Swapchain { |
| 62 | sapp_sc := C.sapp_get_swapchain() |
| 63 | mut swapchain := gfx.Swapchain{} |
| 64 | unsafe { vmemset(&swapchain, 0, int(sizeof(swapchain))) } |
| 65 | swapchain.width = sapp_sc.width |
| 66 | swapchain.height = sapp_sc.height |
| 67 | swapchain.sample_count = sapp_sc.sample_count |
| 68 | swapchain.color_format = sapp_to_gfx_pixelformat(sapp_sc.color_format) |
| 69 | swapchain.depth_format = sapp_to_gfx_pixelformat(sapp_sc.depth_format) |
| 70 | $if macos && !darwin_sokol_glcore33 ? { |
| 71 | swapchain.metal.current_drawable = sapp_sc.metal.current_drawable |
| 72 | swapchain.metal.depth_stencil_texture = sapp_sc.metal.depth_stencil_texture |
| 73 | swapchain.metal.msaa_color_texture = sapp_sc.metal.msaa_color_texture |
| 74 | } $else { |
| 75 | swapchain.gl.framebuffer = sapp_sc.gl.framebuffer |
| 76 | } |
| 77 | return swapchain |
| 78 | } |
| 79 | |
| 80 | // returns true after sokol-app has been initialized |
| 81 | @[inline] |
| 82 | pub fn isvalid() bool { |
| 83 | return C.sapp_isvalid() |
| 84 | } |
| 85 | |
| 86 | // returns the current framebuffer width in pixels |
| 87 | @[inline] |
| 88 | pub fn width() int { |
| 89 | return C.sapp_width() |
| 90 | } |
| 91 | |
| 92 | // returns the current framebuffer height in pixels |
| 93 | @[inline] |
| 94 | pub fn height() int { |
| 95 | return C.sapp_height() |
| 96 | } |
| 97 | |
| 98 | // color_format gets default framebuffer color pixel format |
| 99 | @[inline] |
| 100 | pub fn color_format() int { |
| 101 | return C.sapp_color_format() |
| 102 | } |
| 103 | |
| 104 | // depth_format gets default framebuffer depth pixel format |
| 105 | @[inline] |
| 106 | pub fn depth_format() int { |
| 107 | return C.sapp_depth_format() |
| 108 | } |
| 109 | |
| 110 | // sample_count gets default framebuffer sample count |
| 111 | @[inline] |
| 112 | pub fn sample_count() int { |
| 113 | return C.sapp_sample_count() |
| 114 | } |
| 115 | |
| 116 | // returns true when high_dpi was requested and actually running in a high-dpi scenario |
| 117 | @[inline] |
| 118 | pub fn high_dpi() bool { |
| 119 | return C.sapp_high_dpi() |
| 120 | } |
| 121 | |
| 122 | // returns the dpi scaling factor (window pixels to framebuffer pixels) |
| 123 | @[inline] |
| 124 | pub fn dpi_scale() f32 { |
| 125 | return C.sapp_dpi_scale() |
| 126 | } |
| 127 | |
| 128 | // show or hide the mobile device onscreen keyboard |
| 129 | @[inline] |
| 130 | pub fn show_keyboard(visible bool) { |
| 131 | C.sapp_show_keyboard(visible) |
| 132 | } |
| 133 | |
| 134 | // return true if the mobile device onscreen keyboard is currently shown |
| 135 | @[inline] |
| 136 | pub fn keyboard_shown() bool { |
| 137 | return C.sapp_keyboard_shown() |
| 138 | } |
| 139 | |
| 140 | // show or hide the mouse cursor |
| 141 | @[inline] |
| 142 | pub fn show_mouse(visible bool) { |
| 143 | C.sapp_show_mouse(visible) |
| 144 | } |
| 145 | |
| 146 | // set mouse cursor |
| 147 | @[inline] |
| 148 | pub fn set_mouse_cursor(cursor MouseCursor) { |
| 149 | C.sapp_set_mouse_cursor(cursor) |
| 150 | } |
| 151 | |
| 152 | // show or hide the mouse cursor |
| 153 | @[inline] |
| 154 | pub fn mouse_shown() bool { |
| 155 | return C.sapp_mouse_shown() |
| 156 | } |
| 157 | |
| 158 | @[inline] |
| 159 | pub fn lock_mouse(locked bool) { |
| 160 | C.sapp_lock_mouse(locked) |
| 161 | } |
| 162 | |
| 163 | @[inline] |
| 164 | pub fn mouse_locked() bool { |
| 165 | return C.sapp_mouse_locked() |
| 166 | } |
| 167 | |
| 168 | // return the userdata pointer optionally provided in sapp_desc |
| 169 | @[inline] |
| 170 | pub fn userdata() voidptr { |
| 171 | return C.sapp_userdata() |
| 172 | } |
| 173 | |
| 174 | // return a copy of the sapp_desc structure |
| 175 | @[inline] |
| 176 | pub fn query_desc() Desc { |
| 177 | return C.sapp_query_desc() |
| 178 | } |
| 179 | |
| 180 | // initiate a "soft quit" (sends SAPP_EVENTTYPE_QUIT_REQUESTED) |
| 181 | @[inline] |
| 182 | pub fn request_quit() { |
| 183 | C.sapp_request_quit() |
| 184 | } |
| 185 | |
| 186 | // cancel a pending quit (when SAPP_EVENTTYPE_QUIT_REQUESTED has been received) |
| 187 | @[inline] |
| 188 | pub fn cancel_quit() { |
| 189 | C.sapp_cancel_quit() |
| 190 | } |
| 191 | |
| 192 | // initiate a "hard quit" (quit application without sending SAPP_EVENTTYPE_QUIT_REQUESTED) |
| 193 | @[inline] |
| 194 | pub fn quit() { |
| 195 | C.sapp_quit() |
| 196 | } |
| 197 | |
| 198 | // call from inside event callback to consume the current event (don't forward to platform) |
| 199 | @[inline] |
| 200 | pub fn consume_event() { |
| 201 | C.sapp_consume_event() |
| 202 | } |
| 203 | |
| 204 | // get the current frame counter (for comparison with sapp_event.frame_count) |
| 205 | @[inline] |
| 206 | pub fn frame_count() u64 { |
| 207 | return C.sapp_frame_count() |
| 208 | } |
| 209 | |
| 210 | // get an averaged/smoothed frame duration in seconds |
| 211 | @[inline] |
| 212 | pub fn frame_duration() f64 { |
| 213 | return C.sapp_frame_duration() |
| 214 | } |
| 215 | |
| 216 | // write string into clipboard |
| 217 | @[inline] |
| 218 | pub fn set_clipboard_string(str &u8) { |
| 219 | C.sapp_set_clipboard_string(&char(str)) |
| 220 | } |
| 221 | |
| 222 | // read string from clipboard (usually during SAPP_EVENTTYPE_CLIPBOARD_PASTED) |
| 223 | @[inline] |
| 224 | pub fn get_clipboard_string() &char { |
| 225 | return &char(C.sapp_get_clipboard_string()) |
| 226 | } |
| 227 | |
| 228 | // special run-function for SOKOL_NO_ENTRY (in standard mode this is an empty stub) |
| 229 | pub fn run(desc &Desc) { |
| 230 | if desc.allocator.alloc_fn == unsafe { nil } && desc.allocator.free_fn == unsafe { nil } { |
| 231 | unsafe { |
| 232 | desc.allocator.alloc_fn = memory.salloc |
| 233 | desc.allocator.free_fn = memory.sfree |
| 234 | desc.allocator.user_data = voidptr(0x10005a44) |
| 235 | } |
| 236 | } |
| 237 | if desc.logger.func == unsafe { nil } { |
| 238 | unsafe { |
| 239 | desc.logger.func = memory.slog |
| 240 | } |
| 241 | } |
| 242 | g_desc = *desc |
| 243 | C.sapp_run(desc) |
| 244 | } |
| 245 | |
| 246 | // HTML5: enable or disable the hardwired "Leave Site?" dialog box |
| 247 | @[inline] |
| 248 | pub fn html5_ask_leave_site(ask bool) { |
| 249 | C.sapp_html5_ask_leave_site(ask) |
| 250 | } |
| 251 | |
| 252 | // macOS: get ARC-bridged pointer to macOS NSWindow |
| 253 | @[inline] |
| 254 | pub fn macos_get_window() voidptr { |
| 255 | return voidptr(C.sapp_macos_get_window()) |
| 256 | } |
| 257 | |
| 258 | // iOS: get ARC-bridged pointer to iOS UIWindow |
| 259 | @[inline] |
| 260 | pub fn ios_get_window() voidptr { |
| 261 | return voidptr(C.sapp_ios_get_window()) |
| 262 | } |
| 263 | |
| 264 | // Win32: get the HWND window handle |
| 265 | @[inline] |
| 266 | pub fn win32_get_hwnd() voidptr { |
| 267 | return voidptr(C.sapp_win32_get_hwnd()) |
| 268 | } |
| 269 | |
| 270 | // GL: get framebuffer object |
| 271 | @[inline] |
| 272 | pub fn gl_get_framebuffer() u32 { |
| 273 | return C.sapp_gl_get_framebuffer() |
| 274 | } |
| 275 | |
| 276 | // X11: get display handle |
| 277 | @[inline] |
| 278 | pub fn x11_get_display() voidptr { |
| 279 | return voidptr(C.sapp_x11_get_display()) |
| 280 | } |
| 281 | |
| 282 | // X11: get window handle |
| 283 | @[inline] |
| 284 | pub fn x11_get_window() voidptr { |
| 285 | return voidptr(C.sapp_x11_get_window()) |
| 286 | } |
| 287 | |
| 288 | // Android: get native activity handle |
| 289 | @[inline] |
| 290 | pub fn android_get_native_activity() voidptr { |
| 291 | return voidptr(C.sapp_android_get_native_activity()) |
| 292 | } |
| 293 | |
| 294 | // Toggle full screen |
| 295 | @[inline] |
| 296 | pub fn toggle_fullscreen() { |
| 297 | C.sapp_toggle_fullscreen() |
| 298 | } |
| 299 | |
| 300 | // Check if full screen rendering |
| 301 | @[inline] |
| 302 | pub fn is_fullscreen() bool { |
| 303 | return C.sapp_is_fullscreen() |
| 304 | } |
| 305 | |
| 306 | @[inline] |
| 307 | pub fn get_num_dropped_files() int { |
| 308 | return C.sapp_get_num_dropped_files() |
| 309 | } |
| 310 | |
| 311 | @[inline] |
| 312 | pub fn get_dropped_file_path(index int) string { |
| 313 | unsafe { |
| 314 | return cstring_to_vstring(C.sapp_get_dropped_file_path(index)) |
| 315 | } |
| 316 | } |
| 317 | |