86 lines
2.2 KiB
C
86 lines
2.2 KiB
C
|
#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;
|
||
|
vs_params_t vs_params;
|
||
|
int index;
|
||
|
} state;
|
||
|
|
||
|
static void init(void) {
|
||
|
sg_setup(&(sg_desc){
|
||
|
.environment = sglue_environment(),
|
||
|
.logger.func = slog_func,
|
||
|
});
|
||
|
|
||
|
state.vs_params.rotation = 0.0f;
|
||
|
state.index = 0;
|
||
|
|
||
|
float vertices[] = {
|
||
|
0.0f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
|
||
|
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f,
|
||
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||
|
};
|
||
|
state.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
|
||
|
.data = SG_RANGE(vertices),
|
||
|
.label = "triangle-vertices"
|
||
|
});
|
||
|
|
||
|
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_color0].format = SG_VERTEXFORMAT_FLOAT4
|
||
|
}
|
||
|
},
|
||
|
.label = "triangle-pipeline",
|
||
|
.cull_mode = SG_CULLMODE_BACK,
|
||
|
.face_winding = SG_FACEWINDING_CCW,
|
||
|
.sample_count = 1,
|
||
|
});
|
||
|
|
||
|
state.pass_action = (sg_pass_action) {
|
||
|
.colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.0f, 0.0f, 0.0f, 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);
|
||
|
state.vs_params.rotation = sin(++state.index * 0.05);
|
||
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(state.vs_params));
|
||
|
sg_draw(0, 3, 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,
|
||
|
};
|
||
|
}
|
||
|
|