add image loading

This commit is contained in:
sam 2024-08-19 18:03:22 +12:00
parent 1db8d530c5
commit 832120d335
4 changed files with 8010 additions and 11 deletions

View file

@ -2,7 +2,7 @@ BINARY=gear
SHADERC=sokol-shdc SHADERC=sokol-shdc
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard -std=c99 CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard
SHADER_FLAGS=--slang glsl430:hlsl5:metal_macos SHADER_FLAGS=--slang glsl430:hlsl5:metal_macos
C_FILES=$(shell find -L * -type f -name '*.c') C_FILES=$(shell find -L * -type f -name '*.c')
@ -18,6 +18,7 @@ else
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S), Linux) ifeq ($(UNAME_S), Linux)
CFLAGS += -DSOKOL_GLCORE CFLAGS += -DSOKOL_GLCORE
LDFLAGS += $(shell pkg-config --libs x11 xi xcursor gl) -ldl -pthread -lm -lasound
endif endif
ifeq ($(UNAME_S), Darwin) ifeq ($(UNAME_S), Darwin)
CFLAGS += -DSOKOL_METAL CFLAGS += -DSOKOL_METAL

BIN
happi.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

7988
include/stb/stb_image.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -5,6 +5,11 @@
#include <slibs/slibs.h> #include <slibs/slibs.h>
#include <HandmadeMath/HandmadeMath.h> #include <HandmadeMath/HandmadeMath.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include <stdio.h>
#include <shaders/transform.glsl.h> #include <shaders/transform.glsl.h>
static struct { static struct {
@ -26,9 +31,9 @@ static void init(void) {
}); });
vertex_t vertices[] = { vertex_t vertices[] = {
{ 1.0f, 1.0f, 0.0f, 0xFFFF0000, 1.0f * 32767, 1.0f * 32767 }, // top right { 1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 1.0f * 32767, 1.0f * 32767 }, // top right
{ 1.0f, -1.0f, 0.0f, 0xFF00FF00, 1.0f * 32767, 0.0f * 32767 }, // bottom right { 1.0f, -1.0f, 0.0f, 0xFFFFFFFF, 1.0f * 32767, 0.0f * 32767 }, // bottom right
{ -1.0f, -1.0f, 0.0f, 0xFF0000FF, 0.0f * 32767, 0.0f * 32767 }, // bottom left { -1.0f, -1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 0.0f * 32767 }, // bottom left
{ -1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 1.0f * 32767 } // top left { -1.0f, 1.0f, 0.0f, 0xFFFFFFFF, 0.0f * 32767, 1.0f * 32767 } // top left
}; };
@ -50,19 +55,21 @@ static void init(void) {
.label = "triangle-indices" .label = "triangle-indices"
}); });
uint32_t pixels[2*2] = { int desired_channels = 4;
0xFFFFFFFF, 0xFF222222, int width, height, channels;
0xFF222222, 0xFFFFFFFF 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){ bind->fs.images[SLOT_tex] = sg_make_image(&(sg_image_desc){
.width = 2, .width = width,
.height = 2, .height = height,
.data.subimage[0][0] = SG_RANGE(pixels), .data.subimage[0][0] = {.ptr = pixels, .size = width * height * desired_channels},
.label = "triangle-texture", .label = "triangle-texture",
.pixel_format = SG_PIXELFORMAT_RGBA8, .pixel_format = SG_PIXELFORMAT_RGBA8,
}); });
stbi_image_free(pixels);
bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){ bind->fs.samplers[SLOT_smp] = sg_make_sampler(&(sg_sampler_desc){
.label = "triangle-sampler", .label = "triangle-sampler",
}); });
@ -97,6 +104,8 @@ float t = 0.0f;
void frame(void) { void frame(void) {
t += sapp_frame_duration(); 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_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = sglue_swapchain() });
sg_apply_pipeline(state.pip); sg_apply_pipeline(state.pip);
@ -137,5 +146,6 @@ sapp_desc sokol_main(int argc, char* argv[]) {
.window_title = "Gear", .window_title = "Gear",
.icon.sokol_default = true, .icon.sokol_default = true,
.logger.func = slog_func, .logger.func = slog_func,
.swap_interval = 0
}; };
} }