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;
}

Recommended Answers

All 3 Replies

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.