Squashed commit of the following:

commit ef2fdaab86
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 14:22:09 2024 +0200

    improve all vector types by deunionifying them

commit e994825668
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 14:19:56 2024 +0200

    use w and h for radii in ellipse intersects

commit 2eb53fb8a3
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 13:47:03 2024 +0200

    add PTK_LIST_CLEAR

commit a63f65067d
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 13:46:29 2024 +0200

    remove semicolon from filled macro

commit 4ac6a1bb70
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 13:31:13 2024 +0200

    ...

commit 5142127ae0
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 12:50:08 2024 +0200

    add clickable to components if encountered

commit b1f31b7965
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 12:41:52 2024 +0200

    stupid fucking switch syntax

commit 8ff74e7235
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 12:40:05 2024 +0200

    remove redundant vertex update

commit 448e9fed14
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 12:10:48 2024 +0200

    handle button hitboxes properly

commit a4025fa924
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 12:04:11 2024 +0200

    add more const on local variables

commit c7386ddfbf
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 12:02:24 2024 +0200

    fix rect triangle comments (xd)

commit ca212ac65e
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 11:57:33 2024 +0200

    spam const over local variables and function parameters

commit 43707826d9
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 11:31:50 2024 +0200

    improve rect triangle init readability

commit 2b3f4c58c4
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 02:08:18 2024 +0200

    set ellipse rect's type to the ellipse's type

commit 4872e59e16
Merge: 336aadc 565aa4d
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:29:01 2024 +0200

    Merge branch 'main' into button

commit 336aadc7a0
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:25:13 2024 +0200

    whoops forgot about this one

commit 29f0d9f56a
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:22:45 2024 +0200

    improve component initialization

    compound literals zero-initialize all omitted fields including
    children.size, now we don't have to explicitly do that in each function

commit 913286e9e7
Merge: 425e670 520b0b9
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:18:39 2024 +0200

    Merge branch 'main' into button

commit 425e6709b4
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:16:38 2024 +0200

    initialize children size to 0 on childless components

commit 70ef0e030f
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:13:39 2024 +0200

    fix vertex input attribute description

commit 44204b5677
Merge: 0788992 f95c47d
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 01:03:26 2024 +0200

    Merge branch 'main' into button

commit 0788992af4
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 00:45:13 2024 +0200

    remove vk_clickable from components

commit 7cc71d3177
Merge: 3020724 b74186b
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 00:44:02 2024 +0200

    Merge branch 'main' into button

commit 3020724319
Merge: 83c7968 80a8069
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 00:18:31 2024 +0200

    Merge branch 'main' into button

commit 83c7968d48
Author: jacekpoz <jacekpoz@proton.me>
Date:   Tue Aug 13 00:03:51 2024 +0200

    a few component changes before separating them from shapes

commit 4b1fdf8e04
Author: jacekpoz <jacekpoz@proton.me>
Date:   Mon Aug 12 23:49:52 2024 +0200

    rename button to clickable in one more place

commit 6aeb2124d9
Author: jacekpoz <jacekpoz@proton.me>
Date:   Mon Aug 12 23:49:39 2024 +0200

    improve box creation

commit af3066ab7b
Author: jacekpoz <jacekpoz@proton.me>
Date:   Mon Aug 12 23:48:39 2024 +0200

    add pointer versions of add and remove all in ptk list

commit b95ddb45a0
Author: jacekpoz <jacekpoz@proton.me>
Date:   Mon Aug 12 23:48:14 2024 +0200

    rename button to clickable

commit 50197a6a21
Author: jacekpoz <jacekpoz@proton.me>
Date:   Sun Aug 11 21:09:02 2024 +0200

    change default component members and add PTK_COMPONENT_DEFINE

commit 2b5be4c732
Author: jacekpoz <jacekpoz@proton.me>
Date:   Sun Aug 11 21:07:53 2024 +0200

    extract PTK_LIST_STATIC_INIT into PTK_ARRAY

commit 276138043d
Author: jacekpoz <jacekpoz@proton.me>
Date:   Fri Aug 9 22:04:01 2024 +0200

    make the example target not remove the binary

commit c101379150
Author: jacekpoz <jacekpoz@proton.me>
Date:   Fri Aug 9 17:16:35 2024 +0200

    initial button test
This commit is contained in:
jacekpoz 2024-08-13 14:39:11 +02:00
parent 565aa4d157
commit 3231124346
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
17 changed files with 420 additions and 243 deletions

View file

@ -13,7 +13,7 @@ NAMES = $(OBJ:$(EXAMPLE_DIR)/%=%)
all:
ifdef EXAMPLE
$(MAKE) -f $(EXAMPLE_DIR)/Makefile example-$(EXAMPLE)
$(MAKE) -f $(EXAMPLE_DIR)/Makefile example
else
@echo "you need to pick a test: make example EXAMPLE=<example_name>"
@echo -n "available examples:"
@ -21,7 +21,7 @@ else
@echo
endif
example-%: $(BIN)/$(EXAMPLE_DIR)/%
example: $(BIN)/$(EXAMPLE_DIR)/$(EXAMPLE)
LD_LIBRARY_PATH=$(BIN) $(BIN)/$(EXAMPLE_DIR)/$(EXAMPLE)
$(BIN)/%: %.c

View file

