PocketMatrix forums

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

New Surfaces/SetRenderTarget

I have a project where I scale a lot of images. I have a class where I keep a pointer to the original and create a new surface for the scaled image any time it needs to be re-sized.

I was using the SetRenderTarget and blit-stretching onto it, then passing back a pointer to the new surface. It appeared to not have the correct size (image was skewed and partially corrupted).

After changing the SetRenderTarget to CreateRasterizer before the stretch-blit, everything works out as expected.

The question is what's the difference? Is there a problem with starting a scene with the "display" and then setting/resetting the target and then calling display->update? Or should it be kept completely separate (ie. don't start scene until after temp images have been re-scaled). And, in theory, do you need to end drawing to the surface by calling a display->Update()?

BTW, thanks to Presto for pointing out the CreateRasterizer function. I hadn't paid much attention to it. It's actually much better than having to set the target and more readable as well. And, unless there's something I haven't seen, there's not really a reason to use the SetRenderTarget unless you had a chunk of code that you wanted to draw to both the screen and a surface.

Jason
Hmm, interesting... I have always felt the SetRenderTarget was more intuitive. I view it as telling all drawing functions what surface I want to draw on and that always felt more straight-forward to me. Ah, whatever works though :)

Unfortunately, I'm an ass... I don't actualyl have an answer to your question :(
fast_rx wrote:I have a project where I scale a lot of images. I have a class where I keep a pointer to the original and create a new surface for the scaled image any time it needs to be re-sized.

I was using the SetRenderTarget and blit-stretching onto it, then passing back a pointer to the new surface. It appeared to not have the correct size (image was skewed and partially corrupted).

After changing the SetRenderTarget to CreateRasterizer before the stretch-blit, everything works out as expected.

The question is what's the difference? Is there a problem with starting a scene with the "display" and then setting/resetting the target and then calling display->update? Or should it be kept completely separate (ie. don't start scene until after temp images have been re-scaled). And, in theory, do you need to end drawing to the surface by calling a display->Update()?

BTW, thanks to Presto for pointing out the CreateRasterizer function. I hadn't paid much attention to it. It's actually much better than having to set the target and more readable as well. And, unless there's something I haven't seen, there's not really a reason to use the SetRenderTarget unless you had a chunk of code that you wanted to draw to both the screen and a surface.

Jason


You're welcome.

A couple of things to note:
Although it seems weird that you can call "CreateSurface(x, y);", "CreateRasterizer(pSurface);" and "delete pSurface;", you can't call "delete pRasterizer;". I've been stumped by that a couple of times because the error message isn't too obvious.

There shouldn't be a difference between:
DisplayDevice *pDisplay = GetDisplay();
Surface *pImg = LoadImage(pDisplay, IDR_IMAGE); // 32x32 image
Surface *pSurface = pDisplay->CreateSurface(64, 64);
pDisplay->SetRenderTarget(pSurface);
pDisplay->BlitStretch(Rect(0,0,64,64),pImg,null);
pDisplay->SetRenderTarget(0);
pDisplay->Blit(0,0,pSurface);
pDisplay->Update();

And:
DisplayDevice *pDisplay = GetDisplay();
Surface *pImg = LoadImage(pDisplay, IDR_IMAGE); // 32x32 image
Surface *pSurface = pDisplay->CreateSurface(64, 64);
Rasterizer *pRasterizer = pDisplay->CreateRasterizer(pSurface);
pRasterizer->BlitStretch(Rect(0,0,64,64),pImg,null);
pDisplay->Blit(0,0,pSurface);
pDisplay->Update();

I haven't tested this code, but it should work the same either way...

-John
I have done a little investigating and have come up with a couple of issues, but not everything.

First, using the SetRenderTarget() to draw on a surface resets the previously set Clipping for the main display. This doesn't happen when using the CreateRasterizer and going that way...

Also, while making sure I wasn't missing something else, I noticed that trying to use FillRect on a clipped portion of the screen causes a crash. If someone else wants to verify both of these issues here's the code:

