The problem I ran into is that I wanted to be able to move a surface onto the screen gradually, or off gradually. There's nothing that I can see (someone correct me if I'm wrong) in GD that makes this easy. You need to be sure that the x, y coordinates you pass to the blitter are clipped to the screen (otherwise nothing gets drawn at all) and also that the source rect Pam mentioned is set to the degion of the source surface that would actually be visible on the screen.
Well, after turning my brain to goo (remember, I'm at work and I've been actually working on stuff all day, had an hour towards the end and wanted to bang this out... BIG mistake!), I finally did get it to work. Here is the code I came up with... I absolutely welcome comment from the gallery, I'm certain it's not the best approach, but it does work as I want it to, as near as I can tell. Feel free to use it if you want, when your done laughing at my code that is!
- Code:
// This is all within ProcessNextFrame. This will
// slide a 200x200 surface that was previously
// created onto the screen from the upper left-hand
// corner and down off the right. It'll keep going,
// so eventually nastiness will occur, but for a
// quick test it's fine. It should be very easy to
// take the clipping algorithm out and make it a
// callable method, that's my next step.
// Clear the screen
backbuffer.FillRect(NULL, RGB(0, 0, 0), 0, NULL);
static int x = -210;
static int y = -210;
x++;
y++;
// >>>> START CLIPPING ALGORITHM
RECT srcRect;
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = mg_surface->GetWidth();
srcRect.bottom = mg_surface->GetHeight();
RECT destRect;
destRect.left = 0;
destRect.top = 0;
destRect.right = backbuffer.GetWidth();
destRect.bottom = backbuffer.GetHeight();
int srcSizeX = srcRect.right;
int srcSizeY = srcRect.bottom;
int destSizeX = destRect.right;
int destSizeY = destRect.bottom;
int drawX = x;
int drawY = y;
// Left edge X clipping
if (x < 0 && x + srcSizeX > 0) {
srcRect.left = srcSizeX - (srcSizeX + x);
drawX = 0;
}
// Right edge X clipping
if (x + srcSizeX > destSizeX) {
srcRect.right = srcSizeX - (x - (destSizeX - srcSizeX));
}
// Top edge Y clipping
if (y < 0 && y + srcSizeY > 0) {
srcRect.top = srcSizeY - (srcSizeY + y);
drawY = 0;
}
// Bottom edge Y clipping
if (y + srcSizeY > destSizeY) {
srcRect.bottom = srcSizeY - (y - (destSizeY - srcSizeY));
}
// >>>> END CLIPPING ALGORITHM
mg_surface->FillRect(NULL, RGB(255, 0, 0), 0, NULL);
mg_surface->DrawLine(10, 10, 190, 190, RGB(255, 255, 255), NULL, NULL);
backbuffer.BltFast(drawX, drawY, mg_surface, &srcRect;, 0, NULL);
Note that it assumes that the entire surface is always to be used. This isn't a problem for me, others may want to build in a bit more flexibility though. (Actually, as I say that, I'm not sure where you'd ever do that, but I digress).
Conan, I know we've discussed this over in the PF forum, but GD users may be interested to read it as well... For me, GD has two main benefits over PF: (1) the image loader, which to my way of thinking is a big problem with PF, and (2) the fact that it's DirectX on the desktop.
I was very pleasently surprised when I quickly converted my VGA font drawing function over to GD, which isn't a big deal on it's own, but the fact is that when displayed on the desktop and the window stretched, it looks fantastic. I'm assuming there is some sort of antialiasing going on behind the scenes because a raster-based VGA font shouldn't look as good as it does. I mean, I don't want to exaggerate, it's not like the best-looking fonts ever, but they are far superior to what you get with PF by doing a zoom on the desktop.
For me, these two reasons make it worth it to seriously look at GD for my next project.
Of course, the fact that you can get at the source for PF is a very big plus. It being continually supported is very important too. Yes, I'm still supporting PF, and I'll continue to do so a while longer at least, but let's be honest... me compared to SBL is like comparing a Lexus to a Lamborghini. A Lexus ain't bad, but the Lamborghini blows it away hands-down.