PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Newbie: Problem blitting a surface

Ok, I'm actually starting to use GD for my new project, so I'm basically a GD newbie, and I have a question...

I have the following member in my application's class:

Code:

CGapiSurface* mg_surface;

I then create the surface in the CreateSurfaces method thus:

Code:

mg_surface = new CGapiSurface();
mg_surface->CreateSurface(NULL, 200, 200);

Then in my ProcessNextFrame method, I do the following:

Code:

backbuffer.FillRect(NULL, RGB(0, 0, 0), 0, NULL);
mg_surface->DrawLine(10, 10, 200, 200, RGB(255, 255, 255), NULL, NULL);
backbuffer.BltFast(20, 20, mg_surface, NULL, 0, NULL);

My problem is that the line does not appear on the screen. If I instead call the DrawLine method of backbuffer, my line is drawn as expected, so I know that the DrawLine function call is correct. I also checked the return value from the BltFast call, and it's coming back GD_OK as expected.

I've looked through the samples and documentation but I don't see anything obvious that I blew. So, what am I doing wrong? Thanks all!
Just guessing here,

Have you tried drawing lines with other endpoints?

It might be possible that when you create the surface it is really 0-199, 0-199. Since GapiDraw doesn't do clipping, maybe it isn't drawing the line since it goes outside the bounds of the surface to 200,200?

Pam
Just a guess Pam?!? :)

That was it! That didn't even occur to me.

Keep those... GUESSES... coming! :)
OK, call it woman's intuition then! :D

Glad I could help.

Pam
You know, it didn't occur to me because I didn't realize that GD doesn't do clipping. Coming from PF, I just naturally assumed it did. I just read through a bunch of posts here, now I know. Argh, extra work. Ah well, what are you gonna do.
Yep, it got me too, that's why it occurred to me in your example. Personally, I think I got a little lazy in PF expecting it to clip for me and I stopped making sure my code didn't go out of bounds.

I use/used PF for almost a year before I started using GapiDraw. As a fellow PocketFrog-er, I'm interested to hear what you think of GapiDraw once you get into it.

Pam
Well, right now I like what I see for the most part, but this clipping thing is giving me fits.

I'm trying to write a clipping algorithm so that I can slide a surface from off the left side of the screen and have it go off the right. So far, every clipping example I've seen won't handle this properly. Once I get this solved I think I'll be relatively happy with GD. I too am spoiled, I can slide an image on and off the screen with PF without having to think about it in the slightest, all this extra work is killing my brain! :) (Not that there was much life left in the 'ole girl to begin with)
? Isn't there anything built-in you can turn on like PF's set clipping rectangle also what's the benefit over PF other than speed (which is of course important) if there are fewer features.

( devil's advocate coming out in me somewhere I guess. I won't have a chance to try GD as I'm deep in this game and could not jump ship )
Conan wrote:
? Isn't there anything built-in you can turn on like PF's set clipping rectangle


You can specify a source Rectangle in the Blt function.

Pam
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.