Game Programming - Episode 13 - Timer
Skills:
Systems Design Basics80%
Key Takeaways
The Cherno implements a basic timer in a game using Java, utilizing system.nanoTime() to measure time in nanoseconds and calculating frames per second (FPS).
Full Transcript
hey what's happening guys My Name Is AO and welcome to episode 13 of game programming so yesterday we um took a look at this pixel and we actually got it to to work properly and this time um you know today I promise that I teach you guys how to uh how to make a timer and how to actually keep track of um of things that don't necessarily have to happen in every CPU cycle like the update method um because obviously that needs to happen 60 times a second so not like as much as possible as second so that's what that's what we're going to look at today that's creating a basic timer um now this is not going to be an FPS counter it's simply going to be a timer okay so there's a bit of a difference um I'll probably do an FPS counter tomorrow okay there's yeah you'll see okay so in the run method I'm in the game. Java class right now in the run method right just before we actually begin our wild running Loop so again what's going to happen is the Once the game starts once the thread starts the run method is going to be called and immediately what's going to happen with that is it's going to do you know whatever's here so in this case right now it's it's just going to jump into the wild running Loop and just do this infinitely until we end our game or you know until we said running equal to false um so it's not going to go any further than that if we type something in here like it's going to do that when we close our game basically and that's kind of useless to us but if we actually put code over here in between the you know at the start of the method it's going to execute this code once and then it's going to go to the while running Loop and loop whatever's in those brackets so when we first start our game when we first initialize our game we want to actually set a few variables and create a few variables the first variable that we're actually going to do is create a variable to actually store time so there is a a way a format to actually measure uh the the the the systems time so in terms of um you know almost like to to measure is like every CPU cycle so that we actually know you know which point in time our computer is at um and then we can use that to for example time how long it takes us to render something um ensure that we don't render anything above a certain number per second um all sorts of all all sorts of uses for that so um yeah but we actually need to retrieve you know that variable that actually holds the time so it is a long okay um because it's a very uh long guess this cuz of a very long thing obviously the long is um the the long variable is just a very very very big number that um is very um you know long and big and stuff um we're just going to call it uh last time because it's is going to be like our last time you'll see see what I mean in a second I'm going to say that equal to system do Nano time um now there's two way to there's two ways to measure this there's system. current time Millis which is like the system. current time milliseconds um that that's in milliseconds this is in Nan as as you can imagine which is very very very very very precise and it's much better to use this than it is to use Uh current time U milliseconds although in some cases and in this timer as well we we will actually be using um current time uh in millisecs as well as as narcs so this what this does is first time we run our game it just sets what whatever our computer is currently at in terms of time to this last time variable now the next thing we need to do is actually create a double uh or or a variable for actually converting um integers uh sorry not integers for for converting milliseconds into nanc or actually vice versa converting nanc into milliseconds so um I'm just going to um we'll make it final because we're not going to change it ever and we're just going to call it um actually we could change it technically but um I'll do that in a second it'll be double and we'll call it NS just that just stands for nanc that'll be like our Nano seconds conversion thing and we'll set that equal to 1 billion so 1 and then 9 Z 1 2 3 4 5 6 7 8 9 UM and then you know that and we put 0 Z to ensure that we're actually in double Precision here and we'll simply divide that by the number of um well this isn't that's obviously the conversion here but what we want to do is we want to um what a good way to put this what we divided by is is how many times we want we want to actually run our update method because you'll see the maths sort of come together at the end I'm sort of looking forward here um the number that we put here which is going to be 60.0 in our case basically what this is going to do is when we actually do divide by Nanos by NS by this variable um we'll be essentially setting um you know what I I can't it's really hard to explain this without actually having code on the on the screen for you guys to see it in effect so I'm just going to continue writing this right now um the other thing we need is a Delta variable which again you'll see in a minute you'll see that in action in a minute and that's pretty much all we need so far in the um in the in you know in the preliminary run method with you know outside of the outside of the loop all right um so what we need to do here is actually create a variable another long called now and we're going to set it again to system. Nano time so now you might be thinking um you know you've created the variables they're both equal to the same thing what's up with that so the deal is when we first run our program when the game first opens we retrieve the computer's current time right uh in in nanocs now while when we're running obviously by the time it's completed all these lines of code and actually gotten to the running Loop um the system's time is going to be different cuz it doesn't do this instantly it does take time even if it is a very small amount of time it still does take time now the best way to actually um the best way to probably uh demonstrate this is if I just hit return here and um just so it gets out of the method and I actually just run this it's going to terminate hang on um so I'll print now followed by okay so last time plus that um and I don't know I'll just do like a comma okay so that's going to print the two variables onto our console and um over here I'll just call the stop method so that we actually stop the program okay so right now you can see that it's printed two variables over here um and the this is actually the system time in nanc I'll zoom in a lot so you guys can see this this is the first one this is the second one as you can see the second one is actually quite a bit larger um than the first one that's because it's actually taking time to get from this variable to this variable okay because this code gets executed then this code gets executed then this code gets executed then this code then we get to here and we've actually been able to measure the distance now we can measure this distance precisely if we simply put now minus last time and if we measure that we can be like oh all right it took that much time that's a very small number um and in fact I'm not even going to bother working out what that is in actual tangible uh measurement like milliseconds but um that is how that is that is how long it took um in in NS okay it took 2,680 NS to actually complete that operation um Now using this theory that we can actually measure how long it takes to do things we can we can actually um measure you know the amount of time that we actually have to spare so if I cut that and we we get back to our normal um our normal stuff uh what I can do is to the Delta variable I can actually add onto it now plus equals means that I'm actually just plus equals is the same is Delta equals Delta plus and then you know what whatever we put you know what whatever number we put here um plus equals does the exact same thing but it's just it's just a shorter way of writing it okay so Delta plus equals now minus last time here I am again measuring measuring um the the the time that it took I'm okay you get this I assume okay okay what I'm doing here is I'm adding to Delta the difference between these two variables so in other words the time that it took to get from here to here in this case um it'll obviously change later um and then I'll divide it by Nan our nanc variable now what what this is going to do is um again onto Delta it's going to add on the difference here and it's going to divide it by 1 billion divid 60 with which when we actually go over here and say while Delta oops is actually greater than or equal to 1 which will only happen 60 times a second because remember we've set 60 over here um then we're going to update so I'll get rid of it here and we're actually going to just subtract one from Delta so we we get it back to zero essentially or um or you know whatever it is so read this code analyze it and I'm sure you'll find out exactly what it does it's just simple maths so again what we're doing here is we're creating a variable actually I'll explain this Loop thing now what what's going to happen and let's get rid of actually we should probably keep stop here we'll keep the stop method here just because um that's going to ensure that if we do get out of this running Lo for some reason it's going to stop the program which is which is good now okay so I'm so tired today I'm really sorry guys I I had a huge day today I can barely understand what I'm writing here um and I have to like run off to an exhibition right now as well for art so well for artwork for my artwork actually so I probably shouldn't be allowed to that s and half an hour um but okay let's try this one more time so last time is going to be equal to the computer's current time our Nan variable is going to calculate um a value which will be this so that we can then divide the difference by it to achieve our Delta variable this now variable is going to get the current Loops um current time because what's going to happen is obviously it's going to get to the end of this Loop and then go back and then this now variable will be updated um oh we actually forgot one more thing and that's actually to set last time equal to now and again what what that's actually going to do is um you know after it after it actually uses now minus last time it's going to update last time and then it's going to calculate the time it takes to um complete all of this so it's going to calculate the time that it takes from us to get um from here all the way back up to up to here and then it's going to calculate that distance so um let's do it let's do it this way I'll demonstrate it quickly so we'll create a bulling called um C we'll set it equal to false um then what's going to happen is at the end we're going to set c equal to true and then over here I'm just going to put if c equal true then let's just render um oops out. print line then let's just render um sorry let's just print out um time tagen and then we'll calculate last time minus now probably okay and we will actually return let's just um let's just exit the program straight away okay so um this is temporary code by the way don't don't bother writing it down I'll delete it at the end of this episode um just before it does this actually I can't oh God okay so um H let's just run this that's wrong okay that's cuz it's set this equal to now let's do that here so if we run this what it's actually going to do is give us a negative number yay cuz it should be now minus last time again sorry I'm really tired okay so now we can see that okay this whole thing to update our game and to render our game it took us this amount of time it took us that many what is that one two three two three 5 55 million NCS it took us to actually render one frame onto the screen and using that basis we can not only control how often we update our game which is going to be 60 times a second but what we also uh control we can also figure out that okay if it took 55 million times uh sorry if it took 55 million NCS to actually render one frame we can be like the game is running at this many frames per second although it's not technically that calculation it's more of a um let's calculate how many times it can actually render per second okay so using that basis tomorrow will actually create that um that uh FPS counter okay I'm sorry I'm so tired right now you guys have no clue but um I did promise one episode per day so take all this code down and you will be I'll just delete the stuff we don't need take all this code down and um and you'll be sweet so don't worry yep all right so um I'll see you guys tomorrow later [Music] guys
Original Description
Support this series and get rewards! ► http://www.patreon.com/thecherno
Welcome to Game Programming, a series in which we take an in depth look at how to make a game from scratch, in Java.
----------------------------------------------------------------------------------------------------------------
Website: http://www.thecherno.com
Twitter: http://www.twitter.com/thecherno
Facebook: http://www.facebook.com/thecherno
Subreddit: http://www.reddit.com/r/thecherno/
Steam Group: http://www.steamcommunity.com/groups/thecherno
----------------------------------------------------------------------------------------------------------------
Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from The Cherno · The Cherno · 54 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
▶
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
I've launched 4 SaaS startups this year - my template
Dev.to · Bucabay
How I Built a Revenue-Ready SaaS in 48 Hours Without Writing a Single Line of Code
Medium · Startup
I Almost Shipped a Hydration App Built on a Myth
Medium · Startup
How Non-Technical Founders Can Turn Great Ideas into Successful Digital Products
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI