Opening a window - Vulkan Game Engine Tutorial 01

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to episode one of this tutorial series an introduction to computer graphics with vulkan and c plus at this point you should have your development environment set up and ready to go if you have not yet watched the introduction video i recommend doing so now and you can find it linked in the description below it goes over what this series will cover and contains instructions on how to set up your development environment my recommendation for how to get the most out of the series is to follow along with the coding in each video alternatively i've also linked the github repo for this project which contains the code for each tutorial for anyone who prefers to download it and make their own adjustments now assuming you've completed that section you should have a main file that looks something like this let's compile and run our code and an empty window should open up and if you look in your console some output saying how many extensions your gpu supports if you've got that you're good to go and it means everything has been set up correctly and since we want to start from scratch let's delete this code so let's create a class to wrap some window based functionality i'll call my file lve underscore window.hpp for little vulcan engine window but feel free to call your project whatever you want i'll be using pragma once since my compiler supports it if your compiler does not don't forget to use define header guards we'll put our code in the namespace also called lve and let's make a class for our window start by creating a private variable that is a pointer to a glfw window we need to include the glf w3 header file so as mentioned in the development environment tutorial setup glfw is a platform agnostic windowing tool this means we can use it to open a window without worrying about if we're running on mac windows or linux we include this define of glfw include vulcan to signal that we want glfw to also include the vulcan headers with it now let's add a helper function which we'll use to initialize our window and some member variables to store the windows width height and name for window name we will use a standard string so we'll need to include that as well next we'll create a public constructor which takes the values that we'll use to initialize our member variables and we can't forget to have a destructor to clean up our window for when we're done using it okay so now add a corresponding cpp file for your method implementations we'll include our header file and put our code in the namespace let's start by implementing our constructor our constructor is pretty basic we'll use a member initializer list to initialize our member variables then in our constructor body we'll call init window next let's implement init window first off we need to initialize the glfw library by calling glfw init now we're going to use the window hint command to tell glfw not to create an opengl context so glfw was originally designed to create an opengl context when a window is created but since we're not using vulcan we do not want that therefore by using the no api hint we disable this functionality next we're going to use another hint to disable our window from being resized after creation this is because we'll need to handle window resizes in a special way which will cover around tutorial 10. now use the create window command with width height and window name to initialize our window pointer for windows title we need to pass a c style string the fourth parameter is for if we want to make a full screen window for now let's just stick with window mode so use a null pointer and for the final parameter we can ignore this as it's related to if we're using an opengl context which in this case we're not finally we can't forget to implement our destructor where we need to destroy the resources we required at initialization first use the destroy window command and pass in our window pointer then lastly call glfw terminate okay we're almost there let's create a new class which is going to be what controls our application we'll add a header guard and include the window wrapper class we just created let's put our code in the same project level namespace and create first app class add a private member variable for our lve window so when our first app class is created a window will be created and opened and when our first app is destroyed our window is automatically destroyed note that we're not using a pointer or any dynamic memory allocation let's also define some constants for our window width and height for now i'll use 800 for width and 600 for height but these can be whatever you want let's add a run function and for now add braces so it has an empty implementation we'll fix this in a moment now in our main file include our first app class and add a main function create an instance of our app and let's call our run function surrounded by a try and catch we don't throw any errors for now but we may in the future we'll need to include a few header files from the standard library include c standard lib io stream and standard accept if there's an error we'll just output it to the console and let's return either failure or success for our program if i try making my program now i'll get an error because i haven't updated my make file so for those of you who are also using a make file here's my simple make file and i'll change it to use all cpp files in this directory and also add a dependency on our header files but most of you should not have this problem if you're using an ide like visual studio or xcode i recommend using whatever you are personally most comfortable with as i won't really be covering this topic now if we compile and run our code we can see the program is successful but nothing happens and that's because our run function is empty start by removing the empty implementation for run in your first app header file then let's create a corresponding cpp file include our first app header add our namespace and let's implement run we're going to use a while loop and call a made up function on our window to see if it wants to close we'll call this function should close inside our wall loop call glfw pull events which checks and processes any window level events so all this function does is while our window does not want to close pull window events window events can be things like keystrokes or for what we care about in this case a user clicked the x button to dismiss the window so now let's add this should close function into our lve window header add a public function called should close that returns a boolean and since this is just a single line function i think it's fine that it's inlined so right here let's implement it and we'll call the glfw function querying whether or not the user has tried to dismiss the window okay let's save our files build and run nice we have a window we can move it around and we can close it exactly what we wanted so that's pretty much it for this lesson one last thing we may want to do is delete the copy constructor and copy operator from our window class this is because we're using a pointer to our glfw window throughout this series we will be adhering to resource acquisition is initialization what this essentially means for us is that resource creation happens when we initialize our variables and our cleanups are performed by our destructors so in this case we don't want to accidentally copy an lve window and then have two pointers to our glfw window because when one of these objects destructors are called the shared glfw window would be terminated and we'd be left with a dangling pointer well thank you for getting this far things will be a bit slow for the first few tutorials as there is a lot of info to get through but once we get to tutorial 5 things really start to pick up please subscribe if you'd like to see more tutorials as soon as they come out thanks
Info
Channel: Brendan Galea
Views: 38,435
Rating: undefined out of 5
Keywords: Vulkan, vulkan api, vulkan tutorial, 3d game engine, coding tutorial, vulkan coding, vulkan graphics, 3d graphics, programming, gpu, vulkan programming, vulkan game engine, vulkan game engine tutorial, vulkan engine tutorial, learn vulkan, how to code vulkan, Vulkan beginner, graphics beginner, vulkan noob, vulkan from scratch
Id: lr93-_cC8v4
Channel Id: undefined
Length: 11min 2sec (662 seconds)
Published: Fri Nov 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.