Skip to content

Instantly share code, notes, and snippets.

@sherjilozair
Created June 9, 2018 14:05
Show Gist options
  • Save sherjilozair/c0fa81250c1b8f5e4234b1588e755bca to your computer and use it in GitHub Desktop.
Save sherjilozair/c0fa81250c1b8f5e4234b1588e755bca to your computer and use it in GitHub Desktop.
Minimal sprite rendering example with SDL2 for windowing, sokol_gfx for graphics API using OpenGL 3.3 on MacOS
#include <OpenGL/gl3.h>
#include <SDL2/SDL.h>
#define SOKOL_IMPL
#define SOKOL_GLCORE33
#include <sokol_gfx.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define CODE(...) #__VA_ARGS__
typedef uint16_t u16;
int main(){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window *window = SDL_CreateWindow("Window",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
1280, 720, SDL_WINDOW_OPENGL|SDL_WINDOW_ALLOW_HIGHDPI);
SDL_GLContext ctx = SDL_GL_CreateContext(window);
sg_setup(&(sg_desc) {0});
assert(sg_isvalid());
float vertices[] = {
-0.5f, 0.5f, 0.0, 1.0,
0.5f, 0.5f, 1.0, 1.0,
0.5f, -0.5f, 1.0, 0.0,
-0.5f, -0.5f, 0.0, 0.0,
};
sg_buffer vbuf = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(vertices),
.content = vertices,
});
u16 indices[] = {
0, 1, 2,
0, 2, 3
};
sg_buffer ibuf = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(indices),
.type = SG_BUFFERTYPE_INDEXBUFFER,
.content = indices,
});
int w, h, n;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("sprites.png", &w, &h, &n, 0);
assert(data);
sg_image img = sg_make_image(&(sg_image_desc){
.width=w,
.height=h,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.min_filter = SG_FILTER_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
.content.subimage[0][0] = {
.ptr = data,
.size = sizeof(data)
}
});
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.fs.images[0] = {.name="tex", .type=SG_IMAGETYPE_2D},
.vs.source = "#version 330\n" CODE(
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 texcoord0;
out vec2 uv;
void main(){
gl_Position = vec4(position, 0.0, 1.0);
uv = texcoord0;
}
),
.fs.source = "#version 330\n" CODE(
uniform sampler2D tex;
in vec2 uv;
out vec4 frag_color;
void main(){
frag_color = texture(tex, uv);
}
)
});
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = shd,
.blend = {
.enabled = true,
.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA,
.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
.color_write_mask = SG_COLORMASK_RGB,
},
.index_type = SG_INDEXTYPE_UINT16,
.layout = {
.attrs = {
[0] = {.format=SG_VERTEXFORMAT_FLOAT2},
[1] = {.format=SG_VERTEXFORMAT_FLOAT2},
}
}
});
sg_draw_state draw_state = {
.pipeline = pip,
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf,
.fs_images[0] = img
};
sg_pass_action pass_action = {0};
while(!SDL_QuitRequested()){
int w, h;
SDL_GL_GetDrawableSize(window, &w, &h);
sg_begin_default_pass(&pass_action, w, h);
sg_apply_draw_state(&draw_state);
sg_draw(0, 6, 1);
sg_end_pass();
sg_commit();
SDL_GL_SwapWindow(window);
}
sg_shutdown();
SDL_GL_DeleteContext(ctx);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment