add ptk list iterator macro

This commit is contained in:
jacekpoz 2024-09-04 20:27:53 +02:00
parent 3f2df4ace8
commit ed9c34e243
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
4 changed files with 57 additions and 51 deletions

View file

@ -31,9 +31,9 @@ static void framebuffer_resize_callback(GLFWwindow *window, int width, int heigh
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
(void)window; (void)scancode; (void)action; (void)mods;
if (key == GLFW_KEY_SPACE) {
for (size_t i = 0; i < g_vertices.size; ++i) {
g_vertices.data[i].pos.x += 1.0f;
}
PTK_LIST_FOR_EACH_P(Vertex, g_vertices, vertex, {
vertex->pos.x += 1.0f;
});
vk_transfer_vertex_data();
}
}

View file

@ -43,6 +43,18 @@ bool _remove_at_PtkList(void *data, uint32_t *size, size_t index, size_t element
.allocated = _size,\
}
#define PTK_LIST_FOR_EACH(T, list, elem_name, ...) \
for (size_t _i = 0; _i < list.size; ++_i) {\
T elem_name = list.data[_i];\
__VA_ARGS__\
}
#define PTK_LIST_FOR_EACH_P(T, list, elem_name, ...) \
for (size_t _i = 0; _i < list.size; ++_i) {\
T *elem_name = &list.data[_i];\
__VA_ARGS__\
}
#define PTK_LIST_CLEAR(list) \
list.size = 0

View file

@ -101,11 +101,10 @@ void component(PtkHandle c) {
} break;
default: break;
}
for (size_t i = 0; i < c->children.size; ++i) {
PtkHandle child = c->children.data[i];
PTK_LIST_FOR_EACH(PtkHandle, c->children, child, {
child->parent = c;
component(child);
}
})
}
void vk_init_components(PtkHandle root) {
@ -153,11 +152,11 @@ inline bool ellipse_intersects(const PtkEllipse e, const PtkPos p) {
bool intersects(const PtkHandle component, const PtkPos point) {
switch (component->type) {
case PTK_COMPONENT_TYPE_BOX: {
for (size_t i = 0; i < component->children.size; ++i) {
if (intersects(component->children.data[i], point)) {
PTK_LIST_FOR_EACH(PtkHandle, component->children, child, {
if (intersects(child, point)) {
return true;
}
}
})
return false;
}
@ -177,8 +176,7 @@ void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, con
return;
}
for (size_t i = 0; i < m_components.size; ++i) {
const PtkHandle current = m_components.data[i];
PTK_LIST_FOR_EACH(PtkHandle, m_components, current, {
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
continue;
}
@ -189,13 +187,13 @@ void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, con
c->on_press(c, button, mods);
vk_update_components();
}
}
})
}
void vk_components_cleanup(void) {
for (size_t i = 0; i < m_components.size; ++i) {
free(m_components.data[i]);
}
PTK_LIST_FOR_EACH(PtkHandle, m_components, c, {
free(c);
})
PTK_LIST_FREE(m_components);
PTK_LIST_FREE(g_vertices);

View file

@ -159,13 +159,12 @@ bool check_validation_layers(const char * const *validation_layers, const uint32
bool layer_found = false;
for (size_t j = 0; j < available_layers.size; ++j) {
VkLayerProperties layer_properties = available_layers.data[j];
PTK_LIST_FOR_EACH(VkLayerProperties, available_layers, layer_properties, {
if (strcmp(layer_name, layer_properties.layerName) == 0) {
layer_found = true;
break;
}
}
})
if (!layer_found) {
return false;
@ -293,23 +292,21 @@ bool select_physical_dev(void) {
}
VkSurfaceFormatKHR select_swap_surface_format(const PTK_LIST(VkSurfaceFormatKHR) available_formats) {
for (size_t i = 0; i < available_formats.size; ++i) {
const VkSurfaceFormatKHR current_format = available_formats.data[i];
PTK_LIST_FOR_EACH(VkSurfaceFormatKHR, available_formats, current_format, {
if (current_format.format == VK_FORMAT_B8G8R8A8_SRGB && current_format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return current_format;
}
}
})
return available_formats.data[0];
}
VkPresentModeKHR select_swap_present_mode(const PTK_LIST(VkPresentModeKHR) available_present_modes) {
for (size_t i = 0; i < available_present_modes.size; ++i) {
const VkPresentModeKHR current_present_mode = available_present_modes.data[i];
PTK_LIST_FOR_EACH(VkPresentModeKHR, available_present_modes, current_present_mode, {
if (current_present_mode == VK_PRESENT_MODE_MAILBOX_KHR) {
return current_present_mode;
}
}
})
return VK_PRESENT_MODE_FIFO_KHR;
}
@ -389,12 +386,12 @@ bool are_extensions_supported(const VkPhysicalDevice dev) {
size_t supported_extensions = 0;
for (size_t i = 0; i < m_device_extension_count; ++i) {
for (size_t j = 0; j < available_extensions.size; ++j) {
if (strcmp(m_device_extensions[i], available_extensions.data[j].extensionName) == 0) {
PTK_LIST_FOR_EACH(VkExtensionProperties, available_extensions, current_extension, {
if (strcmp(m_device_extensions[i], current_extension.extensionName) == 0) {
supported_extensions += 1;
break;
}
}
})
}
return supported_extensions == m_device_extension_count;
@ -410,17 +407,17 @@ bool is_device_suitable(const VkPhysicalDevice dev) {
m_queue_family_indices.graphics = PTK_OPTION_NONE(uint32_t);
m_queue_family_indices.present = PTK_OPTION_NONE(uint32_t);
for (size_t i = 0; i < queue_families.size; ++i) {
if (queue_families.data[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
m_queue_family_indices.graphics = PTK_OPTION_SOME(uint32_t, i);
PTK_LIST_FOR_EACH(VkQueueFamilyProperties, queue_families, queue_family, {
if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
m_queue_family_indices.graphics = PTK_OPTION_SOME(uint32_t, _i);
}
VkBool32 present_support = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(dev, i, m_surface, &present_support);
vkGetPhysicalDeviceSurfaceSupportKHR(dev, _i, m_surface, &present_support);
if (present_support) {
m_queue_family_indices.present = PTK_OPTION_SOME(uint32_t, i);
m_queue_family_indices.present = PTK_OPTION_SOME(uint32_t, _i);
}
}
})
const bool indices_found = m_queue_family_indices.graphics.exists
&& m_queue_family_indices.present.exists;
@ -567,7 +564,7 @@ bool create_swapchain(void) {
bool create_image_views(void) {
m_swapchain_image_views = PTK_LIST_NEW(VkImageView, m_swapchain_images.size);
for (size_t i = 0; i < m_swapchain_images.size; ++i) {
PTK_LIST_FOR_EACH(VkImage, m_swapchain_images, swapchain_image, {
VK_TRY(false,
vkCreateImageView(
g_dev,
@ -575,7 +572,7 @@ bool create_image_views(void) {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.pNext = NULL,
.flags = 0,
.image = m_swapchain_images.data[i],
.image = swapchain_image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = m_swapchain_image_format,
.components = (VkComponentMapping){
@ -593,11 +590,11 @@ bool create_image_views(void) {
},
},
NULL,
&m_swapchain_image_views.data[i]
&m_swapchain_image_views.data[_i]
)
);
m_swapchain_image_views.size += 1;
}
})
return true;
}
@ -950,7 +947,7 @@ bool create_graphics_pipeline(void) {
bool create_framebuffers(void) {
m_swapchain_framebuffers = PTK_LIST_NEW(VkFramebuffer, m_swapchain_image_views.size);
for (size_t i = 0; i < m_swapchain_image_views.size; ++i) {
PTK_LIST_FOR_EACH(VkImageView, m_swapchain_image_views, swapchain_image_view, {
VK_TRY(false,
vkCreateFramebuffer(
g_dev,
@ -960,17 +957,17 @@ bool create_framebuffers(void) {
.flags = 0,
.renderPass = m_render_pass,
.attachmentCount = 1,
.pAttachments = &(VkImageView){m_swapchain_image_views.data[i]},
.pAttachments = &(VkImageView){swapchain_image_view},
.width = m_swapchain_extent.width,
.height = m_swapchain_extent.height,
.layers = 1,
},
NULL,
&m_swapchain_framebuffers.data[i]
&m_swapchain_framebuffers.data[_i]
)
);
m_swapchain_framebuffers.size += 1;
}
})
return true;
}
@ -1116,12 +1113,11 @@ bool copy_buffer(const VkBuffer src, const VkBuffer dst, const VkDeviceSize size
bool vk_transfer_vertex_data(void) {
PTK_DEBUG("vertices updated!");
for (size_t i = 0; i < g_vertices.size; ++i) {
PTK_DEBUG("v[%d]:", i);
const Vertex current = g_vertices.data[i];
PTK_LIST_FOR_EACH(Vertex, g_vertices, current, {
PTK_DEBUG("v[%d]:", _i);
PTK_DEBUG(" pos: (%f, %f)", current.pos.x, current.pos.y);
PTK_DEBUG(" color: (%f, %f, %f)", current.color.r, current.color.g, current.color.b);
}
})
PTK_DEBUG("transferring vertices to gpu…");
const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
@ -1393,15 +1389,15 @@ bool create_sync_objects(void) {
}
void cleanup_swapchain(void) {
for (size_t i = 0; i < m_swapchain_framebuffers.size; ++i) {
vkDestroyFramebuffer(g_dev, m_swapchain_framebuffers.data[i], NULL);
}
PTK_LIST_FOR_EACH(VkFramebuffer, m_swapchain_framebuffers, fb, {
vkDestroyFramebuffer(g_dev, fb, NULL);
})
PTK_LIST_FREE(m_swapchain_framebuffers);
PTK_LIST_FREE(m_swapchain_images);
for (size_t i = 0; i < m_swapchain_image_views.size; ++i) {
vkDestroyImageView(g_dev, m_swapchain_image_views.data[i], NULL);
}
PTK_LIST_FOR_EACH(VkImageView, m_swapchain_image_views, iv, {
vkDestroyImageView(g_dev, iv, NULL);
})
PTK_LIST_FREE(m_swapchain_image_views);
vkDestroySwapchainKHR(g_dev, g_swapchain, NULL);