333 lines
8.8 KiB
C
333 lines
8.8 KiB
C
#include "GLFW/glfw3.h"
|
|
#include "ui.h"
|
|
#include "gpu.h"
|
|
#include "draw.h"
|
|
|
|
#include "arpa/inet.h"
|
|
#include "vk_mem_alloc.h"
|
|
#include "vulkan/vk_enum_string_helper.h"
|
|
#include "vulkan/vulkan_core.h"
|
|
#include "pthread.h"
|
|
#include "stdatomic.h"
|
|
|
|
typedef struct ClientContextStruct {
|
|
GLFWwindow* window;
|
|
RenderContext render;
|
|
UIContext ui;
|
|
} ClientContext;
|
|
|
|
VkResult test_ui(RenderContext* gpu, UIContext* ui) {
|
|
VkResult result;
|
|
|
|
GPUString context_strings[] = {
|
|
{
|
|
.pos = {0, 32},
|
|
.size = 32,
|
|
.color = {1.0, 1.0, 1.0, 1.0},
|
|
.offset = 0,
|
|
.length = strlen("Example Text"),
|
|
.font = 0,
|
|
},
|
|
{
|
|
.pos = {0, 64},
|
|
.size = 32,
|
|
.color = {1.0, 1.0, 1.0, 1.0},
|
|
.offset = strlen("Example Text"),
|
|
.length = strlen("Example Text"),
|
|
.font = 1,
|
|
},
|
|
{
|
|
.pos = {0, 96},
|
|
.size = 32,
|
|
.color = {1.0, 1.0, 1.0, 1.0},
|
|
.offset = 2*strlen("Example Text"),
|
|
.length = strlen("Example Text"),
|
|
.font = 2,
|
|
},
|
|
};
|
|
|
|
uint32_t context_codes[256];
|
|
map_string("Example Text", context_codes, 0, 0, ui);
|
|
map_string("Example Text", context_codes, strlen("Example Text"), 1, ui);
|
|
map_string("Example Text", context_codes, 2*strlen("Example Text"), 2, ui);
|
|
|
|
LayerInput context_layers[] = {
|
|
{
|
|
.num_strings = sizeof(context_strings)/sizeof(GPUString),
|
|
.strings = context_strings,
|
|
.num_codes = sizeof(context_codes)/sizeof(uint32_t),
|
|
.codes = context_codes,
|
|
},
|
|
};
|
|
|
|
ContainerInput context_info = {
|
|
.id = 0x01,
|
|
.anchor = ANCHOR_TOP_LEFT,
|
|
.size = {WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT},
|
|
.layer_count = sizeof(context_layers)/sizeof(LayerInput),
|
|
.layers = context_layers,
|
|
};
|
|
|
|
vec2 map_size = {250, 200};
|
|
|
|
GPUDrawable map_rects[] = {
|
|
{
|
|
.pos = {0.0, 0.0},
|
|
.size = {map_size[0], map_size[1]},
|
|
.color = {0.0, 0.5,0.8, 1.0},
|
|
.type = 0,
|
|
},
|
|
};
|
|
|
|
LayerInput map_layers[] = {
|
|
{
|
|
.num_drawables = sizeof(map_rects)/sizeof(GPUDrawable),
|
|
.drawables = map_rects,
|
|
},
|
|
};
|
|
|
|
ContainerInput map_info = {
|
|
.id = 0x02,
|
|
.anchor = ANCHOR_TOP_RIGHT,
|
|
.size = {map_size[0], map_size[1]},
|
|
.layer_count = sizeof(map_layers)/sizeof(LayerInput),
|
|
.layers = map_layers,
|
|
};
|
|
|
|
vec2 inventory_size = {map_size[0], WINDOW_MIN_HEIGHT-map_size[1]};
|
|
|
|
GPUDrawable inventory_rects[] = {
|
|
{
|
|
.pos = {0, 0},
|
|
.size = {inventory_size[0], inventory_size[1]},
|
|
.color = {0.5, 0.8, 0.0, 1.0},
|
|
.type = 0,
|
|
},
|
|
};
|
|
|
|
LayerInput inventory_layers[] = {
|
|
{
|
|
.num_drawables = sizeof(inventory_rects)/sizeof(GPUDrawable),
|
|
.drawables = inventory_rects,
|
|
},
|
|
};
|
|
|
|
ContainerInput inventory_info = {
|
|
.id = 0x03,
|
|
.anchor = ANCHOR_BOTTOM_RIGHT,
|
|
.size = {inventory_size[0], inventory_size[1]},
|
|
.layer_count = sizeof(inventory_layers)/sizeof(LayerInput),
|
|
.layers = inventory_layers,
|
|
};
|
|
|
|
vec2 chat_size = {WINDOW_MIN_WIDTH-inventory_size[0], 200};
|
|
|
|
GPUDrawable chat_rects[] = {
|
|
{
|
|
.pos = {0, 0},
|
|
.size = {chat_size[0], chat_size[1]},
|
|
.color = {0.8, 0.0, 0.5, 1.0},
|
|
.type = 0,
|
|
},
|
|
};
|
|
|
|
LayerInput chat_layers[] = {
|
|
{
|
|
.num_drawables = sizeof(chat_rects)/sizeof(GPUDrawable),
|
|
.drawables = chat_rects,
|
|
},
|
|
};
|
|
|
|
ContainerInput chat_info = {
|
|
.id = 0x04,
|
|
.anchor = ANCHOR_BOTTOM_LEFT,
|
|
.size = {chat_size[0], chat_size[1]},
|
|
.layer_count = sizeof(chat_layers)/sizeof(LayerInput),
|
|
.layers = chat_layers,
|
|
};
|
|
|
|
VK_RESULT(create_container(&context_info, gpu, ui));
|
|
VK_RESULT(create_container(&map_info, gpu, ui));
|
|
VK_RESULT(create_container(&inventory_info, gpu, ui));
|
|
VK_RESULT(create_container(&chat_info, gpu, ui));
|
|
|
|
return VK_SUCCESS;
|
|
}
|
|
|
|
void* network_thread(void* data) {
|
|
ClientContext* context = (ClientContext*)data;
|
|
(void)context;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
int main_thread(ClientContext* context) {
|
|
GPUString fps_string = {
|
|
.pos = {0, 32},
|
|
.size = 32,
|
|
.color = {1.0, 1.0, 1.0, 1.0},
|
|
.offset = 0,
|
|
.length = 4,
|
|
.font = 0,
|
|
};
|
|
|
|
LayerInput fps_layer = {
|
|
.num_strings = 1,
|
|
.strings = &fps_string,
|
|
.max_codes = 10,
|
|
};
|
|
|
|
ContainerInput fps_container = {
|
|
.id = 1,
|
|
.size = {200, 200},
|
|
.layer_count = 1,
|
|
.layers = &fps_layer,
|
|
};
|
|
|
|
create_container(&fps_container, &context->render, &context->ui);
|
|
|
|
double last_draw = -1;
|
|
double draw_interval = 1;
|
|
double frame_count = 0;
|
|
while(glfwWindowShouldClose(context->window) == 0) {
|
|
glfwPollEvents();
|
|
double frame_time = glfwGetTime();
|
|
|
|
// TODO: replace with API calls to add these structures to the per-frame transfer and update CPU structures
|
|
if(frame_time - last_draw > draw_interval) {
|
|
context->render.frame[context->render.current_frame].transfer_count = 2;
|
|
|
|
TransferInfo* transfer_infos = context->render.frame[context->render.current_frame].transfer_infos;
|
|
transfer_infos[0].size = 10*sizeof(uint32_t);
|
|
transfer_infos[0].dst_offset = 0;
|
|
transfer_infos[0].buffers[0] = context->ui.containers[0].layers[0].codes[0];
|
|
transfer_infos[0].buffers[1] = context->ui.containers[0].layers[0].codes[1];
|
|
|
|
transfer_infos[1].size = sizeof(GPUString);
|
|
transfer_infos[1].dst_offset = 0;
|
|
transfer_infos[1].buffers[0] = context->ui.containers[0].layers[0].strings[0];
|
|
transfer_infos[1].buffers[1] = context->ui.containers[0].layers[0].strings[1];
|
|
|
|
void* mapped = context->render.frame[context->render.current_frame].transfer_mapped;
|
|
uint32_t* mapped_codes = (uint32_t*)mapped;
|
|
GPUString* mapped_string = (GPUString*)(mapped + 10*sizeof(uint32_t));
|
|
char str[11];
|
|
snprintf(str, 11, "%3.2f", frame_count/(frame_time-last_draw));
|
|
map_string(str, mapped_codes, 0, 0, &context->ui);
|
|
mapped_string->size = 32;
|
|
mapped_string->pos[0] = 0;
|
|
mapped_string->pos[1] = 32;
|
|
mapped_string->color[0] = 1.0;
|
|
mapped_string->color[1] = 1.0;
|
|
mapped_string->color[2] = 1.0;
|
|
mapped_string->color[3] = 1.0;
|
|
mapped_string->font = 0;
|
|
mapped_string->offset = 0;
|
|
mapped_string->length = strlen(str);
|
|
|
|
last_draw = frame_time;
|
|
frame_count = 0;
|
|
}
|
|
//
|
|
|
|
VkResult result = draw_frame(&context->render, &context->ui, frame_time);
|
|
if(result != VK_SUCCESS) {
|
|
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
|
|
glfwDestroyWindow(context->window);
|
|
}
|
|
|
|
frame_count += 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
|
(void)window;
|
|
(void)key;
|
|
(void)scancode;
|
|
(void)action;
|
|
(void)mods;
|
|
}
|
|
|
|
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
|
ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window);
|
|
|
|
(void)mods;
|
|
(void)context;
|
|
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) {
|
|
// NOTE: this logic is the same as it would be for right-click(just with different outcomes), so dont repeat yourself
|
|
// 1. Search through the UI context for the first element that the contains the mouse point
|
|
// 2. If no UI element intersection, cast a ray through the world scene to find the "clicked entity"
|
|
// 3. Based on what was clicked, start the mouse click animation at the target area
|
|
}
|
|
break;
|
|
case GLFW_MOUSE_BUTTON_RIGHT:
|
|
if(action == GLFW_PRESS) {
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
|
|
(void)window;
|
|
(void)xoffset;
|
|
(void)yoffset;
|
|
}
|
|
|
|
void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) {
|
|
(void)window;
|
|
(void)xpos;
|
|
(void)ypos;
|
|
}
|
|
|
|
int main() {
|
|
ClientContext context = {};
|
|
context.window = init_window();
|
|
if(context.window == NULL) {
|
|
return 1;
|
|
}
|
|
|
|
glfwSetWindowUserPointer(context.window, &context);
|
|
glfwSetKeyCallback(context.window, key_callback);
|
|
glfwSetMouseButtonCallback(context.window, mouse_button_callback);
|
|
glfwSetScrollCallback(context.window, scroll_callback);
|
|
glfwSetCursorPosCallback(context.window, cursor_pos_callback);
|
|
|
|
int error;
|
|
VkResult result;
|
|
VK_RESULT(init_vulkan(context.window, &context.render));
|
|
|
|
// TODO: make # of fonts/textures/containers scaling, recreate GPU buffers as necessary
|
|
VK_RESULT(create_ui_context(10, 10, 10, &context.render, &context.ui));
|
|
|
|
pthread_t network_thread_handle;
|
|
|
|
error = pthread_create(&network_thread_handle, NULL, &network_thread, &context);
|
|
if(error != 0) {
|
|
return error;
|
|
}
|
|
|
|
error = main_thread(&context);
|
|
if(error != 0) {
|
|
return error;
|
|
}
|
|
|
|
error = pthread_join(network_thread_handle, NULL);
|
|
if(error != 0) {
|
|
return error;
|
|
}
|
|
|
|
return 0;
|
|
}
|