gear/src/main.c

116 lines
3.1 KiB
C
Raw Normal View History

2024-08-17 19:12:22 +12:00
#include <sokol/sokol_app.h>
#include <sokol/sokol_gfx.h>
#include <sokol/sokol_log.h>
#include <sokol/sokol_glue.h>
#include <math.h>
#include <shaders/shader.glsl.h>
static struct {
sg_pipeline pip;
sg_bindings bind;
sg_pass_action pass_action;
} state;
2024-08-18 15:21:25 +12:00
typedef struct {
float x, y, z;
int16_t u, v;
} vertex_t;
2024-08-17 19:12:22 +12:00
static void init(void) {
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
2024-08-18 15:21:25 +12:00
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
};
2024-08-17 19:12:22 +12:00
2024-08-18 15:21:25 +12:00
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
2024-08-17 19:12:22 +12:00
};
2024-08-18 15:21:25 +12:00
2024-08-17 19:12:22 +12:00
state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(vertices),
.label = "triangle-vertices"
});
2024-08-18 15:21:25 +12:00
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"
});
2024-08-17 19:12:22 +12:00
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,
2024-08-18 15:21:25 +12:00
[ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N,
2024-08-17 19:12:22 +12:00
}
},
.label = "triangle-pipeline",
2024-08-18 15:21:25 +12:00
.index_type = SG_INDEXTYPE_UINT32,
2024-08-17 19:12:22 +12:00
.cull_mode = SG_CULLMODE_BACK,
.sample_count = 1,
});
state.pass_action = (sg_pass_action) {
2024-08-18 15:21:25 +12:00
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.4f, 0.3f, 1.0f } }
2024-08-17 19:12:22 +12:00
};
}
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);
2024-08-18 15:21:25 +12:00
sg_draw(0, 6, 1);
2024-08-17 19:12:22 +12:00
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,
2024-08-18 13:07:10 +12:00
.window_title = "Gear",
2024-08-17 19:12:22 +12:00
.icon.sokol_default = true,
.logger.func = slog_func,
};
}