Beginner Tutorial 21 gfx

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
hannes321123
Posts: 3
Joined: August 11th, 2019, 8:37 pm

Beginner Tutorial 21 gfx

Post by hannes321123 » December 1st, 2019, 11:40 pm

Hello,

I have a short question:
In Tutorial 21 we avoided passing gfx as an argument to the Draw-function and we did not have to call the DrawCell-function an a gfx-object.
Is this all because we save gfx as a reference and is the "syntax-behaviour" now completely similar to the behaviour of other member-variables?
He mentioned it to be the cause I think, but I wonder if there are any limits or is there a reason why we are not always passing in a gfx-object this way instead of including "Graphics.h" etc.

Greetings from Germany

User avatar
krautersuppe
Posts: 91
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

Re: Beginner Tutorial 21 gfx

Post by krautersuppe » December 2nd, 2019, 9:16 am

Does this answer your question?
DSU
Discord: dsu1, GitHub: https://github.com/DSpUz

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Beginner Tutorial 21 gfx

Post by albinopapa » December 2nd, 2019, 6:06 pm

Is this all because we save gfx as a reference
Yes.
is the "syntax-behaviour" now completely similar to the behaviour of other member-variables?
Not sure what you mean, but I think the answer is yes.
but I wonder if there are any limits
Like what? The Graphics class is only used for access to PutPixel and other general purpose draw functions or at least that's all it should be used for.
or is there a reason why we are not always passing in a gfx-object this way instead of including "Graphics.h" etc.
Simply including Graphics.h doesn't give you access to members such as PutPixel() or BeginFrame/EndFrame, you must actually pass a non-const reference to another object to have access to it's member functions.

There could be a couple of reasons Chili decided to make this design decision. Since the board, goal and obstacles are all drawn in cells and not in pixels, the board is like a wrapper or adapter for Graphics::PutPixel. If you were to use Graphics instead, you'd have to pass the board reference and a graphics reference to the Goal and Snake draw functions and inside those, you be just forwarding gfx to brd.DrawCell.

So it's cleaner and simpler to just have the Board class hold a reference since Board is just an adapter for drawing bigger pixels ( cells ). This way, Goal and Snake don't even need to know about Graphics, they only need to know about Board to draw, and Board can draw whatever it wants, wherever it wants.

In this way, you can have the board be smaller or larger, the cells can be smaller or larger and you wouldn't have to do the math to change from the cell locations to pixel locations in each class that needs to be drawn, you let the board handle the math conversions and forward that information ( pixel position and color ) to the Graphics::PutPixel function.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply