Results 1 to 3 of 3

Thread: Colouring polygons - CreateSolidBrush

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 1999
    Location
    Perth, Australia
    Posts
    5
    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

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 1999
    Location
    Perth, Australia
    Posts
    5

    Fixed it!

    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

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Your problem is that you haven't selected the old object back into the DC...use code like this:
    Code:
    Dim hOldObj as Long
    Dim hNewObj as Long
    
    hNewObj = (some kind of creation)
    
    hOldObj = SelectObject(hDC, hNewObj)
    
    ... use DC ...
    
    SelectObject hDC, hOldObj
    DeleteObject hNewObj
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width