how to make 2d red square
- User
how to make 2d red square
Assistant
c#include "sokol_gfx.h" #include "sokol_app.h" #include "sokol_glue.h" #define SOKOL_IMPL #include "sokol_shape.h" static sg_pipeline pip; static sg_bindings bind; static sshape_buffer_t buf; void init(void) { sg_setup(&(sg_desc){ .context = sglue_context() }); /* allocate a vertex and index buffer */ buf = (sshape_buffer_t){0}; buf.vertices.buffer = SG_MALLOC(1024); // Enough for a simple quad buf.indices.buffer = SG_MALLOC(1024); // Enough for a simple quad /* create a plane shape, default size is 1x1, so scale it to 2x2 */ sshape_mat4_t transform_matrix = sshape_mat4_scale(sshape_mat4_identity(), 2.0f, 2.0f, 1.0f); buf = sshape_build_plane(&buf, &(sshape_plane_t){ .color = sshape_color_4f(1.0f, 0.0f, 0.0f, 1.0f), // Red .transform = transform_matrix, }); bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ .data = sshape_vertex_buffer_desc(&buf).data }); bind.index_buffer = sg_make_buffer(&(sg_buffer_desc){ .type = SG_BUFFERTYPE_INDEXBUFFER, .data = sshape_index_buffer_desc(&buf).data }); /* create shader and pipeline object */ sg_shader shd = sg_make_shader(&(sg_shader_desc){ .vs.source = "#version 330\n" "in vec4 position;\n" "in vec4 color0;\n" "out vec4 color;\n" "void main() {\n" " gl_Position = position;\n" " color = color0;\n" "}\n", .fs.source = "#version 330\n" "in vec4 color;\n" "out vec4 frag_color;\n" "void main() {\n" " frag_color = color;\n" "}\n" }); pip = sg_make_pipeline(&(sg_pipeline_desc){ .shader = shd, .index_type = SG_INDEXTYPE_UINT16, .layout = { .attrs = { [0] = sshape_position_vertex_attr_state(), [1] = sshape_normal_vertex_attr_state(), // Although unused [2] = sshape_texcoord_vertex_attr_state(),// Although unused [3] = sshape_color_vertex_attr_state(), } } }); } void frame(void) { sg_pass_action pass_action = { .colors[0] = { .action = SG_ACTION_CLEAR, .value = { 0.0f, 0.0f, 0.0f, 1.0f } } }; sg_begin_default_pass(&pass_action, sapp_width(), sapp_height()); sg_apply_pipeline(pip); sg_apply_bindings(&bind); sshape_element_range_t elms = sshape_element_range(&buf); sg_draw(elms.base_element, elms.num_elements, 1); sg_end_pass(); sg_commit(); } void cleanup(void) { SG_FREE(buf.vertices.buffer.ptr); SG_FREE(buf.indices.buffer.ptr); 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 = 800, .height = 600, .window_title = "Red Square (sokol app)", .gl_force_gles2 = true, }; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.