CreativeMFS wrote: |
---|
Also I am thinking that the functions that currently draw should just fill _maingrid[][] and in the render function I should just redraw _maingrid, is that too much? or the correct way? |
This would be the correct way with most graphic APIs (SFML/OpenGL/D3D) because in those APIs you pretty much
need to completely redraw the screen every frame.
However for WinGDI that isn't necessary. And in fact it would be slower than the alternative. You
could do it that way. And your game isn't very demanding so it probably wouldn't slow it down. So if you want to do it that way, go ahead.
But, what I would do is this:
1) Create that offscreen DC like you're doing in your post (first 3 lines)
2) Keep that DC alive for the life of your program. Create it once at startup and destroy it once at shutdown. Don't keep creating/destroying it for every draw.
3) The drawing code you have now you can keep, just draw it to your offscreen DC instead of to the screen.
4) When all drawing is complete, get the screen DC, blit the offscreen DC to it, release the screen DC. This would be done once per frame.
Now you can break #4 up into smaller steps and only redraw the "dirty" portions of the screen, but that's more work than it's worth.
Also because WinAPI is extra-fun, there's actually multiple ways to get the screen DC. Which you use depends on what you're doing. For your per-frame drawing, you would want GetDC/ReleaseDC as you're doing now.
But if you want to fix the #5 bug, that's something else...
Whenever a window passes over yours, or whenever something else happens that causes the window contents to be disrupted, Windows will tell your program that it needs to redraw itself by sending it a WM_PAINT message. To fix the #5 bug, you'll need to catch the WM_PAINT message in your message handler and redraw the window.
This is pretty easy if you're using an offscreen DC, since you can just blit over the contents to the display. Easy peasy.
The goofy thing is, you can't (or rather, shouldn't) use GetDC to get the screen DC in WM_PAINT messages. If you do, it will still draw correctly, but it'll have other consequences...
In WM_PAINT, you should use the BeginPaint and EndPaint functions to get/release the DC. The reason for this is twofold:
1) Windows clips the DC it gives you so that you'll only draw the portions of the window that need to be drawn.
2) Using these functions effectively tells Windows "okay, I redrew the window. Everything is OK now". If you don't use these functions, Windows will keep thinking the window is dirty and will send you a never-ending stream of WM_PAINT messages -- causing your program to draw the same thing over and over and over again.
But of course BeginPaint/EndPaint should only be used in response to a WM_PAINT message. So for other drawing to the window outside of WM_PAINT, you would want to use GetDC/ReleaseDC instead.
-----------------------------------
Albatross wrote: |
---|
I know I'm sort of reviving the SFML topic, but I'm a little bit worried how they haven't officially released a major version since July 2010. That's making me a little bit hesitant to use it for any projects, while SDL I believe is still under active development. Version 1.3 of SDL in the works right now. |
SFML is heavily in active development. SFML 2.0 is actively in the works and you can get the latest version of it from the website. It's updated maybe once every week or two. It's stable and works great -- the only reason it hasn't been an official release is because he's still making changes to the API, so if you use the WIP version and then update it, it might break code that uses earlier versions.
I'm a semi-regular on the SFML forums, and from the looks of it, he's working out the details for the new graphic API, but once that's done and the code is complete, SFML 2.0 will effectively be done and released. IIRC it was projected to be another month or two.
On the other hand... SDL has been at version 1.2 since
I used it last, which would have been at least 5 or 6 years ago. (Actually -- checking the timestamp on the SDL download page, it looks like the last update was 2001, so make that 10 years ago)