Game Programming - Episode 6 - Graphics Initialized

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

Key Takeaways

The video demonstrates game programming concepts using Java, including graphics initialization, buffer management, and rendering, utilizing tools such as Graphics, Buffer Strategy, and JFrame.

Full Transcript

hey what's up guys My Name Is AO welcome to episode six of game programming Okay so yesterday we had a huge episode that I definitely don't have time to ever repeat ever again um so this episode will be short but we but we will actually be displaying something today we're going to want to do something really simple um essentially um we're not going to talk too much about this but what we're going to do is basically fill the entire window black so at the moment what we've got if we just launch our game we've got um well we've just got this blank window and as you can see it's it's literally just an window there's nothing filling it there's no graphical anything in here like Graphics are not being used this is just this is just the standard jframe um you know layout I guess um so what we're going to do do today is actually initialize and you know start rendering a black um just black pixels really to the uh to the screen so we'll do that um uh doing a few things first of all and I will discuss everything here um what we need to do is we've created this buffer strategy and what we need to do now is this buffer strategy is sweet for handling buffers but what we need to do now is actually apply data to the buffers right because at the moment we've got an empty buffer we've got we've got triple buffering on but they're not accepting data yet so we need to actually start um getting them to accept data or essentially um I guess a different way to put this would be to actually um rather apply the buffer to a graphics object so all we need to do is type Graphics G now we'll call it J because well first of all that's a convention but second of all it's just you know J is going to stand for graphics no point typing Graphics here Graphics J is going to equal BS so our buffer strategy do get draw Graphics okay so what does get draw Graphics do get draw Graphics um essentially what it does is it is it creates a l link between Graphics to which you can write data unless just import it by the way um it's basically creating a link between Graphics so in other words it's creating a link between you being able to actually draw Graphics to the screen and the actual buffer strategy okay so it's linking the two together it's um I guess it's like it's it's creating a graphics context you know for for the drawing buffer um so that's set right what we also need to do is now that we've in between here and here before we type g. dispose I'll explain this in a minute this is where and you could even like do a little um uh thing for yourself so that you know I'm not going to do that though um but in between here and here is where you do all of your graphics so any graphics that need to be displayed on the screen must be displayed here before you call J do dispose so why are we calling J do dispose and what does it do dispose does exactly what you think it's going to do it just it just disposes of the current Graphics now what it does is it essentially releases all your system resources so as you can imagine render is in a loop so what's happening is every time you're setting Graphics equal to the buffer strategy what you're doing is every time you're actually creating Graphics but at the end of the frame you need to remove those Graphics otherwise as you can imagine the graphics from each frame aren't going to be removed that's a problem because that will just crash your game eventually after after we render every frame we want to um we want to remove uh the graphics that we're not using anymore so we do that by disposing of the graphics and finally what we need to do and this is very important because not only will your game not work if you do this it will crash you need to actually show the buffer now last last time yesterday I explained that what happens is I'm not going to bring up paint again I don't have time but yesterday I explained that what happens is with buffers you have your screen and then you have buffers that actually render and contain images now we need to swap the buffers essentially it's called buffer swapping um or blitting you could call blitting as well um basically what what it's going to do is it's going to swap the buffers and you know remove that that image that we had last time and I'm sorry I've got rss's on um oh you guys probably w even say that cuz I'm not I'm not recording in full screen well I am recording in full screen but I'll be zooming in so um yeah um essentially what you'll be doing is uh swapping out the buffers so the buffer that needs to be rendered right it's already like it's already computed but it's not being displayed what we need to do is actually you know make I guess this next available buffer object actually visible we need we need to display it if we don't right it's going to still take up memory and it's just game's not going to go anywhere and that's a huge problem so we actually need to be able to release the memory and move because remember a buffer a buffer is a place for temporary storage we can't keep file we we can't keep um objects and graphics and pixel data there forever we need it's it's only temporary it it'll literally be there for probably like honestly maybe a few Nan seconds a few um a few milliseconds um again I'm using nanc really Loosely here cuz nanc is obviously a very very um tiny uh you know section of time um but I'll just say milliseconds um it might be less than 1 millisecond that's what I say there but anyway point is for a period of time very very short period of time we we're actually calculating which pixels need to go away and then we're displaying them straight away now we need to we need to actually clear the memory and display that those pixels to the screen and we do that by actually showing the buffer strategy essentially so we called BS s buffer strategy. show show and what this will do again is it will make the next available buffer visible it will show the buffer that's been calculated now to make the screen black remember I said that we do all of our Graphics here to make the screen black all we need to do is first of all it's going to work by filling a rectangle so if we call G so graphics. fill wct it will actually fill a rectangle um with a specified color and again we Define the size of that rectangle so we Define the starting x and y coordinate which is 0 0 0 x and 0 Y and then the width and height of the actual rectangle so because we want to fill it full screen we want the width to essentially be the same width as our width of our window which is actually width time scale so you could type width time scale here I'm simply going to put get width now what get width is is it's a it's an integer um method it returns an INT it returns an integer all it does is it's part of the canvas class and possibly even the component class actually I'm not 100% sure on that it's yeah it's part of the component class um essentially what it does is it just Returns the size of our window so get width is probably the best way to ensure you actually fill the entire width and again for height we'll just do get height so in other words what this is doing is is filling a rectangle starting from the point 00 which is the top left corner right not the bottom left corner the top left corner uh in Java if we get to openg which we will eventually maybe not in this series though cuz I I did promise keeping it library free and making it from scratch but maybe in like a future series or something because you guys have Begg have been begging me for like a year to do open jail um in openg and stuff like that is actually the bottom left corner that's 0 but in Java and other computer science stuff it's the top left so it's filling a rectangle from the top left coordinate of 0 0 and the size of that rectangle is width time is width by height it's going to fill the whole screen now it is filling a rectangle but we don't know what color it is yet so what we can do is actually just before that remember you do this before you fill the rectangle we want to actually set the color so we simply call G do set color and again we'll just go color. black and uh there we go it's going to set the color of it's just going to set the graphical color to be black and then whatever follows this will have that color applied to it so fil rectangle will have black color applied to it and uh that should be it so remember our game is being started we're hitting we're hitting um up run equal true where okay actually this is a question I want to address right now um you guys are wondering how is the run method called we don't actually call the run method anywhere why where is it called and what happens is because game implements run rable right what happens and it extends canvas what happens is because it implements runnable and we've said new thread to be this when we start the thread it's automatically going to run the run method is that clear cuz that sounds pretty clear to me um so yeah that's what's going to happen and again we're in a loop so we'll be rendering and that's how this method gets called so yeah let's just hit debug and we'll see what we get all right so I can hear my graphics cards squealing right now cuz it's probably rendering this at 100,000 frames per second uh which is a good sign but what's happening is you can see that we've got a black uh screen right now and the thing is like this isn't just we we haven't just changed the color of the jframe background we have we haven't just changed the color of the background to be to be black um we're actually fullon using the graphics card to actually render an array of black pixels onto the screen we're actually using the graphics card here to fill to actually render an image to the screen so effectively that's pretty much the first step so um try it out for yourself you guys can have fun changing the colors to whatever you want if you want a specific color um you don't have to go color dot cuz obviously there's like a limited amount of colors you can choose from here what you could actually do here is Select something like new color and then in here you just enter the RGB values so something like I don't know 80 red 40 uh 40 green and like maybe like uh 100 blue blue for example that would give us oops did that twice that will give us this nice purple color for example all right I'm just going to leave it as black though um cuz that's how I roll and um yeah next time we'll probably take a look at actual individual pixel manipulation and stuff like that and um again I'm going to try and keep these episodes short I'm sorry uh I'll try and keep them below 10 minutes definitely so yeah I hope you enjoyed this episode of game programming if you did please hit that like button lets me know that you guys are actually appreciating the series and uh I'll see you guys later [Music] bye

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 Subreddit: http://www.reddit.com/r/thecherno/ Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/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 · 47 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
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
55 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 game programming concepts, including graphics initialization and rendering, using Java and various tools. It covers buffer management, memory management, and graphics programming, providing a comprehensive understanding of game development.

Key Takeaways
  1. Create a Graphics object and link it to the buffer strategy
  2. Dispose of the Graphics object after rendering each frame
  3. Show the buffer to display the rendered graphics
  4. Swap buffers to display new images
  5. Release memory to prevent game crashes
  6. Fill a rectangle to make the screen black
  7. Use get width and get height to determine window size
  8. Set the color before filling the rectangle
  9. Fill the rectangle with the specified color
💡 Buffer swapping, also known as blitting, is crucial for displaying new images in game development, and memory management is essential to prevent game crashes.

Related Reads

📰
I Tried an AI Side Hustle for 90 Days. Here Is the Unfiltered Truth
Learn the unfiltered truth about starting an AI side hustle, including challenges and potential outcomes
Medium · AI
📰
Your first business opportunity in 3 commands: /register_directory in @biznode_bot, wait for matches, then /my_pulse to view...
Learn how to leverage BizNode's AI-powered business opportunity finder in 3 simple commands to work smarter, not harder
Dev.to AI
📰
Finland’s CurifyLabs raises $14M to 3D-print personalised medicine in the US
CurifyLabs raises $14M to expand 3D-printed personalized medicine in the US, leveraging technology to customize medication
The Next Web AI
📰
Thought Machine crosses $100M revenue and delays its London IPO to 2028
Thought Machine reaches $100M revenue and delays London IPO to 2028, focusing on growth over valuation
The Next Web AI
Up next
Watch this before applying for jobs as a developer.
Tech With Tim
Watch →