-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
162 lines (129 loc) · 5.09 KB
/
main.c
File metadata and controls
162 lines (129 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include "Loader.h"
#include "Vulkan.h"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui/cimgui.h"
#include "cimgui/cimgui_impl.h"
typedef struct RenderData
{
VkRenderPass renderpass;
VkFramebuffer* framebuffers;
VkExtent2D swapchain_extent;
mlx_context mlx;
mlx_window win;
mlx_image img;
} RenderData;
void VulkanRender(VkCommandBuffer cmd, void* param)
{
RenderData* data = (RenderData*)param;
VkExtent2D swapchain_extent = GetMlxFuncs()->mlx_get_vk_swapchain_extent(data->mlx, data->win);
if(data->swapchain_extent.width != swapchain_extent.width || data->swapchain_extent.height != swapchain_extent.height)
{
data->swapchain_extent = swapchain_extent;
VulkanDestroyFramebuffers(data->mlx, data->win, data->framebuffers);
GetVulkanFuncs()->vkDestroyRenderPass(GetMlxFuncs()->mlx_get_vk_device(data->mlx), data->renderpass, NULL);
data->renderpass = VulkanCreateRenderPass(data->mlx, data->win);
data->framebuffers = VulkanCreateFramebuffers(data->mlx, data->win, data->renderpass);
}
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
igNewFrame();
igShowDemoWindow(NULL);
igRender();
ImDrawData* draw_data = igGetDrawData();
VkRenderPassBeginInfo info = { 0 };
info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
info.renderPass = data->renderpass;
info.framebuffer = data->framebuffers[GetMlxFuncs()->mlx_get_current_vk_swapchain_image_index(data->mlx, data->win)];
info.renderArea.extent.width = swapchain_extent.width;
info.renderArea.extent.height = swapchain_extent.height;
GetVulkanFuncs()->vkCmdBeginRenderPass(cmd, &info, VK_SUBPASS_CONTENTS_INLINE);
ImGui_ImplVulkan_RenderDrawData(draw_data, cmd, VK_NULL_HANDLE);
GetVulkanFuncs()->vkCmdEndRenderPass(cmd);
}
void WindowHook(int event, void* param)
{
if(event == 0)
mlx_loop_end((mlx_context)param);
}
void CheckVk(VkResult result)
{
(void)result;
}
void MlxRender(void* param)
{
static int i = 0;
RenderData* data = (RenderData*)param;
mlx_clear_window(data->mlx, data->win, (mlx_color){ .rgba = 0x334D4DFF });
mlx_put_transformed_image_to_window(data->mlx, data->win, data->img, 220, 40, 0.5f, 0.5f, i);
uint32_t color = 0;
for(int j = 0; j < 400; j++)
{
mlx_pixel_put(data->mlx, data->win, j, j, (mlx_color){ .rgba = 0x0000FFFF + (color << 24) });
mlx_pixel_put(data->mlx, data->win, 399 - j, j, (mlx_color){ .rgba = 0x0000FFFF });
color += (color < 255);
}
i++;
if(i > 360)
i = 0;
}
int main(void)
{
mlx_context mlx = mlx_init();
mlx_window_create_info info = { 0 };
info.title = "MLX Window";
info.width = 1250;
info.height = 720;
info.is_resizable = true;
mlx_window win = mlx_new_window(mlx, &info);
LoadMlxBindings(mlx, GetMlxFuncs());
LoadVulkanBindings(mlx, GetMlxFuncs(), GetVulkanFuncs());
mlx_on_event(mlx, win, MLX_WINDOW_EVENT, WindowHook, mlx);
GetMlxFuncs()->mlx_set_sdl_input_hook(mlx, (sdl_hook)ImGui_ImplSDL2_ProcessEvent);
ImGuiContext* ctx = igCreateContext(NULL);
igGetIO()->IniFilename = NULL;
VkInstance instance = GetMlxFuncs()->mlx_get_vk_instance(mlx);
VkPhysicalDevice physical_device = GetMlxFuncs()->mlx_get_vk_physical_device(mlx);
VkDevice device = GetMlxFuncs()->mlx_get_vk_device(mlx);
VkDescriptorPool pool = VulkanCreateDescriptorPool(mlx);
VkRenderPass renderpass = VulkanCreateRenderPass(mlx, win);
VkFramebuffer* framebuffers = VulkanCreateFramebuffers(mlx, win, renderpass);
ImGui_ImplVulkan_LoadFunctions(ImGuiLoadVulkan, instance);
VkSurfaceCapabilitiesKHR capabilities = { 0 };
GetVulkanFuncs()->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, GetMlxFuncs()->mlx_get_vk_surface(mlx, win), &capabilities);
ImGui_ImplSDL2_InitForVulkan((SDL_Window*)GetMlxFuncs()->mlx_get_window_handle(mlx, win));
ImGui_ImplVulkan_InitInfo init_info = { 0 };
init_info.Instance = instance;
init_info.PhysicalDevice = physical_device;
init_info.Device = device;
init_info.QueueFamily = GetMlxFuncs()->mlx_get_vk_graphics_queue_family(mlx);
init_info.Queue = GetMlxFuncs()->mlx_get_vk_graphics_queue(mlx);
init_info.DescriptorPool = pool;
init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.MinImageCount = capabilities.minImageCount;
init_info.ImageCount = GetMlxFuncs()->mlx_get_vk_swapchain_image_count(mlx, win);
init_info.CheckVkResultFn = CheckVk;
init_info.RenderPass = renderpass;
ImGui_ImplVulkan_Init(&init_info);
RenderData data = { 0 };
data.renderpass = renderpass;
data.mlx = mlx;
data.win = win;
data.framebuffers = framebuffers;
data.swapchain_extent = GetMlxFuncs()->mlx_get_vk_swapchain_extent(mlx, win);
data.img = mlx_new_image_from_file(mlx, "MacroLibX/example/42_logo.png", NULL, NULL);
mlx_add_loop_hook(mlx, MlxRender, &data);
GetMlxFuncs()->mlx_add_pre_render_hook(mlx, win, VulkanRender, &data);
mlx_loop(mlx);
VulkanDestroyFramebuffers(mlx, win, framebuffers);
ImGui_ImplVulkan_Shutdown();
ImGui_ImplSDL2_Shutdown();
igDestroyContext(ctx);
mlx_destroy_window(mlx, win);
GetVulkanFuncs()->vkDestroyDescriptorPool(device, pool, NULL);
GetVulkanFuncs()->vkDestroyRenderPass(device, renderpass, NULL);
mlx_destroy_image(mlx, data.img);
mlx_destroy_context(mlx);
return 0;
}