Added time to draw function and started to layout game ui. Add window resizing next and in-shader anchoring next.

main
noah metz 2024-10-21 21:57:05 -06:00
parent 79e3f99d4f
commit d448eb81d1
9 changed files with 121 additions and 30 deletions

@ -5,7 +5,8 @@
#include "ui.h" #include "ui.h"
VkResult draw_frame( VkResult draw_frame(
RenderContext* context); RenderContext* context,
double time);
VkResult record_draw_commands( VkResult record_draw_commands(
RenderContext* context, RenderContext* context,

@ -127,7 +127,7 @@ typedef struct UIlayerStorageStruct {
} UILayerStorage; } UILayerStorage;
typedef struct UIContainerStruct { typedef struct UIContainerStruct {
vec2 pos; vec2 offset;
vec2 size; vec2 size;
} UIContainer; } UIContainer;
@ -220,7 +220,8 @@ typedef struct UILayerInputStruct {
typedef struct UIContainerInputStruct { typedef struct UIContainerInputStruct {
uint32_t id; uint32_t id;
vec2 pos; uint32_t anchor;
vec2 offset;
vec2 size; vec2 size;
uint32_t layer_count; uint32_t layer_count;

@ -25,7 +25,7 @@ layout(location = 0) out vec4 outColor;
void main() { void main() {
vec2 pos = (gl_FragCoord.xy - vec2(0.5, 0.5))/pc.context.scale; vec2 pos = (gl_FragCoord.xy - vec2(0.5, 0.5))/pc.context.scale;
vec2 min = pc.layer.container.pos; vec2 min = pc.layer.container.offset;
vec2 max = min + pc.layer.container.size; vec2 max = min + pc.layer.container.size;
if(pos.x < min.x || pos.y < min.y if(pos.x < min.x || pos.y < min.y
|| pos.x > max.x || pos.y > max.y) { || pos.x > max.x || pos.y > max.y) {

@ -38,7 +38,7 @@ void main() {
fragUV = pos; fragUV = pos;
} }
gl_Position = vec4((pos * drawable.size + drawable.pos + pc.layer.container.pos) * pc.context.screen * 2, 0.0, 1.0) - vec4(1.0, 1.0, 0.0, 0.0); gl_Position = vec4((pos * drawable.size + drawable.pos + pc.layer.container.offset) * pc.context.screen * 2, 0.0, 1.0) - vec4(1.0, 1.0, 0.0, 0.0);
fragColor = drawable.color; fragColor = drawable.color;
code = drawable.code; code = drawable.code;

@ -69,7 +69,7 @@ layout(std430, buffer_reference) readonly buffer DrawableList {
}; };
layout(std430, buffer_reference) readonly buffer Container { layout(std430, buffer_reference) readonly buffer Container {
vec2 pos; vec2 offset;
vec2 size; vec2 size;
}; };

@ -141,7 +141,8 @@ VkResult record_draw_commands(
} }
VkResult draw_frame( VkResult draw_frame(
RenderContext* context) { RenderContext* context,
double time) {
VkResult result; VkResult result;
result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX); result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX);

@ -7,7 +7,7 @@
const uint32_t MAX_FRAMES_IN_FLIGHT = 2; const uint32_t MAX_FRAMES_IN_FLIGHT = 2;
const uint32_t WINDOW_WIDTH = 800; const uint32_t WINDOW_WIDTH = 800;
const uint32_t WINDOW_HEIGHT = 500; const uint32_t WINDOW_HEIGHT = 600;
const char * validation_layers[] = { const char * validation_layers[] = {
"VK_LAYER_KHRONOS_validation", "VK_LAYER_KHRONOS_validation",

@ -32,50 +32,103 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
} }
VK_RESULT(load_font("test.ttf", 16, VK_TRUE, library, render, &ui, &font_index)); VK_RESULT(load_font("test.ttf", 16, VK_TRUE, library, render, &ui, &font_index));
VK_RESULT(load_texture("background.png", render, &ui, &background_index));
UIDrawable rects[] = { vec2 map_size = {250, 200};
UIDrawable map_rects[] = {
{ {
.pos = {0.0, 0.0}, .pos = {0.0, 0.0},
.size = {WINDOW_WIDTH, WINDOW_HEIGHT}, .size = {map_size[0], map_size[1]},
.color = {1.0, 1.0, 1.0, 1.0}, .color = {0.0, 0.5, 0.8, 1.0},
.type = 2, .type = 0,
.code = background_index, },
};
UILayerInput map_layers[] = {
{
.num_drawables = sizeof(map_rects)/sizeof(UIDrawable),
.drawables = map_rects,
}, },
};
UIContainerInput map_info = {
.id = 0x01,
.offset = {WINDOW_WIDTH-map_size[0], 0.0},
.size = {map_size[0], map_size[1]},
.layer_count = sizeof(map_layers)/sizeof(UILayerInput),
.layers = map_layers,
};
vec2 inventory_size = {map_size[0], WINDOW_HEIGHT-map_size[1]};
UIDrawable inventory_rects[] = {
{
.pos = {0, 0},
.size = {inventory_size[0], inventory_size[1]},
.color = {0.5, 0.8, 0.0, 1.0},
.type = 0,
},
};
UILayerInput inventory_layers[] = {
{
.num_drawables = sizeof(inventory_rects)/sizeof(UIDrawable),
.drawables = inventory_rects,
},
};
UIContainerInput inventory_info = {
.id = 0x02,
.offset = {WINDOW_WIDTH-inventory_size[0], WINDOW_HEIGHT-inventory_size[1]},
.size = {inventory_size[0], inventory_size[1]},
.layer_count = sizeof(inventory_layers)/sizeof(UILayerInput),
.layers = inventory_layers,
};
vec2 chat_size = {WINDOW_WIDTH-inventory_size[0], 200};
UIDrawable chat_rects[] = {
{ {
.pos = {150.0, 100.0}, .pos = {0, 0},
.size = {500.0, 300.0}, .size = {chat_size[0], chat_size[1]},
.color = {1.0, 1.0, 1.0, 1.0}, .color = {0.8, 0.0, 0.5, 1.0},
.type = 0,
}, },
}; };
UILayerInput layers[] = { UILayerInput chat_layers[] = {
{ {
.num_drawables = sizeof(rects)/sizeof(UIDrawable), .num_drawables = sizeof(chat_rects)/sizeof(UIDrawable),
.drawables = rects, .drawables = chat_rects,
}, },
}; };
UIContainerInput container_info = { UIContainerInput chat_info = {
.id = 0xDEADBEEF, .id = 0x02,
.pos = {0.0, 0.0}, .offset = {0, WINDOW_HEIGHT-chat_size[1]},
.size = {WINDOW_WIDTH, WINDOW_HEIGHT}, .size = {chat_size[0], chat_size[1]},
.layer_count = sizeof(layers)/sizeof(UILayerInput), .layer_count = sizeof(chat_layers)/sizeof(UILayerInput),
.layers = layers, .layers = chat_layers,
}; };
VK_RESULT(create_container(&container_info, render, &ui)); VK_RESULT(create_container(&map_info, render, &ui));
VK_RESULT(create_container(&inventory_info, render, &ui));
VK_RESULT(create_container(&chat_info, render, &ui));
VK_RESULT(record_draw_commands(render, &ui)); VK_RESULT(record_draw_commands(render, &ui));
double last_frame_time = glfwGetTime();
while(glfwWindowShouldClose(window) == 0) { while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents(); glfwPollEvents();
result = draw_frame(render); double frame_time = glfwGetTime();
double delta_time = frame_time - last_frame_time;
result = draw_frame(render, frame_time);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
return result; return result;
} }
last_frame_time = frame_time;
} }
return 0; return 0;
} }
@ -88,12 +141,47 @@ int network_thread() {
return 0; return 0;
} }
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
switch(button) {
// Handle camera hover
case GLFW_MOUSE_BUTTON_MIDDLE:
if(action == GLFW_PRESS) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
break;
case GLFW_MOUSE_BUTTON_LEFT:
if(action == GLFW_PRESS) {
}
break;
case GLFW_MOUSE_BUTTON_RIGHT:
if(action == GLFW_PRESS) {
}
break;
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
}
void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) {
}
int main() { int main() {
GLFWwindow* window = init_window(); GLFWwindow* window = init_window();
if(window == NULL) { if(window == NULL) {
return 1; return 1;
} }
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback);
RenderContext render = {}; RenderContext render = {};
if(init_vulkan(window, &render) != VK_SUCCESS) { if(init_vulkan(window, &render) != VK_SUCCESS) {
return 2; return 2;

@ -298,8 +298,8 @@ VkResult create_container(
void* mapped; void* mapped;
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UIContainer), &transfer, &transfer_memory, &mapped)); VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UIContainer), &transfer, &transfer_memory, &mapped));
context->containers[index].data.pos[0] = container->pos[0]; context->containers[index].data.offset[0] = container->offset[0];
context->containers[index].data.pos[1] = container->pos[1]; context->containers[index].data.offset[1] = container->offset[1];
context->containers[index].data.size[0] = container->size[0]; context->containers[index].data.size[0] = container->size[0];
context->containers[index].data.size[1] = container->size[1]; context->containers[index].data.size[1] = container->size[1];
memcpy(mapped, &context->containers[index].data, sizeof(UIContainer)); memcpy(mapped, &context->containers[index].data, sizeof(UIContainer));