Jump to content


Help in C++ programming tutorial.


9 replies to this topic

#1 Shackhal

    New Member

  • Members
  • Pip
  • 9 posts

Posted 08 March 2011 - 07:45 AM

Hello, everybody.

Right now i'm learning C++ from the tutorial "Introduction to C++ with Game Development" in this website and i have to say that is really useful. But, in Part 08: About Memory Addresses and Pointers, i'm having problems with the code in the 2nd assignment:

Here is the code that i have made for now (a white pixel that have to move from the top to the bottom of a image while evades obstacles, is the objetive of the code):

Sprite mySprite( new Surface( "assets/pointers.tga" ), 1 );//the loaded image.

Sprite mySprite2( new Surface( "assets/pixel.tga" ), 1 );//the white pixel.


int x = 320;

int y = 0;


void Game::Tick( float a_DT )

{

	Pixel* address = m_Screen->GetBuffer();


	mySprite.Draw( 0, 0, m_Screen );//Change it to "m_Screen->Clear( 0 );" and it will work fine.

	mySprite2.Draw( x, y, m_Screen );

	

	for (int o = 128290; o < 128350; o++ ) address[o] = 0xff0000;//Make a red line in the center, if i use the "m_Screen->Clear( 0 );" method.


	int i = (y + 1) * 640 + x;

		

	if ( i > 307199 )//restart the loop from the bennining and avoids a crash. 

	{

		y = 0;

		x = 320;

	}

	if ( i - 640 == y * 640 + x )//detect the position of the pixel.

	{

		if ( address[i] == 0x000000 ) y++;//It have to detect the color of the position, but that part doesn't work with the loaded image, i don't know why...	

		else

		{

			if ( y % 2 == 0 ) x++;//Detect from what side it will go the pixel to evade obstacles. Left if it's an odd number and right if it's an even number... 

			else x--;

		}

	}

}

If i change the "mySprite.Draw( 0, 0, m_Screen );" for "m_Screen->Clear( 0 );" the code runs like it have to be, but the assigmnent tell me exactly that i need to load a 640x480 image (a black image with a horizontal red line in the center, in my example). And the "if ( address == 0x000000 )" part isn't detecting the color for the image.

Even i have try to remade the image in Photoshop, Corel Photo-Paint and even in Paint, but without success.

Now i don't know what can i do and i hoping for your help.

Thanks.

P.D.: The "for (int o = 128290; o < 128350; o++ ) address[o] = 0xff0000;" is to make a red line if i use the [i]"m_Screen->Clear( 0 );"
method :)
I won't be called a novice...in the future...

#2 Mattias Gustavsson

    Senior Member

  • Members
  • PipPipPipPip
  • 413 posts

Posted 08 March 2011 - 08:03 AM

Shackhal said:

Now i don't know what can i do and i hoping for your help.
Set breakpoints, step through the code line by line and inspect values in the debugger.
  • www.mattiasgustavsson.com - My blog and current projects
  • www.rivtind.com - My Fantasy world and isometric RPG engine
  • www.pixieuniversity.com - My Software 2D Game Engine

#3 Shackhal

    New Member

  • Members
  • Pip
  • 9 posts

Posted 08 March 2011 - 08:15 AM

Of course i used breakpoints with the debugger, but i don't understand why cannot the address (the pointer) detects the colors of the loaded image...that the core of the problem...
I won't be called a novice...in the future...

#4 Mattias Gustavsson

    Senior Member

  • Members
  • PipPipPipPip
  • 413 posts

Posted 08 March 2011 - 07:54 PM

So what value is the debugger saying that the colours have in the loaded image, and what value is it giving for the same colours of the non-loaded variety?
  • www.mattiasgustavsson.com - My blog and current projects
  • www.rivtind.com - My Fantasy world and isometric RPG engine
  • www.pixieuniversity.com - My Software 2D Game Engine

#5 Shackhal

    New Member

  • Members
  • Pip
  • 9 posts