@ -8,7 +8,7 @@ int main(void) {
return EXIT_FAILURE;
}
PtkColor red = { .r = 1.0f, .g = 0.0f, .b = 0.0f };
const PtkColor red = { .r = 1.0f, .g = 0.0f, .b = 0.0f };
return ptk_run(PTK_BOX(
ptk_rect((PtkPos){ .x = 250.0f, .y = 200.0f }, (PtkSize){ .w = 300.0f, .h = 200.0f }, red),

30
examples/button.c Normal file
View file

@ -0,0 +1,30 @@
#include <stdlib.h>
#include <ptk.h>
#include <ptk_log.h>
static PtkRect *r;
static void on_press(const int button, const int action, const int mods) {
(void)button; (void)action; (void)mods;
PTK_DEBUG("pressed!");
r->color.r = r->color.r + (1.0f / 255.0f);
if (r->color.r == 256.0f) {
r->color.r = 0.0f;
}
}
int main(void) {
if (!ptk_init(800, 600, "button", (PtkVersion){ .major = 0, .minor = 1, .patch = 0 })) {
return EXIT_FAILURE;
}
r = (PtkRect *)ptk_rect(
(PtkPos){ .x = 100.0f, .y = 100.0f },
(PtkSize){ .w = 100.0f, .h = 50.0f },
(PtkColor){ .r = 1.0f, .g = 0.0f, .b = 0.0f }
);
return ptk_run(
ptk_clickable((PtkHandle)r, on_press)
);
}

View file

@ -1,6 +1,7 @@
#ifndef PTK_PTK_H_
#define PTK_PTK_H_
#include <ptk_list.h>
#include <ptk_vec.h>
#include <stdbool.h>
@ -13,7 +14,7 @@ typedef struct {
uint32_t patch;
} PtkVersion;
bool ptk_init(size_t width, size_t height, const char *title, PtkVersion application_version);
bool ptk_init(const size_t width, const size_t height, const char *title, const PtkVersion application_version);
typedef struct PtkComponent *PtkHandle;
#define PTK_NULL_HANDLE (void *)0
@ -23,44 +24,52 @@ typedef enum {
PTK_COMPONENT_TYPE_TRIANGLE = 1,
PTK_COMPONENT_TYPE_RECT = 2,
PTK_COMPONENT_TYPE_ELLIPSE = 3,
PTK_COMPONENT_TYPE_CLICKABLE = 4,
} PtkComponentType;
typedef struct PtkComponent {
PtkComponentType type;
} PtkComponent;
PTK_LIST_DEFINE(PtkHandle);
typedef struct PtkBox {
PtkComponentType type;
size_t child_count;
PtkHandle *children;
} PtkBox;
#define PTK_COMPONENT_DEFINE(name, ...) \
typedef struct name {\
PtkComponentType type;\
PtkHandle parent;\
PTK_LIST(PtkHandle) children;\
__VA_ARGS__\
} name
typedef struct PtkTriangle {
PtkComponentType type;
PTK_COMPONENT_DEFINE(PtkComponent, );
PTK_COMPONENT_DEFINE(PtkTriangle,
PtkPos vertices[3];
PtkColor color;
} PtkTriangle;
);
typedef struct PtkRect {
PtkComponentType type;
PTK_COMPONENT_DEFINE(PtkRect,
PtkPos top_left;
PtkSize size;
PtkColor color;
} PtkRect;
);
typedef struct PtkEllipse {
PtkComponentType type;
PTK_COMPONENT_DEFINE(PtkEllipse,
PtkPos center;
PtkSize radii;
PtkColor color;
} PtkEllipse;
);
PtkHandle ptk_box(size_t child_count, PtkHandle *children);
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);
typedef void (*MouseButtonCallback)(const int button, const int action, const int mods);
PTK_COMPONENT_DEFINE(PtkClickable,
PtkHandle hitbox;
MouseButtonCallback on_press;
);
PtkHandle ptk_box(const size_t child_count, PtkHandle *children);
PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkColor color);
PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkColor color);
PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkColor color);
PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkColor color);
PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkColor color);
PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press);
#define PTK_BOX(...) ptk_box(sizeof((PtkHandle []){ __VA_ARGS__ }) / sizeof(PtkHandle), (PtkHandle []) { __VA_ARGS__ })

View file

@ -14,15 +14,15 @@ typedef enum {
PTK_LOG_LEVEL_ALL = INT_MAX,
} PtkLogLevel;
void ptk_log_init(PtkLogLevel level);
void ptk_log_init(const PtkLogLevel level);
void ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, ...);
void ptk_log(const char *file, const int line, const PtkLogLevel level, const char *fmt, ...);
void ptk_err (const char *file, int line, const char *fmt, ...);
void ptk_warn (const char *file, int line, const char *fmt, ...);
void ptk_info (const char *file, int line, const char *fmt, ...);
void ptk_debug(const char *file, int line, const char *fmt, ...);
void ptk_trace(const char *file, int line, const char *fmt, ...);
void ptk_err (const char *file, const int line, const char *fmt, ...);
void ptk_warn (const char *file, const int line, const char *fmt, ...);
void ptk_info (const char *file, const int line, const char *fmt, ...);
void ptk_debug(const char *file, const int line, const char *fmt, ...);
void ptk_trace(const char *file, const int line, const char *fmt, ...);
#define PTK_LOG(level, ...) ptk_log(__FILE__, __LINE__, level, __VA_ARGS__)

View file

@ -1,65 +1,63 @@
#ifndef PTK_PTK_VEC_H_
#define PTK_PTK_VEC_H_
typedef union {
struct { // PtkVec2, PtkPos
typedef struct {
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
struct {
float w;
float h;
};
struct {
float width;
float height;
};
} PtkSize;
typedef struct {
float x;
float y;
float z;
};
struct { // PtkColor
} PtkVec3;
typedef union {
struct {
float r;
float g;
float b;
};
struct { // PtkColor
struct {
float red;
float green;
float blue;
};
} PtkVec3;
} PtkColor;
typedef PtkVec3 PtkColor;
typedef union {
struct { // PtkVec4
typedef struct {
float x;
float y;
float z;
float w;
};
struct { // PtkColorA
} PtkVec4;
typedef union {
struct {
float r;
float g;
float b;
float a;
};
struct { // PtkColorA
struct {
float red;
float green;
float blue;
float alpha;
};
} PtkVec4;
} PtkColorA;
typedef PtkVec4 PtkColorA;
#endif // _PTK_PTK_VEC_H
#endif // PTK_PTK_VEC_H_

110
src/ptk.c
View file

@ -23,11 +23,27 @@
static GLFWwindow *g_window = NULL;
void framebuffer_resize_callback(GLFWwindow *window, int width, int height) {
static void framebuffer_resize_callback(GLFWwindow *window, int width, int height) {
(void)window; (void)width; (void)height;
g_framebuffer_resized = true;
}
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;
}
vk_transfer_vertex_data();
}
}
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
double x, y;
glfwGetCursorPos(window, &x, &y);
vk_handle_mouse_button_input((PtkPos){ .x = x, .y = y }, button, action, mods);
}
PTK_OPTION_DEFINE(PtkLogLevel);
PTK_OPTION(PtkLogLevel) get_log_level(void) {
@ -38,7 +54,7 @@ PTK_OPTION(PtkLogLevel) get_log_level(void) {
return PTK_OPTION_NONE(PtkLogLevel);
}
size_t log_level_len = strlen(log_level);
const size_t log_level_len = strlen(log_level);
char *lowercase = malloc(log_level_len * sizeof(char));
@ -72,8 +88,8 @@ PTK_OPTION(PtkLogLevel) get_log_level(void) {
return ret;
}
bool ptk_init(size_t width, size_t height, const char *title, PtkVersion application_version) {
PTK_OPTION(PtkLogLevel) log_level = get_log_level();
bool ptk_init(const size_t width, const size_t height, const char *title, const PtkVersion application_version) {
const PTK_OPTION(PtkLogLevel) log_level = get_log_level();
if (log_level.exists) {
ptk_log_init(log_level.value);
}
@ -103,6 +119,8 @@ bool ptk_init(size_t width, size_t height, const char *title, PtkVersion applica
}
glfwSetFramebufferSizeCallback(g_window, framebuffer_resize_callback);
glfwSetKeyCallback(g_window, key_callback);
glfwSetMouseButtonCallback(g_window, mouse_button_callback);
vk_components_init();
@ -114,74 +132,82 @@ bool ptk_init(size_t width, size_t height, const char *title, PtkVersion applica
return true;
}
PtkHandle ptk_box(size_t child_count, PtkHandle *children) {
PtkBox *ret = malloc(sizeof(PtkBox));
ret->type = PTK_COMPONENT_TYPE_BOX;
ret->child_count = child_count;
ret->children = children;
PtkHandle ptk_box(const size_t child_count, PtkHandle *children) {
PtkComponent *ret = malloc(sizeof(PtkComponent));
*ret = (PtkComponent){
.type = PTK_COMPONENT_TYPE_BOX,
.children = PTK_LIST_NEW(PtkHandle, child_count),
};
PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count);
return (PtkHandle)ret;
}
PtkHandle ptk_triangle(PtkPos vertices[3], PtkColor color) {
PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkColor color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle));
ret->type = PTK_COMPONENT_TYPE_TRIANGLE;
*ret = (PtkTriangle){
.type = PTK_COMPONENT_TYPE_TRIANGLE,
.color = color,
};
memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3);
ret->color = color;
return (PtkHandle)ret;
}
PtkHandle ptk_rect(PtkPos top_left, PtkSize size, PtkColor color) {
PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkColor color) {
PtkRect *ret = malloc(sizeof(PtkRect));
ret->type = PTK_COMPONENT_TYPE_RECT;
ret->top_left = top_left;
ret->size = size;
ret->color = color;
*ret = (PtkRect){
.type = PTK_COMPONENT_TYPE_RECT,
.top_left = top_left,
.size = size,
.color = color,
};
return (PtkHandle)ret;
}
PtkHandle ptk_square(PtkPos top_left, float size, PtkColor color) {
PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkColor color) {
return ptk_rect(top_left, (PtkSize){ .w = size, .h = size }, color);
}
PtkHandle ptk_ellipse(PtkPos center, PtkSize radii, PtkColor color) {
PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkColor color) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse));
ret->type = PTK_COMPONENT_TYPE_ELLIPSE;
ret->center = center;
ret->radii = radii;
ret->color = color;
*ret = (PtkEllipse){
.type = PTK_COMPONENT_TYPE_ELLIPSE,
.center = center,
.radii = radii,
.color = color,
};
return (PtkHandle)ret;
}
PtkHandle ptk_circle(PtkPos center, float radius, PtkColor color) {
PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkColor color) {
return ptk_ellipse(center, (PtkSize){ .w = radius, .h = radius }, color);
}
void init_components(PtkHandle root) {
switch (root->type) {
case PTK_COMPONENT_TYPE_TRIANGLE: {
vk_triangle((PtkTriangle *)root);
} break;
case PTK_COMPONENT_TYPE_RECT: {
vk_rect((PtkRect *)root);
} break;
case PTK_COMPONENT_TYPE_ELLIPSE: {
vk_ellipse((PtkEllipse *)root);
} break;
case PTK_COMPONENT_TYPE_BOX: {
PtkBox *box = (PtkBox *)root;
for (size_t i = 0; i < box->child_count; ++i) {
init_components(box->children[i]);
}
} break;
PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) {
PtkClickable *ret = malloc(sizeof(PtkClickable));
*ret = (PtkClickable){
.type = PTK_COMPONENT_TYPE_CLICKABLE,
.hitbox = hitbox,
.on_press = on_press,
};
return (PtkHandle)ret;
}
void init_component(PtkHandle component) {
vk_component(component);
for (size_t i = 0; i < component->children.size; ++i) {
PtkHandle child = component->children.data[i];
child->parent = component;
init_component(child);
}
}
int ptk_run(PtkHandle root) {
init_components(root);
init_component(root);
while (!glfwWindowShouldClose(g_window)) {
glfwPollEvents();

18
src/ptk_array.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef PTK_PTK_ARRAY_H_
#define PTK_PTK_ARRAY_H_
#define PTK_ARRAY(T) struct PtkArray_##T
#define PTK_ARRAY_DEFINE(T) \
PTK_ARRAY(T) {\
T *data;\
uint32_t size;\
}\
#define PTK_ARRAY_NEW(T, ...) \
(PTK_ARRAY(T)){\
.data = (T []) __VA_ARGS__ ,\
.size = sizeof((T []) __VA_ARGS__) / sizeof(T),\
}
#endif // PTK_PTK_ARRAY_H_

View file

@ -14,10 +14,14 @@
#define PTK_LIST_ADD_ALL(T, list, ...) _add_PtkList((void **)&list.data, &list.size, &list.allocated, (T []) __VA_ARGS__, sizeof((T []) __VA_ARGS__) / sizeof(T), sizeof(T))
#define PTK_LIST_ADD_ALL_P(T, list, p_elem, elem_count) _add_PtkList((void **)&list.data, &list.size, &list.allocated, p_elem, elem_count, sizeof(T))
#define PTK_LIST_REMOVE(T, list, elem) _remove_PtkList((void *)list.data, &list.size, &elem, 1, sizeof(T))
#define PTK_LIST_REMOVE_ALL(T, list, ...) _remove_PtkList((void *)list.data, &list.size, (T []) __VA_ARGS__, sizeof((T []) __VA_ARGS__) / sizeof(T), sizeof(T))
#define PTK_LIST_REMOVE_ALL_P(T, list, p_elem, elem_count) _remove_PtkList((void *)list.data, &list.size, p_elem, elem_count, sizeof(T))
#define PTK_LIST_REMOVE_AT(T, list, index) _remove_at_PtkList((void *)list.data, &list.size, index, sizeof(T))
bool _grow_PtkList(void **data, uint32_t *allocated, size_t element_size);
@ -39,18 +43,14 @@ bool _remove_at_PtkList(void *data, uint32_t *size, size_t index, size_t element
.allocated = _size,\
}
#define PTK_LIST_STATIC_INIT(T, ...) \
(PTK_LIST(T)){\
.data = (T []) __VA_ARGS__ ,\
.size = sizeof((T []) __VA_ARGS__) / sizeof(T),\
.allocated = sizeof((T []) __VA_ARGS__) / sizeof(T),\
}
#define PTK_LIST_CLEAR(list) \
list.size = 0
#define PTK_LIST_FREE(list) \
free(list.data)
#define PTK_LIST_FILLED(list) \
list.size = list.allocated;
list.size = list.allocated
PTK_LIST_DEFINE(char);

View file

@ -8,7 +8,7 @@
#define BG(r, g, b) "\033[48;2;" #r ";" #g ";" #b "m"
#define RESET "\033[0m"
static const char *log_level_to_str(PtkLogLevel level) {
static const char *log_level_to_str(const PtkLogLevel level) {
const char *ret;
switch (level) {
case PTK_LOG_LEVEL_ERR: ret = FG(235, 28, 35) " ERR" RESET; break;
@ -27,11 +27,11 @@ static PtkLogLevel log_level = PTK_LOG_LEVEL_DEBUG;
static PtkLogLevel log_level = PTK_LOG_LEVEL_WARN;
#endif
void ptk_log_init(PtkLogLevel level) {
void ptk_log_init(const PtkLogLevel level) {
log_level = level;
}
void _ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, va_list args) {
void _ptk_log(const char *file, const int line, const PtkLogLevel level, const char *fmt, va_list args) {
if (log_level < level) { return; }
fprintf(stderr, "[%s | %s:%d] ", log_level_to_str(level), file, line);
@ -41,15 +41,15 @@ void _ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, va
fprintf(stderr, "\n");
}
void ptk_log(const char *file, int line, PtkLogLevel level, const char *fmt, ...) {
void ptk_log(const char *file, const int line, const PtkLogLevel level, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
_ptk_log(file, line, level, fmt, args);
va_end(args);
}
void ptk_err (const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_ERR, fmt, args); va_end(args); }
void ptk_warn (const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_WARN, fmt, args); va_end(args); }
void ptk_info (const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_INFO, fmt, args); va_end(args); }
void ptk_debug(const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_DEBUG, fmt, args); va_end(args); }
void ptk_trace(const char *file, int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_TRACE, fmt, args); va_end(args); }
void ptk_err (const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_ERR, fmt, args); va_end(args); }
void ptk_warn (const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_WARN, fmt, args); va_end(args); }
void ptk_info (const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_INFO, fmt, args); va_end(args); }
void ptk_debug(const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_DEBUG, fmt, args); va_end(args); }
void ptk_trace(const char *file, const int line, const char *fmt, ...) { va_list args; va_start(args, fmt); _ptk_log(file, line, PTK_LOG_LEVEL_TRACE, fmt, args); va_end(args); }

