v / vlib / sokol / sgl / sgl.c.v
443 lines · 357 sloc · 7.91 KB · 227dbc7d4a3943fe8f1541f595b6ef9851bef552
Raw
1module sgl
2
3import sokol.gfx
4import sokol.memory
5
6pub const version = gfx.version + 1
7
8pub const context = Context{0x00010001} // C.SGL_DEFAULT_CONTEXT = { 0x00010001 }
9
10// setup/shutdown/misc
11pub 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
27pub fn shutdown() {
28 C.sgl_shutdown()
29}
30
31@[inline]
32pub fn error() SglError {
33 return unsafe { SglError(int(C.sgl_error())) }
34}
35
36@[inline]
37pub fn context_error(ctx Context) SglError {
38 return unsafe { SglError(int(C.sgl_context_error(ctx))) }
39}
40
41@[inline]
42pub fn rad(deg f32) f32 {
43 return C.sgl_rad(deg)
44}
45
46@[inline]
47pub fn deg(rad f32) f32 {
48 return C.sgl_deg(rad)
49}
50
51// context functions
52@[inline]
53pub fn make_context(desc &ContextDesc) Context {
54 return C.sgl_make_context(desc)
55}
56
57@[inline]
58pub fn destroy_context(ctx Context) {
59 C.sgl_destroy_context(ctx)
60}
61
62@[inline]
63pub fn set_context(ctx Context) {
64 C.sgl_set_context(ctx)
65}
66
67@[inline]
68pub fn get_context() Context {
69 return C.sgl_get_context()
70}
71
72@[inline]
73pub fn default_context() Context {
74 return C.sgl_default_context()
75}
76
77// create and destroy pipeline objects
78@[inline]
79pub fn make_pipeline(desc &gfx.PipelineDesc) Pipeline {
80 return C.sgl_make_pipeline(desc)
81}
82
83@[inline]
84pub fn context_make_pipeline(ctx Context, desc &gfx.PipelineDesc) Pipeline {
85 return C.sgl_context_make_pipeline(ctx, desc)
86}
87
88@[inline]
89pub fn destroy_pipeline(pip Pipeline) {
90 C.sgl_destroy_pipeline(pip)
91}
92
93// render state functions
94@[inline]
95pub fn defaults() {
96 C.sgl_defaults()
97}
98
99@[inline]
100pub 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]
105pub 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]
110pub 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]
115pub fn enable_texture() {
116 C.sgl_enable_texture()
117}
118
119@[inline]
120pub fn disable_texture() {
121 C.sgl_disable_texture()
122}
123
124@[inline]
125pub fn texture(img gfx.Image, smp gfx.Sampler) {
126 C.sgl_texture(img, smp)
127}
128
129// pipeline stack functions
130@[inline]
131pub fn load_default_pipeline() {
132 C.sgl_load_default_pipeline()
133}
134
135@[inline]
136pub fn default_pipeline() {
137 C.sgl_load_default_pipeline()
138}
139
140@[inline]
141pub fn load_pipeline(pip Pipeline) {
142 C.sgl_load_pipeline(pip)
143}
144
145@[inline]
146pub fn push_pipeline() {
147 C.sgl_push_pipeline()
148}
149
150@[inline]
151pub fn pop_pipeline() {
152 C.sgl_pop_pipeline()
153}
154
155// matrix stack functions
156@[inline]
157pub fn matrix_mode_modelview() {
158 C.sgl_matrix_mode_modelview()
159}
160
161@[inline]
162pub fn matrix_mode_projection() {
163 C.sgl_matrix_mode_projection()
164}
165
166@[inline]
167pub fn matrix_mode_texture() {
168 C.sgl_matrix_mode_texture()
169}
170
171@[inline]
172pub fn load_identity() {
173 C.sgl_load_identity()
174}
175
176@[inline]
177pub fn load_matrix(m []f32) {
178 C.sgl_load_matrix(m.data)
179}
180
181@[inline]
182pub fn load_transpose_matrix(m []f32) {
183 C.sgl_load_transpose_matrix(m.data)
184}
185
186@[inline]
187pub fn mult_matrix(m []f32) {
188 C.sgl_mult_matrix(m.data)
189}
190
191@[inline]
192pub fn mult_transpose_matrix(m []f32) {
193 C.sgl_mult_transpose_matrix(m.data)
194}
195
196@[inline]
197pub fn rotate(angle_rad f32, x f32, y f32, z f32) {
198 C.sgl_rotate(angle_rad, x, y, z)
199}
200
201@[inline]
202pub fn scale(x f32, y f32, z f32) {
203 C.sgl_scale(x, y, z)
204}
205
206@[inline]
207pub fn translate(x f32, y f32, z f32) {
208 C.sgl_translate(x, y, z)
209}
210
211@[inline]
212pub 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]
217pub 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]
222pub 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]
227pub 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]
233pub fn push_matrix() {
234 C.sgl_push_matrix()
235}
236
237@[inline]
238pub 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]
244pub fn t2f(u f32, v f32) {
245 C.sgl_t2f(u, v)
246}
247
248@[inline]
249pub fn c3f(r f32, g f32, b f32) {
250 C.sgl_c3f(r, g, b)
251}
252
253@[inline]
254pub fn c4f(r f32, g f32, b f32, a f32) {
255 C.sgl_c4f(r, g, b, a)
256}
257
258@[inline]
259pub fn c3b(r u8, g u8, b u8) {
260 C.sgl_c3b(r, g, b)
261}
262
263@[inline]
264pub fn c4b(r u8, g u8, b u8, a u8) {
265 C.sgl_c4b(r, g, b, a)
266}
267
268@[inline]
269pub fn c1i(rgba u32) {
270 C.sgl_c1i(rgba)
271}
272
273@[inline]
274pub 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]
280pub fn begin_points() {
281 C.sgl_begin_points()
282}
283
284@[inline]
285pub fn begin_lines() {
286 C.sgl_begin_lines()
287}
288
289@[inline]
290pub fn begin_line_strip() {
291 C.sgl_begin_line_strip()
292}
293
294@[inline]
295pub fn begin_triangles() {
296 C.sgl_begin_triangles()
297}
298
299@[inline]
300pub fn begin_triangle_strip() {
301 C.sgl_begin_triangle_strip()
302}
303
304@[inline]
305pub fn begin_quads() {
306 C.sgl_begin_quads()
307}
308
309@[inline]
310pub fn v2f(x f32, y f32) {
311 C.sgl_v2f(x, y)
312}
313
314@[inline]
315pub fn v3f(x f32, y f32, z f32) {
316 C.sgl_v3f(x, y, z)
317}
318
319@[inline]
320pub fn v2f_t2f(x f32, y f32, u f32, v f32) {
321 C.sgl_v2f_t2f(x, y, u, v)
322}
323
324@[inline]
325pub 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]
330pub 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]
335pub 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]
340pub 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]
345pub 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]
350pub fn v2f_c1i(x f32, y f32, rgba u32) {
351 C.sgl_v2f_c1i(x, y, rgba)
352}
353
354@[inline]
355pub 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]
360pub 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]
365pub 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]
370pub 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]
375pub fn v3f_c1i(x f32, y f32, z f32, rgba u32) {
376 C.sgl_v3f_c1i(x, y, z, rgba)
377}
378
379@[inline]
380pub 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]
385pub 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]
390pub 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]
395pub 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]
400pub 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]
405pub 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]
410pub 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]
415pub 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]
420pub 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]
425pub 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]
430pub fn end() {
431 C.sgl_end()
432}
433
434// render recorded commands
435@[inline]
436pub fn draw() {
437 C.sgl_draw()
438}
439
440@[inline]
441pub fn context_draw(ctx Context) {
442 C.sgl_context_draw(ctx)
443}
444