PDA

Click to See Complete Forum and Search --> : Colouring polygons - CreateSolidBrush


Just
Sep 30th, 2000, 08:38 AM
Hi all,

I'm currently writing a program for my university Graphics unit, and I would very much appreciate some help (it's due in just a few days!) In this program, I use the API function CreateSolidBrush to choose a colour before calling the API procedure Polygon to draw a polygon to the screen.

The problem, however, is that continually calling CreateSolidBrush causes my computer to run out of memory. Every single polygon in my scene is a different colour (due to lighting) and so I need to create a different brush for every single polygon.

After I draw the current polygon, I call the API procedure DeleteObject to delete the brush I just used. I definitely call DeleteObject as many times as I call CreateSolidBrush. Despite this, my computer very, very quickly runs out memory (causing Windows to do all sorts of weird things, such as not even draw its icons correctly, to come up with system errors, etc).

Does anyone have any idea why I have this memory leak? If no, does anyone know of a way to modify a brush to change its colour without having to create a new brush? Here's a few lines of code from my program, in case it helps (Brush is of type Long, I and Count are Integers):

For I = 1 To Count ' Count is the number of polygons
Brush = CreateSolidBrush(Polygons(I).Colour) ' Create a "brush"
SelectObject frmGraphics.hdc, Brush ' Select that brush
Polygon frmGraphics.hdc, Polygons(I).Screen(1), UBound(Polygons(I).Screen) ' Draw the polygon
DeleteObject Brush ' Delete the brush
Next I


Thanks a-plenty,

Justin

Just
Sep 30th, 2000, 10:27 AM
Hi all,

Never mind, I managed to fix the problem. I still don't know why my previous code didn't work, but I got around it by using the API procedure SetPolyFillMode and by setting the FillColor property of the form (I wasn't doing this before only because I couldn't get SetPolyFillMode to work!).

Now if only I knew how to get my program to run fast... it absolutely crawls at the moment. Any suggestions on how to speed up the drawing and filling of polygons? I read quite some time ago on here (or maybe it was a different site, can't remember) that using the API function SendMessage to clear a form in between each frame, rather than using Form.Refresh, is 3 to 4 times faster. I just tried it out, and found it to not be any different whatsoever. Any other suggestions?

Thanks,

Justin

parksie
Sep 30th, 2000, 10:47 AM
Your problem is that you haven't selected the old object back into the DC...use code like this:

Dim hOldObj as Long
Dim hNewObj as Long

hNewObj = (some kind of creation)

hOldObj = SelectObject(hDC, hNewObj)

... use DC ...

SelectObject hDC, hOldObj
DeleteObject hNewObj