954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XOR pixels , clear screen

Hey guys,
I'm making a small game on c++ using code::blocks.I use winbgi graphics for doing it. Its basically a ball game like dx-ball. I am an amateur programmer and I did upto making a moving ball which will rebounce whenever the boundaries of the screen are encountered.My method is to clear viewport each time the ball is printed so that the motion of the ball will be achieved . But I have been told that it is an inefficient method and there is another way by XORing the pixels to clear only parts of the screen instead of clearviewport()

Please enlighten me on it
here is my code

#include<winbgim.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;

int main()
{
    int x,y,i,j;
    i=1;
    j=1;
    x=11;
    y=11;

   initwindow(1366,768);
     setbkcolor(BLACK);
     setcolor(RED);
               setfillstyle(SOLID_FILL,RED);
                rectangle(500,730,700,740);
   while(!kbhit())
    {

            if(x==getmaxx()-10)
            {

               i=-1;

            }
            if(x==10)
            {

                i=1;

            }
            if(y==10)
            {

                j=1;
            }
            if(y==getmaxy()-30)
            {

                j=-1;
            }
              x=x+i;
              y=y+j;

          fillellipse (x,y,10,10);
          delay(5);
          clearviewport();


    }


   getch();
   closegraph();
   return 0;
}
jeevsmyd
Junior Poster
142 posts since Oct 2008
Reputation Points: 8
Solved Threads: 0
 

It is not very complicated. You have to remember the previous position and size of the ball. Before you update the screen you have to clear only that part of your viewport. In your case I would guess that you can paint a black rectangle at (x-10, y-10) with size 20x20 instead of clearviewport().

It gets a little bit more complicated if you draw stuff on top of each other. If your ball is drawn on top of some other object like an image you have to replace the black clear-rectangle with a portion of the image that occupies that area.

Xoring pixels is usually much slower than drawing a black rectangle. But that depends on the graphics library, and I am not familiar with this library and have not been able to find its source files. This could easily be measured with a simple test application.

rxlim
Junior Poster in Training
51 posts since Feb 2011
Reputation Points: 12
Solved Threads: 8
 

The thing is I want to learn how to XOR pixels . Please tell me how to do that

jeevsmyd
Junior Poster
142 posts since Oct 2008
Reputation Points: 8
Solved Threads: 0
 

I have not used this library but it looks like it only supports RGB colors. So if you want to xor pixels without an alpha component I guess you would bitwise-xor the source color with the destination pixel color (the ^ operator) and write it back to the destination pixel using getpixel and setpixel. A normal xor blend operation with only opaque colors would clear pixels where the source pixel and the destination pixel both contain a color (opaque) and the source color would only be drawn to destination pixels without a color (transparent pixels). I am not sure how this is defined for RGB colors. However it is always slower to do operations on pixels rather than just clearing them even when using hardware acceleration. So it is not the preferred way to clear pixels.

rxlim
Junior Poster in Training
51 posts since Feb 2011
Reputation Points: 12
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: