summaryrefslogtreecommitdiff
path: root/src/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.h')
-rw-r--r--src/utils.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..ee0b6c6
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,71 @@
+#ifndef _UTILS_H_
+#define _UTILS_H_
+
+#define len(X) sizeof(X) / sizeof(*X)
+
+// Yes, i know.
+#define min(X, Y) X < Y ? X : Y
+#define max(X, Y) X > Y ? X : Y
+
+#define array(type) { size_t len, cap; type *data; }
+#define array_init(array) do {\
+ (array).len = 0; \
+ (array).cap = 5; \
+ (array).data = calloc(5, sizeof(*(array).data)); \
+ } while (0)
+#define array_new(type) { \
+ .len = 0, \
+ .cap = 5, \
+ .data = calloc(5, sizeof(type)) \
+ }
+
+#define foreach(item, array) \
+ for (typeof((array).data) item = (array).data; item < (array).data + (array).len; item++)
+
+#define array_append(array, item) do { \
+ ensure_ptr((array).data, (array).len + 1, (array).cap); \
+ (array).data[(array).len++] = item; \
+ } while(0)
+
+#define ensure(var, ptr_mem, len_mem, cap_mem) ensure_ptr((var).ptr_mem, (var).len_mem, (var).cap_mem)
+#define ensure_ptr(ptr, len, cap) do { \
+ assert(ptr); \
+ if ((len) > (cap)) { \
+ (ptr) = realloc((ptr), ((cap) = (cap) * 2 > (len) ? (cap) * 2 : (len)) * sizeof(*(ptr))); \
+ if (!(ptr)) \
+ abort(); \
+ } \
+ } while (0)
+
+#define STRINGFY(X) #X
+#define STRGY(X) STRINGFY(X)
+
+
+#ifndef NDEBUG
+# include <stdio.h>
+# define dbglog(F) fprintf(stderr, __FILE__ ":%s:" STRGY(__LINE__) ": " F "\n", __func__)
+# define dbglogf(F, ...) fprintf(stderr, __FILE__ ":%s:" STRGY(__LINE__) ": " F "\n", __func__, __VA_ARGS__)
+#else
+# define dbglog(F) do { /* noop */ } while(0)
+# define dbglogf(F, X) do { /* noop */ } while(0)
+#endif
+
+#include <vulkan/vk_enum_string_helper.h>
+#include <stdbool.h>
+
+static inline bool vlkn_check_expr(VkResult exp, const char *exp_str) {
+ if (exp == VK_SUCCESS)
+ return true;
+ dbglogf("%s failed with %s", exp_str, string_VkResult(exp));
+ return false;
+}
+
+#define vlkn_check(exp) vlkn_check_expr(exp, #exp)
+
+#include <vulkan/vulkan.h>
+struct vlkn_renderer;
+
+VkCommandBuffer begin_single_command(struct vlkn_renderer *ren);
+void end_single_command(struct vlkn_renderer *ren, VkCommandBuffer buffer);
+
+#endif