So I'm trying to write a relatively basic blackjack programming for learning purposes. It seems like something really odd is happening when WM_PAINT is called. It works fine until I introduce an if statement. Doing a watch the correct values pass, it goes through the whole function, watch it hit BitBlt and nothing draws. Take out the if statement and it draws just fine.
I'm sure the code is probably sloppy as I'm pretty new to this, so any recommendations on that end would be appreciated as well, but mainly just trying to figure out why the BitBlt won't paint for me. Thanks in advance.
Last edited by ovidiucucu; April 19th, 2011 at 04:06 AM.
Reason: Added [CODE] tags
So I'm trying to write a relatively basic blackjack programming for learning purposes. It seems like something really odd is happening when WM_PAINT is called. It works fine until I introduce an if statement. Doing a watch the correct values pass, it goes through the whole function, watch it hit BitBlt and nothing draws. Take out the if statement and it draws just fine.
One thing about your code:
None of your code checks for errors when calling the Windows API functions. This is not a habit you should get into -- all of those functions, including BitBlt return values that denote whether there is an error or not.
What if BitBlt returned a failure? You should always check your API calls for success or failure. I once had to rewrite a mountain of code, because the original authors did not check for API error codes -- they assumed that every function was successful, which wasn't the case.
Secondly, your problem is not just the if() statement. Look at what you're uncovering when that statement is removed. You're unconditionally calling the DrawCard() function every single time without any constraints. So right there, that is a huge difference.
Yeah, the end paint thing I noticed once I posted and saw them right next to each other. Unfortunately cleaning that up didn't seem to fix anything.
Paul:
As far as the error checking, it's slightly over my head at this point. I understand what you mean, but honestly no idea where to begin at this point to work that into the code. Pretty much my first foray into c++ with just enough knowledge in java and vb to be dangerous.
I'm not sure I follow what you mean as far as uncovering. The unconditional call was really just to make sure the code in the drawcard function was working properly, which it seems to. My bafflement is it's lack of working when introducing what seems like a relatively simple condition. More baffling *at least to me* is that it does indeed go through all the code with the condition, just no paint.
Just to test it out, I tried the GetLastError() after the first EndPaint even though it's not supposed to be there. BitBlt is returning 1 and GetLastError returns 0, only furthering my confusion.
You have two different variables for PAINTSTRUCT ps (one global and one local to WM_PAINT), but I doubt that such might be causing an issue.
What is the return value for LoadBitmap on IDB_TABLE? You test for the return value on IBD_BACK but not on IDB_TABLE.
Please also confirm that your most recently posted code behaves the same as in your initial post. In other words, soes the code "work fine" without the "if" statement? What does "work fine" mean as compared to not.
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Look at the section that says "Return Values". That is how you're supposed to use these functions. You call them, and a return value is sent back to you and you query it to make sure it returns success.
So to answer your question, the code should have looked like this:
Code:
if ( BitBlt( whatever ) == 0 )
{
DWORD err = GetLastError( ); // this is the reason for the failure
// do other stuff, because there is an error
}
else
{
// the function was successful
}
This is an example. Otherwise, you'll wind up with code that doesn't check for errors, and thereby you'll be executing code with the wrong assumption that things worked OK when they didn't.
Pretty much my first foray into c++ with just enough knowledge in java and vb to be dangerous.
All of those languages you mentioned has the concept of functions and return values. As a matter of fact, there are very few computer languages that do not have this concept.
When you use any API, library, third-party code, etc., and the documentation says "when you call this function, an error code is returned if something goes wrong", or "an exception is thrown on error", or similar wording, it is imperative you write code to check for these possible error conditions. If not, when your program dies on you for some reason, you won't know the reason.
Thank you all for the tips, I really appreciate it. A lot easier to get into the habit earlier than later.
As far as the drawing issue, it ended up being something pretty simple. Minimized and restored and there it was... I stuck an IvalidateRect() in the button code and it worked like a charm.
Bookmarks