PocketMatrix forums

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

Clipper in 2.02? Comments and suggestions please

Ok, I have been thinking a bit about clippers and found out that it actually is a trivial addition to the current feature set.

I went through the functions, and clipping could be added to the following functions easily:
CGapiSurface::Blt (all flags supported, including rotation)
CGapiSurface::BltFast
CGapiSurface::AlphaBlt
CGapiSurface::AlphaBltFast
CGapiSurface::DrawLine (using correct line clipping)
CGapiSurface::DrawRect (using correct line clipping)
CGapiSurface::FillRect
CGapiSurface::SetPixel
CGapiSurface::SetPixels
CGapiMaskSurface::DrawMask

The only function that will not get generic clipping support is:
CGapiSurface::DrawText (this function will get other features which are more suitable than viewport clipping - usually you don't want to clip to surface viewport, but rather typeset text in columns or squares)

So, please bear with the known issues with GD 2.01b for a bit longer.. And report back any suspections of non-reported ones.. GD 2.02 will be a good one...
Sounds great Johan!
Johan, how will this affect performance? Can it be turned on/off?
Adding clipping into GD would be great! Here's my thoughts around this one.

Create a function called pSurface.SetViewPort(RECT&) that will allow for an adjustable viewport, giving one the capability of adjusting the viewport inbetween blits to the surface. One could then simulate multiple viewports in a single surface.

How to make it part of GD?... Just add another flag for clipping eg. GDBLTFAST_CLIP that's passed to the function. This should solve the performance concern. Then the developer can switch clipping on and off to fit his liking. One would typically not blit an object drawn right in the middle of the surface, but then again, GD should be able to determine this by itself too.

Hopefully this input has helped a bit.
I'm all for native clipping. I got spoiled with PocketFrog having it built-in.

I'm guessing that adding clipping doesn't hurt performance much at all. Doing our own clipping externally doesn't seem to cost very much, I'd bet that doing it internally, and based on the fact that your an optimization monster Johan!, I very much look forward to this.

The idea of a SetViewport function I like a great deal. It's very helpful just to clip to the physical screen, but as an example, my current game would benefit greatly if I could say "ok, clip against this particular RECT until further notice". That would allow me to go directly against backbuffer rather than the temporary surface concept I've been using. I'd bet I would see a frame rate increase if I unbound the frame rate.

By all means, you absolutely have my support! (not as if you needed it, but still :) )
That would be great Johan! Although I already did my own clipping routine, I'd be glad to see it in GD 2.02.
Sounds good :)

I'd also like to see the clipping being optional, and with an adjustable clip window

(although the performance impact of always-on clipping is probably going to be insignificant in most cases?)
Clipping is one of the things that GapiDraw could realy use! I'm all for the adjustable viewport and GDBLTFAST_CLIPP solution.

Maurice
I don't think there will be much performance overhead of clipping. My question however is how it should be integrated.

Consider a simple game that only draws to the backbuffer. The game area is in the middle and it has a GUI display around it. It also has some status messages that cross both the game area and the gui display.

Objects in game area - should be clipped to viewport
GUI - should be clipped to surface (or not clipped at all)
Status messages - same as GUI

So.. Considering that a viewport has been set on the surface, would one want to specifically enable clipping using a flag such as GDBLTFAST_CLIP (for everything in the game area), or would one want to temporarily disabling clipping using GDBLTFAST_NOCLIP (for the GUI and status messages).

I am leaning towards the GDBLTFAST_CLIP flag, since it also becomes very clear what functions support clipping (meaning that we don't have to integrate clipping in simple functions such as SetPixel for example).

The way it would work then is:
(1) The default viewport is the entire surface. A function SetViewPort(NULL) resets the viewport to the entire surface, SetViewPort(&rect) sets the viewport to the specified rectangle.
(2) Clipping would have to be enabled in every function with flags such as GDBLTFAST_CLIP and GDALPHABLT_CLIP. The clipping will be done to the currently selected viewport (entire surface or a smaller rectangle).

Would this be ok?
Fine by me. I also prefer the GDBLTFAST_CLIP i.s.o. NOCLIP flag.
You've got my vote for the GDBLTFAST_CLIP route! One can always complicate things, but I think you should K.I.S.S (Keep It Simple Sam)... :)
Johan, I agree with the other guys clipping should be turned off by default so I also support the GDBLTFAST_CLIP flag. I'm wondering if you plan to use the 'inclusive right and bottom member' approach for the RECT in the SetViewPort command. Eg. will I need to type SetViewPort(0,0,240,320) to cover the whole screen area? To be honest this approach has always been weird to me while I understand its benefits and know that it actually complies with the DirectDraw standard.
InexorableTash · 10 Jul 2003, 17:40 UTC
I have to admit that I'm a fan of making SetViewport apply to the surface and all subsequent operations to the surface, without requiring additional flags on the calls.

So in Johan's example of a game with smaller viewport for the playing area and an overlayed UI, presumably overlapping the edges of the play area to make it non-rectangular:

surface.SetViewport( CRect(10, 10, 50, 50) );
surface.BltFast( sprite1 );
surface.BltFast( sprite2 );
surface.BltFast( sprite3 );

surface.SetViewport( NULL );
surface.BltFast( ui_overlay );

Requiring the flags on the individual calls doesn't seem like a benefit to me; the perf impact of testing for a clipper inside the Blt functions is infinitesimal compared to the cost of pushing pixels around. It keeps the client code cleaner since the bulk of the game code doesn't even need to be aware that a clipper is being used.

You could imagine taking the GD sprite demo and adding a few lines of code outside the main sprite loop which implement a clipper rect which is moving from frame to frame; it would be nice to be able to simply add that code without modifying any of the rest of the code.
I agree with InexorableTash.

This soluton is similar what is used in most of GUI toolkits = some sort of graphics context which is set/reset using its own set of calls and which then gets applied to every graphic call.
If you don't stop adding so many good features, I will go crazy waiting for the release! :D I like the idea. Hope GD 2.02 comes out soon.
Clipping has been in PocketFrog for a long time. You can take a look at how to efficiently implement it.
I agree with the thought of a switch to turn clipping on/off at the surface level (or maybe just turn it on globally for all supported GD functions until told to turn it off, I could go for that just as well).

In any case, it's a good edition in my book, implementation details aside.
Here is what you could do, allow us to pass in a clipping RECT that basically defines the screen coordinates of what the surface will be clipped on.

for instance, if I want the surface to be clipped relative to a 10X10 square in the bottom left of the screen, then I'll pass a RECT defining that location and your clipping code can clip relative to that.