Advertisement
Kitomas

vlktut main.cpp as of 2023-12-15

Dec 15th, 2023
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.20 KB | None | 0 0
  1. #include <libxmp-lite/xmp.h>
  2. #define SDL_MAIN_HANDLED
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_vulkan.h> //why isn't this included inside SDL.h?
  5. #include <glm/glm.hpp>
  6. #include <vulkan/vulkan.h>
  7.  
  8. #include <iostream>
  9. #include <stdexcept>
  10. #include <cstdlib>
  11.  
  12. #include <vk/vk_all.hpp>
  13. #include <sfx.hpp>
  14. #include <file.hpp>
  15.  
  16.  
  17. class HelloTriangleApplication {
  18. public:
  19.   void run(Uint32 winW = 640, Uint32 winH = 480){
  20.     _initWindow(winW, winH);
  21.     _initVulkan();
  22.     _mainLoop();
  23.     _cleanup();
  24.   }
  25.  
  26.  
  27. private:
  28.   SDL_Window* _window = nullptr;
  29.   Uint32 _windowID = 0;
  30.  
  31.   VkInstance _instance = nullptr;
  32.  
  33.  
  34.   void _createInstance(){
  35.     VkApplicationInfo appInfo{};
  36.     appInfo.sType              = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  37.     appInfo.pApplicationName   = "cool triangle";
  38.     appInfo.applicationVersion = VK_MAKE_VERSION(1,0,0);
  39.     appInfo.pEngineName        = "No Engine";
  40.     appInfo.engineVersion      = VK_MAKE_VERSION(1,0,0);
  41.     appInfo.apiVersion         = VK_API_VERSION_1_0;
  42.  
  43.  
  44.     bool success;  VkResult result;
  45.  
  46.     Uint32 extensionsReq_len = 0; //# of required extensions
  47.     success = SDL_Vulkan_GetInstanceExtensions(_window, &extensionsReq_len, nullptr);
  48.     if(!success) throw SDL_GetError();
  49.     std::vector<const char*> extensionsReq(extensionsReq_len);
  50.     success = SDL_Vulkan_GetInstanceExtensions(_window, &extensionsReq_len, extensionsReq.data());
  51.     if(!success) throw SDL_GetError();
  52.  
  53.     Uint32 extensionsHas_len = 0; //# of available extensions
  54.     result = vkEnumerateInstanceExtensionProperties(nullptr, &extensionsHas_len, nullptr);
  55.     if(result != VK_SUCCESS) throw "vkEnumerateInstanceExtensionProperties() failed";
  56.     std::vector<VkExtensionProperties> extensionsHas(extensionsHas_len);
  57.     result = vkEnumerateInstanceExtensionProperties(nullptr, &extensionsHas_len, extensionsHas.data());
  58.     if(result != VK_SUCCESS) throw "vkEnumerateInstanceExtensionProperties() failed";
  59. /*
  60.     Uint32 extensionsWant_len = 0; //# of desired extensions
  61.     //extensionsWant cannot be larger than extensionsHas
  62.     std::vector<char[VK_MAX_EXTENSION_NAME_SIZE]> extensionsWant(extensionsHas_len);
  63.     for(Uint32 i=0; i<extensionsReq_len; ++i){
  64.       extensionsWant[extensionsWant_len++] = "aaaa";
  65.     }
  66. */
  67.  
  68.  
  69.     SDL_Log("# of required extensions = %u:",extensionsReq_len);
  70.     for(Uint32 i=0; i<extensionsReq_len; ++i){
  71.       SDL_Log("  %u: \"%s\"",i,extensionsReq[i]);
  72.     }
  73.  
  74.     SDL_Log("\n# of available extensions = %u:",extensionsHas_len);
  75.     for(Uint32 i=0; i<extensionsHas_len; ++i){
  76.       SDL_Log("  %2u: \"%s\" (%u)",i,extensionsHas[i].extensionName,extensionsHas[i].specVersion);
  77.     }
  78.  
  79.  
  80.     VkInstanceCreateInfo createInfo{};
  81.     createInfo.sType                   = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  82.     createInfo.pApplicationInfo        = &appInfo;
  83.     createInfo.enabledLayerCount       = 0; //TBD
  84.     //createInfo.enabledExtensionCount   = extensions_len;
  85.     //createInfo.ppEnabledExtensionNames = extensions.data();
  86.  
  87.     result = vkCreateInstance(&createInfo, nullptr, &_instance);
  88.     if(result == VK_ERROR_EXTENSION_NOT_PRESENT) throw "vkCreateInstance() = VK_ERROR_EXTENSION_NOT_PRESENT";
  89.     if(result != VK_SUCCESS) throw "vkCreateInstance() failed";
  90.   }
  91.  
  92.  
  93.   void _initWindow(Uint32 winW = 640, Uint32 winH = 480){
  94.     _window = SDL_CreateWindow("a cool triangle",
  95.                                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  96.                                winW, winH, SDL_WINDOW_VULKAN);
  97.     if(_window == nullptr) throw SDL_GetError();
  98.     _windowID = SDL_GetWindowID(_window);
  99.     if(_windowID == 0) throw SDL_GetError();
  100.   }
  101.  
  102.  
  103.   void _initVulkan(){
  104.     _createInstance();
  105.   }
  106.  
  107.  
  108.   void _mainLoop(){
  109.     bool running = true;
  110.     SDL_Event event;
  111.  
  112.     while(running){
  113.       while(SDL_PollEvent(&event) && running){
  114.         switch(event.type){
  115.         case SDL_WINDOWEVENT:
  116.           //the window id isn't checked here, so it could theoretically
  117.            //be another window entirely lol
  118.           if(event.window.event == SDL_WINDOWEVENT_CLOSE) running = false;
  119.         }
  120.       }
  121.       SDL_Delay(16);
  122.     }
  123.   }
  124.  
  125.  
  126.   void _cleanup(){
  127.     vkDestroyInstance(_instance, nullptr);
  128.     _instance = nullptr;
  129.     if(_window != nullptr){
  130.       SDL_DestroyWindow(_window);
  131.       _window = nullptr;
  132.     }
  133.   }
  134. };
  135.  
  136.  
  137.  
  138. #define M_2PI (3.1415927f*2)
  139. float get_sin(float hertz = 440, int increment = 1){
  140.   static int index = 0;
  141.   float value = SDL_sinf( ((float)index/44100)*M_2PI*hertz );
  142.   index += increment;
  143.   return value;
  144. }
  145.  
  146. //(t>>4)*(t>>3)|t>>2
  147. float get_sample(){
  148.   static int _t = 0;
  149.   int t = _t * (8000.0f/44100);
  150.  
  151.   //Uint8 _value = t*(t&16384?6:5)*(4-(1&t>>8))>>(3&t>>9)|t>>(t&4096?3:4);
  152.   Uint8 _value = t<<2^t>>4^t<<4&t>>8|t<<1&-t>>4;
  153.   float value = _sfx_u8conv(_value);
  154.  
  155.   ++_t;
  156.   return value;
  157. }
  158.  
  159.  
  160. void auxCallback(void* userdata, void* _stream, int size){
  161.   int       len = size/sizeof(float);
  162.   float* stream = (float*)_stream;
  163.   for(int i=0; i<len; ++i){
  164.     //stream[i] = get_sin()*0.25f;
  165.     stream[i] = get_sample()*0.25f;
  166.  
  167.   }
  168. }
  169.  
  170.  
  171.  
  172.  
  173. int main(int argc, char** argv){
  174.   if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)) return -1;
  175. /*
  176.   //wip vulkan stuff; ignore
  177.   HelloTriangleApplication app;
  178.   try {
  179.     app.run();
  180.   } catch(const std::exception& e){
  181.     std::cerr << e.what() << std::endl;
  182.     return EXIT_FAILURE;
  183.   } catch(const char* e){
  184.     std::cout << "error = " << e << std::endl;
  185.     return EXIT_FAILURE;
  186.   }
  187. */
  188.  
  189.   try {
  190.     sfx_class sfx(64); //a maximum of 64 tracks that can play simultaneously
  191.     sfx.setAux(auxCallback, nullptr, AUDIO_F32, 1);
  192.     sfx_pcm loop1("dat/UNX - LSDLOOP1.kpm",&sfx); //load sound effect
  193.  
  194.     sfx.pauseDeviceAndWait(false); //unpause device, and wait for fade-in
  195.  
  196.     loop1.loopCount = 2; //play 1 + 2 times for a total of 3
  197.     //loop1.play(); //alias for sfx.play(loop1) basically
  198.     SDL_Delay(8000);
  199.     //sfx.waitForTracks(0); //wait for all tracks to stop, with an indefinite timeout
  200.  
  201.     sfx.pauseDeviceAndWait(true); //pause device, and wait for fade-out
  202.  
  203.   } catch(const char* e){
  204.     std::cout << "error = " << e << std::endl;
  205.     return EXIT_FAILURE;
  206.   }
  207.  
  208.   SDL_Quit();
  209.   return EXIT_SUCCESS;
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement