diff --git a/client/include/draw.h b/client/include/draw.h index 687dbeb..80742d6 100644 --- a/client/include/draw.h +++ b/client/include/draw.h @@ -5,7 +5,8 @@ #include "ui.h" VkResult draw_frame( - RenderContext* context); + RenderContext* context, + double time); VkResult record_draw_commands( RenderContext* context, diff --git a/client/include/ui.h b/client/include/ui.h index 453b871..d98dc92 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -127,7 +127,7 @@ typedef struct UIlayerStorageStruct { } UILayerStorage; typedef struct UIContainerStruct { - vec2 pos; + vec2 offset; vec2 size; } UIContainer; @@ -220,7 +220,8 @@ typedef struct UILayerInputStruct { typedef struct UIContainerInputStruct { uint32_t id; - vec2 pos; + uint32_t anchor; + vec2 offset; vec2 size; uint32_t layer_count; diff --git a/client/shader_src/ui.frag b/client/shader_src/ui.frag index 955c251..1c401b1 100644 --- a/client/shader_src/ui.frag +++ b/client/shader_src/ui.frag @@ -25,7 +25,7 @@ layout(location = 0) out vec4 outColor; void main() { 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; if(pos.x < min.x || pos.y < min.y || pos.x > max.x || pos.y > max.y) { diff --git a/client/shader_src/ui.vert b/client/shader_src/ui.vert index b9f0772..2a01c78 100644 --- a/client/shader_src/ui.vert +++ b/client/shader_src/ui.vert @@ -38,7 +38,7 @@ void main() { 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; code = drawable.code; diff --git a/client/shader_src/ui_common.glsl b/client/shader_src/ui_common.glsl index f739d3c..e01d4a4 100644 --- a/client/shader_src/ui_common.glsl +++ b/client/shader_src/ui_common.glsl @@ -69,7 +69,7 @@ layout(std430, buffer_reference) readonly buffer DrawableList { }; layout(std430, buffer_reference) readonly buffer Container { - vec2 pos; + vec2 offset; vec2 size; }; diff --git a/client/src/draw.c b/client/src/draw.c index 0151d0d..dcbc629 100644 --- a/client/src/draw.c +++ b/client/src/draw.c @@ -141,7 +141,8 @@ VkResult record_draw_commands( } VkResult draw_frame( - RenderContext* context) { + RenderContext* context, + double time) { VkResult result; result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX); diff --git a/client/src/gpu.c b/client/src/gpu.c index 0d72ada..d2e0b5e 100644 --- a/client/src/gpu.c +++ b/client/src/gpu.c @@ -7,7 +7,7 @@ const uint32_t MAX_FRAMES_IN_FLIGHT = 2; const uint32_t WINDOW_WIDTH = 800; -const uint32_t WINDOW_HEIGHT = 500; +const uint32_t WINDOW_HEIGHT = 600; const char * validation_layers[] = { "VK_LAYER_KHRONOS_validation", diff --git a/client/src/main.c b/client/src/main.c index 0adb2e3..50491b6 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -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_texture("background.png", render, &ui, &background_index)); - UIDrawable rects[] = { + vec2 map_size = {250, 200}; + + UIDrawable map_rects[] = { { .pos = {0.0, 0.0}, - .size = {WINDOW_WIDTH, WINDOW_HEIGHT}, - .color = {1.0, 1.0, 1.0, 1.0}, - .type = 2, - .code = background_index, + .size = {map_size[0], map_size[1]}, + .color = {0.0, 0.5, 0.8, 1.0}, + .type = 0, + }, + }; + + 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}, - .size = {500.0, 300.0}, - .color = {1.0, 1.0, 1.0, 1.0}, + .pos = {0, 0}, + .size = {chat_size[0], chat_size[1]}, + .color = {0.8, 0.0, 0.5, 1.0}, + .type = 0, }, }; - UILayerInput layers[] = { + UILayerInput chat_layers[] = { { - .num_drawables = sizeof(rects)/sizeof(UIDrawable), - .drawables = rects, + .num_drawables = sizeof(chat_rects)/sizeof(UIDrawable), + .drawables = chat_rects, }, }; - UIContainerInput container_info = { - .id = 0xDEADBEEF, - .pos = {0.0, 0.0}, - .size = {WINDOW_WIDTH, WINDOW_HEIGHT}, - .layer_count = sizeof(layers)/sizeof(UILayerInput), - .layers = layers, + UIContainerInput chat_info = { + .id = 0x02, + .offset = {0, WINDOW_HEIGHT-chat_size[1]}, + .size = {chat_size[0], chat_size[1]}, + .layer_count = sizeof(chat_layers)/sizeof(UILayerInput), + .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)); + double last_frame_time = glfwGetTime(); while(glfwWindowShouldClose(window) == 0) { 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) { fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); return result; } + last_frame_time = frame_time; } return 0; } @@ -88,12 +141,47 @@ int network_thread() { 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() { GLFWwindow* window = init_window(); if(window == NULL) { return 1; } + glfwSetKeyCallback(window, key_callback); + glfwSetMouseButtonCallback(window, mouse_button_callback); + glfwSetScrollCallback(window, scroll_callback); + glfwSetCursorPosCallback(window, cursor_pos_callback); + RenderContext render = {}; if(init_vulkan(window, &render) != VK_SUCCESS) { return 2; diff --git a/client/src/ui.c b/client/src/ui.c index 37c2159..5745965 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -298,8 +298,8 @@ VkResult create_container( void* 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.pos[1] = container->pos[1]; + context->containers[index].data.offset[0] = container->offset[0]; + context->containers[index].data.offset[1] = container->offset[1]; context->containers[index].data.size[0] = container->size[0]; context->containers[index].data.size[1] = container->size[1]; memcpy(mapped, &context->containers[index].data, sizeof(UIContainer));