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 :)











