Board index » cppbuilder » Polygon clipping region problem.

Polygon clipping region problem.

Hello all,

I have following problem with Borland C++ Builder 4.

I'm drawing hex shaped images on screen using SelectClipRgn and the region
works well for about few thousand times, but after that clipregions stop
working and little later the program crashes.

So what should I do, does it need somehow to release every region or
something?

here is sample code, which is repeated multiple times:

void Game::DrawHexMap()
{
    HRGN hRgn;
    TPoint points[6];
    TRect NewRect;
    TRect dst,scr;

    // Clearing the background
    Form1->Image1->Canvas->Brush->Color = clMenu;
    NewRect = Rect(0,0,630,489);
    Form1->Image1->Canvas->FillRect(NewRect);

    int xspot=0,yspot=0;

    // draws 34x35 -> 612x630 pixels size image
    for (int x=0;x<34;x++)
    {
        xspot=0;
        yspot=0;
        for(int y=0;y<35;y++)
        {
            // position change of hexes
            if (((y+ChangeY)/2)*2!=y+ChangeY) // ChangeY and ChangeX are
private variables
            {
                xspot=x*18+9;
                yspot=yspot+13;
             }
            else
            {
                xspot=x*18;
                yspot=yspot+13;
            }

            // setting the drawing region (is there better way to do this,
like in BCB 1)
            points[0] = TPoint(xspot+9,yspot);
            points[1] = TPoint(xspot+18,yspot+5);
            points[2] = TPoint(xspot+18,spot+13);
            points[3] = TPoint(xspot+9,yspot+18);
            points[4] = TPoint(xspot,yspot+13);
            points[5] = TPoint(xspot,yspot+5);

            hRgn =CreatePolygonRgn(points,6,ALTERNATE);
            SelectClipRgn(Form1->Image1->Canvas->Handle,hRgn);

            assist=;
            // setting drawing areas
            scr = Rect(0,0,18,18);
            dst = Rect(xspot,yspot,xspot+18,yspot+18);

            // Draws images and get area data From MapMatrix variable
            if
(MapMatrix[x+ChangeX][y+ChangeY]==1)Form1->Image1->Canvas->CopyRect(dst,Form
1->SeaImage->Canvas,scr);
            else
Form1->Image1->Canvas->CopyRect(dst,Form1->GroundImage->Canvas,scr);
            // GroundImage and SeaImage are 18x18 size images

        }
    }

Quote
};

--
.......................................................
Aki Suuronen
aki.suuro...@pp.phnet.fi
www.phnet.fi/public/aki.suuronen
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''
 

Re:Polygon clipping region problem.


Well, i remember getting paid a generous amout one time for helping some one
that had the same problem
you are doing....
  you know what, i am willing to give you the answer for free....
---
  when creating regions you get a handle to the region , then you use it ect....

 that part is ok....
 how ever....
  you are forgetting to delete the region after it's use..., there for the next
time you create region you
grab another handle from windows and so on...
  after a while you will run out of system resources....
  try using  DeleteObject(hRgn) before recreating another or existing the block
of code..

Quote
Aki Suuronen wrote:
> Hello all,

> I have following problem with Borland C++ Builder 4.

> I'm drawing hex shaped images on screen using SelectClipRgn and the region
> works well for about few thousand times, but after that clipregions stop
> working and little later the program crashes.

> So what should I do, does it need somehow to release every region or
> something?

> here is sample code, which is repeated multiple times:

> void Game::DrawHexMap()
> {
>     HRGN hRgn;
>     TPoint points[6];
>     TRect NewRect;
>     TRect dst,scr;

>     // Clearing the background
>     Form1->Image1->Canvas->Brush->Color = clMenu;
>     NewRect = Rect(0,0,630,489);
>     Form1->Image1->Canvas->FillRect(NewRect);

>     int xspot=0,yspot=0;

>     // draws 34x35 -> 612x630 pixels size image
>     for (int x=0;x<34;x++)
>     {
>         xspot=0;
>         yspot=0;
>         for(int y=0;y<35;y++)
>         {
>             // position change of hexes
>             if (((y+ChangeY)/2)*2!=y+ChangeY) // ChangeY and ChangeX are
> private variables
>             {
>                 xspot=x*18+9;
>                 yspot=yspot+13;
>              }
>             else
>             {
>                 xspot=x*18;
>                 yspot=yspot+13;
>             }

>             // setting the drawing region (is there better way to do this,
> like in BCB 1)
>             points[0] = TPoint(xspot+9,yspot);
>             points[1] = TPoint(xspot+18,yspot+5);
>             points[2] = TPoint(xspot+18,spot+13);
>             points[3] = TPoint(xspot+9,yspot+18);
>             points[4] = TPoint(xspot,yspot+13);
>             points[5] = TPoint(xspot,yspot+5);

>             hRgn =CreatePolygonRgn(points,6,ALTERNATE);
>             SelectClipRgn(Form1->Image1->Canvas->Handle,hRgn);

>             assist=;
>             // setting drawing areas
>             scr = Rect(0,0,18,18);
>             dst = Rect(xspot,yspot,xspot+18,yspot+18);

>             // Draws images and get area data From MapMatrix variable
>             if
> (MapMatrix[x+ChangeX][y+ChangeY]==1)Form1->Image1->Canvas->CopyRect(dst,Form
> 1->SeaImage->Canvas,scr);
>             else
> Form1->Image1->Canvas->CopyRect(dst,Form1->GroundImage->Canvas,scr);
>             // GroundImage and SeaImage are 18x18 size images

>         }
>     }
> };

> --
> .......................................................
> Aki Suuronen
> aki.suuro...@pp.phnet.fi
> www.phnet.fi/public/aki.suuronen
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> '''''

Re:Polygon clipping region problem.


Hi Aki,

Quote
> I'm drawing hex shaped images on screen using SelectClipRgn and the region
> works well for about few thousand times, but after that clipregions stop
> working and little later the program crashes.
> So what should I do, does it need somehow to release every region or
> something?

After you're done with the device context, remove its regiond binding by using
the SelectClipRgn() function specifying a NULL hrgn parameter.  Also, for every
region that you create, you should destroy it using the DeleteObject() function.

Good luck.

--------------------------------------
Damon Chandler

http://bcbcaq.freeservers.com
Answers to <Commonly Asked Questions>

Re:Polygon clipping region problem.


Thank you all for help!
--
.......................................................
Aki Suuronen
aki.suuro...@pp.phnet.fi
www.phnet.fi/public/aki.suuronen
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''

Other Threads