EP0001 - Introduction - Making a video game from scratch in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to Episode one of my video series in which we program a video game from scratch from beginning to end so this is episode 1 before we get started with a lot of programming there's a lot of questions that we need to talk about some introductions that we need to do so first Who am I my name is Ryan Reese and I work for Microsoft of course this is not endorsed or sponsored by Microsoft in any way this is just my personal hobby my official title though at Microsoft is escalation engineer and I'm not a software engineer so I don't I'm not on the product group I work in support so I spend more of my time in the debugger debugging other people's code than I do writing my own so just keep that in mind that I'm I have my primary skill set is something other than programming and I do programming for myself as a hobby it's just something that I've been interested in since I was a kid secondly what are we doing is the next question and while we're programming a video game from scratch and something that we are not going to do is we're not going to be using engines like Unreal Engine or unity or things like this that are sort of frameworks that make game making really simple and those have a use I you know don't get me wrong it's just we're not going to be using we're going to be doing everything the long way the hard way from scratch what sort of video game are we making you're probably wondering we're not gonna be making a triple-a we're not gonna be making assassin's creed or Call of Duty nothing like that so I'm in my late 30s and the Atari 2600 and the 8-bit NES and 16-bit Super Nintendo those are the sorts of consoles that I grew up with those are the sorts of games that have the most nostalgia for me it's what I remember the most so that's the sort of video game that we're gonna be making and in fact why don't I just show you right over here so for example this is this is Dragon Warrior for the NES and this is for you know 8-bit Nintendo came out probably 1986 or something like this and as you can see you know these are the sort of graphics I'm going for this is an RPG it's a role-playing game and as you can see hold on and we get something where he's not in the dark there we go sort of an overworld map set up going here and as you can see yeah you know he's walking around and everything is laid out in in tiles the map is laid out in tiles he can only move he moves a set distance every single time you move and then you encounter these monsters in the wild and you fight them you have stats like HP and magic and experience points and all this kind of stuff then compare that with let's see something that everyone probably knows this is the original Legend of Zelda on the NES and something that makes this different you have the same sort you know obviously it's 2d you go and you find monsters but this is more it's more real-time you walk around and you don't have a system where you saw in the last game where every time you move you move in a you move one full tile this has a more sort of freedom of movement where you can move every pixel is basically open to you to stand on so that's a possibility there's one another one there's not one other one that I want to show this is Final Fantasy 6 for the Super Nintendo and this one is you know it's a newer console newer generation console so it's a lot more advanced however we've gone back to that system that we have that we saw in the original Dragon Warrior where every step you take is a full tile let's see and then you you know you go around and you talk to the PCs and you read a bunch of dialogue and you progress the story and you find monsters and you you know have a party maybe full of different different heroes and you equip them with different weapons and stuff like this ok that's enough of that so that's the kind of game that I want to make I haven't actually decided yet done if I want to do sort of a fantasy theme like the ones you just saw or if I want to do more sci-fi themed I think that could be really cool it's also something that hasn't been done quite so much to death Oh we'll see we're still very early haven't really planned it all out yet so you're here along here for the ride so the next question is what is it the format of these episodes going to be like so this is episode 1 of what I intend to be many episodes probably more than a hundred probably more than 500 episodes because it's going to take a while to make this game each episode is going to be about an hour long so I don't want to kill anybody's attention span I want to kill myself trying to make these six hour long videos of me just sitting here programming so I'm going to try to distill the interesting bits down into one-hour episodes and then if you have any questions or comments after that says over after you've watched it you can you know ask your questions in the comments I'll either answer them right there in the comments or if they're really interesting then how we'll talk about them I will talk about your questions or comments in the next episode so the next question why am I doing this why am I doing this well because I've always wanted to make a video game and I also really love low-level programming and I feel like if I make these episodes then serves two purposes number one I want them to be educational I would hope that the audience gets something out of this learned something out of this but secondly it I'm hoping that it will also help me actually finish this project it gives me a reason to keep going as something that programmers have a lot of issues with is we keep starting projects but never finishing them so I'm hoping that this video series will help me actually finish this project and I hope that I learned some things along the way and I hope that the community will sort of continue to encourage me to finish this and push me so I guess that is the intro out of the way so next we need to start talking about actually programming because that's what this video series is is all about we are going to be using the C programming language and that in today's age you know in the year 2020 it seems kind of an odd choice although not so much maybe not so much let me let me start by saying when I was younger when I was going to school I started programming just you know self-taught as a hobby when I was about 13 14 and I started trying to you know sign up for classes like programming classes and and started trying to consume a material that would make me a better programmer problem is is back in those days this would have been the late mid to late 90s back in those days object-oriented programming was the only game in town C++ and Java those were like the only only thing going no one was going to teach you see anymore C is actually the predecessor to C++ C is commonly referred to as a subset of C++ so C and C++ are two separate languages C is the older ancestor it originated it somewhere around 1972 and it's one of the oldest programming languages around that is still in common use today in fact it's still in very common use today at my job for example I debug Microsoft Windows and Microsoft Windows is still primarily the operating system is written in a C just pure C no C++ now of course there are bits and pieces of it that are in C++ but you know like those are like the auxiliary bits of it like what I mean the core of the kernel is written in C same with same goes with Linux the core of that operating system is also written in C so anyway back to my story C++ was like the only thing C++ in Java were the two operating systems that anyone was willing to teach us at that time because at that time it was it was very much in fashion object-oriented programming was and it was it was the wave of the future object-oriented programming and C++ Java those are just like all that we're going to need going forward and they sort of tried we tried to leave C behind for various reasons but it we did it the fact of the matter is is that the world still runs on see because it's rock solid and reliable and you can do anything you can imagine with it so we're gonna use it it's my favorite language to this day so let's see what else didn't want to talk about oh yeah so I you know so I guess I still want to talk a little bit more about object-oriented programming and why you might want to use it why you might want to use it might not want to use it so it I would say that my education in in programming actually hit a hurdle like I I was actually very slow to learn to become a programmer because I didn't like I didn't like object-oriented programming and this wasn't how my brain was wired I guess - I didn't like it and so I came away sort of disillusioned thinking that I didn't actually like programming it wasn't until much later when I got back into old fashioned C and it works the way that it works is so much different and I I loved so much more and it really got me back into programming when I was in college I took a class on another programming language C sharp which C sharp is object oriented programming taken to the extreme it's it's almost like programming with training wheels on because it's so easy everything snaps together all the classes interact with each other in these very elegant ways and it is a very elegant elegant language and C sharp definitely has its uses one thing that I really don't like about it though is it's slow and let me tell you for the last more than 20 years people have been trying to tell me you know college professors and other programmers have been trying to tell me for 20 years that length languages like C sharp and Java that are garbage collected and they run it within a vm they're trying to tell me that those languages are just as fast as native code and it's just not true never has been and never will be and it's because of that performance is one of the main things that that keeps me cemented with C it's just like you can't beat the performance of C may be rust may be getting close rust is a new language but I don't know rust so we'll have to save that for some other time maybe we can learn rust together but let's see I guess we should start programming shouldn't we so let's see okay so the first thing that you're gonna want to do is you're going to want to download we're going to use Visual Studio and I'm gonna go ahead and just open it here now you're looking at you're looking at just a you know basic installed Windows 10 and there's nothing really installed on it and all I've done I mean I installed a couple games to play installed steam but nothing else really installed on it so far I just installed Visual Studio so you can actually find this yourself if you go here Visual Studio community edition I think it's called yeah Visual Studio Community Edition extra 2019 there's a lot of different like professional editions of Visual Studio that cost thousands of dollars but we don't need that for what we're because she's the community edition which is free so do you have to use Visual Studio no you know you can use all sorts of different compilers you can use you know LLVM you can use GCC if you're on on linux you can use any sort of tool chain you want but since we're on Windows and Visual Studio is made for Windows and everyone and everyone including me is familiar with the whole windows ecosystem Visual Studio is still just a natural fit you know it's it's not perfect but it's pretty good so here we are in Visual Studio and we're going to create a new project now whenever I create a new project notice there's all sorts of templates over here empty project console app windows desktop wizard I should actually go ahead and mention that in Windows there are different types of programs you can make for example you can make a console application that only runs from within a command line like this and there are Windows applications like win32 applications like this and these you know applications have like a graphical user interface and they enter you can use the mouse to interact with them things like this technically speaking in today's age you know in Windows 10 there's more than that there's like Universal windows apps and all this kind of stuff and I definitely don't wanna mess with any of that stuff so for now let's just think about console apps and GUI apps that's what all these different templates are for different types of apps that you can make but I always just start with an OT project that way you can just kind of start from there and nothing else will go into it that you don't already want for example if I created a if I were to create like the win32 GUI app from this template it would put a whole bunch of boilerplate code in there that isn't necessarily what I want so I would rather just start with an empty project something else here that is empty is interesting to note is notice how it mentions C++ and we're here to mention C++ and there's nothing in here about C and remember earlier I told you we were going to be using C you know C++ the reason for that is because Visual Studio is not very friendly to C remember how I was telling you about how programming the programming industry felt like object-oriented C++ was the the wave of the future and it's all that anyone whatever mean and we never need do you see ever again that's sort of where Visual Studio is coming from like they don't have the best support for the C programming language in fact I think Visual Studio is still in the year 2020 I think that Visual Studio is still only compliant up to like the C 89 standard which is the standard of the C programming language that was standardized in 1989 and that's still what we're going to be using today there have been more recent versions of the C programming language probably most notably C 99 which is 1990-1999 which is um you know it brings a lot of new features to the C programming language we don't get to use those because Visual Studio doesn't support them they are a C++ first compiler and Visual Studio historically only supports the minimum amount of C necessary to also to also support all of the C++ bells and whistles so you know it's it's almost like we are swimming upstream here we were against the current because we're trying to program and see when the whole world really just wishes that we would use C++ instead but we don't want to I'm gonna go ahead and hit next and create this empty project and what am I gonna call it I'm gonna call it game B and the reason why is because I don't know what this game is gonna be called yet and I'm not gonna call it game a because I already have a game made so I'm gonna I'm gonna call this game beat and you can save it wherever you want and then I'm just going to place the solution in the project in same directory and hit create see it's creating my project for C++ and for the windows console and so here's Visual Studio there's nothing in this project yet by the way if my text is small try to remind me to increase the size of this text because the resolution of this monitor that you're looking at is 1440 so it's 2560 by 1440 so if you're on a 1080p monitor it might look the text might look a bit small to you however I was afraid of you you know downscaling my resolution because I was afraid of it might make the text blurry so we'll see how it turns out so if I look over here on the right side here's my solution and if I right click on source files and the first thing I'm gonna do is I'm gonna add a new file I'm gonna add a new item now here again notice you there's no I can add C++ files but you won't find any options in here for adding C files least I don't think so so what you can do though is you can actually rename this file name the file extension from CPP if you name it dot C then the compiler the visual studio compiler will actually treat it as just AC file not a C++ file so that's what we're gonna do is we're going to rename the file extension to dot C that means that we won't be able to use any C++ in this file only see also I don't know if I want to name it source Stasi I may rename it later I'll call it main dot C for now then hit add and there you have it here we have our first file and if we were to look at this in the file system let's see can I can i right-click on this and then no okay well I was trying to let C stand by here it is on the file system so if you if you look in Windows Explorer you can see it right here it's game B and it's there just files on disk and here's my source code file and then the you know here's my solution file and then you know these chunks of these files are you know for visual studios use they just describe your project they're their metadata basically so back to the back to the source code file C so the first thing we're going to do is we're going to start with a main function there we just wrote our first function so traditionally in a C program they pretty much always the entry point is this function called main you just call it main now technically you can name it whatever you want as long as you tell the linker with the name of the function it doesn't necessarily have to be called main but traditionally it's always called me and that's the starting point of your program and when I wrote in here that short for integer and what it means is that we have a function and when this function runs is going to return an integer so functions can return all sorts of things they can return any sort of datatype they can return characters they can return pointers they can return all sorts of stuff but traditionally main returns an integer and the reason why this is a you might care what main main returns is like imagine if you were on a like a UNIX or Linux type operating system and you're using a you know shell script and you want to run your command line program in a script and you want to know was my did my program run successfully well you might actually want to capture the return value from the main function you know say oh my program exited with an error code that means that something went wrong that I should investigate do you have to do that no you could just put void here and say that's symbolic for saying this function returns nothing we don't care about the return value works fine that way but you know a lot of my you know traditionally it's best practice to go ahead and make it return an integer so remember if you are returning something from your function you'd always the function always needs to return something so you can write this as you can write it like that with or without the parentheses doesn't really matter but I personally prefer the parentheses again it's just more of an old-school way of writing it and if you haven't figured it out by now I am pretty old-school especially when it comes to C so so now we have a function this is actually see if we can compile this I go to build and then go to build solution or if it'll work yeah see you down here it says build succeeded and if you want you know I can make this text bigger right here there we go that's huge okay so build succeeded zero error zero warning so this is a valid we have created a valid C program we just compiled it and if I go back to the file system over here you'll see that it has created an executable there's my executable game B exe so this is actually a good point for me to talk about these different profiles in Visual Studio so these different profiles you have a when you have these drop-down menus right here you have a debug you have release and over here you have x64 and x86 so these different profiles tell Visual Studio they tell the compiler what sort of project you want to make if you select x64 it's telling Visual Studio hey I want to compile this executable for a 64-bit operating system a 64-bit computer 64-bit operating systems are the norm these days ten years ago 32-bit operating systems used to be the norm and even before that we used to use 16-bit operating systems a long time ago so typically I'm gonna leave this on 64-bit because that's the most common thing the most common platform these days but you could go with x86 and x86 and 32-bit are basically synonymous so just remember that we have debug and release this is a bit more interesting because if I compile this project with in debug mode debug mode basically tells the compiler to turn off all of its optimizations by turning off all of its optimizations it makes the executable a lot easier to debug and that's why they call it debug mode my phone reacted strangely I think to me saying the word debug anyway if I compile the same executable in release mode the compiler will turn on all of its optimizations so the compiler will do things like not initialize all of the uninitialized memory it will it will optimize out any sort of like variables that it means to that it can't that it can optimize it out during while it's running it will reorder CPU instructions as necessary every last thing that it can do every last trick in the book to try to make your executable run faster so when you compile in release mode you get you get a smaller executable you get a faster executable leaner meaner all of that on the downside though it's much more difficult to debug because if you had to break into a release mode executable and try to try to debug it you would be all of those optimizations that I just talked about would actually make it look much more confusing to you as you were trying to debug the executable make it much harder to debug so just remember that if you're trying to actually debug something make sure you're actually debugging the debug version of your executable not the release not the release version so I'm going to leave this in debug mode typically until you know it comes time to actually share this executable with someone else until it comes time to actually quote-unquote release it so all right so so back to the program we have our main function here which while perfectly valid also does absolutely nothing oh and by the way my neighbor over here is grinding paint off of some 55-gallon drums with an angle grinder because he makes barbeque grills for a living and its really annoying while I support his his hobby and his alternate means of income it really is harshing my mellow right now but we'll live so we have this main function over here that does nothing so the next thing I'm going to do is what you'll probably see over and over again is I'm going to enhance our main function with some parameters there we go yeah okay so what this means is now I have my main function it's saying that my main function can now accept some input parameters and these are very common this is what you always see in a bane function and it's there basically this is how you pass command-line options into your program so let's say for example you had you know what instead of doing that let's just do it with our program I was gonna show you just like a random program but why I want to show you our program so let's do this I am going to I'm gonna I explain all this okay so this is saying I this is the number of arguments that have been passed into the program over the command line and this string right here is basically the the arguments the the command-line arguments in string form that have been passed over into us into the main function so what I wrote here was printf which is basically this is how we print things to the console and if you notice that the text of printf is light and if I or over it with my mouse it looks like Visual Studio does not recognize it it doesn't know what the printf function is and if I'm going to use it and like I'll compile it right now and it will they'll be an error because the compiler doesn't know what printf is it says printf undefined unresolved symbol etc Center so what we're gonna do now is we're going to add something called and include include stdio.h okay so that include directive basically tells of the compiler I want you to go find this file STD to stdio.h we need to go find that file and I want you to basically stick it into this file so I mean it's literally a copy-paste operation it's where pasting the contents of this file right here into our home C file so the STD STD IO dot H file that's hard for me to say stands for standard IO and it contains some function declarations for some very very commonly used input/output functions one of which is the printf function and if you'll notice when as soon as I included that file it the color of printf it turned yellow and that means oh now all of a sudden the new Pilar knows what printf means because we included this include file so so now I know I think he has an air horn out there just to mess with us ridiculous so the printf printf function prints a string prints a string to the console and I'm going to print the number of arguments that were passed in to this program and then I'm gonna exit so I'm going to print the word arguments and then this percent I this is called a token or a format specifier it says I don't want you to actually print a percent sign and I actually want you to print the variable that I'm gonna specify after after this comma which the variable that I want I want the compiler to actually print in this place is this variable right here Argosy which is it was passed in to us from the main function it's the number of command-line arguments so let's see and then this is a newline character this is a Casilla forward slash here or back slash what are you going to call it is it an escape character it basically tells the printf function I don't want you to actually print /n onto the screen I want you to print this which basically symbolizes a new line and then more exiting with return 0 so let's compile that see what is built succeeded now I'm going to I'm going to navigate over there where is it by the way documents source repos okay worse repos maybe debug no jack 64 either finally there's our program Andy exe so if I hit enter what it's gonna do is going to sprint it's going to print that line arguments and then one then printed one because it counts the name of the executable itself as one of the arguments so printed one and then it exited the program exit now if I were to type some other stuff in here like foo and run it again now I'm saying I have two command-line arguments because the name of the executable is one and then this first command-line argument actually makes it equal to if I run the same thing again with another command-line argument there space delimited now I have three arguments now the arguments themselves are stored in this array right over here Arg V which is it's an array of strings so let's do something here this is called an if statement and we're telling we're telling the compiler if the number of arguments is greater than one then I want you to do whatever I say inside this if block right here we're just going to be print out first argument semicolon there and then what here are the one so this means because this is an array then this is the array index right here arrays they start at zero so if I were right R to be zero in these brackets that would be the first element of the array which is the name of the executable right so the first actual command-line argument would be the the second element of the array which is actually our v1 which should be whatever I type into the command line whatever my first command line parameter is so let me rebuild let me go back there so it says I have two arguments which there's my two arguments it says that my first argument is food which is what I wrote here so everything is working as intended the reason why I wrote only I only want to print that line if you actually specified see if I don't specify any I don't specify command line parameter there then it won't print that second that second piece and the reason why that's important is because there's no there's no checking on like if I if I didn't have this I'm gonna comment that out and so I'm just going to unconditionally print that where it says first argument but what if I didn't supply an argument what if the user didn't supply an argument I can tell you right now it's gonna crash it's gonna crash because I'm telling printf to I want you to print the value of the RV one right there but there is no arc v1 if I don't supply a command line parameter so it actually gave me a null I think it was supposed to crash but it was it was nice enough to give me a null there see what happens ready there there we go now I got a crash go look in here yeah so I went and looked in my Windows Event Viewer and I can see that I did just have an application some application on this system just crashed application error 1000 and who just crashed game B dot exe just crashed and why did it crash it crashed somewhere in this Universal C runtime yeah it means I did something wrong with basically it's because I referenced I referenced something that doesn't exist I referenced an element in that array that doesn't exist because I'd never supplied any camilan arguments all right so hopefully that makes sense I tried to say it in several different ways try to clarify it so that's why I'm saying I only want to print this if the user actually supplied some command-line arguments ok so what else do we want to do oh okay I never actually I never actually said anything about variables you know just cleaning up here so in our function now let's make another function that's what I'll do that's bubble okay let's make another function called add and this function is going to take as an input as you can remove you remember from before it means we're going to return an integer in fact I said I should actually clarify if this function only works with integers you can name it whatever you want but you should name it something descriptive so that people who are coming back and reading your code know what you were talking about this is going that this function is going to take as an input it's going to take two inputs X&Y and it said your X and an integer Y there we go sometimes visual studio like likes generate errors for no reason and then they go away randomly it's one of the things I don't like about this IDE annoys me sometimes let's see where were we okay so I'm going to return X plus 1 simple so all I'm doing is I'm basically I have a function that takes two numbers as them together and then returns the result to the caller so if I comment all this out let's just forget about that stuff right now and then I'm going to declare a new integer here and I'm gonna I'm gonna call it Z you call it whatever you want to call it Z and I'm going to assign to that variable the output of that function that I just wrote which is add two integers in which two integers am I going to add I'll say 3 and 2 so we already know that the output will be that the answer will be 5 right so I just pop the penis up here say yes sir and then the number that'll be z okay so we're printing the value of Z to the console so we'll compile that we'll run it and sure enough says answers 5 you know what I can make this text bigger - can't I make it a little easier for you to see yeah ok all right what else do I want to show ah okay so here is demonstrating a little bit of the debug debugging capabilities of the compiler or of IDE basically of the visual studio environment if you will so if I if I click right here and I go run to cursor this program will actually run until it hits this mark right here where I have my mouse cursor and if you look down here we have all these variables in memory which is really cool so you were we are debugging our program right now say if it had a bug in it and we were trying to figure it out this this is what would help us find the bugs so arc C which we already know is one because I didn't supply any command line parameters we have our V which is that array of string based command-line arguments and since we didn't supply any the only one in here is just game B Exe and then we have our variable which is Z we we declared this variable an integer Z and as you can see right now it has basically it has a nonsense value in it because it hasn't been initialized so if I take one step you can see the arrow right here is where I am in my debugging if I take one step by hitting B it's f10 there now you can see that the value of Z has changed to 5 you can see that the function add two integers has returned a value 5 and the value of 5 was assigned to the variable Z and then I'm about to print it out to console right there so that's s debugging so a lot of time a lot of people make fun of people who debug using the the printf statement like if you're trying to if you have a complicated program and you're trying to say you know I got to this area in my code you just write a little printf statement that says here I am and that's how you know that you got to that that area in your code that's a very in elegant way of of debugging and since I by profession I'm a debugger I'm going to definitely put an emphasis on writing proper instrumentation and proper tools and proper clues in our code that makes the debuggers life easier it makes debugging easier and I will say that I will say that you should do that you should add debugging to your program as soon as possible right from the beginning because if you don't start doing it right from the beginning then you probably never will do it and then your program will eternally be difficult to debug and if if you do go back and do it it's gonna be a lot harder than if you had just started doing it from the beginning so that's probably a topic for another episode one of these next episodes will will add something add some proper debugging instrumentation in there let's see where are we okay so I think we have probably I think you've probably had about an hour I should have put a timer on one other thing I did want to say though is if you go over here all of this stuff is documented this is Doc's Microsoft comm and this article that I'm looking at right here you could probably earlier see that my eyes were darting off the screen it's because I was looking at this stuff just to make sure that I was typing it correctly all this stuff is all this stuff is documented now this actually this website used to be called MSDN Microsoft developer Network and I bring it up because if you're writing a program that's going to run on the Windows operating system then you're going to be referencing MSDN a lot MSDN basically is the documentation for Windows so if you're going to be interacting with the Windows operating system you're going to be looking at this documentation a lot and now it's not called MSDN anymore though they actually migrated it all to docs Microsoft comm so if you hear me talking about MSDN this is what I'm talking about they just moved it they don't call it an MSD in it anymore let's see there is oh you know what there is one other thing I want to talk about there is definitely one other thing I want to talk about and that is that is I'm gonna erase this bit here so that's not been our way so here's our little C program I want to make a quick note the difference between an Z and Unicode so right now this program is in was what we would call an za n si or ASCII if you prefer basically means if I were to look this up now it's it's basically a character encoding ASCII it's a way of representing characters using numbers because everything that a CPU does is numbers under the hood right so everything is has to be translated back and forth between you know ones and zeros and those get translated into into hexadecimal and then that gets translated into characters that you and I can read off of the screen so for example this right here is how we for a long time represented every printable character on the keyboard using codes like numerical codes and as you can see this only goes up to 255 because each character was only 8 bits and if you have 8 bits you can only count up to a maximum of 255 we might we might do a binary lesson later but maybe we'll do that next time but I think we're running low on time so I'm going to skip over that you only have 8 bits with which to represent all these characters which means you can only represent a maximum of 255 characters which you see on your screen right now we have your standard punctuation marks we have your numbers yeah you know colon/semi-colon equal sign question mark we have capital A through Z we have a lowercase a through Z and then won't be pretty much done like we can't have any more characters other than that so then computers evolved to a point where we were like you know what we have a lot of characters besides just these 255 that we would like to represent we have lots of different languages you know French and German they have weird characters little sharp s and you know the characters that have till DS and accent marks over them all those different types of characters and you know we don't just have English in this world so we needed a new system that could display all these characters and more and we called that Unicode and the way we do that essentially is by allowing us to represent characters using more than 8 bits we might use 16 bits instead of 8 and we might call that utf-16 that's one type of Unicode so with that little computer science lesson hopefully that was clear as mud so with that lesson I wanted to say that windows the Windows operating system is natively a unicode operating system and what I mean by that is if we're interacting with the Windows operating system we're going to be using what is called Windows api's there's our application programmer interfaces something like that programming interfaces 8 that's what API stands for I said I don't need to show you that I can show you that right here what I mean by that is this let's take a Windows API function like output debug text which I cannot use yet because remember with remember the situation with printf before I had to include something before I could use that function because right now we don't know what output we don't know about the output debug text function so I'm gonna include something called windows down age and again if you are programming for windows on windows this header file windows on H is something that you're going to be using all the time and by the way you can actually also create your own header files let's put the dot H extension is 4 but right now we're just using ones that already come with they came with Visual Studio they come with any you know SDK the debugger they come with all sorts of different there's all sorts of different ways to get these header files these are here came with Visual Studio anyway so the output debug the output debug text function does it not come with Windows apparently not so I'm gonna output D by post sending texts to D by output window and that's not what I want let's output debug string damn straight I'm just gonna write the word I'm gonna write the string now when I do that I'm going to right click here I don't know right I'm gonna click run to cursor and then I'm going to go back over here down to where's my output window window windows open finally okay so I had to enable that output window and this is an interesting output here when I okay so the purpose of the output debug string function is that it's supposed to help the debugger it's supposed to help you coma the debugger debug your own program by outputting text-based output to this window right here to a debugger which we're in one right now problem is is as you can see it's all sort of mangled and this actually plays in perfectly with the Anssi ascii slash unicode lesson that i'm going for here it's mangled because this function thinks that it's getting unicode input when i actually just gave it a ski input now let me go ahead and stop the execution right there and let me go ahead and correct it by putting this little L character means that I'm writing a Unicode string instead of an ASCII string now let's run it again let's see what happens this time as you can see and let me increase the text size Ken I know it won't let me but as you can see down here in the corner it properly printed the word hello into my debugger which is exactly what I was trying to do so now let me stop again let me go back here now notice how the output debug string function is purple that's because if you hover over it notice how the output debug string function has been defined to mean something else and this is one of the core tenets of programming in Windows this is something that you will see over and over and over again in Windows is because the the compiler right now is assuming that I'm compiling either for Unicode or ASCII it translates all of the windows API function into one or the other so let me try to say that again all of the windows API functions have two entry points they have a ASCII version which as you can see right there I just put the letter A on the end of it it means that this is the ASCII entry point and then they also have a wide or Unicode version which is right there so now these functions do the same thing this is the ASCII version so I'm going to take that l specifier away I'm going to write hello ASCII and then on this line notice this is the wide wide me wide or Unicode same thing because they call it wide because we use 16 bits to represent every character instead of 8 but I have to remember to put my L right there to tell the compiler that this is a Unicode string there now if I and you know what just to get this clutter out of the way I'm gonna get this clutter out of the way you'll need all that nonsense now let me run to right here what should happen is I get to properly formatted strings one is ASCII and one is Unicode the reason why I put all that is because let's go ahead and stop execution as I was saying earlier Windows is natively a Unicode operating system so if you are interacting with the Windows operating system a lot you want to prefer using the Unicode version of the api's so you should always try to use the unicode version of the api's so if I just write output debug string notice how it's been defined it's already it it defaults to the Unicode version and if I hover over that you can see that pound define output debug string is actually output debug string W so what's a pound to find if I do pound line if I say magic number if I I can you know 22 and that could be anything I want it to be and if I do let's do it's like a variable there that's my pound define I could have made it a variable or I could have made it a pound of line so that's what a that's what a defiant is if I run that let's see what happens I crashed oh wait no I did there it is magic number 22 so it did where I thought I would do okay so that's what that is I'll get that out of the way if I go over here to the properties of my project and I go down to advanced here character set use Unicode character set I can actually change that to tell the compiler to use a multi by character set which is just another name for ASCII or an z to what we were talking about it's the smaller character set I hit okay oh and I want to make sure this applies to all configurations and all platforms that's these of profiles aren't up here okay so when I do that look what happens now output debug string is now defined as output debug string a so it makes sense so what happens whenever what happens when I use this output debug string a this function is actually going to send my ASCII input to Windows Windows has to translate it to Unicode process it get the result translate it back into ASCII and then back to my program all those translations are costly they're they cost you in terms of performance now most of the time you won't notice on a fast computer but it's worth noting that you because you want to avoid these unnecessary translations between you and Windows you should strive to use the Unicode version of Windows API whenever possible that is that so with that I'm sure that we've gone over time I wanted to get some C programming basics into episode one as an introduction so starting with episode two I think we'll just pick up where we left off here we are going to eventually turn this if you can believe it what we have right here we're going to eventually turn that into a video game with graphics and sound and all of that kind of stuff so should be pretty cool stay tuned and I will see you next time
Info
Channel: Ryan Ries
Views: 39,931
Rating: undefined out of 5
Keywords: programming, coding, video games, tutorial, visual studio, c/c++
Id: 3zFFrBSdBvA
Channel Id: undefined
Length: 65min 44sec (3944 seconds)
Published: Sat Apr 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.