i need help in tweaking blitting sourcecode

Started by
10 comments, last by graeme 22 years, 7 months ago
and the source code I need to tweak is here: http://www.codeproject.com/bitmap/gditutorial.asp#xx82862xx Now, I realise I am asking quite a lot by asking you to download the zipped file for tutorial 2 and taking a look at it, but I have been trying to crack this all day! What I am trying to do is modify the code in anticipation of using it as the basis for a program which will animate a series of bitmaps. The program initializes and paints the bitmap to the window within functions called by the message handler, WM_PAINT + WM_CREATE. At the moment I am trying to bypass the WM_PAINT call to the gdi02_OnPaint function, and call the function from within winMain instead, but nothing happens. I realise that this is probably due to the scope of the data structures within the functions. During the switch of a message is all data shared? How would I get around this? I realise that it''s a pain to go download a zip file but I would be really gratefiul! Thanks in advance, Graeme.
Advertisement
quote: At the moment I am trying to bypass the WM_PAINT call to the gdi02_OnPaint function, and call the function from within winMain instead, but nothing happens. I realise that this is probably due to the scope of the data structures within the functions. During the switch of a message is all data shared?
How would I get around this?


It really depends on what the overall program is supposed to do. If you''re seeking to move the blitting into a game loop or something similar, you''re headed in the right direction. If the animation is secondary to the app, then you''re heading in the wrong direction.

You might want to consider using memory dc (off screen buffer) and using the WM_PAINT message to blit that to the primary surface.

You might have an easier go of it using Joseph Farrell''s tuts
http://www.gamedev.net/reference/articles/article1251.asp

Sorry to not be more specifically helpful.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
The article was indeed very helpful. Thanks.

But still I would love to know the answer to the specific problem of what needs to be done to transplant the calling of the functions from WM_CREATE and WM_PAINT tio the winmain loop.
I have tried making different variables global, even transplanting alll of the code from the three blitting functions to winmain but I have had no success.
I''d like to stick with this bit of sourcecode, the URL of which is iin my first post, because it''s the only place that I have found a resource that shows how to blit with masks without resorting to using directX.

It''s driving me crazy!
OK, maybe this will make it easier for people to answer my question, although I always though dumping a load of code and asking for fixes was a bit much (but maybe not as cheeky as asking for someone to download a zip file!!)

hbmImage = LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_BITMAP2));
BITMAP bm;
GetObject(hbmImage,sizeof(bm),&bm);
hbmMask = CreateBitmap(bm.bmWidth,bm.bmHeight,1,1,NULL);

HDC hdcSrc = CreateCompatibleDC(NULL);
HDC hdcDst = CreateCompatibleDC(NULL);

HBITMAP hbmSrcT = SelectBitmap(hdcSrc,hbmImage);
HBITMAP hbmDstT = SelectBitmap(hdcDst,hbmMask);

COLORREF clrTopLeft = GetPixel(hdcSrc,0,0);
COLORREF clrSaveBk = SetBkColor(hdcSrc,clrTopLeft);

// This call sets up the mask bitmap.
BitBlt(hdcDst,0,0,bm.bmWidth,bm.bmHeight,hdcSrc,0,0,SRCCOPY);
HDC hdcMem = CreateCompatibleDC(NULL);

HBITMAP hbmT = SelectBitmap(hdcMem,hbmMask);

//BITMAP bm;
GetObject(hbmMask,sizeof(bm),&bm);

hdc = GetDC(hwnd);

BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hdcMem,myX,myY,SRCAND);


SelectBitmap(hdcMem,hbmImage);
BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hdcMem,myX,myY,SRCPAINT);

// Now, clean up.
SelectBitmap(hdcMem,hbmT);
DeleteDC(hdcMem);
DeleteObject(hbmImage);
ReleaseDC(hwnd, hdc);

I should imagine that''s all pretty straightforwards for someone who is more experienced thatn I. The one thing which may not look obvious at first sight is the COLORREF getting of pixels, this is a device by the author of the code to work out the background transparancy colour.

Oh yes, and hbmMask and hbmImage are bothe declared as globals earlier in the script.
First, I''m not aware of the SelectBitmap api - looks to me like that ought to be SelectObject.

After you get that set of code working, you''ll have to integrate it into the message handling loop of WinMain. You might want to use PeekMessage instead of GetMessage. For example:
  // main message loop	while( TRUE )	{		// test for message in queue, if so get it		if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )		{			if ( msg.message == WM_QUIT )			{				break;			}			// send the message to the window proc			DispatchMessage(&msg);		}		else {                      // insert blitting code                  }         }  


Be careful with this construct as your app will use up all of the CPU time.

You''ll also want to set the hbrBackground member of the WNDCLASS structure to NULL or return 0 from the WM_ERASEBKGND handler so as to prevent any flicker from happening. You don''t need to deal with WM_PAINT at all.

HTH :-)


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Thanks alot for your reply, less bread. I shall try changing the SelectBitmap command, but I think that it is probably ok, because the source code that I took it from compilled and ran just fine. Perhaps I should have mentioned it, but it is allready in the winMain function, after the message queue is checked...
Aha!
I rebuilt the whole thing from scratch and it worked, so whatever was wrong will always be a mystery to me!

I couldn''t find SelectBitmap listed in the exports from gdi32.dll so perhaps SelectBitmap was defined in the sample code (I glanced at it but didn''t pay that close of attention) - or maybe its a W9x specific function, I''m running W2K. Anyway, glad I could help!
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
hi there LessBread,
I found it in MSDN : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmeother/graphcnt_1lys.asp , selectBitmap, that is. It says at the top of the page that it''s supported by Win 98/ME (and it also says Windows DDK, what''s that? developers something Kit I expect.) Does this mean that any executable I write using this function won''t work under other windows platforms, or does it mean that the function can only be compiled by compilers running under these platforms? Complecated stuff, this windows programming...
Greets,

Turns out that SelectBitmap is a macro that calls SelectObject.
  #define	SelectBitmap(dc,bm)	((HBITMAP)SelectObject((dc),(HGDIOBJ)(bm)))  


The C preprocessor will replace your calls to SelectBitmap with a call to SelectObject as defined above before sending the source code to the compiler. This means your prog will run on NT also, granted that the rest of the program doesn''t make W9x specific calls.

To unravel the macro a bit. If you examine your source code after preprocessing, where you wrote

HBITMAP hbmSrcT = SelectBitmap(hdcSrc,hbmImage);

you would find

HBITMAP hbmSrcT = ((HBITMAP)SelectObject((hdcSrc),(HGDIOBJ)(hbmImage)));

Since I never use that macro, I didn''t recognize it.

Windows DDK -> Driver Development Kit: "...specific information needed to write any type of driver for Windows xxx platforms." There are different DDK''s for each of the different versions of Windows as well as for each Service Pack. Lots of low level stuff, mostly C and some assembly language too. Definitely not for the faint of heart. :-)







"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement