PocketMatrix forums

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

Understanding of rendertarget

Hello all. I had a situation the other day where I wanted to have a plasma screen with some menu options over it, and when you click one of the menu items, they all dissolve away without affecting the plasma.

Now, I wound up getting it to work just fine, but not the way I expected to. It's a few days later, and I think I now have an idea how to do what I originally thought of, but I'd like you guys to let me know if my understanding is correct. Let me know what I get wrong here...

If I create a Surface using DisplayDevice's CreateSurface method, I can then pass that surface to DisplayDevices's SetRenderTarget method. Then, if I do DisplayDevice* display = GetDisplay(); I should be able to draw to that Surface I created using all the familiar blits and primitives.

Assuming that's true, what I would do is create two Surfaces. On the first I draw my plasma, on the second I draw my menu items. I then set the render target back to it's default (not sure how I'd do that?) and then blit my plasma Surface, then my menu Surface, and I should be all set.

Does this sequence sound accurate, or is there some basic concept I don't quite get here? I guess what this boils down to is that I want to have two Surfaces that I can alter and then later blit them as you would any Surface to the screen. But, I don't see how I can draw to a Surface using all the primitives and blitters UNLESS I set that as the render target of DisplayDevice. And then the question is, how do I set whatever the default render target is, I'm guessing maybe display->SetRenderTarget(display->GetBackBuffer());

Thanks!
Although your method will work, you can simply use the NULL pointer to restore the default render target:

Code:

display->SetRenderTarget( 0 );
Thanks Thierry. that sounds good. I was hoping there was some constant defined for default, that's just as good.

A further question for you: How efficient is doing something like this as opposed to say a bunch of normal blits with some logic wrapped around it (which is what I wound up doing). In other words, assuming I'm not creating a surface every time (which I presume would be bad), how much impact would doing all this target switching and such have on performance?
Changing the rendering target is almost free. All it does is copy the Surface pointer internally. So don't worry about it.

You might also consider instantiating a second (or more) rasterizers. For example:

Code:

Surface* mySurface = display->CreateSurface(...);
Rasterizer myRaster( mySurface );
myRaster.SetBlending(...)
myRaster.Line(...)
display->Blit( mySurface, ...)


Not exactly sure if everything here is ok, but it should be something similar. This way you don't have to change the display's render target.

In the end, the performances are quite similar, I simply made sure the architecture was flexible by separating the rasterizer code from the surface code. This allows you to instantiate as many rasterizers as you need/want. Note that there is no real performance gain in doing so, nor is there any significant memory overhead.
Ah, that's very interesting. I think I prefer that method if for no other reason than the code makes more sense that way.

I'm not sure I really understood the whole mechanism before, and assuming I get it now...

What your saying is you create your surface, "attach" a rasterizer to it, and your good to go, you can draw on it just like you do when you draw to the DisplayDevice* returned by GetDisplay(). The only difference is the syntax you use (dot notation instead of ->)

Furthermore, and this is the part that's just dawning on me, what your really getting when you do DisplayDevice* display = GetDisplay(); is essentially a rasterizer which has automatically been attached to the screen display (I presume the back buffer surface) by PF itself.

Now this thread is getting kind of theoretical, but it's helped me to understand PF a lot better now, assuming I got the above two paragraphs right.

Thanks a million Thierry, this has been very valuable!
You understand perfectly. The display is itself a rasterizer and it's render target is the back buffer (by default).
As a follow-up, I used what we talked about here for the instructions screen for my current project. The screen consists of a 3-D starfield (approx. 100 stars, some more than one pixel in size) with a bouncing logo up top (approx. 100x50), two 240x1 line images , two 24x24 arrow images and the words Return To Menu at the bottom (plotted with my custom text plotter).

The instructions are displayed in a large 240x209 area in the center of the screen, and there are two arrows that will scroll the text left or right accordingly. A very quick and VERY rough estimate indicates that we're talking about something like 185,000 pixels per frame.

I originally had the text of the instructions being plotted on a separate surface:

display->SetRenderTarget(insSurface);
display->Clear(Color(0, 0, 0));

insSurface was a previously created surface (created only once, not with every GameLoop iteration):

insSurface = display->CreateSurface(240, 209);
insSurface->SetColorMask(Color(0, 0, 0));

I then drew all my text to this surface and when I was done with that:

display->SetRenderTarget(0);
display->Blit(0, 81, insSurface);

To blit the "text" surface to the screen. This worked just fine, except that my frame rate dropped from a rock-steady 30FPS (game is hard-coded to run at that rate) to around 20 or so.

I'm not saying this is a problem, it probably should be expected considering the number of pixels being blasted out, and I certainly didn't try and code this efficiently by any stretch. I'm just writing it here so that anyone wanting to use this technique realizes that, while I'm sure I didn't do it the best way possible, there is a performance hit to consider.

The very fact that it WORKED though I love... it means if I code with efficiency in mind, this would likely work without the frame rate decrease, and that of course opens up a world of possibilities!