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!