#include #include #include #include #include #include static struct { sg_pipeline pip; sg_bindings bind; sg_pass_action pass_action; } state; typedef struct { float x, y, z; int16_t u, v; } vertex_t; static void init(void) { sg_setup(&(sg_desc){ .environment = sglue_environment(), .logger.func = slog_func, }); vertex_t vertices[] = { { 0.5f, 0.5f, 0.0f, 1.0f * 32767, 1.0f * 32767 }, // top right { 0.5f, -0.5f, 0.0f, 1.0f * 32767, 0.0f * 32767 }, // bottom right { -0.5f, -0.5f, 0.0f, 0.0f * 32767, 0.0f * 32767 }, // bottom left { -0.5f, 0.5f, 0.0f, 0.0f * 32767, 1.0f * 32767 } // top left }; unsigned int indices[] = { // note that we start from 0! 0, 1, 3, // first triangle 1, 2, 3 // second triangle }; state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ .data = SG_RANGE(vertices), .label = "triangle-vertices" }); state.bind.index_buffer = sg_make_buffer(&(sg_buffer_desc){ .type = SG_BUFFERTYPE_INDEXBUFFER, .data = SG_RANGE(indices), .label = "triangle-indices" }); uint32_t pixels[4*4] = { 0xFF000000, 0xFFFF00FF, 0xFF000000, 0xFFFF00FF, 0xFFFF00FF, 0xFF000000, 0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF, 0xFF000000, 0xFFFF00FF, 0xFFFF00FF, 0xFF000000, 0xFFFF00FF, 0xFF000000, }; state.bind.fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){ .width = 4, .height = 4, .data.subimage[0][0] = SG_RANGE(pixels), .label = "triangle-texture", .pixel_format = SG_PIXELFORMAT_RGBA8, }); state.bind.fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){ .label = "triangle-sampler" }); sg_shader shd = sg_make_shader(triangle_shader_desc(sg_query_backend())); state.pip = sg_make_pipeline(&(sg_pipeline_desc){ .shader = shd, .layout = { .attrs = { [ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3, [ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N, } }, .label = "triangle-pipeline", .index_type = SG_INDEXTYPE_UINT32, .cull_mode = SG_CULLMODE_BACK, .sample_count = 1, }); state.pass_action = (sg_pass_action) { .colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.4f, 0.3f, 1.0f } } }; } void frame(void) { sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() }); sg_apply_pipeline(state.pip); sg_apply_bindings(&state.bind); sg_draw(0, 6, 1); sg_end_pass(); sg_commit(); } void cleanup(void) { sg_shutdown(); } sapp_desc sokol_main(int argc, char* argv[]) { (void)argc; (void)argv; return (sapp_desc){ .init_cb = init, .frame_cb = frame, .cleanup_cb = cleanup, .width = 640, .height = 480, .window_title = "Gear", .icon.sokol_default = true, .logger.func = slog_func, }; }