On 8/13/12 10:34 AM, Chris Weisiger wrote:
> On Mon, Aug 13, 2012 at 10:24 AM, Robin Dunn <
ro...@alldunn.com> wrote:
>> On 8/13/12 9:09 AM, Chris Weisiger wrote:
>>>
>>> On Mon, Aug 13, 2012 at 9:01 AM, Eric Jensen <
m...@j-dev.de> wrote:
>>
>>
>>>>
>>>> The actual crash is probably cause by this line:
>>>> self.bitmap.SetSize((width, height))
>>>>
>>>> If this directly related to the underlying C++ method, this will *not*
>>>> actually change the size of the bitmap (quite frankly, i don't know
>>>> what's the purpose of this method at all ;) ).
>>>>
>>>> If the size of the bitmap is different from the size of the DC, you
>>>> have to recreate the bitmap.
>>>
>>>
>>> Tsk. I assumed that SetSize would reallocate memory as needed.
>>>
>>> Recreating the Bitmap every time the window changes appears to avoid
>>> the error I originally reported, anyway. So I guess that's the
>>> "workaround" for this problem. Thanks for the help.
>>
>>
>> The next step would probably be to only recreate the bitmap when the size of
>> the window changes. Then that bitmap can be used as your buffer instead of
>> creating a new one in each paint event, and your paint event handler can
>> then be reduced to a single line.
>
> Exactly. Oh, the paint handler won't be quite a single line, since I
> still need to update the portions of the bitmap that have been dirtied
> by changes in game state (and I might as well do this in the paint
> handler), but the important thing is that I won't be performing
> thousands of DrawRectangle() and DrawText() calls every refresh.
I don't know about your case, but in some cases at least it makes sense
to update the buffer bitmap at the point where the program state
changes, such as when a bitmap representing a game object moves from
position A to position B, and then do a RefreshRect to get a paint event
scheduled for the dirtied area. Then the paint event handler can be just:
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self, self.buffer)
because no additional drawing needs to be done. The buffered dc object
is created, and then immediately is deleted because the end of the
function is reached, and at that time the buffered dc will blit the
buffer bitmap to the paint dc.
>
> It would be nice, by the way, if trying to increase a bitmap's area
> with SetSize would throw an exception. Or if that function were
> removed altogether. Or at the very least if there were a warning in
> the documentation.
$ pydoc wx.Bitmap.SetSize
Help on method SetSize in wx.Bitmap:
wx.Bitmap.SetSize = SetSize(*args, **kwargs) unbound wx._gdi.Bitmap method
SetSize(self, Size size)
Set the bitmap size (does not affect the existing bitmap data).