summaryrefslogtreecommitdiffstats
path: root/src/state.h
blob: 2f02a202665608044a240f5d72122ddd86f47029 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <shaderc/shaderc.h>
#include <vulkan/vulkan_core.h>

#define CGLM_FORCE_DEPTH_ZERO_TO_ONE

#include "../lib/cglm/include/cglm/cglm.h"

#pragma once

#define MAX_FRAMES_IN_FLIGHT 2

typedef struct {
  vec3 pos;
  vec3 front;
  vec3 up;
  float yaw;
  float pitch;
  float lastX;
  float lastY;
  float fov;
} camera3d;

typedef struct {
  mat4 model;
  mat4 view;
  mat4 proj;
  vec2 resolution;
  float time;
  float dt;
} UniformBufferObject;

typedef struct {
  VkCommandBuffer vk_command_buffer;

  VkSemaphore image_available_semaphore;
  VkSemaphore render_finished_semaphore;
  VkFence in_flight_fence;

  VkBuffer vk_uniform_buffer;
  VkDeviceMemory vk_uniform_buffer_memory;
  void * vk_uniform_buffer_mapped;

  VkDescriptorSet vk_descriptor_set;
} frame_data;

typedef struct {
  float x;
  float y;
} V2;

typedef struct {
  float x;
  float y;
  float z;
} V3;

typedef struct {
  V3 pos;
  V3 normal;
  V2 texCoord;
} Vertex;

typedef struct state {
  camera3d camera;

  int window_w, window_h;
  int cell_w, cell_h;
  int grid_w, grid_h;
  int char_w, char_h;
  int char_x, char_y;

  int rotate;
  int mouse_pressed;
  int middle_click;

  UniformBufferObject ubo;

  float x;
  int zoom;

  SDL_Window *sdl_window;
  int sdl_window_resized;

  VkInstance vk_instance;
  VkDebugUtilsMessengerEXT vk_debug_messenger;
  PFN_vkCreateDebugUtilsMessengerEXT pfnCreateDebugUtilsMessengerEXT;
  PFN_vkDestroyDebugUtilsMessengerEXT pfnDestroyDebugUtilsMessengerEXT;

  VkPhysicalDevice vk_physical_device;
  VkDevice vk_device;
  VkQueue vk_graphics_queue;
  VkSurfaceKHR vk_surface;
  VkQueue vk_present_queue;

  VkSwapchainKHR vk_swap_chain;

  uint32_t vk_swap_chain_image_count;
  VkImage vk_swap_chain_images[5];
  VkImageView vk_swap_chain_image_views[5]; // 5 for some reason

  VkFormat vk_swap_chain_image_format;
  VkExtent2D vk_swap_chain_extent;

  VkDescriptorSetLayout vk_descriptor_set_layout;
  VkPipelineLayout vk_pipeline_layout;
  VkPipeline vk_graphics_pipeline;

  VkCommandPool vk_command_pool;

  frame_data frames[MAX_FRAMES_IN_FLIGHT];

  VkBuffer vk_vertex_buffer;
  VkDeviceMemory vk_vertex_buffer_memory;

  VkBuffer vk_index_buffer;
  VkDeviceMemory vk_index_buffer_memory;

  VkDescriptorPool vk_descriptor_pool;

  shaderc_compilation_result_t prev_vert_result;
  shaderc_compilation_result_t prev_frag_result;

  uint32_t vk_mip_levels;
  VkImage vk_texture_image;
  VkDeviceMemory vk_texture_image_memory;
  VkImageView vk_texture_image_view;
  VkSampler vk_texture_sampler;

  VkImage vk_depth_image;
  VkDeviceMemory vk_depth_image_memory;
  VkImageView vk_depth_image_view;

  VkPolygonMode polygon_mode;

  Vertex *vertices;
  size_t vertices_count;
  uint32_t * indices;
  size_t indices_count;
  VkBuffer vertexBuffer;
  VkDeviceMemory vertexBufferMemory;

  char model_path[1000];
} state_t;

static void
init_state(state_t *s)
{

  strcpy(s->model_path, "assets/viking_room.obj");
  s->camera.pos[0] = 2.0f;
  s->camera.pos[1] = 2.0f;
  s->camera.pos[2] = 2.0f;

  s->camera.front[0] = 0.0f;
  s->camera.front[1] = 0.0f;
  s->camera.front[2] = 0.0f;

  s->camera.up[0] = 0.0f;
  s->camera.up[1] = 0.0f;
  s->camera.up[2] = 1.0f;

  s->camera.yaw = 0.0f;
  s->camera.pitch = -90.0f;
  s->camera.lastX = 400.0f;
  s->camera.lastY = 300.0f;
  s->camera.fov = 45.0f;

  s->rotate = 0;
  s->mouse_pressed = 0;
  s->middle_click = 0;

  s->sdl_window_resized = 0;

  s->window_w = 800;
  s->window_h = 600;
  s->cell_w = 10;
  s->cell_h = 10;
  s->grid_w = 0;
  s->grid_h = 0;
  s->char_w = 0;
  s->char_h = 0;
  s->char_x = 0;
  s->char_y = 0;

  s->zoom = 10;
  s->x = 0.0f;

  glm_mat4_identity(s->ubo.model);

  s->sdl_window = NULL;

  s->vk_swap_chain_image_count = 0;
  s->vk_physical_device = VK_NULL_HANDLE;

  s->prev_vert_result = NULL;
  s->prev_frag_result = NULL;

  s->polygon_mode = VK_POLYGON_MODE_FILL;
}