Page 1 of 1

[SOLVED]Help with SDL/C++ Space Invaders Clone

Posted: Mon Jun 08, 2009 9:11 pm
by hurstshifter
Hey Everyone,

First time poster here. Please no hazing :worship:


I'm a user over at gamedev.net as well, and while that is admittedly an excellent resource, I haven't been able to get any bites on help with this problem. Here is the context of my last post of this problem on gamedev.net. To see the whole thread check here http://www.gamedev.net/community/forums ... _id=537113. Thanks in advance for anyone who can shed some light on this for me.


------------------------------
Alrighty, ran into another problem. Now that I can draw my array of surfaces I want to test removing individual blocks from the screen. To do this I basically construct an equally sized array of integers, and set all values to 1.

Code: Select all

blocks::blocks()
{
	for(int blockY = 0; blockY < 5; blockY++)
	{
		for(int blockX = 0; blockX < 8; blockX++)
		{
			blocksAlive[blockY][blockX] = 1;
		}
	}

	for(int blockY = 0; blockY < 5; blockY++)
	{
		for(int blockX = 0; blockX < 8; blockX++)
		{
			blocksArray[blockY][blockX] = block;
		}
	}
}



Once this is done, during the draw function I loop through this array and check to see if any values == 0. If they do, don't draw that block. But for some reason this is only working with elements in the [0][x] section of the array. Any other value of Y and it fails.

Code: Select all

void blocks::drawblocks()
{
	for(int drawY = 0; drawY < 5; drawY++)
	{
		for(int drawX = 0; drawX < 8; drawX++)
		{
			if(blocksAlive[drawY][drawX] == 0)
			{
				continue;
			}
		switch(drawY)
		{
		case 0:
		apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), 40, blocksArray[drawY][drawX], screen, NULL);

		case 1:
		apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), 80, blocksArray[drawY][drawX], screen, NULL);

		case 2:
		apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), 120, blocksArray[drawY][drawX], screen, NULL);

		case 3:
		apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), 160, blocksArray[drawY][drawX], screen, NULL);

		case 4:
		apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), 200, blocksArray[drawY][drawX], screen, NULL);
		}

		}
	}
}

So basically, I can only affect the top row of blocks with this method. I must be missing something minor here as the top row works exactly as intended.

-----------------------------------------------

Re: Help with SDL/C++ Space Invaders Clone

Posted: Mon Jun 08, 2009 9:44 pm
by Joeyotrevor
Not sure if this is your problem, but in your switch statement you never break, which means everything after the true case will also be executed, meaning you are drawing multiple times for the same block but in different places.

Code: Select all

 switch(drawY)
      {
      case 0:
      apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), 40, blocksArray[drawY][drawX], screen, NULL);
      break;//should be here
etc..

But you don't need a switch statement there anyways, since all of them seem to follow the formula 'y = drawY*40 + 40'.

So you can replace your entire switch statement with this:

Code: Select all

      apply_surface(((SCREEN_WIDTH / 8) - (BLOCK_WIDTH / 2)) * (drawX + 1), drawY*40 + 40, blocksArray[drawY][drawX], screen, NULL);

Re: Help with SDL/C++ Space Invaders Clone

Posted: Mon Jun 08, 2009 9:48 pm
by hurstshifter
Sometimes all you need is an extra set of eyes :shock2:

That was it! I knew I must have overlooked something stupid. Also good idea for omitting the switch statement. I'll definitely implement it that way.

Re: Help with SDL/C++ Space Invaders Clone

Posted: Mon Jun 08, 2009 9:49 pm
by Joeyotrevor
Thanks, glad I could help. :)

Re: Help with SDL/C++ Space Invaders Clone

Posted: Tue Jun 09, 2009 7:59 pm
by MarauderIIC
So, solved? Can you edit your topic to say that? Sort of an unwritten...request -- but since I read every last post on the forum, saves me time (skim vs read) :D

Re: Help with SDL/C++ Space Invaders Clone

Posted: Tue Jun 09, 2009 8:31 pm
by hurstshifter
MarauderIIC wrote:So, solved? Can you edit your topic to say that? Sort of an unwritten...request -- but since I read every last post on the forum, saves me time (skim vs read) :D

You got it