diff --git a/Makefile b/Makefile index 47e5d17..6806f32 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = clang -CFLAGS = -std=c99 -Wall -Wextra -Wpedantic +CFLAGS = -std=c11 -Wall -Wextra -Wpedantic CFLAGS += -fstrict-aliasing CFLAGS += $(shell pkg-config --cflags glfw3 vulkan) LDFLAGS = $(shell pkg-config --libs glfw3 vulkan) diff --git a/default.nix b/default.nix index 0f9ddb5..125a066 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,6 @@ { lib, stdenv, - cglm, clang, glfw, gnumake, @@ -20,7 +19,6 @@ in stdenv.mkDerivation { src = ./.; buildInputs = [ - cglm glfw vulkan-headers vulkan-loader diff --git a/include/ptk.h b/include/ptk.h index 990500a..cc4dbef 100644 --- a/include/ptk.h +++ b/include/ptk.h @@ -1,12 +1,12 @@ #ifndef _PTK_PTK_H #define _PTK_PTK_H +#include + #include #include #include -#include - typedef struct { uint32_t major; uint32_t minor; @@ -37,30 +37,30 @@ typedef struct PtkBox { typedef struct PtkTriangle { PtkComponentType type; - vec2 vertices[3]; - vec3 color; + PtkPos vertices[3]; + PtkColor color; } PtkTriangle; typedef struct PtkRect { PtkComponentType type; - vec2 top_left; - vec2 size; - vec3 color; + PtkPos top_left; + PtkSize size; + PtkColor color; } PtkRect; typedef struct PtkEllipse { PtkComponentType type; - vec2 center; - vec2 radii; - vec3 color; + PtkPos center; + PtkSize radii; + PtkColor color; } PtkEllipse; PtkHandle ptk_box(size_t child_count, PtkHandle *children); -PtkHandle ptk_triangle(vec2 vertices[3], vec3 color); -PtkHandle ptk_rect(vec2 top_left, vec2 size, vec3 color); -PtkHandle ptk_square(vec2 top_left, float size, vec3 color); -PtkHandle ptk_ellipse(vec2 center, vec2 radii, vec3 color); -PtkHandle ptk_circle(vec2 center, float radius, vec3 color); +PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color); +PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color); +PtkHandle ptk_square(PtkPos top_left, float size, PtkColor color); +PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color); +PtkHandle ptk_circle(PtkPos center, float radius, PtkColor color); #define PTK_BOX(...) ptk_box(sizeof((PtkHandle []){ __VA_ARGS__ }) / sizeof(PtkHandle), (PtkHandle []) { __VA_ARGS__ }) diff --git a/src/ptk.c b/src/ptk.c index 6f82f6c..912531d 100644 --- a/src/ptk.c +++ b/src/ptk.c @@ -14,8 +14,6 @@ #endif #include -#include - #include #include #include @@ -123,41 +121,41 @@ PtkHandle ptk_box(size_t child_count, PtkHandle *children) { return (PtkHandle)ret; } -PtkHandle ptk_triangle(vec2 vertices[3], vec3 color) { +PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) { PtkTriangle *ret = malloc(sizeof(PtkTriangle)); ret->type = PTK_COMPONENT_TYPE_TRIANGLE; - memcpy(ret->vertices, vertices, sizeof(vec2) * 3); - memcpy(ret->color, color, sizeof(vec3)); + memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3); + ret->color = color; return (PtkHandle)ret; } -PtkHandle ptk_rect(vec2 top_left, vec2 size, vec3 color) { +PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) { PtkRect *ret = malloc(sizeof(PtkRect)); ret->type = PTK_COMPONENT_TYPE_RECT; - memcpy(ret->top_left, top_left, sizeof(vec2)); - memcpy(ret->size, size, sizeof(vec2)); - memcpy(ret->color, color, sizeof(vec3)); + ret->top_left = top_left; + ret->size = size; + ret->color = color; return (PtkHandle)ret; } -PtkHandle ptk_square(vec2 top_left, float size, vec3 color) { - return ptk_rect(top_left, (vec2){size, size}, color); +PtkHandle ptk_square(PtkPos top_left, float size, PtkColor color) { + return ptk_rect(top_left, (PtkSize){ .w = size, .h = size}, color); } -PtkHandle ptk_ellipse(vec2 center, vec2 radii, vec3 color) { +PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color) { PtkEllipse *ret = malloc(sizeof(PtkEllipse)); ret->type = PTK_COMPONENT_TYPE_ELLIPSE; - memcpy(ret->center, center, sizeof(vec2)); - memcpy(ret->radii, radii, sizeof(vec2)); - memcpy(ret->color, color, sizeof(vec3)); + ret->center = center; + ret->radii = radii; + ret->color = color; return (PtkHandle)ret; } -PtkHandle ptk_circle(vec2 center, float radius, vec3 color) { - return ptk_ellipse(center, (vec2){radius, radius}, color); +PtkHandle ptk_circle(PtkPos center, float radius, PtkColor color) { + return ptk_ellipse(center, (PtkSize){ .w = radius, .h = radius}, color); } void init_components(PtkHandle root) { diff --git a/src/ptk_vec.h b/src/ptk_vec.h new file mode 100644 index 0000000..8c1ec8d --- /dev/null +++ b/src/ptk_vec.h @@ -0,0 +1,65 @@ +#ifndef _PTK_PTK_VEC_H +#define _PTK_PTK_VEC_H + +typedef union { + struct { // PtkVec2, PtkPos + float x; + float y; + }; + struct { // PtkSize + float w; + float h; + }; + struct { // PtkSize + float width; + float height; + }; +} PtkVec2; + +typedef PtkVec2 PtkPos; +typedef PtkVec2 PtkSize; + +typedef union { + struct { // PtkVec3 + float x; + float y; + float z; + }; + struct { // PtkColor + float r; + float g; + float b; + }; + struct { // PtkColor + float red; + float green; + float blue; + }; +} PtkVec3; + +typedef PtkVec3 PtkColor; + +typedef union { + struct { // PtkVec4 + float x; + float y; + float z; + float w; + }; + struct { // PtkColorA + float r; + float g; + float b; + float a; + }; + struct { // PtkColorA + float red; + float green; + float blue; + float alpha; + }; +} PtkVec4; + +typedef PtkVec4 PtkColorA; + +#endif // _PTK_PTK_VEC_H diff --git a/src/ptk_vk/components.c b/src/ptk_vk/components.c index a1c61ca..11fc114 100644 --- a/src/ptk_vk/components.c +++ b/src/ptk_vk/components.c @@ -2,19 +2,17 @@ #include #include -#include - PTK_LIST_DEFINE(PtkHandle); PTK_LIST(PtkHandle) m_components; PTK_LIST(Vertex) g_vertices; -static const vec2 uvs[] = { - {0, 0}, - {0, 1}, - {1, 0}, - {1, 1}, +static const PtkVec2 uvs[] = { + { .x = 0.0f, .y = 0.0f }, + { .x = 0.0f, .y = 1.0f }, + { .x = 1.0f, .y = 0.0f }, + { .x = 1.0f, .y = 1.0f }, }; void vk_components_init(void) { @@ -25,11 +23,12 @@ void vk_components_init(void) { void _vk_triangle(PtkTriangle *triangle, PtkComponentType type, size_t uvs_offset) { for (size_t i = 0; i < 3; ++i) { - Vertex v; - memcpy(v.pos, triangle->vertices[i], sizeof(vec2)); - memcpy(v.color, triangle->color, sizeof(vec3)); - v.shape_type = type; - memcpy(v.uv, uvs[i + uvs_offset], sizeof(vec2)); + Vertex v = { + .pos = triangle->vertices[i], + .color = triangle->color, + .shape_type = type, + .uv = uvs[i + uvs_offset], + }; PTK_LIST_ADD(Vertex, g_vertices, v); } @@ -43,25 +42,25 @@ void vk_triangle(PtkTriangle *triangle) { } void _vk_rect(PtkRect *rect, PtkComponentType type) { - vec2 t1_positions[3]; + PtkPos t1_positions[3]; // top left - memcpy(t1_positions[0], &(vec2){ rect->top_left[0], rect->top_left[1] }, sizeof(vec2)); + t1_positions[0] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y }; // bottom left - memcpy(t1_positions[1], &(vec2){ rect->top_left[0] + rect->size[0], rect->top_left[1] }, sizeof(vec2)); + t1_positions[1] = (PtkPos){ .x = rect->top_left.x + rect->size.y, .y = rect->top_left.y }; // top right - memcpy(t1_positions[2], &(vec2){ rect->top_left[0], rect->top_left[1] + rect->size[1] }, sizeof(vec2)); - PtkTriangle *t1 = (PtkTriangle *)ptk_triangle((vec2 *)t1_positions, rect->color); + t1_positions[2] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y + rect->size.y }; + PtkTriangle *t1 = (PtkTriangle *)ptk_triangle((PtkPos *)t1_positions, rect->color); _vk_triangle(t1, type, 0); - vec2 t2_positions[3]; + PtkPos t2_positions[3]; // bottom left - memcpy(t2_positions[0], &(vec2){ rect->top_left[0] + rect->size[0], rect->top_left[1] }, sizeof(vec2)); + t2_positions[0] = (PtkPos){ .x = rect->top_left.x + rect->size.x, .y = rect->top_left.y }; // top right - memcpy(t2_positions[1], &(vec2){ rect->top_left[0], rect->top_left[1] + rect->size[1] }, sizeof(vec2)); + t2_positions[1] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y + rect->size.y }; // bottom right - memcpy(t2_positions[2], &(vec2){ rect->top_left[0] + rect->size[0], rect->top_left[1] + rect->size[1] }, sizeof(vec2)); - PtkTriangle *t2 = (PtkTriangle *)ptk_triangle((vec2 *)t2_positions, rect->color); + t2_positions[2] = (PtkPos){ .x = rect->top_left.x + rect->size.x, .y = rect->top_left.y + rect->size.y }; + PtkTriangle *t2 = (PtkTriangle *)ptk_triangle((PtkPos *)t2_positions, rect->color); _vk_triangle(t2, type, 1); vk_transfer_vertex_data(); @@ -76,14 +75,14 @@ void vk_rect(PtkRect *rect) { void vk_ellipse(PtkEllipse *ellipse) { PTK_LIST_ADD(PtkHandle, m_components, ellipse); - vec2 top_left = { - ellipse->center[0] - ellipse->radii[0], - ellipse->center[1] - ellipse->radii[1], + PtkPos top_left = { + .x = ellipse->center.x - ellipse->radii.y, + .y = ellipse->center.x - ellipse->radii.y, }; - vec2 size = { - ellipse->radii[0] * 2.0f, - ellipse->radii[1] * 2.0f, + PtkSize size = { + .w = ellipse->radii.x * 2.0f, + .h = ellipse->radii.y * 2.0f, }; _vk_rect((PtkRect *)ptk_rect(top_left, size, ellipse->color), PTK_COMPONENT_TYPE_ELLIPSE); diff --git a/src/ptk_vk/components.h b/src/ptk_vk/components.h index be56bb4..4b7c78b 100644 --- a/src/ptk_vk/components.h +++ b/src/ptk_vk/components.h @@ -5,10 +5,10 @@ #include typedef struct { - vec2 pos; - vec3 color; + PtkPos pos; + PtkColor color; PtkComponentType shape_type; - vec2 uv; + PtkVec2 uv; } Vertex; PTK_LIST_DEFINE(Vertex); diff --git a/src/ptk_vk/init.c b/src/ptk_vk/init.c index 5a8ebf7..7ef7f76 100644 --- a/src/ptk_vk/init.c +++ b/src/ptk_vk/init.c @@ -7,10 +7,9 @@ #include #include #include +#include #include -#include - #include PTK_LIST_DEFINE(VkImage); @@ -122,8 +121,8 @@ typedef void *voidptr; PTK_LIST_DEFINE(voidptr); typedef struct { - vec2 initial_window_size; - vec2 window_size; + PtkSize initial_window_size; + PtkSize window_size; } UniformBufferObject; static UniformBufferObject m_uniform_buffer_object; @@ -1106,8 +1105,8 @@ bool vk_transfer_vertex_data(void) { for (size_t i = 0; i < g_vertices.size; ++i) { PTK_DEBUG("v[%d]:", i); Vertex current = g_vertices.data[i]; - PTK_DEBUG(" pos: (%f, %f)", current.pos[0], current.pos[1]); - PTK_DEBUG(" color: (%f, %f, %f)", current.color[0], current.color[1], current.color[2]); + PTK_DEBUG(" pos: (%f, %f)", current.pos.x, current.pos.y); + PTK_DEBUG(" color: (%f, %f, %f)", current.color.x, current.color.y, current.color.z); } PTK_DEBUG("transferring vertices to gpu…"); VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size; @@ -1426,8 +1425,8 @@ bool vk_recreate_swapchain(void) { } bool vk_update_uniform_buffer(size_t current_frame) { - m_uniform_buffer_object.window_size[0] = m_swapchain_extent.width; - m_uniform_buffer_object.window_size[1] = m_swapchain_extent.height; + m_uniform_buffer_object.window_size.x = m_swapchain_extent.width; + m_uniform_buffer_object.window_size.y = m_swapchain_extent.height; memcpy(m_uniform_buffers_mapped.data[current_frame], &m_uniform_buffer_object, sizeof(m_uniform_buffer_object)); @@ -1436,8 +1435,8 @@ bool vk_update_uniform_buffer(size_t current_frame) { bool vk_init(GLFWwindow *window, size_t width, size_t height, const char *title, const PtkVersion version) { m_window = window; - m_uniform_buffer_object.initial_window_size[0] = width; - m_uniform_buffer_object.initial_window_size[1] = height; + m_uniform_buffer_object.initial_window_size.x = width; + m_uniform_buffer_object.initial_window_size.y = height; if (!create_vk_instance(title, version)) { PTK_ERR("failed creating VkInstance"); diff --git a/test/init.c b/test/init.c index 1cd62d5..dbed951 100644 --- a/test/init.c +++ b/test/init.c @@ -8,9 +8,9 @@ TEST_START() bool init_result = ptk_init(800, 600, "test", (PtkVersion){ .major = 0, .minor = 1, .patch = 0 }); TEST_ASSERT(init_result, "ptk_init() failed"); int run_result = ptk_run(PTK_BOX( - ptk_triangle((vec2 []){{400.0f, 50.f}, {700.0f, 500.0f}, {100.0f, 500.0f}}, (vec3){0.0f, 1.0f, 0.0f}), - ptk_rect((vec2){200.0f, 200.0f}, (vec2){300.0f, 300.0f}, (vec3){1.0f, 0.0f, 0.0f}), - ptk_ellipse((vec2){600.0f, 100.0f}, (vec2){50.0f, 25.0f}, (vec3){0.0f, 0.0f, 1.0f}), + ptk_triangle((PtkPos []){{ .x = 400.0f, .y = 50.f}, { .x = 700.0f, .y = 500.0f}, { .x = 100.0f, .y = 500.0f}}, (PtkColor){ .r = 0.0f, .g = 1.0f, .b = 0.0f}), + ptk_rect((PtkPos){ .x = 200.0f, .y = 200.0f}, (PtkSize){ .w = 300.0f, .h = 300.0f}, (PtkColor){ .r = 1.0f, .g = 0.0f, .b = 0.0f}), + ptk_ellipse((PtkPos){ .x = 600.0f, .y = 100.0f}, (PtkSize){ .w = 50.0f, .h = 25.0f}, (PtkColor){ .r = 0.0f, .g = 0.0f, .b = 1.0f}), )); TEST_ASSERT(run_result == 0, "ptk_run() failed"); });