2024-10-04 23:07:53 -05:00
2024-10-06 04:52:10 -05:00
# include "DeviceLibrary.h" // Device Library includes global, redundant to include with it here
2024-10-04 23:07:53 -05:00
# include "debug/VulkanDebugLibs.h"
# include <cstdint>
# include <cstring>
2024-10-04 03:36:56 -05:00
# include <cstdlib>
2024-10-04 01:03:13 -05:00
# include <iostream>
2024-10-05 05:50:37 -05:00
const uint32_t WIDTH = 800 ;
const uint32_t HEIGHT = 600 ;
2024-10-04 03:36:56 -05:00
// Define a base class structure to handle public and private methods
class TriangleTestApplication {
2024-10-04 19:56:58 -05:00
2024-10-04 03:36:56 -05:00
public :
void run ( ) {
initWindow ( ) ;
initVulkan ( ) ;
mainLoop ( ) ;
cleanup ( ) ;
}
private :
2024-10-06 04:35:53 -05:00
DeviceControl : : DeviceLibrary deviceLibs ;
Debug : : VulkanDebugLibs debugController ;
2024-10-05 05:50:37 -05:00
GLFWwindow * window ;
2024-10-04 16:00:07 -05:00
VkInstance instance ;
2024-10-05 19:59:15 -05:00
VkDevice device ;
2024-10-06 04:35:53 -05:00
2024-10-04 03:36:56 -05:00
// Initialize GLFW Window. First, Initialize GLFW lib, disable resizing for
// now, and create window.
void initWindow ( ) {
glfwInit ( ) ;
glfwWindowHint ( GLFW_CLIENT_API , GLFW_NO_API ) ;
glfwWindowHint ( GLFW_RESIZABLE , GLFW_FALSE ) ;
// Settings for the window are set, create window reference.
window = glfwCreateWindow ( WIDTH , HEIGHT , " Vulkan " , nullptr , nullptr ) ;
}
2024-10-04 19:56:58 -05:00
2024-10-04 16:00:07 -05:00
void initVulkan ( ) {
2024-10-04 19:56:58 -05:00
createInstance ( ) ;
2024-10-06 04:35:53 -05:00
debugController . setupDebugMessenger ( instance ) ; // The debug messenger is out holy grail, it gives us Vulkan related debug info when built with the -DNDEBUG flag (as per the makefile)
deviceLibs . createSurface ( instance , window ) ;
2024-10-05 19:59:15 -05:00
deviceLibs . pickPhysicalDevice ( instance ) ;
deviceLibs . createLogicalDevice ( device ) ;
2024-10-04 19:56:58 -05:00
}
void createInstance ( ) {
2024-10-06 04:35:53 -05:00
debugController . checkUnavailableValidationLayers ( ) ; // Check if there is a mistake with our Validation Layers.
2024-10-04 19:56:58 -05:00
2024-10-04 16:00:07 -05:00
// Set application info for the vulkan instance!
VkApplicationInfo appInfo { } ;
2024-10-05 05:50:37 -05:00
appInfo . sType = VK_STRUCTURE_TYPE_APPLICATION_INFO ; // Tell vulkan that appInfo is a Application Info structure
appInfo . pApplicationName = " Triangle Test " ; // Give the struct a name to use
appInfo . applicationVersion = VK_MAKE_VERSION ( 1 , 0 , 0 ) ; // Create a Major Minor Patch version number for the application!
appInfo . pEngineName = " Agnosia Engine " ; // Give an internal name for the engine running
appInfo . engineVersion = VK_MAKE_VERSION ( 1 , 0 , 0 ) ; // Similar to the App version, give vulkan an *engine* version
2024-10-06 04:35:53 -05:00
appInfo . apiVersion = VK_API_VERSION_1_1 ; // Tell vulkan what the highest API version we will allow this program to run on
2024-10-04 16:00:07 -05:00
2024-10-05 05:50:37 -05:00
VkInstanceCreateInfo createInfo { } ; // Define parameters of new vulkan instance
createInfo . sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ; // Tell vulkan this is a info structure
createInfo . pApplicationInfo = & appInfo ; // We just created a new appInfo structure, so we pass the pointer to it.
2024-10-04 16:00:07 -05:00
2024-10-06 04:35:53 -05:00
debugController . vulkanDebugSetup ( createInfo , instance ) ; // Handoff to the debug library to wrap the validation libs in! (And set the window up!)
2024-10-04 16:00:07 -05:00
}
2024-10-04 23:07:53 -05:00
2024-10-05 05:50:37 -05:00
void mainLoop ( ) { // This loop just updates the GLFW window.
2024-10-04 03:36:56 -05:00
while ( ! glfwWindowShouldClose ( window ) ) {
glfwPollEvents ( ) ;
}
}
2024-10-04 16:00:07 -05:00
2024-10-05 05:50:37 -05:00
void cleanup ( ) { // Similar to the last handoff, destroy the debug util in a safe manner in the library!
2024-10-05 19:59:15 -05:00
vkDestroyDevice ( device , nullptr ) ;
2024-10-06 04:35:53 -05:00
if ( Global : : enableValidationLayers ) {
debugController . DestroyDebugUtilsMessengerEXT ( instance , nullptr ) ;
2024-10-05 05:50:37 -05:00
}
2024-10-06 04:35:53 -05:00
deviceLibs . destroySurface ( instance ) ;
2024-10-04 16:00:07 -05:00
vkDestroyInstance ( instance , nullptr ) ;
2024-10-04 03:36:56 -05:00
glfwDestroyWindow ( window ) ;
glfwTerminate ( ) ;
}
} ;
2024-10-03 23:55:40 -05:00
2024-10-04 01:03:13 -05:00
int main ( ) {
2024-10-04 03:36:56 -05:00
TriangleTestApplication app ;
try {
app . run ( ) ;
} catch ( const std : : exception & e ) {
std : : cerr < < e . what ( ) < < std : : endl ;
return EXIT_FAILURE ;
}
return EXIT_SUCCESS ;
2024-10-03 23:55:40 -05:00
}
2024-10-04 23:07:53 -05:00