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

                            
                               
 

                              
 
                                    






                                          










                


             
                  

             


                      





                                        

                               
 
                                    


                                            

             












                
            


              






                
                      

                  





                         

                    
                   
 


           

                          
                         
 


                                                                       
 

                 
                                                 
                                 
 
                                          
 

                           

                                      


                                                
                                                
 
                         
                          

                               
                             




                        

                        
 
 



                                                         
          
 
           
                      
 
                                             


                          
















                            
                      
 

                            












                    
 

                                  
                      
 

                                         
 

                             

                                         
 
                                             
 
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <shaderc/shaderc.h>
#include <vulkan/vulkan_core.h>

#define VKSETUP_IMPLEMENTATION
#include "vksetup.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;

  vks_buffer uniform_buffer;
  void * uniform_buffer_mapped;

  VkDescriptorSet vk_descriptor_set;

  VkDescriptorSet vk_compute_descriptor_set;
  vks_buffer shader_storage_buffer;
} 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 {
  vec2 position;
  vec2 velocity;
  vec4 color;
} Particle;


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;

  float x;
  int zoom;

  UniformBufferObject ubo;

  int sdl_window_resized;

  VkDebugUtilsMessengerEXT vk_debug_messenger;
  PFN_vkCreateDebugUtilsMessengerEXT pfnCreateDebugUtilsMessengerEXT;
  PFN_vkDestroyDebugUtilsMessengerEXT pfnDestroyDebugUtilsMessengerEXT;

  vks_context vk;

  VkDescriptorSetLayout vk_descriptor_set_layout;
  vks_pipeline graphics_pipeline;

  frame_data frames[MAX_FRAMES_IN_FLIGHT];

  vks_buffer vertex_buffer;
  vks_buffer index_buffer;

  VkDescriptorPool vk_descriptor_pool;

  shaderc_compilation_result_t prev_vert_result;
  shaderc_compilation_result_t prev_frag_result;
  shaderc_compilation_result_t prev_comp_result;

  uint32_t vk_mip_levels;
  vks_image texture_image;
  VkSampler vk_texture_sampler;

  VkPolygonMode polygon_mode;

  Vertex *vertices;
  size_t vertices_count;
  uint32_t * indices;
  size_t indices_count;

  char model_path[1000];



  VkDescriptorSetLayout vk_compute_descriptor_set_layout;
  VkPipelineLayout vk_compute_pipeline_layout;
  VkPipeline vk_compute_pipeline;
} state_t;

static void
init_state(state_t *s)
{
  strcpy(s->model_path, "assets/monkey.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->vk.window = NULL;

  s->vk.swapchain.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;

  s->vk.msaa_samples = VK_SAMPLE_COUNT_1_BIT;
}