Further upgrading put pixel.

The Partridge Family were neither partridges nor a family. Discuss.
cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Further upgrading put pixel.

Post by cameron » December 14th, 2012, 6:13 am

Is there anyway to further optimise put pixell after intermediate lesson 6?
Computer too slow? Consider running a VM on your toaster.

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Further upgrading put pixel.

Post by NaturalDemon » December 14th, 2012, 9:59 pm

yes, there is ... but not performance wise ... it´s bare ...
but ... i added a line .. for pixels that fall outside of the window client rect ...
but i can´t post it right now, i don´t have the laptop ...

Zionich
Posts: 24
Joined: December 11th, 2012, 12:57 am

Re: Further upgrading put pixel.

Post by Zionich » December 14th, 2012, 10:13 pm

I did also. It looked like

if (x >= 0 && x <= screenwidth && y >= 0 && y <= screenheight)
{
PutPixel (.........);
}



This allowed me to pull things off screen into the screen a little at a time.

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Further upgrading put pixel.

Post by Musi » December 14th, 2012, 10:33 pm

Although you should test to see if the object you're drawing is moving off the screen before you use that. Using an if statement on every pixel all the time will slow your program.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Further upgrading put pixel.

Post by cameron » December 14th, 2012, 10:44 pm

I'm mainly looking for a further performance boost.
Computer too slow? Consider running a VM on your toaster.

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Further upgrading put pixel.

Post by NaturalDemon » December 14th, 2012, 10:46 pm

well, tell me how?

any test .. would add further if statements

Code: Select all

if (x >= 0 && x <= screenwidth && y >= 0 && y <= screenheight)
{
PutPixel (.........);
}
mine looks like this

Code: Select all

// screenwidthv & screenheigh are defined in windows.cpp and passed by reference
if ( ( x >= 0 & x <= &screenwidth ) && ( y >= 0 & y <= &screenheigh ) )
{
PutPixel (.........);
}

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Further upgrading put pixel.

Post by Musi » December 14th, 2012, 11:13 pm

For example, if you're drawing a sprite thats 20x20. Test it's coordinates to see if any part of it is outside the screen. Assuming the the coordinates are at the top left of the sprite, something like:

Code: Select all

void DrawSprite( int x, int y )
{
        if( x < 0 || x + 20 > SCREENWIDTH ||
            y < 0 || y + 20 > SCREENHEIGHT )
         {
               DrawSpriteClipped( x,y );
         }
        else
         {
               DrawSprite( x,y );
         }
}
The DrawSpriteClipped function would use the PutPixel with the if statement, the normal DrawSprite function would use the normal PutPixel. This way if the sprite is fully on the screen, you only use one if statement, instead of 400.
Last edited by Musi on December 14th, 2012, 11:21 pm, edited 1 time in total.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Further upgrading put pixel.

Post by NaturalDemon » December 14th, 2012, 11:15 pm

cameron wrote:I'm mainly looking for a further performance boost.
i think i know what you mean.
the loops that use putpixel .... only handle rectangles.

i was thinking about adding dynamic D3DCOLOR buffers to store reuseble "images" and copy them directly

Code: Select all

void D3DGraphics::PutPixel( int x,int y,int r,int g,int b )
{	
	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < SCREENWIDTH );
	assert( y < SCREENHEIGHT );
	pSysBuffer[ x + SCREENWIDTH * y ] = D3DCOLOR_XRGB( r,g,b );
}

void D3DGraphics::PutPixel( int x,int y,D3DCOLOR c )
{	
	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < SCREENWIDTH );
	assert( y < SCREENHEIGHT );
	pSysBuffer[ x + SCREENWIDTH * y ] = c;
}
on the other hand ... this equation

Code: Select all

x + SCREENWIDTH * y 
can be removed from the put pixel method.
x + SCREENWIDTH * y in realty is a fixed value during the session and could be declared somewhere else and would save atleast 2 cpu cycles each time put pixel gets called.


all this ...

Code: Select all

	assert( x >= 0 );
	assert( y >= 0 );
	assert( x < SCREENWIDTH );
	assert( y < SCREENHEIGHT );
can be removed if you use Zionich solution, it's meaningsless after you test if the x,y are inside the cleint rect and would save atleast another 8 cpu cycles.
so in total you can speed up and save some 10 cpu cycles .. but lose about 4 on the test ..
Last edited by NaturalDemon on December 14th, 2012, 11:28 pm, edited 1 time in total.

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Further upgrading put pixel.

Post by NaturalDemon » December 14th, 2012, 11:22 pm

Musi wrote:For example, if you're drawing a sprite thats 20x20. Tests it's coordinates to see if any part of it is outside the screen. Assuming the the coordinates are at the top left of the sprite, something like:

Code: Select all

void DrawSprite( int x, int y )
{
        if( x < 0 || x + 20 > SCREENWIDTH ||
            y < 0 || y + 20 > SCREENHEIGHT )
         {
               DrawSpriteClipped( x,y );
         }
        else
         {
               DrawSprite( x,y );
         }
}
The DrawSpriteClipped function would use the PutPixel with the if statement, the normal DrawSprite function would use the normal PutPixel. This way if the sprite is fully on the screen, you only use one if statement, instead of 400.
you're wrong ... sorry to say
(see image) http://www.planetchili.net/forum/viewto ... e&start=10

Code: Select all

( x < 0 || x + 20 > SCREENWIDTH || y < 0 || y + 20 > SCREENHEIGHT )
is much slower than

Code: Select all

i(x >= 0 && x <= screenwidth && y >= 0 && y <= screenheight)
Last edited by NaturalDemon on December 14th, 2012, 11:43 pm, edited 2 times in total.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Further upgrading put pixel.

Post by cameron » December 14th, 2012, 11:25 pm

A test will only make the game slow down. I want a performance boost to make the game run faster.
Computer too slow? Consider running a VM on your toaster.

Post Reply