Ep. 6: Loading Textures - Let's Make Flappy Bird

The Cherno · Beginner ·🧒 Coding for Kids ·11y ago

Key Takeaways

The video demonstrates how to load textures in a game like Flappy Bird using modern OpenGL and lwjgl, covering texture creation, buffer allocation, and anti-aliasing.

Full Transcript

hey what's up guys My Name Is AO and welcome to episode 6 of let's make Flappy Bird today we'll be talking about loading textures into our game as well as buffers since we want to be able to use Textures in our game we need a way to go from an image file to something that can be displayed on our screen the workflow is pretty simple firstly we need to load our image file then we'll grab the pixels from that image and finally use them to create an open gel texture there are a few things to pay attention to in this process and we'll address those things as we write them let's make a class called texture which will represent a seure that we load I'll put this into a package called Graphics since it refers to the graphical aspects of our game we'll need to define a few fields to hold important information such as the width and height of the texture and the ID that opengl will assign to the texture when we create it this ID will be used to activate our texture when rendering I call this field texture but you can call it ID if it makes more sense to you let's make a Constructor that text in a string this will be the file path of our texture we'll leave it blank for now we'll make a private method that also takes an string again being the file path this will be a rather large method that will essentially do everything from loading the file to creating the open gel texture it'll return an integer being the open G ID for the texture which we need to assign to our field firstly we'll use a buffer image to load our image file using image io. read with the file input stream this will throw some exceptions so let's around that line with a try and catch we can get rid of the file not found exception since IO exception includes that again handle them properly if you wish we're going to be keeping it simple if the image file is loaded successfully we'll have all the DOA we need in our buffet image object so let's set width equal to image. getet width and then do the same for height we'll create an integer array called pixels that will contain all of the pixels in our image with each integer in the array being the color of the pixel and the index of that integer referring to where the pixel is located in our image inside the tri block we'll set the size of our pixels array to be the size of our image as soon as we find that out now let's call image. getet RGB with the appropriate parameters including our pixels array arrays in Java are passed by reference which is why get RGB doesn't need to return anything it will simply set our array through the parameter what we have now is an array of pixels which makes up our texture before we can give it to open though we need to change the format of it to comply with what opengl wants basically we just need to move some byes around each color in our pixels array is represented as a RGB Alpha red green blue that's the order of our color channels because that's how buffer image loaded in our pixels if we look at an example color in hexadecimal format what this essentially means is that the leftmost two digits represent the alpha Channel then the red green and blue Channel whilst we can tell open gel to use a variety of formats none of them suit this one the easiest and most practical to adjust to is rgba or abgr if we read from left to right so as you can see we need to move around the ordering of channels luckily this isn't too difficult to do if you know what you're doing first let's make a new integer array called data which will store this new format of pixels obviously it will be the same size as our pixels array now we need a for Loop that will iterate through each of the pixels in our array since each color needs to get reformatted for each of these colors we need to extract each Channel which can be done by masking it using the bitwise and operator and then shifting it the appropriate number of bits to get it to the rightmost bits the start for example let's take a look at the alpha channel it's the leftmost channel so we need to end it by FF 0000000000 which will preserve just the required bits and finally move it to the right since each of these hex digits represents four bits we need to move it a total of 24 bits to the right the leading zeros have no effect just as they wouldn't with a decimal number so we're left with just the alpha Channel nice once we've extracted all the channels let's put them back together in the correct order to comply with our new format the alpha channel will go back to where it was no change then we'll use the bitwise or operator to combine the other channels next Blue will'll go to where red used to be green will stay where it was and red will take Blue's old place now we've got our new array data populated with pixels that are in the right format for opengl we Ed a lot of bitwise operators and bit level calculations here so make sure you research and understand them I might make a dedicated video in the future explaining them in depth since they're quite important in graphics programming the last step is to use openg GL to create this texture from our pixels firstly we'll statically import gl11 now let's generate a new texture and assign its ID to text for now to activate this texture for use we need to bind it using G bind texture it's a 2d texture hence the texture 2D flag we'll pass in text since that's the ID of the texture we want to select now that it's selected we can modify and use it think of this like selecting a layer in Photoshop or any other similar program we can't modify it if it's not selected before we feed our pixels into this texture let's set two important parameters basically what this will do is disable anti-aliasing for our textures we'll be dealing with some lowish resolution Graphics particularly for the bird and we don't want those textures to be blurred if our game runs at a high resolution this will keep our textures nice and sharp as I mentioned two episodes ago lwjgl doesn't want arays it wants buffers we'll need a way to convert arrays into buffers let's make a new utility class for this we'll need this quite frequently we'll pop it into our util package and call it buffer utils lwjgl actually has a very similar class but our one is going to be slightly different this will be a static class so once again let's add a private Constructor in this series we'll need to use bite buffers float buffers and of course int buffers for the pixels we just created let's create a static method which takes in a bite array and returns a bite buffer don't forget to hit control shift o to import things as we'll be doing this throughout the series we'll create a bite buffer result and allocate a new direct buffer with the same size as our array we'll set the order to be native order which is what lwjgl wants next we'll put the array into the buffer and flip the buffer flip basically flips the buffer and prepares it for reading again something lwl wants that's all we need to do to go from array to lwjgl compliant buffer let's duplicate this method twice and adjust it for use with float and int buffers the only real difference here apart from changing the types and appending the as float or int buffer is the size of the allocated buffer since we're still technically allocating bite arrays to begin with the size needs to be in byes one bite in Java takes up one bite however floats and integers both take out four bytes which is 32 bits because of this we need to multiply the size of the array by four or shift left two places which is mathematically identical to multiplying by four and that's our buffer utils class back in our texture class let's use this new functionality we'll call GL text image 2D setting our format to be rgba the width and height to the values the type of data to be GL unsigned by and finally fitting in our data array by calling buffer u. create in buffer data you might accidentally be using the lwjgl version of buffer utils so scroll up to the top and make sure that you're not you need to use the one you just created note that we use GL unsigned bite as the type colors are essentially unsigned bytes meaning they range from 0 to 255 inclusive Java unfortunately doesn't have unsigned types and Java's signed bite is rather useless to us since it ranges from -28 to POS 127 inclusive because it can't store anything above 127 and we need to we have to use another data type such as int short would have also worked but I chose to use ins finally we'll unbind the texture since we're done with it and can now deselect it so that we don't accidentally use it in the future we'll return our text integer which holds our ID and we are done our textures are now ready to use an openg I'm going to go ahead and add gets for the three fields we have since we'll need to access them in the future particularly the ID I'm also going to go back up to the Constructor and assign the texture field to our load method with the path as the parameter and that's it our texture class is done we can now easily use it to load images into our game and use them as open gel textures fantastic thanks for watching and I hope you enjoyed the video as always a like is greatly appreciated next episode we'll be rendering an object onto the screen goodbye

Original Description

Support this series directly: http://www.patreon.com/thecherno Private Tutoring: http://thecherno.com/tutoring Let's Make Flappy Bird is a series on how to make a game like the popular mobile game "Flappy Bird", from scratch. We'll be making it for Windows, Mac and Linux, using modern OpenGL, using Java and LWJGL. This series isn't designed to teach you Java or OpenGL, but rather teach you how to USE those two technologies to make a rather simple game, using modern techniques. ----------------------------------------------------------------------------------------- Website ► http://www.thecherno.com Twitter ► http://www.twitter.com/thecherno Facebook ► http://www.facebook.com/thecherno
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
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
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 how to load textures in a game like Flappy Bird using modern OpenGL and lwjgl, covering key concepts like texture creation and buffer allocation.

Key Takeaways
  1. Create a new texture and assign its ID to a variable
  2. Bind the texture using glBindTexture
  3. Set parameters to disable anti-aliasing
  4. Convert an array to a buffer using a utility class
  5. Allocate a buffer of the correct size
  6. Use GL_TEXTURE_2D to load the texture
  7. Set the format to RGBA
  8. Set the width and height to the values
  9. Set the type of data to GL_UNSIGNED_BYTE
💡 The buffer size needs to be in bytes, and using GL_UNSIGNED_BYTE as the type for colors is crucial for correct texture loading.

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →