diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..e5e12e4 --- /dev/null +++ b/.clangd @@ -0,0 +1,3 @@ +CompileFlags: + Add: -ferror-limit=0 + diff --git a/src/graphics/renderer/create.c b/src/graphics/renderer/create.c new file mode 100644 index 0000000..39babc3 --- /dev/null +++ b/src/graphics/renderer/create.c @@ -0,0 +1,18 @@ +#include + +g_renderer g_renderer_create(g_color clear_color) { + g_renderer renderer; + memset(&renderer, 0, sizeof(g_renderer)); + + sg_color_attachment_action color_action = { + .load_action = SG_LOADACTION_CLEAR, + .clear_value = G_COLOR_TO_FLOAT_ARR(clear_color), + }; + + renderer.pass_action = (sg_pass_action){ + .colors = { color_action, color_action, color_action, color_action }, + .depth = { .load_action = SG_LOADACTION_CLEAR, .clear_value = 1.0f }, + }; + + return renderer; +} diff --git a/src/images/create.c b/src/images/create.c new file mode 100644 index 0000000..256588f --- /dev/null +++ b/src/images/create.c @@ -0,0 +1,27 @@ +#include +#include + +g_image* g_image_create(u8* pixels, i32 width, i32 height) { + g_image* p_image = calloc(1, sizeof(g_image)); + if (p_image != NULL) { + p_image->width = width; + p_image->height = height; + p_image->channels = G_IMAGE_CHANNELS; + p_image->pixels = pixels; + } + + return p_image; +} + +g_image* g_image_default() { + g_image* p_image = calloc(1, sizeof(g_image)); + if(p_image != NULL) { + p_image->width = G_IMAGE_MISSING_DIMENSIONS, + p_image->height = G_IMAGE_MISSING_DIMENSIONS, + p_image->channels = G_IMAGE_CHANNELS, + p_image->pixels = calloc(1, G_IMAGE_MISSING_BYTESIZE); + memcpy(p_image->pixels, G_IMAGE_MISSING_PIXELS, G_IMAGE_MISSING_BYTESIZE); + } + + return p_image; +} diff --git a/src/images/destroy.c b/src/images/destroy.c new file mode 100644 index 0000000..1cf9f65 --- /dev/null +++ b/src/images/destroy.c @@ -0,0 +1,7 @@ +#include +#include + +void g_image_free(g_image* image) { + stbi_image_free(image->pixels); + free(image); +} diff --git a/src/images/load.c b/src/images/load.c new file mode 100644 index 0000000..d22f4cf --- /dev/null +++ b/src/images/load.c @@ -0,0 +1,16 @@ +#include +#include + +g_image* g_image_load(const char* filename) { + const i32 desired_channels = 4; + + i32 width, height, channels; + stbi_set_flip_vertically_on_load(true); + + u8* pixels = stbi_load(filename, &width, &height, &channels, desired_channels); + if(pixels == NULL) { + return g_image_default(); + } + + return g_image_create(pixels, width, height); +} diff --git a/src/main.c b/src/main.c index 6c7784d..a79f1d6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,6 @@ +#include "gear/images.h" +#include + #include #include #include @@ -5,18 +8,9 @@ #include #include -#define STB_IMAGE_IMPLEMENTATION -#include - -#include - #include -static struct { - sg_pipeline pip; - sl_vec(sg_bindings*) bindings; - sg_pass_action pass_action; -} state; + typedef struct { float x, y, z; @@ -46,33 +40,16 @@ static void init(void) { sl_vec_push(state.bindings, bind); bind->vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ .data = SG_RANGE(vertices), - .label = "triangle-vertices" }); bind->index_buffer = sg_make_buffer(&(sg_buffer_desc){ .type = SG_BUFFERTYPE_INDEXBUFFER, .data = SG_RANGE(indices), - .label = "triangle-indices" }); - int desired_channels = 4; - int width, height, channels; - stbi_set_flip_vertically_on_load(true); - uint8_t* pixels = stbi_load("happi.jpg", &width, &height, &channels, desired_channels); - - bind->fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){ - .width = width, - .height = height, - .data.subimage[0][0] = {.ptr = pixels, .size = width * height * desired_channels}, - .label = "triangle-texture", - .pixel_format = SG_PIXELFORMAT_RGBA8, - }); - - stbi_image_free(pixels); - - bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){ - .label = "triangle-sampler", - }); + g_texture* texture = g_texture_create(g_image_load("happi.jpg")); + bind->fs.images[SLOT_tex] = texture->image; + bind->fs.samplers[SLOT_smp] = texture->sampler; sg_shader shd = sg_make_shader(transform_shader_desc(sg_query_backend())); @@ -83,7 +60,7 @@ static void init(void) { [ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT3, [ATTR_vs_color0].format = SG_VERTEXFORMAT_UBYTE4N, [ATTR_vs_texcoord0].format = SG_VERTEXFORMAT_SHORT2N, - } + }, }, .index_type = SG_INDEXTYPE_UINT16, .cull_mode = SG_CULLMODE_BACK, @@ -91,12 +68,7 @@ static void init(void) { .compare = SG_COMPAREFUNC_LESS_EQUAL, .write_enabled = true, }, - .label = "triangle-pipeline", }); - - state.pass_action = (sg_pass_action) { - .colors[0] = { .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.4f, 0.3f, 1.0f } } - }; } float t = 0.0f; @@ -104,19 +76,17 @@ float t = 0.0f; void frame(void) { t += sapp_frame_duration(); - printf("%f\n", 1.0f/sapp_frame_duration()); - sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() }); sg_apply_pipeline(state.pip); HMM_Mat4 view = HMM_Translate(HMM_V3(0.0f, 0.0f, -3.0f)); HMM_Mat4 projection = HMM_Perspective_RH_NO(HMM_AngleDeg(45.0f), sapp_widthf() / sapp_heightf(), 0.1f, 100.0f); - HMM_Mat4 view_proj = HMM_MulM4(projection, view); + HMM_Mat4 view_proj = HMM_Mul(projection, view); for(size_t i = 0; i < state.bindings.size; i++) { HMM_Mat4 model = HMM_Rotate_RH(HMM_AngleDeg(-55.0f), HMM_V3(1.0f, 0.0f, 0.0f)); - model = HMM_MulM4(model, HMM_Rotate_RH(HMM_AngleDeg(sin(t * 2) * 10), HMM_V3(0.0f, 0.2f, 0.0f))); - HMM_Mat4 mvp = HMM_MulM4(view_proj, model); + model = HMM_Mul(model, HMM_Rotate_RH(HMM_AngleDeg(sin(t * 2) * 10), HMM_V3(0.0f, 0.2f, 0.0f))); + HMM_Mat4 mvp = HMM_Mul(view_proj, model); vs_params_t params = { .mvp = mvp, diff --git a/src/renderer.c b/src/renderer.c new file mode 100644 index 0000000..c390728 --- /dev/null +++ b/src/renderer.c @@ -0,0 +1 @@ +#include diff --git a/src/textures/create.c b/src/textures/create.c new file mode 100644 index 0000000..fa3218c --- /dev/null +++ b/src/textures/create.c @@ -0,0 +1,29 @@ +#include +#include + +g_texture* g_texture_create(g_image* image) { + sg_image sg_img = sg_make_image(&(sg_image_desc){ + .width = image->width, + .height = image->height, + .data.subimage[0][0] = { + .ptr = image->pixels, + .size = image->width * image->height * image->channels + }, + .pixel_format = SG_PIXELFORMAT_RGBA8 // has to be used for opengl support + }); + + g_image_free(image); + + sg_sampler sg_samp = sg_make_sampler(&(sg_sampler_desc){ + .min_filter = SG_FILTER_LINEAR, + .mag_filter = SG_FILTER_LINEAR + }); + + g_texture* texture = calloc(1, sizeof(g_texture)); + if(texture != NULL) { + texture->image = sg_img; + texture->sampler = sg_samp; + } + + return texture; +} diff --git a/src/sokol/sokol.c b/src/thirdparty/sokol/sokol.c similarity index 100% rename from src/sokol/sokol.c rename to src/thirdparty/sokol/sokol.c diff --git a/src/sokol/sokol_metal.m b/src/thirdparty/sokol/sokol_metal.m similarity index 100% rename from src/sokol/sokol_metal.m rename to src/thirdparty/sokol/sokol_metal.m diff --git a/src/thirdparty/stb/stb_image.c b/src/thirdparty/stb/stb_image.c new file mode 100644 index 0000000..7819971 --- /dev/null +++ b/src/thirdparty/stb/stb_image.c @@ -0,0 +1,2 @@ +#define STB_IMAGE_IMPLEMENTATION +#include