Very simple edit to PutPixel that stops out of bounds errors

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
GraniteHope
Posts: 4
Joined: July 30th, 2012, 7:46 pm

Very simple edit to PutPixel that stops out of bounds errors

Post by GraniteHope » August 20th, 2012, 9:12 am

I'm not sure if this is a behavior that would help, and I'm not so sure that this is going to be useful since we are getting into 3D, but, for those still making games and such, this edition will, instead of giving you an error, just do a simple wraparound. Can't believe I didn't think of it sooner.

void D3DGraphics::PutPixel( int x,int y,int r,int g,int b )
{
x %= 799; // (or your window length - 1)
y %= 599; // (or your window height - 1)
int wrapAround = x + (rect.Pitch >> 2) * y;
((D3DCOLOR*)rect.pBits)[wrapAround] = D3DCOLOR_XRGB( r,g,b );
}

This will wrap any sprites and drawings around the screen instead of that annoying error when you go over the edge.
Last edited by GraniteHope on August 20th, 2012, 9:34 am, edited 1 time in total.

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: Very simple edit to PutPixel that stops out of bounds er

Post by codinitup » August 20th, 2012, 9:18 am

Nice bro, I haven't got a chance to try it but it looks pretty good ;)
MOOOOOO

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Very simple edit to PutPixel that stops out of bounds er

Post by LuX » August 20th, 2012, 12:02 pm

Nice idea, but this will only loop the pixels around the screen, what you want to do is check if the pixel is inside the screen and make that decide if the pixel gets drawn.

Also, I think the mod function is rather slow since it's really an advanced division operation, division being fairly slow.
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Very simple edit to PutPixel that stops out of bounds er

Post by chili » August 20th, 2012, 12:06 pm

On old cpus division / mod was slow but on new ones the cpu can usually process two integer divides per clock even without sse.

P.S. That annoying error is there to let you know when you're going over the edge. Going over the edge means programing: you're doing it completely wrong. ;)
Chili

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: Very simple edit to PutPixel that stops out of bounds er

Post by viruskiller » August 20th, 2012, 7:24 pm

yeah is better to know when u'r doing something wrong,and also to be told what went wrong in some code right where it happened....
but sometimes it does a bit of domino effect and in the end it usually spits a drawing related error when in fact is a pointer related error,or a loop going out of boundary or something completely different.
tbh this error saved me lots of times with my games so far, by telling me something was wrong.

once i had a case where my code worked completely fine in debug mode,but on release mode it acted really strange, i got no warnings,no errors,but it turned to be a loop going out of boundary's.

Area78
Posts: 1
Joined: September 18th, 2012, 2:19 pm

Re: Very simple edit to PutPixel that stops out of bounds er

Post by Area78 » September 18th, 2012, 2:24 pm

I'd go with

void D3DGraphics::PutPixel( int x,int y,int r,int g,int b )
{
if (x >= 0 && y >= 0 && x < 800 && y < 600) {
((D3DCOLOR*)backRect.pBits)[ x + (backRect.Pitch >> 2) * y ] = D3DCOLOR_XRGB( r,g,b );
}
}

Im currently also looking for a way that I get the window size

Post Reply