Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <libxmp/xmp.h>
- #define SDL_MAIN_HANDLED
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_vulkan.h> //why isn't this included inside SDL.h?
- #include <glm/glm.hpp>
- #include <vulkan/vulkan.h>
- #include <iostream>
- #include <stdexcept>
- #include <cstdlib>
- #include <vk/vk_all.hpp>
- #include <sfx.hpp>
- #include <file.hpp>
- #include <xmp.hpp>
- class HelloTriangleApplication {
- public:
- void run(Uint32 winW = 640, Uint32 winH = 480){
- _initWindow(winW, winH);
- _initVulkan();
- _mainLoop();
- _cleanup();
- }
- private:
- SDL_Window* _window = nullptr;
- Uint32 _windowID = 0;
- VkInstance _instance = nullptr;
- void _createInstance(){
- VkApplicationInfo appInfo{};
- appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
- appInfo.pApplicationName = "cool triangle";
- appInfo.applicationVersion = VK_MAKE_VERSION(1,0,0);
- appInfo.pEngineName = "No Engine";
- appInfo.engineVersion = VK_MAKE_VERSION(1,0,0);
- appInfo.apiVersion = VK_API_VERSION_1_0;
- bool success; VkResult result;
- Uint32 extensionsReq_len = 0; //# of required extensions
- success = SDL_Vulkan_GetInstanceExtensions(_window, &extensionsReq_len, nullptr);
- if(!success) throw SDL_GetError();
- std::vector<const char*> extensionsReq(extensionsReq_len);
- success = SDL_Vulkan_GetInstanceExtensions(_window, &extensionsReq_len, extensionsReq.data());
- if(!success) throw SDL_GetError();
- Uint32 extensionsHas_len = 0; //# of available extensions
- result = vkEnumerateInstanceExtensionProperties(nullptr, &extensionsHas_len, nullptr);
- if(result != VK_SUCCESS) throw "vkEnumerateInstanceExtensionProperties() failed";
- std::vector<VkExtensionProperties> extensionsHas(extensionsHas_len);
- result = vkEnumerateInstanceExtensionProperties(nullptr, &extensionsHas_len, extensionsHas.data());
- if(result != VK_SUCCESS) throw "vkEnumerateInstanceExtensionProperties() failed";
- //make sure to account for # of desired extensions too when they're added
- if(extensionsReq_len > extensionsHas_len)
- throw "# of required extensions > # of available extensions";
- //copy everything from Req to Want
- Uint32 extensionsWant_len = extensionsReq_len; //# of desired extensions
- std::vector<std::string> extensionsWant;
- extensionsWant.reserve(extensionsHas_len);
- for(const char* extReq : extensionsReq)
- extensionsWant.emplace_back(extReq);
- //then, append any desired extensions to extension
- //extensionsWant.emplace_back("VK_EXT_swapchain_colorspace");
- //...
- //extensionsWant_len += 1; //+= <# of desired extensions>;
- //make sure extensionsWant is a superset of extensionsHas
- for(std::string& extWant : extensionsWant){
- bool foundAMatch = false;
- for(VkExtensionProperties& extHas : extensionsHas){
- if(extWant.compare(extHas.extensionName) == 0 || extWant == ""){
- foundAMatch = true; break;
- }
- }
- if(!foundAMatch) throw "extensionsHas doesn't contain extensionsWant";
- }
- //translate extensionsWant to a C string array for use in vkCreateInstance
- Uint32 extensions_len = extensionsWant_len; //for consistency
- std::vector<const char*> extensions(extensionsWant_len);
- for(Uint32 i=0; i<extensionsWant_len; ++i)
- extensions[i] = extensionsWant[i].c_str();
- VkInstanceCreateInfo createInfo{};
- createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
- createInfo.pApplicationInfo = &appInfo;
- createInfo.enabledLayerCount = 0; //TBD
- createInfo.enabledExtensionCount = extensions_len;
- createInfo.ppEnabledExtensionNames = extensions.data();
- result = vkCreateInstance(&createInfo, nullptr, &_instance);
- if(result == VK_ERROR_EXTENSION_NOT_PRESENT) throw "vkCreateInstance() = VK_ERROR_EXTENSION_NOT_PRESENT";
- if(result != VK_SUCCESS) throw "vkCreateInstance() failed";
- //debug print stuff
- SDL_Log("# of required extensions = %u:",extensionsReq_len);
- for(Uint32 i=0; i<extensionsReq_len; ++i) SDL_Log(" %u: \"%s\"",i,extensionsReq[i]);
- SDL_Log("# of available extensions = %u:",extensionsHas_len);
- for(Uint32 i=0; i<extensionsHas_len; ++i) SDL_Log(" %2u: \"%s\" (%u)",i,extensionsHas[i].extensionName,extensionsHas[i].specVersion);
- SDL_Log("# of desired extensions = %u:",extensionsWant_len);
- for(Uint32 i=0; i<extensionsWant_len; ++i) SDL_Log(" %2u: \"%s\"",i,extensionsWant[i].c_str());
- }
- void _initWindow(Uint32 winW = 640, Uint32 winH = 480){
- _window = SDL_CreateWindow("a cool triangle",
- SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- winW, winH, SDL_WINDOW_VULKAN);
- if(_window == nullptr) throw SDL_GetError();
- _windowID = SDL_GetWindowID(_window);
- if(_windowID == 0) throw SDL_GetError();
- }
- void _initVulkan(){
- _createInstance();
- }
- void _mainLoop(){
- bool running = true;
- SDL_Event event;
- while(running){
- while(SDL_PollEvent(&event) && running){
- switch(event.type){
- case SDL_WINDOWEVENT:
- //the window id isn't checked here, so it could theoretically
- //be another window entirely lol
- if(event.window.event == SDL_WINDOWEVENT_CLOSE) running = false;
- }
- }
- SDL_Delay(16);
- }
- }
- void _cleanup(){
- vkDestroyInstance(_instance, nullptr);
- _instance = nullptr;
- if(_window != nullptr){
- SDL_DestroyWindow(_window);
- _window = nullptr;
- }
- }
- };
- #define M_2PI (3.1415927f*2)
- float get_sin(float hertz = 440, int increment = 1){
- static int index = 0;
- float value = SDL_sinf( ((float)index/44100)*M_2PI*hertz );
- index += increment;
- return value;
- }
- //(t>>4)*(t>>3)|t>>2
- float get_sample(){
- static int _t = 0;
- int t = _t * (8000.0f/44100);
- t *= 106.0/117;
- Uint8 _value = t*(t&16384?6:5)*(4-(1&t>>8))>>(3&t>>9)|t>>(t&4096?3:4);
- //Uint8 _value = t<<2^t>>4^t<<4&t>>8|t<<1&-t>>4;
- float value = _sfx_u8conv(_value);
- ++_t;
- return value;
- }
- void auxCallback(void* userdata, void* _stream, int size){
- int len = size/sizeof(float);
- float* stream = (float*)_stream;
- for(int i=0; i<len; ++i){
- //stream[i] = get_sin()*0.25f;
- stream[i] = get_sample()*0.2f;
- }
- }
- int main(int argc, char** argv){
- if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)) return -1;
- //wip vulkan stuff; ignore
- HelloTriangleApplication app;
- try {
- app.run();
- } catch(const std::exception& e){
- std::cerr << e.what() << std::endl;
- return EXIT_FAILURE;
- } catch(const char* e){
- std::cout << "error = " << e << std::endl;
- return EXIT_FAILURE;
- }
- /*
- try {
- sfx_class sfx(64); //a maximum of 64 sound effects tracks
- sfx_pcm cow("dat/Cow.kpm",&sfx); //load sound effect of a cow moo
- xmp_class xmp(&sfx); //create music thingy
- xmp.loadModule("dat/rob_me_if_you_can.xm"); //load music
- sfx.pauseDeviceAndWait(false); //unpause device, and wait for fade-in
- //play cow moo and play music for about 10 seconds
- xmp.startPlayer();
- cow.play();
- SDL_Delay(10000);
- sfx.pauseDeviceAndWait(true); //pause device, and wait for fade-out
- xmp.endPlayer();
- } catch(const char* e){
- std::cout << "error = " << e << std::endl;
- return EXIT_FAILURE;
- }
- */
- SDL_Quit();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement