Page 1 of 1

Constructor Problem Snek Game

Posted: February 24th, 2018, 10:02 am
by LittleDude
Hello lads and... lasses ;) .

Saw tutorial 14 and challenged myself to make the snek game.
I started with making the board (made from 20x20 cells and borders). The cells seem to work but the borders... kind of. Here's the problem: The borders are drawn if DrawBoardBorder() (inside board.h) is called with the help of the object (eg. brd.DrawBoardBorder() inside Game.cpp). But the borders are not drawn if i let the constructor call the function. Why is the constructor such a twat?

Re: Constructor Problem Snek Game

Posted: February 24th, 2018, 10:57 am
by albinopapa
Here goes nothing:

Code: Select all

void Game::Go()
{
	gfx.BeginFrame();	
	UpdateModel();
	ComposeFrame();
	gfx.EndFrame();
}
This function is the heart of the game.
The first function call is gfx.BeginFrame(). It's job is to clear the back buffer to get it ready to draw the next frame of the game.
The second function call is UpdateModel(). It's job is to update the game pieces, check for collisions and so on...the game logic.
The third function call is ComposeFrame(). It's job is to take the current state of the game and draw everything to the back buffer that was cleared in gfx.BeginFrame().
The fourth function call is gfx.EndFrame(). It's job is to transfer all the drawn pixels to the graphics card to be displayed.

The constructor of Board is called before the gfx.BeginFrame() and so is erased ever before it is drawn.

Re: Constructor Problem Snek Game

Posted: February 24th, 2018, 11:17 am
by LittleDude
I guess i'll have to see master chilli's homework solution then. That ofc after i make my own version. Thanks! :mrgreen:

Re: Constructor Problem Snek Game

Posted: February 24th, 2018, 5:33 pm
by chili
Sounds like you're missing some key concepts about how constructors work and what they are for. They should never be concerned with drawing shit m8 ;)

Re: Constructor Problem Snek Game

Posted: February 24th, 2018, 5:48 pm
by LittleDude
I have quite a lot to learn from mistakes to be made, just found out they should mainly be used to initialize variables. :D This may be a dumb question but why shouldn't a constructor be used for drawing?
I assume because of performance issues.

Re: Constructor Problem Snek Game

Posted: February 24th, 2018, 6:08 pm
by albinopapa
No, as you said, it's for initializing variables that are held within a class or struct. When you are drawing to the screen, it's usually going to be way after initialization.

Re: Constructor Problem Snek Game

Posted: February 25th, 2018, 1:34 am
by chili
A ctor is a fire-once routine. Usually, you want to be drawing your game entities more than once (like 60 times a second, for instance).

Also, just like you and papa said, it is for initialization, and drawing is not that.

Re: Constructor Problem Snek Game

Posted: February 25th, 2018, 11:33 am
by LittleDude
Ah, now i understand what you meant when you said "Sounds like you're missing some key concepts about how constructors work and what they are for"