gfx.PutPixel()

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Justcus
Posts: 2
Joined: October 15th, 2018, 5:55 pm

gfx.PutPixel()

Post by Justcus » October 15th, 2018, 6:28 pm

I was trying to make each pixel in the viewport its own color. but for some reason, it fills the whole screen with the same color I know the problem lies in my nested for loops. could someone tell me what I am doing wrong? I've been trying to do this for like 10 hours I have come across some very interesting results from sinewaves to a diagonal line. I also know the colors will be repeated because of the pseudo-randomness of the rand function.

Code: Select all

void Game::ComposeFrame()
{
	int  width = gfx.ScreenWidth - 1, height = gfx.ScreenHeight - 1, x, y;
	int rcolor = rand() % 256;
	int gcolor = rand() % 256;
	int bcolor = rand() % 256;
	for (x = 0; x <= width; x++) {
		for (y = 0; y <= height; y++) {
			gfx.PutPixel(x, y, rcolor, gcolor, bcolor);
		}
	}
}
I am fairly new to C++ but have done some coding in Vb and Java.

Thanks in advance

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: gfx.PutPixel()

Post by albinopapa » October 15th, 2018, 7:33 pm

The issue you are having is because you are getting random values before the loops, and then using those same values in each iteration of the loops, try this:

Code: Select all

void Game::ComposeFrame()
{
   const int  width = gfx.ScreenWidth, height = gfx.ScreenHeight;
   for( int y = 0; y < height; ++y ) {
      for( int x = 0; x < width; ++x ) {
      }
         const int rcolor = rand() % 256;
         const int gcolor = rand() % 256;
         const int bcolor = rand() % 256;
         gfx.PutPixel(x, y, rcolor, gcolor, bcolor);
      }
   }
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Justcus
Posts: 2
Joined: October 15th, 2018, 5:55 pm

Re: gfx.PutPixel()

Post by Justcus » October 16th, 2018, 5:01 pm

albinopapa wrote:The issue you are having is because you are getting random values before the loops, and then using those same values in each iteration of the loops, try this:

Code: Select all

void Game::ComposeFrame()
{
   const int  width = gfx.ScreenWidth, height = gfx.ScreenHeight;
   for( int y = 0; y < height; ++y ) {
      for( int x = 0; x < width; ++x ) {
      }
         const int rcolor = rand() % 256;
         const int gcolor = rand() % 256;
         const int bcolor = rand() % 256;
         gfx.PutPixel(x, y, rcolor, gcolor, bcolor);
      }
   }
}
Thanks for putting me in the right direction!

When I use what you showed me above I get an undeclared x,y variables it won't compile. for some unknown reason, you have them declared in the for statements.
Image
then when I do an int x,y; above your const int I get this:
Image for some reason when I put any gfx.PutPixel() in the location where you have the gfx.PutPixel() and the rand() it crashes the program. but when I move it up into the second for it seems to work.

Code: Select all

void Game::ComposeFrame()
{
	
	const int  width = gfx.ScreenWidth, height = gfx.ScreenHeight;
	for (int y = 0; y < height; ++y) {
		for (int x = 0; x < width; ++x) {
			const int rcolor = rand() % 256;
			const int gcolor = rand() % 256;
			const int bcolor = rand() % 256;
			gfx.PutPixel(x, y, rcolor, gcolor, bcolor);
		}

	}
}
I think I get the result I was looking for it sort of looks like Television Snow
Image
I believe that is because of the game loop running the function over and over.

I just noticed you had ++y and ++x is that the same as x++ and y++? if not what is the difference?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: gfx.PutPixel()

Post by albinopapa » October 16th, 2018, 6:09 pm

Hahaha, wow, I messed that up, good job figuring out my own mistake and what I was trying to get across to you.

++variable_name is pre-increment
variable_name++ is post-increment

Difference between the two:

Code: Select all

int increment( int value )
{  
     value += 1;
     return value;
};

int i = 0;
// Preincrement
i = increment( i );
// ... use i ( now i is 1 )

// Post increment
int j = i;
i = increment( i );
// ... use j ( j == 1 and i == 2 )
Here is equivalent code using an increment function in a while loop to simulate pre-increment

Code: Select all

int i = 0;
Enemy enemies[10];
while( i < 10 )
{
     enemies[ i ].SetPositionX( 200 );
     enemies[ i ].SetPositionY( 100 );

     // value of i = 0
     i = increment( i );
}
Here is the same loop, but simulating post increment

Code: Select all

int i = 0;
Enemy enemies[10];
while( i < 10 )
{
     enemies[ i ].SetPositionX( 200 );
     enemies[ i ].SetPositionY( 100 );

     int j = i;
     i = increment( i );
}
Hopefully, you can see that post-increment has an added step. Even though the result is the same in the loop ( because it's the last instruction ), the preincrement operator is slightly ( very slightly ) more efficient. While it doesn't play a huge role here with integers, there are some uses for each.

Let's say you wanted to get the value of something at index 'i' and needed to increment 'i' afterward.

Code: Select all

int pos_x = enemy[ i ].GetPositionX();
i += 1;
You could just increment i in the [ ] brackets using the post increment version

Code: Select all

int pos_x = enemy[ i++ ].GetPositionX();
Here's a use for pre-increment that I've used before. I didn't want to count each element,
so I just increment i as I go.

Code: Select all

int i = -1;
Enemy enemies[] = {
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 0 ( x = 400 + 0, y = 300 + 0 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 1 ( x = 400 + 10, y = 300 + 5 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 2 ( x = 400 + 20, y = 300 + 10 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 3 ( x = 400 + 30, y = 300 + 15 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 4 ( x = 400 + 40, y = 300 + 20 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 5 ( x = 400 + 50, y = 300 + 25 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 6 ( x = 400 + 60, y = 300 + 30 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 7 ( x = 400 + 70, y = 300 + 35 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) },  // i = 8 ( x = 400 + 80, y = 300 + 40 )
     Position{ 400 + ( 10 * ++i ), 300 + ( 5 * i ) }   // i = 9 ( x = 400 + 90, y = 300 + 45 )
};
// value of i after setting enemy[9] position is 10
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply