Move a bitmap around a window quickly in C++ [modified]
-
I'm looking for some C++ code to let me quickly move a bitmap around a window, restoring the background as it moves. At present I capture the Window contents to a bitmap during the app initialization and in the OnPaint() I draw the this bitmap and then I draw my overlayed bitmap. I am double buffering the paint. The overlayed bitmap position moves with the mouse which invalidates the Window. This works fine except it is too slow when the background window is large (think Windows Desktop) and the PC is slow. My guess is that redrawing the large background bitmap on every mouse move is the bottleneck. There has to be a much better and faster way to do this, but my searching hasn't found the answer I need.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
modified on Sunday, October 25, 2009 5:25 PM
-
I'm looking for some C++ code to let me quickly move a bitmap around a window, restoring the background as it moves. At present I capture the Window contents to a bitmap during the app initialization and in the OnPaint() I draw the this bitmap and then I draw my overlayed bitmap. I am double buffering the paint. The overlayed bitmap position moves with the mouse which invalidates the Window. This works fine except it is too slow when the background window is large (think Windows Desktop) and the PC is slow. My guess is that redrawing the large background bitmap on every mouse move is the bottleneck. There has to be a much better and faster way to do this, but my searching hasn't found the answer I need.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
modified on Sunday, October 25, 2009 5:25 PM
Try writing a handler for WM_ERASEBKGND[^] and return non-zero.
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Try writing a handler for WM_ERASEBKGND[^] and return non-zero.
«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)«_Superman_» wrote:
Try writing a handler for WM_ERASEBKGND[^] and return non-zero.
Thanks, but I'm already doing that.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
I'm looking for some C++ code to let me quickly move a bitmap around a window, restoring the background as it moves. At present I capture the Window contents to a bitmap during the app initialization and in the OnPaint() I draw the this bitmap and then I draw my overlayed bitmap. I am double buffering the paint. The overlayed bitmap position moves with the mouse which invalidates the Window. This works fine except it is too slow when the background window is large (think Windows Desktop) and the PC is slow. My guess is that redrawing the large background bitmap on every mouse move is the bottleneck. There has to be a much better and faster way to do this, but my searching hasn't found the answer I need.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
modified on Sunday, October 25, 2009 5:25 PM
Try to redraw only parts on your window that really change instead of redrawing the whole thing.
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <
-
I'm looking for some C++ code to let me quickly move a bitmap around a window, restoring the background as it moves. At present I capture the Window contents to a bitmap during the app initialization and in the OnPaint() I draw the this bitmap and then I draw my overlayed bitmap. I am double buffering the paint. The overlayed bitmap position moves with the mouse which invalidates the Window. This works fine except it is too slow when the background window is large (think Windows Desktop) and the PC is slow. My guess is that redrawing the large background bitmap on every mouse move is the bottleneck. There has to be a much better and faster way to do this, but my searching hasn't found the answer I need.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
modified on Sunday, October 25, 2009 5:25 PM
if the background is static, you really only need to draw the overlay at its new position and then draw the parts of the background that were uncovered when the overlay moved. there's no need to redraw the whole background. 1. the overlay's previous rect = R1. 2. the overlay's new rect = R2. 3. R3 = the bounding rect of R1 and R2. 4. alloc a bitmap of size R3 5. fill it with the appropriate chunk of background 6. draw the overlay into the temp bitmap 7. draw the temp bitmap to the screen as long as the overlay is moving only a few pixels at a time, R3 should be just slightly bigger than the overlay size. if R1 and R2 don't instersect, you can split it into two operations: restore the background to one rect, then draw the overlay into the other - don't bother with the temp bitmap.
-
if the background is static, you really only need to draw the overlay at its new position and then draw the parts of the background that were uncovered when the overlay moved. there's no need to redraw the whole background. 1. the overlay's previous rect = R1. 2. the overlay's new rect = R2. 3. R3 = the bounding rect of R1 and R2. 4. alloc a bitmap of size R3 5. fill it with the appropriate chunk of background 6. draw the overlay into the temp bitmap 7. draw the temp bitmap to the screen as long as the overlay is moving only a few pixels at a time, R3 should be just slightly bigger than the overlay size. if R1 and R2 don't instersect, you can split it into two operations: restore the background to one rect, then draw the overlay into the other - don't bother with the temp bitmap.
Thanks Chris, sounds like a good plan.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
I'm looking for some C++ code to let me quickly move a bitmap around a window, restoring the background as it moves. At present I capture the Window contents to a bitmap during the app initialization and in the OnPaint() I draw the this bitmap and then I draw my overlayed bitmap. I am double buffering the paint. The overlayed bitmap position moves with the mouse which invalidates the Window. This works fine except it is too slow when the background window is large (think Windows Desktop) and the PC is slow. My guess is that redrawing the large background bitmap on every mouse move is the bottleneck. There has to be a much better and faster way to do this, but my searching hasn't found the answer I need.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
modified on Sunday, October 25, 2009 5:25 PM
-
I am already using a memory buffer as I said at the start: "I am double buffering the paint."
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
I'm looking for some C++ code to let me quickly move a bitmap around a window, restoring the background as it moves. At present I capture the Window contents to a bitmap during the app initialization and in the OnPaint() I draw the this bitmap and then I draw my overlayed bitmap. I am double buffering the paint. The overlayed bitmap position moves with the mouse which invalidates the Window. This works fine except it is too slow when the background window is large (think Windows Desktop) and the PC is slow. My guess is that redrawing the large background bitmap on every mouse move is the bottleneck. There has to be a much better and faster way to do this, but my searching hasn't found the answer I need.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
modified on Sunday, October 25, 2009 5:25 PM
Would you get better performance by using your current methods but using (say) DirextX or OpenGL to do the blitting? They might have better performing drivers that make better use of hardware?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Would you get better performance by using your current methods but using (say) DirextX or OpenGL to do the blitting? They might have better performing drivers that make better use of hardware?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thanks Stuart. I'd considered that but didn't want to add the overhead and complication of using these libraries. As of late yesterday I now have an implementation that works well enough on my slow old Vista Notebook and very well on my Desktop PC. Basically doing what Chris suggested.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com