Posted 08 March 2011 - 08:39 PM

In the loaded image, the black color, with address[960] (is the coords 320, 0), give me 4278190080. The red line (address[153280], coords 320, 239) give me 4294901760.

In the non-loaded image (a black screen) the black color, with the same address[960], give me 0 and the red line (the address[o], in this case using the same address[153280]) give me 16711680.

I don't understand why is giving me different values...

For curiosity, i'm using Visual Studio C++ 2010 Express in Windows Vista Home Premium 64-Bits and the template in the tutorial presents in Part 02...just in this case i'm using a own asset made for me for the assignment. The personal asset is a black screen (#000000) with a red line in the center (#ff0000).
I won't be called a novice...in the future...

#6 Shackhal

    New Member

  • Members
  • Pip
  • 9 posts

Posted 08 March 2011 - 11:10 PM

Well, in this case i can change from:

if ( address[i] == 0 ) y++;//or if ( address[i] == 0x000000 ) y++;

to:

if ( address[i] == 4278190080 ) y++;

to make it work, but even so...is really strange that a 0x00000 color (black) isn't detected like expected...

Did you know why, Mattias??
I won't be called a novice...in the future...

#7 Reedbeta

    DevMaster Staff

  • Administrators
  • 4967 posts
  • LocationBellevue, WA

Posted 09 March 2011 - 12:34 AM

4278190080 is 0xff000000. It's black, with 255 in the alpha channel. It's unfortunate that the article asks you to do this assignment when colors aren't covered yet until lesson 9, as this has repeatedly caused confusion...anyway, the correct way to check for black is to AND the color with 0xffffff, and check that the result is zero.
reedbeta.com - developer blog, OpenGL demos, and other projects

#8 Shackhal

    New Member

  • Members
  • Pip
  • 9 posts

Posted 09 March 2011 - 07:36 AM

Hmmm. Very interesting....i remember to see something like that in the forum about the tutorial. It's says that nullifies the alpha channel and only read RGB.

So i remade the code to one more clean, organized and (of course) with the correction:

Sprite mySprite( new Surface( "assets/pointers.tga" ), 1 );//the loaded image.

Sprite mySprite2( new Surface( "assets/pixel.tga" ), 1 );//the white pixel.


int x = 320;

int y = 479;


void Game::Tick( float a_DT )

{

	Pixel* address = m_Screen->GetBuffer();


	mySprite.Draw( 0, 0, m_Screen );

	mySprite2.Draw( x, y, m_Screen );

	

	int i = y * 640 + x;

		

	if ( i > 306559 )//restart the loop from the bennining and avoids a crash. 

	{

		x = 320;

		y = 0;

	}

	else

	{

		if ( (address[i + 640] & 0xffffff) == 0 ) y++;

		else

		{

			//Detect from what side it will go the pixel to evade obstacles. 

                        //Left if it's an odd number and right if it's an even number... 

			if ( y % 2 == 0 ) x++;

			else x--;

		}

	}

}

It's works!! But i'm curious. How the "((address[i + 640] & 0xffffff) == 0)" nullify the alpha channel??
I won't be called a novice...in the future...

#9 Reedbeta

    DevMaster Staff

  • Administrators
  • 4967 posts
  • LocationBellevue, WA

Posted 09 March 2011 - 05:27 PM

0xffffff is a number that has all ones in the lower three bytes, and zeros in the upper byte. ANDing with it means that the lower three bytes (which are the RGB channels) are preserved, while the upper byte (the alpha channel) is replaced by zero, whatever its previous value was. So you see, it's just setting the alpha channel to a known value (zero), so you can compare the color channels without worrying about the alpha. Anyway, all this masking stuff is covered in more detail in Lesson 9.
reedbeta.com - developer blog, OpenGL demos, and other projects

#10 Shackhal

    New Member

  • Members
  • Pip
  • 9 posts

Posted 09 March 2011 - 06:54 PM

Ok, i get it. Thanks for the advice.
I won't be called a novice...in the future...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users