View file

@ -1,11 +1,10 @@
// Copyright (jacekpoz 2024). Licensed under the EUPL-1.2 or later.
#include <ptk.h>
#include <ptk_log.h>
#include <ptk_vk/components.h>
#include <ptk_vk/init.h>
PTK_LIST_DEFINE(PtkHandle);
PTK_LIST(PtkHandle) m_components;
PTK_LIST(Vertex) g_vertices;
@ -23,12 +22,12 @@ void vk_components_init(void) {
g_vertices = PTK_LIST_NEW(Vertex, 1);
}
void _vk_triangle(PtkTriangle *triangle, PtkComponentType type, size_t uvs_offset) {
void triangle(const PtkTriangle *triangle, const size_t uvs_offset) {
for (size_t i = 0; i < 3; ++i) {
Vertex v = {
.pos = triangle->vertices[i],
.color = triangle->color,
.shape_type = type,
.shape_type = triangle->type,
.uv = uvs[i + uvs_offset],
};
@ -36,58 +35,150 @@ void _vk_triangle(PtkTriangle *triangle, PtkComponentType type, size_t uvs_offse
}
}
void vk_triangle(PtkTriangle *triangle) {
PTK_LIST_ADD(PtkHandle, m_components, triangle);
void rect(const PtkRect *rect) {
const float x = rect->top_left.x;
const float y = rect->top_left.y;
const float w = rect->size.w;
const float h = rect->size.h;
_vk_triangle(triangle, PTK_COMPONENT_TYPE_TRIANGLE, 0);
vk_transfer_vertex_data();
}
void _vk_rect(PtkRect *rect, PtkComponentType type) {
PtkPos t1_positions[3];
// top left
t1_positions[0] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y };
// bottom left
t1_positions[1] = (PtkPos){ .x = rect->top_left.x + rect->size.w, .y = rect->top_left.y };
t1_positions[0] = (PtkPos){ .x = x, .y = y };
// top right
t1_positions[2] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y + rect->size.h };
t1_positions[1] = (PtkPos){ .x = x + w, .y = y };
// bottom left
t1_positions[2] = (PtkPos){ .x = x, .y = y + h };
PtkTriangle *t1 = (PtkTriangle *)ptk_triangle((PtkPos *)t1_positions, rect->color);
t1->type = rect->type;
_vk_triangle(t1, type, 0);
triangle(t1, 0);
PtkPos t2_positions[3];
// bottom left
t2_positions[0] = (PtkPos){ .x = rect->top_left.x + rect->size.w, .y = rect->top_left.y };
// top right
t2_positions[1] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y + rect->size.h };
t2_positions[0] = (PtkPos){ .x = x + w, .y = y };
// bottom left
t2_positions[1] = (PtkPos){ .x = x, .y = y + h };
// bottom right
t2_positions[2] = (PtkPos){ .x = rect->top_left.x + rect->size.w, .y = rect->top_left.y + rect->size.h };
t2_positions[2] = (PtkPos){ .x = x + w, .y = y + h };
PtkTriangle *t2 = (PtkTriangle *)ptk_triangle((PtkPos *)t2_positions, rect->color);
t2->type = rect->type;
_vk_triangle(t2, type, 1);
vk_transfer_vertex_data();
triangle(t2, 1);
}
void vk_rect(PtkRect *rect) {
PTK_LIST_ADD(PtkHandle, m_components, rect);
_vk_rect(rect, PTK_COMPONENT_TYPE_RECT);
}
void vk_ellipse(PtkEllipse *ellipse) {
PTK_LIST_ADD(PtkHandle, m_components, ellipse);
PtkPos top_left = {
void ellipse(const PtkEllipse *ellipse) {
const PtkPos top_left = {
.x = ellipse->center.x - ellipse->radii.w,
.y = ellipse->center.y - ellipse->radii.h,
};
PtkSize size = {
const PtkSize size = {
.w = ellipse->radii.w * 2.0f,
.h = ellipse->radii.h * 2.0f,
};
_vk_rect((PtkRect *)ptk_rect(top_left, size, ellipse->color), PTK_COMPONENT_TYPE_ELLIPSE);
PtkRect *r = (PtkRect *)ptk_rect(top_left, size, ellipse->color);
r->type = ellipse->type;
rect(r);
}
void vk_component(PtkHandle component) {
if (component->type == PTK_COMPONENT_TYPE_BOX) {
return;
}
PTK_LIST_ADD(PtkHandle, m_components, component);
if (component->type == PTK_COMPONENT_TYPE_CLICKABLE) {
PtkClickable *c = (PtkClickable *)component;
vk_component(c->hitbox);
return;
}
switch (component->type) {
case PTK_COMPONENT_TYPE_TRIANGLE: {
triangle((PtkTriangle *)component, 0);
} break;
case PTK_COMPONENT_TYPE_RECT: {
rect((PtkRect *)component);
} break;
case PTK_COMPONENT_TYPE_ELLIPSE: {
ellipse((PtkEllipse *)component);
} break;
default: {
return;
}
}
vk_transfer_vertex_data();
}
inline bool triangle_intersects(const PtkTriangle t, const PtkPos p) {
const PtkPos p0 = t.vertices[0];
const PtkPos p1 = t.vertices[1];
const PtkPos p2 = t.vertices[2];
const float a = 1.0f/2.0f * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
const int sign = a < 0.0f ? -1 : 1;
const float s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
const float r = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
return s > 0 && r > 0 && (s + r) < 2 * a * sign;
}
inline bool rect_intersects(const PtkRect r, const PtkPos p) {
const bool intersects_x = p.x >= r.top_left.x &&
p.x <= r.top_left.x + r.size.w;
const bool intersects_y = p.y >= r.top_left.y &&
p.y <= r.top_left.y + r.size.h;
return intersects_x && intersects_y;
}
inline bool ellipse_intersects(const PtkEllipse e, const PtkPos p) {
const float x = p.x - e.center.x;
const float rx = e.radii.w;
const float y = p.y - e.center.y;
const float ry = e.radii.h;
return ((x * x) / (rx * rx)) + ((y * y) / (ry * ry)) <= 1.0f;
}
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)) {
return true;
}
}
return false;
}
case PTK_COMPONENT_TYPE_TRIANGLE:
return triangle_intersects(*(PtkTriangle *)component, point);
case PTK_COMPONENT_TYPE_RECT:
return rect_intersects(*(PtkRect *)component, point);
case PTK_COMPONENT_TYPE_ELLIPSE:
return ellipse_intersects(*(PtkEllipse *)component, point);
case PTK_COMPONENT_TYPE_CLICKABLE:
return intersects(((PtkClickable *)component)->hitbox, point);
}
}
void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mods) {
for (size_t i = 0; i < m_components.size; ++i) {
const PtkHandle current = m_components.data[i];
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
continue;
}
const PtkClickable *c = (PtkClickable *)current;
if (intersects((PtkHandle)c, cursor_pos)) {
c->on_press(button, action, mods);
}
}
}
void vk_components_cleanup(void) {

View file

@ -19,9 +19,9 @@ extern PTK_LIST(Vertex) g_vertices;
void vk_components_init(void);
void vk_triangle(PtkTriangle *triangle);
void vk_rect(PtkRect *rect);
void vk_ellipse(PtkEllipse *ellipse);
void vk_component(PtkHandle component);
void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mode);
void vk_components_cleanup(void);

