Solved

Urgent:Using a BMP picture to show the Joystick movement!

Posted on 1998-05-14
8
356 Views
Last Modified: 2013-11-20
Hi! When I change the X axis and Y axis position of Joystick, I would like to use a BMP picture from Resoruce editor (LoadBitmap(IDB_XXX) ) to show the Joystick movement. Right now I do understand how to calculate the relative position on the Client area of Windows , so you do not teach me how to do it. My big problem is that I do not know how to erase the BMP picture at the old position and generate this BMP picture at the new position  after Joystick moved. If you could write a piece of code for doing this, and explain it a little, I would be very grateful.
Thank a lot!.
0
Comment
Question by:jennyH
  • 4
  • 4
8 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 50 total points
ID: 1310569
In general this problem is not trivial. Ideally if you bitmap picture is ralatevly small, as small as cursur icon the best way is to use cursor. If it is larger what you need to do is save screen area where you want to display your bitmap into some temporary bitmap using screen DC. Then draw your bitmap on this place. when you need to move your bitmap you first should draw the bitmap you saved back on screen and then repeat the same steps. The problem is if you bitmap is rather large you will get terrible flickering. In this case DirectDraw is the only one way.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1310570
In your view class do this (pseudo code)

Get the old bitmap rect
Set the new bitmap rect
InvalidateRect for the old bitmap rectangle
InvalidateRect for the new bitmap rectangle
UpdateWindows

This should do it.

PS: Please reject the current proposed answer so I can propose this as an answer if you find it more helpful


0
 
LVL 7

Expert Comment

by:galkin
ID: 1310571
If user moves losticj rather fast bitmaps will not be updated since InvalidateRect doesn't update rectangle immediately. This will reasult in tail of bitmaps. moreover he needs to draw bitmap anywhere and InvalidateRect will also result in marking invalidte rectangle for many windows so it will further slow down drawing.
you must directly update bitmaps by obtaining device context of the display. Using GDI BitBlt function is good solution if the bitmap is not large otherwise I told only DD can help
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1310572
Where does he say he needs to "draw bitmap anywhere" ??

I assume he is just drawing it in his view (he talks about client coordinates).  So there will not be 'many windows' to invalidate at all.

It is true that InvalidateRect does not update the view immediatle - it just marks it as dirty.  But the UpdateWindow call will do the repainting of the old and new rectangles.  I don't see how you would end up with this trail of bitmaps you mention.

If you want to avoid flicker and get a smoother display, then an offscreen bitmap for rendering would be the way to go.

Another technique is to render the bitmap with an XOR, so then you just paint the bitmap in the old position then in the new position, and the bitmap will move.  Of course, the colours may look a bit screwy as they are xor'd with the background, but that may be suitable for his application.

0
 
LVL 7

Expert Comment

by:galkin
ID: 1310573
Yes, i agree it is not clear that he wants to move bitmap only inside his window client area. But my cooments are still valid. Using your method will cause terrible flickering or rectangle will not be redrwn at all if user moves jostick rather fast.
What I suggest is double buffering and this is the same method used by Windows when you move one window over another. It saves invalide rectangle or region and then blits it back.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1310574
The 'save-under' double buffering you recommend is not really good.  It will resotre the OLD backgroudn (just before the bitmap was drawn at the rect) .. if the background has since changed (eg. you are moving the joystick slowly) then you'll end up with 'rubbish' being repainted .. not the current image.

If you are drawing in your client area only (and not already using directx) then use the invalidate/updatewindow method, and use an offscreen bitmap for rendering (the famouse CMemDC class should help with that .. check it out at http://www.codeguru.com

BTW: I think that your 'double-buffering' (ie save-under) method is NOT used (usually) when moving one window over another in Windows .. instead Windows invalidates and send paint messages.

0
 
LVL 7

Expert Comment

by:galkin
ID: 1310575
I guess while user is moving joystick any windows is not updated. This is at least truth between two speps of moving joystick. I know from my experience that InvalidateRect works extremely slow since it doesn't update rectangle immediately but sets WM_PAINT message in message queue. Taking into account that WM_PAINT message is not processed if there are ohter messages in message queue ans several WM_PAINT messages are combined into single one fast moving of joystick will result in not updating rectangle.
Concerning your comment about invalidating rectangle when moving one window over another you are right. Windows doesn't update invalide rectangle with InvalidateRect when a menu is tracked. It uses the mechanism I mentioned.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1310576
Yes, InvalidateRect marks a rect of the window as requireing update.  These would normally be accumulated as you state.

However, UpdateWindow causes an IMMEDITATE repaint.  So there is not delay.

According to the docs it...

>>>>>>>>>>
Updates the client area by sending a WM_PAINT message if the update region is not empty. The UpdateWindow member function sends a WM_PAINT message directly, bypassing the application queue. If the update region is empty, WM_PAINT is not sent.
>>>>>>>>>>

Note that it BYPASSES the message queue .. so the usually delayed WM_PAINT handling (when there are no other message etc) is bypassed as well.


I think we now need to here from jennyH so we know what she(?) is wanting to do so we can get a solution for her particular requirements.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question