| 1 | module sgl |
| 2 | |
| 3 | import sokol.gfx |
| 4 | import sokol.memory |
| 5 | |
| 6 | pub const version = gfx.version + 1 |
| 7 | |
| 8 | pub const context = Context{0x00010001} // C.SGL_DEFAULT_CONTEXT = { 0x00010001 } |
| 9 | |
| 10 | // setup/shutdown/misc |
| 11 | pub fn setup(desc &Desc) { |
| 12 | if desc.allocator.alloc_fn == unsafe { nil } && desc.allocator.free_fn == unsafe { nil } { |
| 13 | unsafe { |
| 14 | desc.allocator.alloc_fn = memory.salloc |
| 15 | desc.allocator.free_fn = memory.sfree |
| 16 | desc.allocator.user_data = voidptr(0x10000561) |
| 17 | } |
| 18 | } |
| 19 | if desc.logger.func == unsafe { nil } { |
| 20 | unsafe { |
| 21 | desc.logger.func = memory.slog |
| 22 | } |
| 23 | } |
| 24 | C.sgl_setup(desc) |
| 25 | } |
| 26 | |
| 27 | pub fn shutdown() { |
| 28 | C.sgl_shutdown() |
| 29 | } |
| 30 | |
| 31 | @[inline] |
| 32 | pub fn error() SglError { |
| 33 | return unsafe { SglError(int(C.sgl_error())) } |
| 34 | } |
| 35 | |
| 36 | @[inline] |
| 37 | pub fn context_error(ctx Context) SglError { |
| 38 | return unsafe { SglError(int(C.sgl_context_error(ctx))) } |
| 39 | } |
| 40 | |
| 41 | @[inline] |
| 42 | pub fn rad(deg f32) f32 { |
| 43 | return C.sgl_rad(deg) |
| 44 | } |
| 45 | |
| 46 | @[inline] |
| 47 | pub fn deg(rad f32) f32 { |
| 48 | return C.sgl_deg(rad) |
| 49 | } |
| 50 | |
| 51 | // context functions |
| 52 | @[inline] |
| 53 | pub fn make_context(desc &ContextDesc) Context { |
| 54 | return C.sgl_make_context(desc) |
| 55 | } |
| 56 | |
| 57 | @[inline] |
| 58 | pub fn destroy_context(ctx Context) { |
| 59 | C.sgl_destroy_context(ctx) |
| 60 | } |
| 61 | |
| 62 | @[inline] |
| 63 | pub fn set_context(ctx Context) { |
| 64 | C.sgl_set_context(ctx) |
| 65 | } |
| 66 | |
| 67 | @[inline] |
| 68 | pub fn get_context() Context { |
| 69 | return C.sgl_get_context() |
| 70 | } |
| 71 | |
| 72 | @[inline] |
| 73 | pub fn default_context() Context { |
| 74 | return C.sgl_default_context() |
| 75 | } |
| 76 | |
| 77 | // create and destroy pipeline objects |
| 78 | @[inline] |
| 79 | pub fn make_pipeline(desc &gfx.PipelineDesc) Pipeline { |
| 80 | return C.sgl_make_pipeline(desc) |
| 81 | } |
| 82 | |
| 83 | @[inline] |
| 84 | pub fn context_make_pipeline(ctx Context, desc &gfx.PipelineDesc) Pipeline { |
| 85 | return C.sgl_context_make_pipeline(ctx, desc) |
| 86 | } |
| 87 | |
| 88 | @[inline] |
| 89 | pub fn destroy_pipeline(pip Pipeline) { |
| 90 | C.sgl_destroy_pipeline(pip) |
| 91 | } |
| 92 | |
| 93 | // render state functions |
| 94 | @[inline] |
| 95 | pub fn defaults() { |
| 96 | C.sgl_defaults() |
| 97 | } |
| 98 | |
| 99 | @[inline] |
| 100 | pub fn viewport(x int, y int, w int, h int, origin_top_left bool) { |
| 101 | C.sgl_viewport(x, y, w, h, origin_top_left) |
| 102 | } |
| 103 | |
| 104 | @[inline] |
| 105 | pub fn scissor_rect(x int, y int, w int, h int, origin_top_left bool) { |
| 106 | C.sgl_scissor_rect(x, y, w, h, origin_top_left) |
| 107 | } |
| 108 | |
| 109 | @[inline] |
| 110 | pub fn scissor_rectf(x f32, y f32, w f32, h f32, origin_top_left bool) { |
| 111 | C.sgl_scissor_rectf(x, y, w, h, origin_top_left) |
| 112 | } |
| 113 | |
| 114 | @[inline] |
| 115 | pub fn enable_texture() { |
| 116 | C.sgl_enable_texture() |
| 117 | } |
| 118 | |
| 119 | @[inline] |
| 120 | pub fn disable_texture() { |
| 121 | C.sgl_disable_texture() |
| 122 | } |
| 123 | |
| 124 | @[inline] |
| 125 | pub fn texture(img gfx.Image, smp gfx.Sampler) { |
| 126 | C.sgl_texture(img, smp) |
| 127 | } |
| 128 | |
| 129 | // pipeline stack functions |
| 130 | @[inline] |
| 131 | pub fn load_default_pipeline() { |
| 132 | C.sgl_load_default_pipeline() |
| 133 | } |
| 134 | |
| 135 | @[inline] |
| 136 | pub fn default_pipeline() { |
| 137 | C.sgl_load_default_pipeline() |
| 138 | } |
| 139 | |
| 140 | @[inline] |
| 141 | pub fn load_pipeline(pip Pipeline) { |
| 142 | C.sgl_load_pipeline(pip) |
| 143 | } |
| 144 | |
| 145 | @[inline] |
| 146 | pub fn push_pipeline() { |
| 147 | C.sgl_push_pipeline() |
| 148 | } |
| 149 | |
| 150 | @[inline] |
| 151 | pub fn pop_pipeline() { |
| 152 | C.sgl_pop_pipeline() |
| 153 | } |
| 154 | |
| 155 | // matrix stack functions |
| 156 | @[inline] |
| 157 | pub fn matrix_mode_modelview() { |
| 158 | C.sgl_matrix_mode_modelview() |
| 159 | } |
| 160 | |
| 161 | @[inline] |
| 162 | pub fn matrix_mode_projection() { |
| 163 | C.sgl_matrix_mode_projection() |
| 164 | } |
| 165 | |
| 166 | @[inline] |
| 167 | pub fn matrix_mode_texture() { |
| 168 | C.sgl_matrix_mode_texture() |
| 169 | } |
| 170 | |
| 171 | @[inline] |
| 172 | pub fn load_identity() { |
| 173 | C.sgl_load_identity() |
| 174 | } |
| 175 | |
| 176 | @[inline] |
| 177 | pub fn load_matrix(m []f32) { |
| 178 | C.sgl_load_matrix(m.data) |
| 179 | } |
| 180 | |
| 181 | @[inline] |
| 182 | pub fn load_transpose_matrix(m []f32) { |
| 183 | C.sgl_load_transpose_matrix(m.data) |
| 184 | } |
| 185 | |
| 186 | @[inline] |
| 187 | pub fn mult_matrix(m []f32) { |
| 188 | C.sgl_mult_matrix(m.data) |
| 189 | } |
| 190 | |
| 191 | @[inline] |
| 192 | pub fn mult_transpose_matrix(m []f32) { |
| 193 | C.sgl_mult_transpose_matrix(m.data) |
| 194 | } |
| 195 | |
| 196 | @[inline] |
| 197 | pub fn rotate(angle_rad f32, x f32, y f32, z f32) { |
| 198 | C.sgl_rotate(angle_rad, x, y, z) |
| 199 | } |
| 200 | |
| 201 | @[inline] |
| 202 | pub fn scale(x f32, y f32, z f32) { |
| 203 | C.sgl_scale(x, y, z) |
| 204 | } |
| 205 | |
| 206 | @[inline] |
| 207 | pub fn translate(x f32, y f32, z f32) { |
| 208 | C.sgl_translate(x, y, z) |
| 209 | } |
| 210 | |
| 211 | @[inline] |
| 212 | pub fn frustum(l f32, r f32, b f32, t f32, n f32, f f32) { |
| 213 | C.sgl_frustum(l, r, b, t, n, f) |
| 214 | } |
| 215 | |
| 216 | @[inline] |
| 217 | pub fn ortho(l f32, r f32, b f32, t f32, n f32, f f32) { |
| 218 | C.sgl_ortho(l, r, b, t, n, f) |
| 219 | } |
| 220 | |
| 221 | @[inline] |
| 222 | pub fn perspective(fov_y f32, aspect f32, z_near f32, z_far f32) { |
| 223 | C.sgl_perspective(fov_y, aspect, z_near, z_far) |
| 224 | } |
| 225 | |
| 226 | @[inline] |
| 227 | pub fn lookat(eye_x f32, eye_y f32, eye_z f32, center_x f32, center_y f32, center_z f32, up_x f32, up_y f32, |
| 228 | up_z f32) { |
| 229 | C.sgl_lookat(eye_x, eye_y, eye_z, center_x, center_y, center_z, up_x, up_y, up_z) |
| 230 | } |
| 231 | |
| 232 | @[inline] |
| 233 | pub fn push_matrix() { |
| 234 | C.sgl_push_matrix() |
| 235 | } |
| 236 | |
| 237 | @[inline] |
| 238 | pub fn pop_matrix() { |
| 239 | C.sgl_pop_matrix() |
| 240 | } |
| 241 | |
| 242 | // these functions only set the internal 'current texcoord / color' (valid inside or outside begin/end) |
| 243 | @[inline] |
| 244 | pub fn t2f(u f32, v f32) { |
| 245 | C.sgl_t2f(u, v) |
| 246 | } |
| 247 | |
| 248 | @[inline] |
| 249 | pub fn c3f(r f32, g f32, b f32) { |
| 250 | C.sgl_c3f(r, g, b) |
| 251 | } |
| 252 | |
| 253 | @[inline] |
| 254 | pub fn c4f(r f32, g f32, b f32, a f32) { |
| 255 | C.sgl_c4f(r, g, b, a) |
| 256 | } |
| 257 | |
| 258 | @[inline] |
| 259 | pub fn c3b(r u8, g u8, b u8) { |
| 260 | C.sgl_c3b(r, g, b) |
| 261 | } |
| 262 | |
| 263 | @[inline] |
| 264 | pub fn c4b(r u8, g u8, b u8, a u8) { |
| 265 | C.sgl_c4b(r, g, b, a) |
| 266 | } |
| 267 | |
| 268 | @[inline] |
| 269 | pub fn c1i(rgba u32) { |
| 270 | C.sgl_c1i(rgba) |
| 271 | } |
| 272 | |
| 273 | @[inline] |
| 274 | pub fn point_size(s f32) { |
| 275 | C.sgl_point_size(s) |
| 276 | } |
| 277 | |
| 278 | // define primitives, each begin/end is one draw command |
| 279 | @[inline] |
| 280 | pub fn begin_points() { |
| 281 | C.sgl_begin_points() |
| 282 | } |
| 283 | |
| 284 | @[inline] |
| 285 | pub fn begin_lines() { |
| 286 | C.sgl_begin_lines() |
| 287 | } |
| 288 | |
| 289 | @[inline] |
| 290 | pub fn begin_line_strip() { |
| 291 | C.sgl_begin_line_strip() |
| 292 | } |
| 293 | |
| 294 | @[inline] |
| 295 | pub fn begin_triangles() { |
| 296 | C.sgl_begin_triangles() |
| 297 | } |
| 298 | |
| 299 | @[inline] |
| 300 | pub fn begin_triangle_strip() { |
| 301 | C.sgl_begin_triangle_strip() |
| 302 | } |
| 303 | |
| 304 | @[inline] |
| 305 | pub fn begin_quads() { |
| 306 | C.sgl_begin_quads() |
| 307 | } |
| 308 | |
| 309 | @[inline] |
| 310 | pub fn v2f(x f32, y f32) { |
| 311 | C.sgl_v2f(x, y) |
| 312 | } |
| 313 | |
| 314 | @[inline] |
| 315 | pub fn v3f(x f32, y f32, z f32) { |
| 316 | C.sgl_v3f(x, y, z) |
| 317 | } |
| 318 | |
| 319 | @[inline] |
| 320 | pub fn v2f_t2f(x f32, y f32, u f32, v f32) { |
| 321 | C.sgl_v2f_t2f(x, y, u, v) |
| 322 | } |
| 323 | |
| 324 | @[inline] |
| 325 | pub fn v3f_t2f(x f32, y f32, z f32, u f32, v f32) { |
| 326 | C.sgl_v3f_t2f(x, y, z, u, v) |
| 327 | } |
| 328 | |
| 329 | @[inline] |
| 330 | pub fn v2f_c3f(x f32, y f32, r f32, g f32, b f32) { |
| 331 | C.sgl_v2f_c3f(x, y, r, g, b) |
| 332 | } |
| 333 | |
| 334 | @[inline] |
| 335 | pub fn v2f_c3b(x f32, y f32, r u8, g u8, b u8) { |
| 336 | C.sgl_v2f_c3b(x, y, r, g, b) |
| 337 | } |
| 338 | |
| 339 | @[inline] |
| 340 | pub fn v2f_c4f(x f32, y f32, r f32, g f32, b f32, a f32) { |
| 341 | C.sgl_v2f_c4f(x, y, r, g, b, a) |
| 342 | } |
| 343 | |
| 344 | @[inline] |
| 345 | pub fn v2f_c4b(x f32, y f32, r u8, g u8, b u8, a u8) { |
| 346 | C.sgl_v2f_c4b(x, y, r, g, b, a) |
| 347 | } |
| 348 | |
| 349 | @[inline] |
| 350 | pub fn v2f_c1i(x f32, y f32, rgba u32) { |
| 351 | C.sgl_v2f_c1i(x, y, rgba) |
| 352 | } |
| 353 | |
| 354 | @[inline] |
| 355 | pub fn v3f_c3f(x f32, y f32, z f32, r f32, g f32, b f32) { |
| 356 | C.sgl_v3f_c3f(x, y, z, r, g, b) |
| 357 | } |
| 358 | |
| 359 | @[inline] |
| 360 | pub fn v3f_c3b(x f32, y f32, z f32, r u8, g u8, b u8) { |
| 361 | C.sgl_v3f_c3b(x, y, z, r, g, b) |
| 362 | } |
| 363 | |
| 364 | @[inline] |
| 365 | pub fn v3f_c4f(x f32, y f32, z f32, r f32, g f32, b f32, a f32) { |
| 366 | C.sgl_v3f_c4f(x, y, z, r, g, b, a) |
| 367 | } |
| 368 | |
| 369 | @[inline] |
| 370 | pub fn v3f_c4b(x f32, y f32, z f32, r u8, g u8, b u8, a u8) { |
| 371 | C.sgl_v3f_c4b(x, y, z, r, g, b, a) |
| 372 | } |
| 373 | |
| 374 | @[inline] |
| 375 | pub fn v3f_c1i(x f32, y f32, z f32, rgba u32) { |
| 376 | C.sgl_v3f_c1i(x, y, z, rgba) |
| 377 | } |
| 378 | |
| 379 | @[inline] |
| 380 | pub fn v2f_t2f_c3f(x f32, y f32, u f32, v f32, r f32, g f32, b f32) { |
| 381 | C.sgl_v2f_t2f_c3f(x, y, u, v, r, g, b) |
| 382 | } |
| 383 | |
| 384 | @[inline] |
| 385 | pub fn v2f_t2f_c3b(x f32, y f32, u f32, v f32, r u8, g u8, b u8) { |
| 386 | C.sgl_v2f_t2f_c3b(x, y, u, v, r, g, b) |
| 387 | } |
| 388 | |
| 389 | @[inline] |
| 390 | pub fn v2f_t2f_c4f(x f32, y f32, u f32, v f32, r f32, g f32, b f32, a f32) { |
| 391 | C.sgl_v2f_t2f_c4f(x, y, u, v, r, g, b, a) |
| 392 | } |
| 393 | |
| 394 | @[inline] |
| 395 | pub fn v2f_t2f_c4b(x f32, y f32, u f32, v f32, r u8, g u8, b u8, a u8) { |
| 396 | C.sgl_v2f_t2f_c4b(x, y, u, v, r, g, b, a) |
| 397 | } |
| 398 | |
| 399 | @[inline] |
| 400 | pub fn v2f_t2f_c1i(x f32, y f32, u f32, v f32, rgba u32) { |
| 401 | C.sgl_v2f_t2f_c1i(x, y, u, v, rgba) |
| 402 | } |
| 403 | |
| 404 | @[inline] |
| 405 | pub fn v3f_t2f_c3f(x f32, y f32, z f32, u f32, v f32, r f32, g f32, b f32) { |
| 406 | C.sgl_v3f_t2f_c3f(x, y, z, u, v, r, g, b) |
| 407 | } |
| 408 | |
| 409 | @[inline] |
| 410 | pub fn v3f_t2f_c3b(x f32, y f32, z f32, u f32, v f32, r u8, g u8, b u8) { |
| 411 | C.sgl_v3f_t2f_c3b(x, y, z, u, v, r, g, b) |
| 412 | } |
| 413 | |
| 414 | @[inline] |
| 415 | pub fn v3f_t2f_c4f(x f32, y f32, z f32, u f32, v f32, r f32, g f32, b f32, a f32) { |
| 416 | C.sgl_v3f_t2f_c4f(x, y, z, u, v, r, g, b, a) |
| 417 | } |
| 418 | |
| 419 | @[inline] |
| 420 | pub fn v3f_t2f_c4b(x f32, y f32, z f32, u f32, v f32, r u8, g u8, b u8, a u8) { |
| 421 | C.sgl_v3f_t2f_c4b(x, y, z, u, v, r, g, b, a) |
| 422 | } |
| 423 | |
| 424 | @[inline] |
| 425 | pub fn v3f_t2f_c1i(x f32, y f32, z f32, u f32, v f32, rgba u32) { |
| 426 | C.sgl_v3f_t2f_c1i(x, y, z, u, v, rgba) |
| 427 | } |
| 428 | |
| 429 | @[inline] |
| 430 | pub fn end() { |
| 431 | C.sgl_end() |
| 432 | } |
| 433 | |
| 434 | // render recorded commands |
| 435 | @[inline] |
| 436 | pub fn draw() { |
| 437 | C.sgl_draw() |
| 438 | } |
| 439 | |
| 440 | @[inline] |
| 441 | pub fn context_draw(ctx Context) { |
| 442 | C.sgl_context_draw(ctx) |
| 443 | } |
| 444 | |