Jump to content
  • Advertisement
Sign in to follow this  
apocalyptic cow

can someone please explain how this code works

This topic is 1148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am going through the cherno project videos, but I don't understand why the code does what it does.

 

render gets called ecer time we loop through, I have omitted the rest of the code as it is all basic game loop/threads.  However this bit i don't understand.  Can you please explain the details of how it works, so i feel like I am not just copying.

        //these definitions confuse me a bit

        private BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();



        //I know why this gets called
        public void render()
	{
		//this I don't understand
		BufferStrategy bs = getBufferStrategy();
		if (bs == null)
		{
			createBufferStrategy(3);
			return;
		}
		
                //Screen is our own class, this just fills and array with colour at the moment 
		Screen.render();
		
                 //this just transferes the arrays accross
		for (int i = 0; i < pixels.length;i++)
		{
			pixels[i] = Screen.pixels[i];
		}
		
		//I didn't know you could define objects like this, can someone please explain this
		Graphics g = bs.getDrawGraphics();

                //these methods are fine
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(image, 0, 0, getWidth(), getHeight(),null);	

                //I am assuming this puts the rendered image to the buffer
		g.dispose();
                //and that this renders to the screen
		bs.show();
		
	}

thank you

Edited by apocalyptic cow

Share this post


Link to post
Share on other sites
Advertisement

        //these definitions confuse me a bit
// #1 
        private BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 
	int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();



        //I know why this gets called
        public void render()
	{
		//this I don't understand
// #2
		BufferStrategy bs = getBufferStrategy();
		if (bs == null)
		{
			createBufferStrategy(3);
			return;
		}
		
                //Screen is our own class, this just fills and array with colour at the moment 
		Screen.render();
		
                 //this just transferes the arrays accross
		for (int i = 0; i < pixels.length;i++)
		{
			pixels[i] = Screen.pixels[i];
		}
		
// #3
		//I didn't know you could define objects like this, can someone please explain this
		Graphics g = bs.getDrawGraphics();

                //these methods are fine
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(image, 0, 0, getWidth(), getHeight(),null);	

                //I am assuming this puts the rendered image to the buffer
		g.dispose();
                //and that this renders to the screen
		bs.show();
		
	}

.

#1 - create a new object "Image" of the type BufferedImage. "pixels" is an integer array

 

#2 "bs" is a reference to the method "getBufferStrategy()" ( which returns an object )

 

#3 "Graphics g" is referencing  "bs" ... "getDrawGraphics()" is a method from the object returned by "getBufferStrategy()"

Edited by Code Fox

Share this post


Link to post
Share on other sites

The member variable `image` is initialized with a BufferedImage of a specific format (XRGB8, a 32-bit integer per pixel with the high 8 bits ignored), and then another one, `pixels`, pointed at the direct memory access of the buffer's internal representation. This `pixels` buffer is where the view is composed, before it is sent to the OS for presentation.

 

The "buffer strategy" is an abstract interface he is using which deals with the details of sending a composed screen buffer to the OS for presentation, whether that's splatting it to an AWT window or sending it to OpenGL or whatever. From what I can tell, he's using it to implement double- or triple-buffering.

 

What do you mean, "didn't know you could define objects like this"? He is simply calling the buffer strategy's method "getDrawGraphics()", which returns an object of type "Graphics". The return value is assigned to a variable called `g`.

 

 

 

On a related note, I really dislike this Cherno guy's "tutorials". He is very bad at explaining the concepts he is working with. He seems happy to aimlessly experiment on-camera until things work, with no clear explanation why what he is doing does or does not work. It's a horrible learning environment; he's just rambling live, with no editing, no guidance, and very poor enunciation. It's like trying to read code while a schizophreniac mutters word-salad in your ear and jiggles your scrollwheel.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!