Skip to content

Instantly share code, notes, and snippets.

@pjako
Created August 7, 2017 13:38
Show Gist options
  • Save pjako/0c89587d13b60d83ba8f441b3c6eece4 to your computer and use it in GitHub Desktop.
Save pjako/0c89587d13b60d83ba8f441b3c6eece4 to your computer and use it in GitHub Desktop.
//------------------------------------------------------------------------------
// triangle-emsc.c
// Vertex buffer, simple shader, pipeline state object.
//------------------------------------------------------------------------------
#define SOKOL_IMPL
#define SOKOL_GLES2
#include "sokol_gfx.h"
sg_draw_state draw_state;
/* default pass action (clear to grey) */
sg_pass_action pass_action = {0};
float WIDTH = 600f;
float HEIGHT = 400f;
void initSokolHello() {
/* setup sokol_gfx */
sg_desc desc = {0};
sg_setup(&desc);
assert(sg_isvalid());
/* a vertex buffer with 3 vertices */
float vertices[] = {
// positions // colors
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
};
sg_buffer_desc buf_desc = {
.size = sizeof(vertices),
.data_ptr = vertices,
.data_size = sizeof(vertices)
};
/* create a shader */
sg_shader_desc shd_desc = {
.vs.source =
"attribute vec4 position;\n"
"attribute vec4 color0;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_Position = position;\n"
" color = color0;\n"
"}\n",
.fs.source =
"precision mediump float;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_FragColor = color;\n"
"}\n"
};
/* create a pipeline object (default render states are fine for triangle) */
sg_pipeline_desc pip_desc = {
.vertex_layouts[0] = {
.stride = 28,
.attrs = {
[0] = {.name = "position", .offset = 0, .format = SG_VERTEXFORMAT_FLOAT3},
[1] = {.name = "color0", .offset = 12, .format = SG_VERTEXFORMAT_FLOAT4}}},
.shader = sg_make_shader(&shd_desc),
};
/* setup the draw state with resource bindings */
draw_state = (sg_draw_state){
.pipeline = sg_make_pipeline(&pip_desc),
.vertex_buffers[0] = sg_make_buffer(&buf_desc)
};
}
/* draw one frame */
void drawSokolHelloWorld() {
sg_begin_default_pass(&pass_action, WIDTH, HEIGHT);
sg_apply_draw_state(&draw_state);
sg_draw(0, 3, 1);
sg_end_pass();
sg_commit();
}
void endSokolHelloWorld() {
sg_shutdown();
}
void ctoy_begin() {
initSokolHello();
}
void ctoy_end() {
endSokolHelloWorld();
}
void ctoy_main_loop() {
drawSokolHelloWorld();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment