Game Programming - Episode 14 - FPS Counter

The Cherno · Intermediate ·🚀 Entrepreneurship & Startups ·13y ago

Key Takeaways

The video demonstrates how to create a game from scratch in Java, focusing on implementing a frames per second (FPS) counter using variables, timers, and Java tools like JFrame and system.out.print.

Full Transcript

Hey, what is up guys? My name is Aern. Welcome to episode 14 of game programming. Today we're going to take a look at the FPS counter essentially. And um I'm going to make this episode a quick one because I do not have time right now. So yeah, I'm not going to get into why I don't have time cuz that'll just waste more time. So yeah, just bear with me. Um yesterday we took a look at actually making a timer to ensure that update only happens 60 times a second and that's what it does, 60 times a second. Brilliant. Now to actually measure the time, measure the amount of frames that we actually render and measure the amount of times that we update because that should always be 16 and we should be able to visibly see this on the screen. What we can do is is add a few simple tricks. First of all, I want to make two variables here called uh frames. And this frames variable want to set equal to zero. Remember this is initially. So initially we want we want it to be zero. Frame the frames variable is going to count exactly how many frames we have time to render every second, right? so that we can then work out, you know, how fast the game is running basically because we can tell how many frames our computer is actually able to render every second. Um, now the next variable is I'm just going to call it updates. And updates is going to measure how many times this update method gets called every second. So again, that should be 60. That should be 60 at all times. Now, to actually measure this, all I want to do here is every time, you know, this is a while loop, right? So every time that it it does this while loop let's just add one update into it because right every time that we do this while loop it calls the update method. So right the updates the update method has been called let's updates plus+ right what's that what that what what that is going to do is it's the same as updates plus equals 1 right which is the same as updates equals updates + one that's exactly what it's doing. So it's just adding one number it's just adding one to our updates variable every time it does this. So then we can actually calculate how many times the update method is called which should be 60 times every second. So I'm just going to uh simplify this again. Same it's a similar deal with with rendering as well right render gets what gets called here in this um in this loop as well and um in the while running loop. So what we need to do is we need to count how many times frame uh render actually gets called and that'll be our frames per second. So this while running loop right is happening as fast as it can. So how many times does this while running loop actually run every second and we can do that simply by counting again every loop. So every loop we will add one one number just just the number one just add one to our frames variable. So frames plus+ all right hopefully you guys are still with me. Um now the next thing we need to do and this isn't something that we need yeah we kind of do have to do it is actually at the end of every second so in other words once per second we need to get that data and display it somewhere and then reset everything. So in other words what we need to do is set the frames and updates back to zero so that we can start counting from the new second again and we need to actually you know display this somewhere so that we actually get the frames per second recorded somewhere. So, so to do that, we actually have to use our timer to do something only once every second. And to do that, we need like a sort of like a timer variable. So, I'm just going to make a new variable up here. I'm going to make it a long as well. And I'm just going to call it um I'm just going to call it like timer. And this will be like our 1 second timer, right? I'll set that equal to system dot current time milliseconds. Okay. So remember 1,00 milliseconds is 1 second. So if I go over here right just after the frames um variable all I'm going to type is that if system current time milliseconds. So this is the current time at this iteration of the loop if it minus our our timer. So, in other words, if the difference between the current time and the last time we actually ran this method is 1 second. So, in other words, if it's greater than 1,000 milliseconds, because 1,000 milliseconds is 1 second, then we'll do this. Now, what's going to happen is the first thing we need to do is actually add uh 1,000 to our timer so that this doesn't happen again. because we're saying that as long as the difference between these two variables is actually greater than 1,000 which will be once per second because every time that it is greater than 1,000 um which is after 1,000 milliseconds it's adding another,000 milliseconds. So this now becomes you know 2,000 milliseconds basically that's is basically what's happening because this difference this timer variable becomes bigger. So we're adding one more second onto it. So, it's like saying, "Okay, at let's just say 9:01 p.m." cuz that's the current time. And I'm actually supposed to have an episode out by 9:00 p.m., but again, sort of in a hurry and don't have time. Um, speaking of time, by the way, these times anyway. Uh, so at 9:01 p.m. and like 50 seconds, actually, what are we at? Like 24 seconds, right? At 24 seconds, we we do this, right? It's greater than 1,000. And then we're going to be like, "Okay, let's make it equal to 25 seconds." Now, we wait a bit. We do this a bunch of times. Oh, look. It's 25 seconds. Now, let's This statement is true. Let's do what's here. Okay, it's 25 seconds. We'll add another second to it. 26 seconds and so on. So, this will happen once per second. That's all I'm stressing here. Um, and what we'll do once per second is what we basically need to do is display the um the FPS somewhere. Again, we don't really have to do this. You don't have to display if you don't want to, but I will. So, I'm just going to print it out into the console. So system.out.print line, right, which will print one line into the console. Um, and I'm just going to type this updates variable, right? So we'll print updates plus and we'll print we'll print a string as well, just UPS, updates per second, um, comma, and then frames, and then, um, plus a string that just says FPS. And you'll see what this looks like in a minute if you can't already. And then one one more important thing that we need to do is actually reset our variables back to zero. And I'll show you what happens if we don't. So right now, if we just run this, what we should get is um is this. So you can see right here what's happening. First of all, it's not exactly working. If I just stop it, what's happening is again we did not reset the var variables back to zero. So what's happening is 60 updates per second. Brilliant. And you can see this is all multiples of 60 because we're not setting it back to zero. So it's not counting again. It's just adding the FPS onto onto our um onto our current variable. So, it's giving us inaccurate results because it's just adding on to, for example, to to the initial variable, which was 2,421 frames per second, which is roughly what our game is running at running at at the moment. It's adding the next amount of frames per second to that. So, you can see that we get basically doubled that. So, what we need to do is reset the variables back to zero. So, updates equals zero and frames equals zero. Simple as that. We hit the debug button and run this. You can see that we're getting um we're getting brilliant results here. So, it's happening at roughly um an average of 60 updates per second and uh and pretty good FPS here as well. We're nearing 3,000. So, um yeah, you can see that what happens is if it goes 59 frames per if it goes 59 updates per second, one loop, it'll actually jump to 61 the next one. Again, 5961 and generally staying at about 60. So, that's brilliant. We'reing at 2,000 frames per second. We now have a way to measure this. Now, a few of you actually asked, you know, why did I put the JFrame all the way up here? Why didn't I just put it into this static main method? And the reason I did that is because I actually uh made a made a bit of a habit into putting this FPS counter into the actual uh title bar up here. So, what I can do here is because this because this this frame uh field, this frame variable is actually um you know throughout our whole class, it's actually outside of it's not in a specific method. Um over here in the run thing right under here I can simply type frame dot set title and again our title was uh was rain and I might just actually set that as as a final. I'm not going to make it final. Um one second. Okay. So, public static string title to that. And then what I'll do is did I just Yeah, I just closed it. My bad. Then what I'll do over here is frame set title. I'll simply set it to title and then plus um I want to make a bit of a gap. And then essentially just this. All right. It's going to be simple as that. So if I hit this up right now, what's going to happen is you'll actually be able to see the FPS over here. Now the advantage of that first of all plly is that if I actually export this, obviously I'm not going to be able to see the console unless I actually run it through command prompt or something. Um but over here, you know, anywhere this window is um you'll actually be able to see the FPS um on the actual title bar. All right, so I hope you enjoyed this episode of game programming. If you did, please hit the like button and uh I'll see you guys see you guys tomorrow with a brand new episode. And uh actually tomorrow I've got something something special in store for you guys. So I hope you um hope you look forward to that later 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 · 55 of 60

1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches how to create a game from scratch in Java, with a focus on implementing an FPS counter. The FPS counter is used to measure the game's performance, and the video demonstrates how to display it on the title bar. By following the steps in this video, viewers can learn how to design and implement game systems, use Java tools, and optimize game performance.

Key Takeaways
  1. Create a frames variable to count the number of frames rendered every second
  2. Create an updates variable to measure the number of times the update method is called every second
  3. Add 1 to the updates variable every time the update method is called
  4. Add 1 to the frames variable every time the render method is called
  5. Use a timer variable to reset the frames and updates variables and display the FPS every second
  6. Create a 1 second timer using system current time milliseconds
  7. Check if the difference between current time and last time is greater than 1 second
  8. Add 1 second to the timer
  9. Display FPS by printing updates per second and frames per second to the console
  10. Reset variables back to zero to get accurate results
💡 The FPS counter is a crucial tool for measuring game performance, and displaying it on the title bar provides an easy way to monitor the game's performance without needing the console.

Related Reads

📰
Mastercard SME Confidence Index: 81% of Nigerian SMEs are confident about the year ahead as growth ambitions build
Nigerian SMEs are confident about the year ahead, with 81% expressing optimism, driven by growth ambitions
Techpoint Africa
📰
Google Play to back 10 African game studios with $1 million fund
Google Play is investing $1 million in 10 African game studios to support their growth, addressing the funding gap in the continent's $2.29 billion gaming market
TechCabal
📰
Why Freelancers Need a Personal Board of Advisors
Freelancers can benefit from a personal board of advisors to overcome solo decision-making challenges
Dev.to · Alfred P
📰
The Grumpy Guide to Raising Money
Learn how to raise money for your startup with a grumpy yet informative guide, covering key strategies and pitfalls to avoid
Medium · Startup
Up next
Watch this before applying for jobs as a developer.
Tech With Tim
Watch →