roleplay/client/src/main.c

171 lines
4.5 KiB
C

2024-10-09 10:54:19 -06:00
#include "render.h"
#include "pipeline.h"
#include "vk_mem_alloc.h"
#include "vulkan/vk_enum_string_helper.h"
2024-10-09 10:54:19 -06:00
ColoredRect colored_rect(float width, float height, float r, float g, float b, float a, float x, float y, float z) {
ColoredRect rect = {
.size = {width, height},
.pos = {x, y, z},
.color = {r, g, b, a},
};
return rect;
}
const uint32_t WHITE = 0xFFFFFFFF;
const uint32_t CLEAR = 0x00000000;
uint32_t test_font_data[] = {
WHITE, WHITE, CLEAR, WHITE,
CLEAR, CLEAR, WHITE, CLEAR,
};
VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkResult result;
UIContext ui_context;
VkBuffer colored_rect_buffer;
VkBuffer text_buffer;
VmaAllocation colored_rect_memory;
VmaAllocation text_memory;
result = init_pipelines(render_context->device, render_context->allocator, render_context->swapchain_extent, render_context->world_render_pass, render_context->transfer_queue, render_context->transfer_pool, &ui_context);
if(result != VK_SUCCESS) {
return result;
}
FontData test_font = {
.info = {
.size = {2, 2},
.start = 0,
.cols = 2,
.rows = 1,
},
.data = test_font_data,
};
FontDescriptor test_font_descriptor;
result = create_text_descriptor(render_context->device, render_context->allocator, ui_context.font_layout, ui_context.font_pool, &test_font, render_context->transfer_pool, render_context->transfer_queue, &test_font_descriptor);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo colored_rect_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
.size = 3*sizeof(ColoredRect),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo colored_rect_memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
result = vmaCreateBuffer(render_context->allocator, &colored_rect_buffer_info, &colored_rect_memory_info, &colored_rect_buffer, &colored_rect_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
ColoredRect* colored_rects;
result = vmaMapMemory(render_context->allocator, colored_rect_memory, (void**)&colored_rects);
if(result != VK_SUCCESS) {
return result;
}
colored_rects[0] = colored_rect(100.0, 100.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5);
colored_rects[1] = colored_rect(100.0, 100.0, 0.0, 1.0, 0.0, 1.0, 0.0, 100.0, 0.5);
colored_rects[2] = colored_rect(100.0, 100.0, 0.0, 0.0, 1.0, 1.0, 100.0, 0.0, 0.5);
vmaUnmapMemory(render_context->allocator, colored_rect_memory);
VkBufferCreateInfo text_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
2024-10-14 01:09:05 -06:00
.size = 2*sizeof(Char),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo text_memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
result = vmaCreateBuffer(render_context->allocator, &text_buffer_info, &text_memory_info, &text_buffer, &text_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
2024-10-14 01:09:05 -06:00
Char* text;
result = vmaMapMemory(render_context->allocator, text_memory, (void**)&text);
if(result != VK_SUCCESS) {
return result;
}
text[0].size[0] = 200.0f;
text[0].size[1] = 200.0f;
text[0].color[0] = 1.0f;
text[0].color[1] = 1.0f;
text[0].color[2] = 1.0f;
text[0].color[3] = 1.0f;
text[0].pos[0] = 200.0f;
text[0].pos[1] = 200.0f;
text[0].code = 0;
text[1].size[0] = 200.0f;
text[1].size[1] = 200.0f;
text[1].color[0] = 1.0f;
text[1].color[1] = 1.0f;
text[1].color[2] = 1.0f;
text[1].color[3] = 1.0f;
text[1].pos[0] = 400.0f;
text[1].pos[1] = 200.0f;
text[1].code = 1;
vmaUnmapMemory(render_context->allocator, text_memory);
UILayer test_layer = {
.colored_rects = colored_rect_buffer,
.colored_rect_count = 3,
.font = test_font_descriptor,
.text_count = 2,
.texts = text_buffer,
};
2024-10-09 10:54:19 -06:00
while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents();
result = draw_frame(render_context, &ui_context, &test_layer, 1);
if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
return result;
}
2024-10-09 10:54:19 -06:00
}
return 0;
}
int logic_thread() {
return 0;
}
int network_thread() {
return 0;
}
int main() {
GLFWwindow* window = init_window();
if(window == NULL) {
return 1;
}
RenderContext render_context = {};
if(init_vulkan(window, &render_context) != VK_SUCCESS) {
return 2;
}
if(render_thread(window, &render_context) != VK_SUCCESS) {
2024-10-09 10:54:19 -06:00
return 3;
}
return 0;
}