| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #ifndef VKW_SDL_VULKAN_WIDGET3_H
- #define VKW_SDL_VULKAN_WIDGET3_H
- #include "VKWVulkanWindow.h"
- #include "Adapters/SDLVulkanWindowAdapter.h"
- #include "VulkanApplication.h"
- #include "Frame.h"
- #include <iostream>
- #include <thread>
- namespace vkw {
- /**
- * @brief The SDLVulkanWidget struct
- *
- * The SDLVulkanWidget use an SDL_Window to provide
- * a drawing surface. This mimics the QtVulkanWidget
- * so that they can be used interchangeably without
- * much modification.
- */
- class SDLVulkanWidget : public VKWVulkanWindow
- {
- public:
- struct CreateInfo
- {
- std::string windowTitle;
- uint32_t width;
- uint32_t height;
- InstanceInitilizationInfo2 instanceInfo;
- DeviceInitilizationInfo2 deviceInfo;
- SurfaceInitilizationInfo2 surfaceInfo;
- };
- ~SDLVulkanWidget()
- {
- }
- CreateInfo m_createInfo;
- void create(CreateInfo const &C)
- {
- m_createInfo = C;
- SDLVulkanWindowAdapter * m_adapter = new SDLVulkanWindowAdapter();
- m_adapter->createWindow( m_createInfo.windowTitle.c_str(),
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- static_cast<int>(m_createInfo.width),
- static_cast<int>(m_createInfo.height));
- setWindowAdapater(m_adapter);
- createVulkanInstance(m_createInfo.instanceInfo);
- createVulkanSurface(m_createInfo.surfaceInfo);
- createVulkanDevice(m_createInfo.deviceInfo);
- }
- void finalize(Application * app)
- {
- app->releaseSwapChainResources();
- app->releaseResources();
- }
- template<typename callable_t>
- void poll( Application * app, callable_t && c)
- {
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- c(event);
- app->nativeWindowEvent(&event);
- }
- }
- void render( Application * app)
- {
- auto fr = acquireNextFrame();
- fr.beginCommandBuffer();
- app->m_renderNextFrame = false;
- app->render(fr);
- fr.endCommandBuffer();
- frameReady(fr);
- }
- void _initSwapchainVars(Application * app)
- {
- app->m_swapChainSize = getSwapchainExtent();
- app->m_swapChainFormat = getSwapchainFormat();
- app->m_swapChainDepthFormat= getDepthFormat();
- app->m_concurrentFrameCount= static_cast<uint32_t>(m_swapchainFrameBuffers.size());
- app->m_defaultRenderPass = m_renderPass;
- app->m_swapchainImageViews = m_swapchainImageViews;
- app->m_swapchainImages = m_swapchainImages;
- app->m_currentSwapchainIndex=0;
- }
- template<typename SDL_EVENT_CALLABLE>
- int exec(Application * app, SDL_EVENT_CALLABLE && callable)
- {
- return exec(app, callable, [](){});
- }
- /**
- * @brief exec
- * @return
- *
- * Similar to Qt's app.exec(). this will
- * loop until the the windows is closed
- */
- template<typename SDL_EVENT_CALLABLE, typename SDL_MAIN_LOOP_CALLABLE>
- int exec(Application * app, SDL_EVENT_CALLABLE && callable, SDL_MAIN_LOOP_CALLABLE && mainLoop)
- {
- app->m_device = getDevice();
- app->m_physicalDevice = getPhysicalDevice();
- app->m_instance = getInstance();
- app->m_graphicsQueue = getGraphicsQueue();
- app->m_presentQueue = getPresentQueue();
- app->m_graphicsQueueIndex = getGraphicsQueueIndex();
- app->m_presentQueueIndex = getPresentQueueIndex();
- _initSwapchainVars(app);
- app->initResources();
- app->initSwapChainResources();
- while( true )
- {
- bool resize=false;
- poll(app, [&resize,&callable](SDL_Event const &E)
- {
- if (E.type == SDL_WINDOWEVENT && E.window.event == SDL_WINDOWEVENT_RESIZED
- /*&& event.window.windowID == SDL_GetWindowID( window->getSDLWindow()) */ )
- {
- resize=true;
- }
- callable(E);
- });
- if( app->shouldQuit() )
- {
- break;
- }
- if(resize)
- {
- app->releaseSwapChainResources();
- rebuildSwapchain();
- _initSwapchainVars(app);
- app->initSwapChainResources();
- }
- mainLoop();
- if( app->shouldRender() )
- {
- render(app);
- }
- }
- app->releaseSwapChainResources();
- app->releaseResources();
- destroy();
- return 0;
- }
- /**
- * @brief frameReady
- *
- * Call this function to present the frame.
- */
- void frameReady(Frame & fr)
- {
- submitFrame(fr);
- presentFrame(fr);
- waitForPresent();
- }
- };
- }
- #endif
|