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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

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

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

-----------------------------------------------
Last edited by hurstshifter on Tue Jun 09, 2009 8:37 pm, edited 2 times in total.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

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

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

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

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

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

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

Post by Joeyotrevor »

Thanks, glad I could help. :)

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

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

Post 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
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Post Reply