3 Replies - 4007 Views - Last Post: 05 March 2010 - 04:23 PM Rate Topic: -----

#1 rainbow evil   User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 308
  • Joined: 15-July 09

C++ Understanding Dark Invaders code from the Dark GDK tutorials

Posted 05 March 2010 - 02:56 PM

Hi,

I am going through the tutorial code included with Dark GDK and am confused by some in the "Dark Invaders" space invaders game. Here is the piece of code:
	float U = 0.0f;
	float V = 0.0f;

	int iY = c / 8;
	int iX = c % 8;

	float fOffset = 1.0f / 8.0f;

	U = iX * fOffset;
	V = iY * fOffset;

	// 4 vertices in a sprites (quad), we adjust the U and V ( think x and y) coordinates of the texture to show only the letter we want
	// rather than the whole texture
	dbSetSpriteTextureCoord ( iNextTextSprite , 0 , U , V );
	dbSetSpriteTextureCoord ( iNextTextSprite , 1 , U + fOffset , V );
	dbSetSpriteTextureCoord ( iNextTextSprite , 2 , U , V + fOffset );
	dbSetSpriteTextureCoord ( iNextTextSprite , 3 , U + fOffset , V + fOffset );

What this does is to cut the font sprite to show the required character from the font image. What I want to know is why it produces weird results if don't have all 4 or none of them. I understand why, when you comment all four lines out, it shows the entire sprite but I'm not sure why the sprite is bent somewhere around the middle when entering fewer than 4 but more than 0 co-ordinates.

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Understanding Dark Invaders code from the Dark GDK tutorials

#2 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: C++ Understanding Dark Invaders code from the Dark GDK tutorials

Posted 05 March 2010 - 03:15 PM

Read the comments above those four lines.

There are 4 vertices in a square. So, you are creating a "square" that will be used to get each character. Anything in the square will be drawn; everything else will be ignored. So, if you leave out one line, you are leaving out one vertex, and you are left without a square.
Was This Post Helpful? 1
  • +
  • -

#3 rainbow evil   User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 308
  • Joined: 15-July 09

Re: C++ Understanding Dark Invaders code from the Dark GDK tutorials

Posted 05 March 2010 - 03:45 PM

but surely if it needed all 4 corners of the square it wouldn't be able to draw anything yet it draws the image with a kink which crosses the middle at some point.
Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: C++ Understanding Dark Invaders code from the Dark GDK tutorials

Posted 05 March 2010 - 04:23 PM

It doesn't have to be a square, unless you want it to work right. Comment out just the last line and look at the text. Notice how each character is shaped like a triangle (three vertices = triangle)?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1