1 //------------------------------------------------------------------------------ 2 // triangle.d 3 // 4 // Vertex buffer, shader, pipeline state object. 5 //------------------------------------------------------------------------------ 6 module examples.triangle; 7 8 import sg = sokol.gfx; 9 import sapp = sokol.app; 10 import log = sokol.log; 11 import sglue = sokol.glue; 12 import shd = examples.shaders.triangle; 13 14 extern (C): 15 @safe: 16 17 struct State 18 { 19 sg.Bindings bind; 20 sg.Pipeline pip; 21 } 22 23 static State state; 24 25 static void init() 26 { 27 sg.Desc sg_desc = { 28 environment: sglue.environment(), 29 logger: {func: &log.slog_func}, 30 }; 31 sg.setup(sg_desc); 32 33 // create vertex buffer with triangle vertices 34 float[21] vertices = [ 35 // positions colors 36 0.0, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 37 0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0, 38 -0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 39 ]; 40 sg.BufferDesc buf_desc = {data: {ptr: vertices.ptr, 41 size: vertices.sizeof,}}; 42 state.bind.vertex_buffers[0] = sg.makeBuffer(buf_desc); 43 44 // create a shader and pipeline object 45 sg.PipelineDesc pip_desc = { 46 shader: sg.makeShader(shd.triangleShaderDesc(sg.queryBackend())), 47 layout: { 48 attrs: [ 49 shd.ATTR_TRIANGLE_POSITION: {format: sg.VertexFormat.Float3}, 50 shd.ATTR_TRIANGLE_COLOR0: {format: sg.VertexFormat.Float4}, 51 ], 52 } 53 }; 54 state.pip = sg.makePipeline(pip_desc); 55 } 56 57 static void frame() 58 { 59 // default pass-action clears to grey 60 sg.Pass pass = {swapchain: sglue.swapchain()}; 61 sg.beginPass(pass); 62 sg.applyPipeline(state.pip); 63 sg.applyBindings(state.bind); 64 sg.draw(0, 3, 1); 65 sg.endPass(); 66 sg.commit(); 67 } 68 69 static void cleanup() 70 { 71 sg.shutdown(); 72 } 73 74 void main() 75 { 76 sapp.Desc runner = { 77 window_title: "triangle.d", 78 init_cb: &init, 79 frame_cb: &frame, 80 cleanup_cb: &cleanup, 81 width: 800, 82 height: 600, 83 icon: {sokol_default: true}, 84 logger: {func: &log.func} 85 }; 86 sapp.run(runner); 87 }