View file

@ -16,7 +16,7 @@ bool vk_draw_frame(void) {
vkWaitForFences(g_dev, 1, &g_in_flight_fences.data[g_current_frame], VK_TRUE, UINT64_MAX);
uint32_t image_index;
VkResult res = vkAcquireNextImageKHR(
const VkResult acquire_next_image_result = vkAcquireNextImageKHR(
g_dev,
g_swapchain,
UINT64_MAX,
@ -25,13 +25,13 @@ bool vk_draw_frame(void) {
&image_index
);
if (res == VK_ERROR_OUT_OF_DATE_KHR) {
if (acquire_next_image_result == VK_ERROR_OUT_OF_DATE_KHR) {
if (!vk_recreate_swapchain()) {
return false;
}
return true;
} else if (res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR) {
PTK_ERR("%s", vk_result_string(res));
} else if (acquire_next_image_result != VK_SUCCESS && acquire_next_image_result != VK_SUBOPTIMAL_KHR) {
PTK_ERR("%s", vk_result_string(acquire_next_image_result));
return false;
}
@ -45,7 +45,7 @@ bool vk_draw_frame(void) {
return false;
}
VkSemaphore signal_semaphores[] = {g_render_finished_semaphores.data[g_current_frame]};
const VkSemaphore signal_semaphores[] = {g_render_finished_semaphores.data[g_current_frame]};
VK_TRY(false,
vkQueueSubmit(
@ -66,7 +66,7 @@ bool vk_draw_frame(void) {
)
);
res = vkQueuePresentKHR(g_present_queue, &(VkPresentInfoKHR){
const VkResult queue_present_result = vkQueuePresentKHR(g_present_queue, &(VkPresentInfoKHR){
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.pNext = NULL,
.waitSemaphoreCount = 1,
@ -77,14 +77,18 @@ bool vk_draw_frame(void) {
.pResults = NULL,
});
if (res == VK_ERROR_OUT_OF_DATE_KHR || res == VK_SUBOPTIMAL_KHR || g_framebuffer_resized) {
if (
queue_present_result == VK_ERROR_OUT_OF_DATE_KHR ||
queue_present_result == VK_SUBOPTIMAL_KHR ||
g_framebuffer_resized
) {
g_framebuffer_resized = false;
if (!vk_recreate_swapchain()) {
return false;
}
PTK_TRACE("recreated swapchain");
} else if (res != VK_SUCCESS) {
PTK_ERR("%s", vk_result_string(res));
} else if (queue_present_result != VK_SUCCESS) {
PTK_ERR("%s", vk_result_string(queue_present_result));
return false;
}

View file

@ -8,6 +8,7 @@
#include <ptk_log.h>
#include <ptk_option.h>
#include <ptk_array.h>
#include <ptk_list.h>
#include <stdio.h>
#include <string.h>
@ -20,7 +21,7 @@ PTK_OPTION_DEFINE(uint32_t);
PTK_LIST_DEFINE(VkSurfaceFormatKHR);
PTK_LIST_DEFINE(VkPresentModeKHR);
PTK_LIST_DEFINE(VkFramebuffer);
PTK_LIST_DEFINE(VkVertexInputAttributeDescription);
PTK_ARRAY_DEFINE(VkVertexInputAttributeDescription);
PTK_LIST_DEFINE(VkExtensionProperties);
PTK_LIST_DEFINE(VkQueueFamilyProperties);
PTK_OPTION_DEFINE(VkShaderModule);
@ -87,7 +88,7 @@ static const VkVertexInputBindingDescription m_vertex_binding_description = {
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
};
PTK_LIST(VkVertexInputAttributeDescription) m_vertex_attribute_descriptions = PTK_LIST_STATIC_INIT(VkVertexInputAttributeDescription, {
PTK_ARRAY(VkVertexInputAttributeDescription) m_vertex_attribute_descriptions = PTK_ARRAY_NEW(VkVertexInputAttributeDescription, {
(VkVertexInputAttributeDescription){
.location = 0,
.binding = 0,
@ -109,7 +110,7 @@ PTK_LIST(VkVertexInputAttributeDescription) m_vertex_attribute_descriptions = PT
(VkVertexInputAttributeDescription){
.location = 3,
.binding = 0,
.format = VK_FORMAT_R32G32_SINT,
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = offsetof(Vertex, uv),
},
});
@ -144,7 +145,7 @@ static const char * const g_validation_layers[] = {
"VK_LAYER_KHRONOS_validation"
};
bool check_validation_layers(const char * const *validation_layers, uint32_t validation_layer_count) {
bool check_validation_layers(const char * const *validation_layers, const uint32_t validation_layer_count) {
PTK_LIST(VkLayerProperties) available_layers;
vkEnumerateInstanceLayerProperties(&available_layers.allocated, NULL);
@ -181,11 +182,11 @@ bool create_vk_instance(const char *title, const PtkVersion version) {
PTK_ERR("couldn't find requested validation layer");
return false;
}
uint32_t enabledLayerCount = g_validation_layer_count;
const char * const *ppEnabledLayerNames = g_validation_layers;
const uint32_t enabled_layer_count = g_validation_layer_count;
const char * const *enabled_layer_names = g_validation_layers;
#else
uint32_t enabledLayerCount = 0;
const char * const *ppEnabledLayerNames = NULL;
const uint32_t enabled_layer_count = 0;
const char * const *enabled_layer_names = NULL;
#endif
uint32_t extension_count = 0;
@ -206,8 +207,8 @@ bool create_vk_instance(const char *title, const PtkVersion version) {
.engineVersion = VK_MAKE_API_VERSION(0, PTK_VERSION_MAJOR, PTK_VERSION_MINOR, PTK_VERSION_PATCH),
.apiVersion = VK_API_VERSION_1_3,
},
.enabledLayerCount = enabledLayerCount,
.ppEnabledLayerNames = ppEnabledLayerNames,
.enabledLayerCount = enabled_layer_count,
.ppEnabledLayerNames = enabled_layer_names,
.enabledExtensionCount = extension_count,
.ppEnabledExtensionNames = extension_names,
},
@ -293,7 +294,7 @@ 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) {
VkSurfaceFormatKHR current_format = available_formats.data[i];
const VkSurfaceFormatKHR current_format = available_formats.data[i];
if (current_format.format == VK_FORMAT_B8G8R8A8_SRGB && current_format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return current_format;
}
@ -304,7 +305,7 @@ VkSurfaceFormatKHR select_swap_surface_format(const PTK_LIST(VkSurfaceFormatKHR)
VkPresentModeKHR select_swap_present_mode(const PTK_LIST(VkPresentModeKHR) available_present_modes) {
for (size_t i = 0; i < available_present_modes.size; ++i) {
VkPresentModeKHR current_present_mode = available_present_modes.data[i];
const VkPresentModeKHR current_present_mode = available_present_modes.data[i];
if (current_present_mode == VK_PRESENT_MODE_MAILBOX_KHR) {
return current_present_mode;
}
@ -313,7 +314,7 @@ VkPresentModeKHR select_swap_present_mode(const PTK_LIST(VkPresentModeKHR) avail
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D select_swap_extent(VkSurfaceCapabilitiesKHR capabilities) {
VkExtent2D select_swap_extent(const VkSurfaceCapabilitiesKHR capabilities) {
if (capabilities.currentExtent.width != UINT32_MAX) {
return capabilities.currentExtent;
}
@ -342,7 +343,7 @@ VkExtent2D select_swap_extent(VkSurfaceCapabilitiesKHR capabilities) {
return actual_extent;
}
SwapchainSupportInfo query_swapchain_support(VkPhysicalDevice dev) {
SwapchainSupportInfo query_swapchain_support(const VkPhysicalDevice dev) {
SwapchainSupportInfo info;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(dev, m_surface, &info.capabilities);
@ -377,7 +378,7 @@ SwapchainSupportInfo query_swapchain_support(VkPhysicalDevice dev) {
return info;
}
bool are_extensions_supported(VkPhysicalDevice dev) {
bool are_extensions_supported(const VkPhysicalDevice dev) {
PTK_LIST(VkExtensionProperties) available_extensions;
vkEnumerateDeviceExtensionProperties(dev, NULL, &available_extensions.allocated, NULL);
@ -399,7 +400,7 @@ bool are_extensions_supported(VkPhysicalDevice dev) {
return supported_extensions == m_device_extension_count;
}
bool is_device_suitable(VkPhysicalDevice dev) {
bool is_device_suitable(const VkPhysicalDevice dev) {
PTK_LIST(VkQueueFamilyProperties) queue_families;
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queue_families.allocated, NULL);
@ -421,13 +422,13 @@ bool is_device_suitable(VkPhysicalDevice dev) {
}
}
bool indices_found = m_queue_family_indices.graphics.exists
const bool indices_found = m_queue_family_indices.graphics.exists
&& m_queue_family_indices.present.exists;
bool extensions_supported = are_extensions_supported(dev);
const bool extensions_supported = are_extensions_supported(dev);
bool swapchain_adequate = false;
if (extensions_supported) {
SwapchainSupportInfo swapchain_support = query_swapchain_support(dev);
const SwapchainSupportInfo swapchain_support = query_swapchain_support(dev);
swapchain_adequate = swapchain_support.formats.size != 0
&& swapchain_support.present_modes.size != 0;
PTK_LIST_FREE(swapchain_support.formats);
@ -448,7 +449,7 @@ bool create_logical_dev(void) {
VkDeviceQueueCreateInfo queue_create_infos[m_queue_family_count];
for (size_t i = 0; i < m_queue_family_count; ++i) {
PTK_OPTION(uint32_t) index = *(((PTK_OPTION(uint32_t) *)&m_queue_family_indices) + i);
const PTK_OPTION(uint32_t) index = *(((PTK_OPTION(uint32_t) *)&m_queue_family_indices) + i);
queue_create_infos[i] = (VkDeviceQueueCreateInfo){
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
@ -461,11 +462,11 @@ bool create_logical_dev(void) {
}
#ifdef DEBUG
uint32_t enabledLayerCount = g_validation_layer_count;
const char * const *ppEnabledLayerNames = g_validation_layers;
const uint32_t enabled_layer_count = g_validation_layer_count;
const char * const *enabled_layer_names = g_validation_layers;
#else
uint32_t enabledLayerCount = 0;
const char * const *ppEnabledLayerNames = NULL;
const uint32_t enabled_layer_count = 0;
const char * const *enabled_layer_names = NULL;
#endif
VK_TRY(false,
@ -477,8 +478,8 @@ bool create_logical_dev(void) {
.flags = 0,
.queueCreateInfoCount = m_queue_family_count,
.pQueueCreateInfos = queue_create_infos,
.enabledLayerCount = enabledLayerCount,
.ppEnabledLayerNames = ppEnabledLayerNames,
.enabledLayerCount = enabled_layer_count,
.ppEnabledLayerNames = enabled_layer_names,
.enabledExtensionCount = m_device_extension_count,
.ppEnabledExtensionNames = m_device_extensions,
.pEnabledFeatures = &(VkPhysicalDeviceFeatures){0},
@ -495,11 +496,11 @@ bool create_logical_dev(void) {
}
bool create_swapchain(void) {
SwapchainSupportInfo swapchain_support = query_swapchain_support(m_physical_dev);
const SwapchainSupportInfo swapchain_support = query_swapchain_support(m_physical_dev);
VkSurfaceFormatKHR surface_format = select_swap_surface_format(swapchain_support.formats);
VkPresentModeKHR present_mode = select_swap_present_mode(swapchain_support.present_modes);
VkExtent2D extent = select_swap_extent(swapchain_support.capabilities);
const VkSurfaceFormatKHR surface_format = select_swap_surface_format(swapchain_support.formats);
const VkPresentModeKHR present_mode = select_swap_present_mode(swapchain_support.present_modes);
const VkExtent2D extent = select_swap_extent(swapchain_support.capabilities);
uint32_t image_count = swapchain_support.capabilities.minImageCount + 1;
@ -507,7 +508,7 @@ bool create_swapchain(void) {
image_count = swapchain_support.capabilities.maxImageCount;
}
bool queue_families_differ = m_queue_family_indices.graphics.value != m_queue_family_indices.present.value;
const bool queue_families_differ = m_queue_family_indices.graphics.value != m_queue_family_indices.present.value;
VK_TRY(false,
vkCreateSwapchainKHR(
@ -621,7 +622,7 @@ PTK_STRING read_spv(const char *filename) {
buffer = PTK_STRING_NEW(buffer.allocated);
size_t items_read = fread(buffer.data, sizeof(char), buffer.allocated, file);
const size_t items_read = fread(buffer.data, sizeof(char), buffer.allocated, file);
if (items_read != buffer.allocated) {
if (feof(file)) {
@ -639,7 +640,7 @@ PTK_STRING read_spv(const char *filename) {
return buffer;
}
PTK_OPTION(VkShaderModule) create_shader_module(PTK_STRING code) {
PTK_OPTION(VkShaderModule) create_shader_module(const PTK_STRING code) {
VkShaderModule shader_module;
VK_TRY(PTK_OPTION_NONE(VkShaderModule),
@ -741,24 +742,24 @@ bool create_descriptor_set_layout(void) {
}
bool create_graphics_pipeline(void) {
PTK_STRING vert_shader_code = read_spv("target/shaders/shader.vert.spv");
PTK_STRING frag_shader_code = read_spv("target/shaders/shader.frag.spv");
const PTK_STRING vert_shader_code = read_spv("target/shaders/shader.vert.spv");
const PTK_STRING frag_shader_code = read_spv("target/shaders/shader.frag.spv");
PTK_OPTION(VkShaderModule) vert_shader_module = create_shader_module(vert_shader_code);
const PTK_OPTION(VkShaderModule) vert_shader_module = create_shader_module(vert_shader_code);
if (!vert_shader_module.exists) {
PTK_ERR("failed creating vert shader module");
return false;
}
PTK_OPTION(VkShaderModule) frag_shader_module = create_shader_module(frag_shader_code);
const PTK_OPTION(VkShaderModule) frag_shader_module = create_shader_module(frag_shader_code);
if (!frag_shader_module.exists) {
PTK_ERR("failed creating frag shader module");
return false;
}
VkSpecializationInfo spec_info = {
const VkSpecializationInfo spec_info = {
.mapEntryCount = 1,
.pMapEntries = &(VkSpecializationMapEntry){
.constantID = 0,
@ -769,7 +770,7 @@ bool create_graphics_pipeline(void) {
.pData = &(int){PTK_COMPONENT_TYPE_ELLIPSE},
};
VkPipelineShaderStageCreateInfo shader_stages[] = {
const VkPipelineShaderStageCreateInfo shader_stages[] = {
(VkPipelineShaderStageCreateInfo){
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = NULL,
@ -790,7 +791,7 @@ bool create_graphics_pipeline(void) {
},
};
VkPipelineVertexInputStateCreateInfo vertex_input_info = {
const VkPipelineVertexInputStateCreateInfo vertex_input_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -800,7 +801,7 @@ bool create_graphics_pipeline(void) {
.pVertexAttributeDescriptions = m_vertex_attribute_descriptions.data,
};
VkPipelineDynamicStateCreateInfo dynamic_state = {
const VkPipelineDynamicStateCreateInfo dynamic_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -808,7 +809,7 @@ bool create_graphics_pipeline(void) {
.pDynamicStates = NULL,
};
VkPipelineInputAssemblyStateCreateInfo input_assembly = {
const VkPipelineInputAssemblyStateCreateInfo input_assembly = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -816,7 +817,7 @@ bool create_graphics_pipeline(void) {
.primitiveRestartEnable = VK_FALSE,
};
VkPipelineViewportStateCreateInfo viewport_state = {
const VkPipelineViewportStateCreateInfo viewport_state = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -839,7 +840,7 @@ bool create_graphics_pipeline(void) {
},
};
VkPipelineRasterizationStateCreateInfo rasterizer = {
const VkPipelineRasterizationStateCreateInfo rasterizer = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -855,7 +856,7 @@ bool create_graphics_pipeline(void) {
.lineWidth = 1.0f,
};
VkPipelineMultisampleStateCreateInfo multisampling = {
const VkPipelineMultisampleStateCreateInfo multisampling = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -867,7 +868,7 @@ bool create_graphics_pipeline(void) {
.alphaToOneEnable = VK_FALSE,
};
VkPipelineColorBlendAttachmentState color_blend_attachment = {
const VkPipelineColorBlendAttachmentState color_blend_attachment = {
.blendEnable = VK_TRUE,
.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA,
.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
@ -878,7 +879,7 @@ bool create_graphics_pipeline(void) {
.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT,
};
VkPipelineColorBlendStateCreateInfo color_blending = {
const VkPipelineColorBlendStateCreateInfo color_blending = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -992,7 +993,7 @@ bool create_command_pool(void) {
return true;
}
PTK_OPTION(uint32_t) find_memory_type(uint32_t type_filter, VkMemoryPropertyFlags props) {
PTK_OPTION(uint32_t) find_memory_type(const uint32_t type_filter, const VkMemoryPropertyFlags props) {
VkPhysicalDeviceMemoryProperties mem_props;
vkGetPhysicalDeviceMemoryProperties(m_physical_dev, &mem_props);
@ -1005,7 +1006,7 @@ PTK_OPTION(uint32_t) find_memory_type(uint32_t type_filter, VkMemoryPropertyFlag
return PTK_OPTION_NONE(uint32_t);
}
bool create_buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags props, VkBuffer *buffer, VkDeviceMemory *buffer_memory) {
bool create_buffer(const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags props, VkBuffer *buffer, VkDeviceMemory *buffer_memory) {
VK_TRY(false,
vkCreateBuffer(
g_dev,
@ -1027,7 +1028,7 @@ bool create_buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryProperty
VkMemoryRequirements mem_reqs;
vkGetBufferMemoryRequirements(g_dev, *buffer, &mem_reqs);
PTK_OPTION(uint32_t) memory_type = find_memory_type(mem_reqs.memoryTypeBits, props);
const PTK_OPTION(uint32_t) memory_type = find_memory_type(mem_reqs.memoryTypeBits, props);
if (!memory_type.exists) {
PTK_ERR("failed to find suitable memory type");
@ -1053,7 +1054,7 @@ bool create_buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryProperty
return true;
}
bool copy_buffer(VkBuffer src, VkBuffer dst, VkDeviceSize size) {
bool copy_buffer(const VkBuffer src, const VkBuffer dst, const VkDeviceSize size) {
VkCommandBuffer command_buffer;
VK_TRY(false,
@ -1117,12 +1118,12 @@ 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);
Vertex current = g_vertices.data[i];
const Vertex current = g_vertices.data[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…");
VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
VkBuffer staging_buffer;
VkDeviceMemory staging_buffer_memory;
@ -1156,8 +1157,8 @@ bool vk_transfer_vertex_data(void) {
}
bool create_vertex_buffer(void) {
// VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
VkDeviceSize buffer_size = 65536;
// const VkDeviceSize buffer_size = sizeof(g_vertices.data[0]) * g_vertices.size;
const VkDeviceSize buffer_size = 65536;
if (
!create_buffer(
buffer_size,
@ -1175,7 +1176,7 @@ bool create_vertex_buffer(void) {
}
bool create_uniform_buffers(void) {
VkDeviceSize buffer_size = sizeof(UniformBufferObject);
const VkDeviceSize buffer_size = sizeof(UniformBufferObject);
m_uniform_buffers = PTK_LIST_NEW(VkBuffer, g_max_frames_in_flight);
m_uniform_buffers_memory = PTK_LIST_NEW(VkDeviceMemory, g_max_frames_in_flight);
@ -1297,7 +1298,7 @@ bool allocate_command_buffers(void) {
return true;
}
bool vk_record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) {
bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32_t image_index) {
VK_TRY(false,
vkBeginCommandBuffer(
command_buffer,
@ -1359,7 +1360,7 @@ bool create_sync_objects(void) {
g_render_finished_semaphores = PTK_LIST_NEW(VkSemaphore, g_max_frames_in_flight);
g_in_flight_fences = PTK_LIST_NEW(VkFence, g_max_frames_in_flight);
VkSemaphoreCreateInfo semaphore_info = {
const VkSemaphoreCreateInfo semaphore_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
@ -1437,7 +1438,7 @@ bool vk_recreate_swapchain(void) {
return true;
}
bool vk_update_uniform_buffer(size_t current_frame) {
bool vk_update_uniform_buffer(const size_t current_frame) {
m_uniform_buffer_object.window_size.w = m_swapchain_extent.width;
m_uniform_buffer_object.window_size.h = m_swapchain_extent.height;
@ -1446,7 +1447,7 @@ bool vk_update_uniform_buffer(size_t current_frame) {
return true;
}
bool vk_init(GLFWwindow *window, size_t width, size_t height, const char *title, const PtkVersion version) {
bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const char *title, const PtkVersion version) {
m_window = window;
m_uniform_buffer_object.initial_window_size.w = width;
m_uniform_buffer_object.initial_window_size.h = height;

View file

@ -28,15 +28,15 @@ extern PTK_LIST(VkSemaphore) g_image_available_semaphores;
extern PTK_LIST(VkSemaphore) g_render_finished_semaphores;
extern PTK_LIST(VkFence) g_in_flight_fences;
bool vk_init(GLFWwindow *window, size_t width, size_t height, const char *title, const PtkVersion version);
bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const char *title, const PtkVersion version);
bool vk_transfer_vertex_data(void);
bool vk_record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index);
bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32_t image_index);
bool vk_recreate_swapchain(void);
bool vk_update_uniform_buffer(size_t current_frame);
bool vk_update_uniform_buffer(const size_t current_frame);
void vk_cleanup(void);

View file

@ -9,7 +9,7 @@
const char *vk_result_string(VkResult res);
#define VK_TRY(return_value, ...) do {\
VkResult res = __VA_ARGS__;\
const VkResult res = __VA_ARGS__;\
if (res != VK_SUCCESS) {\
PTK_ERR("%s", vk_result_string(res));\
return return_value;\

View file

@ -7,7 +7,7 @@ PTK_LIST_DEFINE(uint32_t);
TEST_START()
TEST("create list", {
size_t size = 5;
const size_t size = 5;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
TEST_ASSERT(list.allocated == size, "incorrect list allocation");
@ -15,7 +15,7 @@ TEST_START()
});
TEST("add elements without growing", {
size_t size = 5;
const size_t size = 5;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -29,7 +29,7 @@ TEST_START()
});
TEST("add elements and grow", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -45,7 +45,7 @@ TEST_START()
});
TEST("add multiple elements", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD_ALL(uint32_t, list, { 21, 37, 2137, 31, 27, 7312 });
@ -56,7 +56,7 @@ TEST_START()
});
TEST("remove elements", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -73,7 +73,7 @@ TEST_START()
});
TEST("remove multiple elements", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});
@ -90,7 +90,7 @@ TEST_START()
});
TEST("remove elements at index", {
size_t size = 1;
const size_t size = 1;
PTK_LIST(uint32_t) list = PTK_LIST_NEW(uint32_t, size);
PTK_LIST_ADD(uint32_t, list, (uint32_t){21});