 What is up everybody welcome back to another C++ and SDL tutorial Hope you've been enjoying the series so far in the last tutorial what we did is we well We had a nice little program that did not do much at all, but at least we had a different color background We were looking at SDL surfaces SDL rectangles SDL rex We were taking a look at the you int or the unsigned in 32 bytes 32 bits Data type and had some new functions with SDL map RGB SDL fill rex and SDL update window surface SDL update window surface is crucial and It's it's kind of an important especially for updating the screen While things are happening during the game in this tutorial though, um, I Don't know you probably can't hear it on mice on my end and with my computer You may very well be able to hear it on your computer though Maybe your processor is starting to kind of kick it in the high gear Maybe your fan is turning on because you've got a program running very very fast right now the thing is when we create this SDL program and Typically when we have this this main event loop it's gonna run as fast as possible as fast as your computer can do it and That's gonna make a little bit of a mess, and that's why your processor and stuff is gonna jump into I don't know. I can't even explain Your friend your fan is gonna kick on there's it's it's gonna be crazy The point that I'm trying to get at is that we need to cap the frame rate we kind of need to limit how fast our game is running and Typically you see frames frames per second or FPS is Not first-person shooters But an FPS or frames per second and kind of a limit as to how fast the game can really run typically we see 60 being a good frames per second, and so that's what we're gonna use in this tutorial though We're going to use some pre-processor definitions. You guys know that I'm using the pound symbol include to include the SDL library and the standard template libraries iostream library as part of the standard namespace Rather than include in the pre-processor definitions in this case. We're going to define You'll notice my keyword the keyword define kind of change there because that is an action and some of you can really do with c++ If you have not heard of pre-processor definitions before that's not what I want to teach you in this tutorial We're just going to use it and take advantage of it Like I said, we're gonna be using frames per second a variable here I'm just going to call mine FPS and I use this so I can keep it as kind of a constant because it's not really going to change in fact You know what else isn't going to change our window width and our window height a nice at least initially At least what we will pass into this create window function so we can go ahead and define a few other things I'm actually not going to call it's just plain width. I'm just going to call mine define window width sorry window width and I know we use 400 and we also define the window height Which we have to be 200 window width and We kind of use it just like any other variable anything that we kind of define although since it is I don't know if Macro is the right word for it, but a definition that we have. Oh, I'm also working in the my changing the color Program I'm going to change. I'm going to save this as something else 0 5 underscore 0 5 a underscore FPS or In this case setting a frame rate You can see I have some old code and some things that I will be getting into later on the series We have this setting a frame rate right here. I'm going to save a different copy of it and I'll go back and edit the other the other one beforehand or at least after the tutorial All right enough talking to myself Let's get into what we have to do next once we've defined this variable or this kind of this constant for us this FPS what we want to do is hop back down to our code over here Let's see. I don't want to change the background color or anything just yet. Actually I'm gonna set this back to white and I guess I suppose I will keep it But I'll just kind of run this all together Underneath all that we want a new variable and I'm gonna kind of cluster it with the SDL event and a bull running equals true Because it is something that is necessary in mandatory if we want to set up this up frames per second or FPS cap So this is a unsigned integer 32-bit data type So you in 32 and the variable that I'm gonna call at least what I'm going to call my variable is starting tick And I don't mean tick as in the insect or the bug I mean tick as in like a clock a clock would tick and that's the thing Well, while we're running inside our event loop. We want to reset this variable starting tick To equal remember. We're just declaring it up here We're actually going to define it down here and this is going to happen every frame because we're looking at it in the Main loop what this is going to equal is a new function. So let's hop on over to our documentation. Let's look for SDL get ticks new function here SDL get ticks use this function to get the number of milliseconds since the SDL library initialized and remember we have our initialization right up at the top here that happens at right at the beginning and This starting tick is going to be refreshed every single frame. So this is going to keep adding and adding and adding and on to it so This doesn't take any any parameters or anything here all it does is returns the value of milliseconds since we initialize SDL So Yeah This value wraps if the program runs for approximately more than 49 days. Holy crap That's crazy. Can you imagine running a program for 49 days? All right, let's run this function SDL get ticks And now I'm going to show you how we can kind of limit the frame rate We do this by kind of a little math trick and that has to do with the milliseconds and stuff that we do I'm not going to explain how all this works I am going to explain what you should write and what you need to be actually programming and the code that you Need to be writing so at the bottom of our well running at the bottom of our main loop Let's set up an if statements and if conditional statement and let's have a small other parentheses section here That says one thousand which is the amount of milliseconds in a second obviously divided by FPS or Frames per second if that is greater than SDL get ticks at the moment subtract starting tick Which would represent the time that this frame is actually running in because Starting tick as in everything that we've seen so far as in the moment that we're getting right now if there is more What we're gonna do We're gonna have a code block here after that condition What we want to do is we want to run the delay function that we've been looking at Way way back at the beginning of this tutorial series SDL delay make SDL wait for a little bit And what we're gonna do is we're gonna do is that same 1,000 milliseconds divided by FPS minus and Open up a new set of parentheses here SDL underscore get ticks Minus that starting tick the same thing that we have Right up here and use the a semi-colon to end the line cool That's really all it takes now You won't obviously see a change in the program Let's uh G plus plus I'm gonna change this to 05 a setting a frame rate. No, I do have an error. Oh cuz I just changed a the color background variable name Now that runs just fine at least it compiles. Let's run the code. We have our white background and Things are pretty quiet My fan isn't acting up I don't I don't hear my CPU spinning or anything going crazy we got a window running just fine and The frames per second is limited. We've got a cap on the FPS So now it runs at a good speed. That's not too crazy There we go very very easy very very simple all it takes is this conditional statement and keeping track of the starting tick or How long it has been since SDL was initialized Now obviously you might not want to have to rewrite this over and over and over again If you have any other programs Also, it just kind of looks bad Honestly having this this weird thing this weird block at the very bottom of our main event loop so what I'm gonna do is Create a function and what I'm gonna do is Have the function called cap frame rate And that'll take Starting tick as an argument now. Let's go up to the top of the program and actually define that function Let's say void because this isn't gonna do anything Cap frame rate is the name of the function that we decided on this is going to take a UN 32 And I'm gonna name the variable the exact same as it was before just so it kind of easily The code easily fits in without me having to have to change anything and I'm gonna fix the white space over here So it looks okay, and now we're all right that this function should work just fine for us I'll code fold that and we can just cap frame rate right down at the bottom of our event loop I recommend you do the same now if I hop over here compile this no errors from the program and Well, there's no change or at least no evident change But I know that the the game in the program is running at a much better pace right now. So that's all it sucks Kind of implementing a frame rate. It's not as easy as pie game Just a little clock clock. Tick with the FPS. This is a little bit of a different process But hey, this is okay once we throw it in a function. So thanks for watching everybody. Hope you enjoyed this Yeah, I was gonna say I'm not really gonna go over this or discuss this much again We're just gonna use this function if you want to know more of how this works I guess you could look for it online, but this is the technique to limit the frame rate Just by testing the amount of seconds that it's been or milliseconds since SDL was initialized I'm gonna stop talking and I'm gonna thank you guys again for watching Please comment, please like please subscribe if you're willing and hey, I'll see you in the next tutorial. Bye