Code:

static int Sx = 150;
static int Sy = 200;
static int dSx = 1;
static int dSy = 1;

Sx+=dSx; Sy+=dSy;
if( Sx > 240 || Sx < 0) dSx = -dSx;
if( Sy > 320 || Sy < 0) dSy = -dSy;


Rect crect;
crect.Set(40,40,200,280);

display->SetClipping(crect);

display->SetRenderTarget(surf);
display->SetRenderTarget(NULL);
display->DrawRect( Sx-10, Sy-10, Sx+10, Sy+10, Color(255,255,255));


On my system, compiling for the desktop with vc6 produces the error with the clipping. If I replace the DrawRect with FillRect (and get rid of the SetRenderTargets that mess up the clipping), I get crash as soon as the entire rectange is clipped...
It crashes because you set the render target to NULL. Trying to do a FillRect() on a NULL surface isn't a good idea.
Thierry,

I should have posted two separate code blocks...

The crash happens with only the following...
Code:

static int Sx = 150;
static int Sy = 200;
static int dSx = 1;
static int dSy = 1;

Sx+=dSx; Sy+=dSy;
if( Sx > 240 || Sx < 0) dSx = -dSx;
if( Sy > 320 || Sy < 0) dSy = -dSy;


Rect crect;
crect.Set(40,40,200,280);

display->SetClipping(crect);


display->DrawRect( Sx-10, Sy-10, Sx+10, Sy+10, Color(255,255,255));


The other problem with the clipping being reset is WITH the SetRenderTarget's in place...

And using NULL to go back to the default display is what your code says to do:
display.h Line 86
// Change the rendering target (specify NULL to restore rendering to backbuffer)

I also tried it using
Code:

Surface * screen = display->SetRenderTarget(surf);
display->SetRenderTarget(screen);

Just to be sure it wasn't something to do with that. It still looses my clipping rect with the switch...

Jason
Oh yeah sorry about that, SetRenderTarget(0) is valid on the Display object. Haven't been coding with PF in quite some time =)

The clipping rect being resetted is also normal (but perhaps not desirable).

I will check all this soon. PHAL 0.6 is almost ready.
If it were up to me, I'd rather see a PHAL/Frog incorporation before fixing little problems like these...

I'm seeing if I can figure out if the problem is with the clipper or not.

Maybe we should have a list of known problems/issues like these, of which I've got another one to add:
The DrawVGAText function will not work with a color of 0,0,0. I just happened across the reason while debugging this other thing. The reason:
Code:

// Seems a little anal, but let's abort if color is NULL
if (color == NULL) { return; }


I guess I don't know why that's anal, but it definitely keeps me from drawing black text, right? I'm sure Frank Z. had a reason at the time...

I will post more info if I can determine what is the problem with the clipped fillRect.


Thanks for everything you've done so far in developing these libraries. I really do appreciate it.
Yeah, I had a reason...

A very good reason...

I AM A DUMB ASS!!

See, for just a moment, it seems I forget that null and zero are the same. I was trying to catch bad input parameters, as people generally want functions to do, right?!?, and I must have been thinking "well, if they don't pass me in a color, I shouldn't try and draw anything".

But it didn't click in my mind that null and zero are the same (well, most of the time anyway), and so I didn't catch that f**k up.

But hey, you should be happy I put at least SOME comment there, shouldn't you?!? :)
Ok, as for the problem with a FillRect that is drawn outside of the Clipping rect...


The FillRect routines in rasterizer.cpp need the following lines just after the r.Intersect

Code:

// Trivial reject
if (r.IsEmpty())
return;


This is how the blit functions handle it... The intersect actually returns an inverted rect (left greater than right, and so on...). So, the call to IsEmpty is required.

All the other functions in rasterizer.cpp seem to have that code.

Jason
fzammetti, don't be so hard on yourself. You've got all kinds of code through there. It's gotta happen sooner or later...

BTW, I was thinking that maybe it made sense if the color mode wasn't 5-6-5, but you set me straight.