Constructor Problem Snek Game

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
LittleDude
Posts: 7
Joined: February 23rd, 2018, 7:03 pm

Constructor Problem Snek Game

Post by LittleDude » February 24th, 2018, 10:02 am

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?
Attachments
Constructor.rar
(1.54 MiB) Downloaded 94 times
Last edited by LittleDude on February 24th, 2018, 11:23 am, edited 1 time in total.

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

Re: Constructor Problem Snek Game

Post by albinopapa » February 24th, 2018, 10:57 am

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.
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

LittleDude
Posts: 7
Joined: February 23rd, 2018, 7:03 pm

Re: Constructor Problem Snek Game

Post by LittleDude » February 24th, 2018, 11:17 am

I guess i'll have to see master chilli's homework solution then. That ofc after i make my own version. Thanks! :mrgreen:

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Constructor Problem Snek Game

Post by chili » February 24th, 2018, 5:33 pm

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 ;)
Chili

LittleDude
Posts: 7
Joined: February 23rd, 2018, 7:03 pm

Re: Constructor Problem Snek Game

Post by LittleDude » February 24th, 2018, 5:48 pm

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.

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

Re: Constructor Problem Snek Game

Post by albinopapa » February 24th, 2018, 6:08 pm

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.
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

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Constructor Problem Snek Game

Post by chili » February 25th, 2018, 1:34 am

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.
Chili

LittleDude
Posts: 7
Joined: February 23rd, 2018, 7:03 pm

Re: Constructor Problem Snek Game

Post by LittleDude » February 25th, 2018, 11:33 am

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"

Post Reply