AssaultCube issue

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Battlefrog
Posts: 69
Joined: March 31st, 2015, 10:10 pm
Location: Florida, United States of America

AssaultCube issue

Post by Battlefrog » February 1st, 2017, 4:29 pm

Hello! For reasons unknown to me I've seem to have created a bug in the drawing of the boxes in the game. It can't be the DrawBox method, since if that didn't work, the Player couldn't be drawn.

Github Link: https://github.com/Battlefrog/AssaultCube

Thanks in advance!
Broc W.

Sole Member of Sledgehog Software, trademark and LLC status pending.

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

Re: AssaultCube issue

Post by albinopapa » February 1st, 2017, 7:16 pm

Not sure what problem you were having, but here's what I see:

Code: Select all

// You have amountOfBlocks = 1, so you are only allowed 1 block
static constexpr int amountOfBlocks = 1;
Block blocks[amountOfBlocks];

// You initialize two blocks
	blocks[0].InitBlock(250, 250, 50, 50);
	blocks[1].InitBlock(145, 145, 12, 12);

	// You go from 0 to i = amountOfBlocks, so 0 and 1 but you only allocated 1
	for (int i = 0; i <= amountOfBlocks; ++i)
	{
		if (blocks[i].IsNotCollidingWithPlayer(player))
		{
			player.UpdateInput(wnd.kbd);
			player.IsOutsideBoundries();
		}
		else
		{
			isGameOver = true;
			// TODO: Do something when the game is over
		}
	}	
	
	// Same goes for this loop
	for (int i = 0; i <= amountOfBlocks; ++i)
	{
		blocks[i].DrawBlock(gfx);
	}

In C++ and other languages, the first element in an array is 0 as you show:
blocks[0].InitBlock(250, 250, 50, 50);
blocks[1].InitBlock(145, 145, 12, 12);
So if you want to create two blocks you need to allocate for two blocks
static constexpr int amountOfBlocks = 2;
Block blocks[amountOfBlocks];

Since you start from 0, the number of loops needs to reflect that, so iteration 1 would be the 0 index and iteration 2 would be the 1 index, and since those are the only two, you need to stop at the 1 index.

Code: Select all

	for (int i = 0; i < amountOfBlocks; ++i)
	{
		blocks[i].DrawBlock(gfx);
	}
The error I was getting was 'Stack corrupted around the varaible Game theGame'
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
Battlefrog
Posts: 69
Joined: March 31st, 2015, 10:10 pm
Location: Florida, United States of America

Re: AssaultCube issue

Post by Battlefrog » February 1st, 2017, 10:07 pm

Thank you so much. :)
Broc W.

Sole Member of Sledgehog Software, trademark and LLC status pending.

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

Re: AssaultCube issue

Post by chili » February 2nd, 2017, 1:08 am

FROGMAN! ARE YOU MESSING UP YOUR ARRAYS+LOOPS AGAIN!!?? BAD FROGGY! :lol:

But seriously, you should like write this on your fridge or above the toilet where you will see it multiple times a day:

for( int i = 0; i < count; i++ )

It is super important.

Love,
Chili ;)

P.S.
When you get to T20, I'm gonna show you something sweet that will let you iterate over all the elements in an array without worrying about their indices. Stay tuned!
Chili

User avatar
Battlefrog
Posts: 69
Joined: March 31st, 2015, 10:10 pm
Location: Florida, United States of America

Re: AssaultCube issue

Post by Battlefrog » February 2nd, 2017, 1:50 am

chili wrote: P.S.
When you get to T20, I'm gonna show you something sweet that will let you iterate over all the elements in an array without worrying about their indices. Stay tuned!
Can't wait.
Broc W.

Sole Member of Sledgehog Software, trademark and LLC status pending.

Post Reply