can someone please explain how this code works

Started by
5 comments, last by apocalyptic cow 8 years, 8 months ago

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

Advertisement


//I didn't know you could define objects like this, can someone please explain this
Graphics g = bs.getDrawGraphics();

That's just a reference to an object.

You got the 'bs' object, call 'getDrawGraphics()' on it. That method returns a reference to an instance of Graphics, so you assign it to 'Graphics g'.


//this I don't understand
BufferStrategy bs = getBufferStrategy();

Exactly the same as before. getBufferStrategy() is called on 'this' object (its just implicit, you don't type 'this.getBufferStrategy()' but that's what its doing), returns a BufferStrategy, and assings it to 'BufferStrategy bs'.

Now in that case it seems that 'getBufferStrategy()' might return null, that is, no reference to any object. In that case it seems it creates a buffer strategy then just returns, ending the function. Probably the next time that 'render()' method is called, getBufferStrategy() will return a valid BufferStrategy instance so it won't be null.


private BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Thats just a constructor.

You got an object field, image, and you assign it a new BufferedImage instance.


int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

That one is a bit more involved. It does, 'image' getRaster(), that returns the raster object, from that raster object it does 'getDataBuffer()', and to that data buffer it casts it to a DataBufferInt reference, and from that casted data buffer it calls 'getData()'. Follow it slowly.

I think you should review a few basic concepts. Constructors, object references, method calls, static method calls, object fields, etc. You can find plenty of official Java tutorials in Oracle's site.

For example: https://docs.oracle.com/javase/tutorial/reflect/member/ctor.html (and look at the sidebar, plenty of the things you asked here are explained there).

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator



        //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()"

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

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.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Thanks everyone for your help, that is clearer now. Wyrframe, if thats the case, what books/tutorials would you recommend to learn java game programming.

thank you

I will shamelessly recommend my book. ph34r.png

"Fundamental 2D Game programming with Java"

http://amzn.com/1305076532

You can also checkout the source code to see if it looks like something that can help.

https://github.com/TimothyWrightSoftware/Fundamental-2D-Game-Programming-With-Java

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This book does look good, however you are the author. So if someone else can give their views on it I would be greatful.

This topic is closed to new replies.

Advertisement