summaryrefslogblamecommitdiffstats
path: root/src/state.h
blob: 6feb9d181d41130e1bb25ec8c20bae7cae6d2e86 (plain) (tree)
1
2
3
4
5
6
                     



                                    
 






                                          










                


             
                  


                      








                                          
 
                                    


                      

                  





                         


                    




                          

                         




                                                                       
 

























                                                  
 
                                          







                                         


                                                

           
           

                       




















                            

                            












                    
 





                                          


                             
 
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <shaderc/shaderc.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;
} 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 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;

  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;
  // LIST vk_swap_chain_images; // VkImage
  VkImage vk_swap_chain_images[1024];
  VkFormat vk_swap_chain_image_format;
  VkExtent2D vk_swap_chain_extent;

  //LIST vk_swap_chain_image_views; // VkImageView
  VkImageView vk_swap_chain_image_views[1000];

  VkRenderPass vk_render_pass;

  VkDescriptorSetLayout vk_descriptor_set_layout;
  VkPipelineLayout vk_pipeline_layout;
  VkPipeline vk_graphics_pipeline;

  VkFramebuffer vk_swap_chain_framebuffers[1000];
  uint32_t vk_swap_chain_framebuffers_count;

  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;
} state_t ;

static void
init_state(state_t * s)
{
  s->camera.pos[0] = 1.0f;
  s->camera.pos[1] = 1.0f;
  s->camera.pos[2] = 1.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->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;

  s->sdl_window = NULL;

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

  s->vk_swap_chain_framebuffers_count = 0;

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