Wacky Windows/Debugger Issue

Thanks so much for anyone who has the patience to read this.

So, I'm on day 5 of Handmade Hero, and I'm following along with Casey's code to create a backbuffer and then use StretchDIBits to draw the backbuffer to the window. I looked ahead and saw that it looks like we will be doing background platform stuff and no "stuff moving around onscreen" for some time.

For experimentation's sake, and to see if I'm getting any of this, I tried making a program that uses simple Windows drawing functions once we have registered a window class, obtained a window handle.

I actually was able to draw a few simple shapes, but when I tried to "animate" the shapes, I ran into trouble.

The code actually seems to work if I step through on the debugger, but it behaves in a totally different way when run at full speed.

The program "clears the screen" using FillRect and a background color and then draws a circle. The draw function is called in a while loop that iterates the draw functions parameters up and down from 0 to 10 and back.

In the debugger, this runs correctly if you step through the code. Each time the draw function is called, the window's client area is cleared (getting rid of the old circle) and the new circle is drawn.

At full speed, there are no circles - only the background color - and, after a few seconds, the background color turns to white even though I never set it to white in the code.

Clearly, I'm doing something horribly, horribly wrong, but why would this code work one way in the debugger and another running at full speed?


This is the draw code.




int DrawCircle(PAINTSTRUCT ps, HWND hWnd, int size){
HDC hdc = GetDC(hWnd);
RECT clientRect;
HBRUSH brush = CreateSolidBrush(RGB(100, 150, 200));
GetClientRect(hWnd,&clientRect);
FillRect(hdc, &clientRect, brush);
Ellipse(hdc,10*size,10*size,5*size,5*size);
ReleaseDC(hWnd, hdc);
return 0;
}




Notably, the loop that calls this draw function runs from WinMain and not in response to a message handled by my Window Procedure, which seems to have something to do with the problem.

On some iterations of this program, I've tried using a device context set as a global variable, FillRgn instead of FillRect and some other things, and in some cases the code will draw all the circles at once or show all the circles at once with each circle "blinking" as it is drawn. Again, behaves correctly when running in the debugger and stepping through.

I should also note that the base code for bringing up a window is from the Windows application template in Visual studio. I didn't check it line-for-line and byte-for-byte but it seems to be basically the same thing casey's doing to resgister a window class, callback function, etc.

I feel like this must be something I'm doing wrong with how I clear the screen and iterate the drawing call, but it blows my mind that it works when stepping through with the debugger and not at full speed.

Any ideas? the source is attached (except for a few minor changes, this is the default Win32Project.cpp when you choose a Windows application in Visual Studio.

Thanks again for your patience if you've read this far. I will probably be ditching this code ad staying closer to what casey's doing in the videos, but this first experiment in applying what I've learned is a frustrating failure I'm having trouble understanding or learning anything from. :(

I'm not really trying to fix this code so much as understand what I'm doing wrong so I can at least learn from the failure.


Thanks!

Edited by Jonathan Loiterman on
Sorry, seems like uploading the .cpp file is not working either for some reason. Not sure what the issue is there...

Edited by Jonathan Loiterman on
The Circle is drawing straight to screen and not using the backbuffer I guess, as you're getting a DC straight to the Window.

Can you upload the cpp anywhere and I'll take a look to see if it makes sense to me (as I'm learning too but seemed to have grasped HDC's purpose now and have been drawing directly to the window using SetPixel for experimentation and that worked for me.
Try sticking the following directly after the StretchDIBits code within the same function:

1
2
Ellipse(DeviceContext, WindowRect->right,WindowRect->top,
WindowRect->left,WindowRect->bottom);


In my code that creates a big white ellipse that flickers. I'm guessing this may be because the code is not timing the updates and the drawing precisely.

Edited by Gav on
I'm trying to attach the code here using the Attachments tool.


there is a line of code instered that appears to be placing a tag for Win32Project2.cpp

No tag is showing up in the Preview.




tried attaching two more times...
I tried adding the ellipse code to StretchDIBits too and got the same result.

The nearest I can tell is that these windows functions are built around a really strange model for doing things that we don't understand and that we're writing our own stuff to avoid those kinds of problems.

I must be missing something on the file upload, but I do have source for this strange debug/realtime problem.