Hi all, I have a few questions regarding how and when to use wxMemoryDC, wxBitmap, and wxImage.
I'm not looking for any specific recipe, but a general approach to the following set of animation goals (all files are '.bmp' files, and this is on a Windows system; all timers are a wx.Timer):
1. I need to take a .bmp file and scroll it horizontally in a panel, each frame responding to a timer event
2. I need to take a series of .bmp files as sprite frames, and flip through them quickly, each frame to a timer event
3. I need to associate a binary mask with one bitmap, then draw it on top of another bitmap, scrolling the top (masked) bitmap a little bit each frame while keeping the background still. Again, one frame per timer event.
4. I need to take a background image, then draw an alpha image on top, and scroll the background behind the alpha image.
5. I need to blend two alpha images back and forth
6. Maybe other stuff as 2d animations, generally involving mixing of bitmaps, alpha, and transformations, without a whole lot of wx.Pen and wx.Brush usage.
So far what I've done is:
1. Create a wx.Panel subclass with a wx.MemoryDC.
2. For each 1-5 above, create an animation_manager class which owns n+1 wx.Bitmaps and n wx.MemoryDC's, where n is the number of original BMP files required. n wx.Bitmaps are loaded up with the BMP file and associated with the n wx.MemoryDC's in the class, and the third wx.Bitmap is associated (wx.DC.SelectObject()) with the wx.MemoryDC in the panel subclass.
3. Create a wx.Timer to click every 16ms
4. During OnTimer call the animation_manager to generate the next frame and build up the newly rendered image with wx.DC.DrawBitmap() or wx.DC.Blit(), on the scratch-pad bitmap. Now the panel's wxMemoryDC points to the final scratch-pad bitmap.
5. Call Refresh() from OnTimer()
6. In OnPaint() call wx.DC.Blit from the panel's wx.MemoryDC (which points to the scratch pad) to the wx.PaintDC which is created locally.
So this is very confusing and I'm not sure when to use wx.DC .Blit versus wx.DC.DrawBitmap. They both seem to work, except neither seems to do well with the alpha image, and in one case mixing the two around actually seems to crash the program (when used with an alpha-enabled .bmp bitmap). I read that only png supports alpha channels.
Although I am (I think) doing double-buffering by only blitting or drawbitmap'ing a single buffer during the OnPaint event, the animation still looks rough. I have not seen any place where I can force the thing to synchronize to the monitor for smoothest effect. I am capturing and then ignoring the OnErase() event as recommended in various places.
It seems at this point I think maybe I need to use wx.Image and do the alpha myself, but I'm not sure if that's the best approach.
So can a wx guru please advise? When ought I use wx.MemoryDC, wx.Bitmap, and wx.Image?
Thanks a lot
michael