Game Development Community

Clipping sprites?

by Vern Jensen · in Torque Game Builder · 08/15/2006 (1:18 pm) · 3 replies

I have the need to clip sprites to a certain rectangle within my window, so that portions of the sprite outside that rectangle aren't drawn. I know I could give the illusion of clipping by putting some other sprite in a higher layer in that location, and therefore having it "block" visibility of the sprites I want clipped, but this won't work, because I actually need the *background* (lowest layers) visible in that location.

Is it possible to clip sprites with Torque?

#1
08/15/2006 (1:42 pm)
Not at the moment. It sure would be useful if there was the ability to set a clipping polygon in a similar way to how you setup collision polys.
#2
08/15/2006 (7:54 pm)
You can use multiple "windows". Not windows in the regular sense, but multiple views within your game window. I really wish they were called "Views" or something other than windows since reusing the name for something other than what we're expecting is just confusing.

So for instance, in my game I have a background window/view which fills the whole window, a play view which is just a smaller rectangle where the playing pieces are (and they're clipped to within that region), a regular scene view that covers the entire window and a mouse view that has the exact same position and extents as the play view that I only use to grab mouse events for the play area (otherwise the scene view grabs the events).

For each window/view I also create a dedicated t2dSceneGraph and attach each view to that sceneGraph. Then when I create sprites I specify which sceneGraph they are in, and they'll only be drawn in that sceneGraph.

I find it cool that each window can also have different camera settings so for instance if you have a 8x8 playfield, you can have the camera/world setup so that view uses 800x800 whereas the overall scene could be using a 4000x3000 "world".

Here's my modified mainScreen.gui file which basically does what I describe above, although I'm sure it could be trimmed down quite a bit since I set the window/view position, extents etc. within script depending on the screen size.


//--- OBJECT WRITE BEGIN ---
new GuiChunkedBitmapCtrl(mainScreenGui) {
	canSaveDynamicFields = "0";
	Profile = "GuiContentProfile";
	HorizSizing = "width";
	VertSizing = "height";
	Position = "0 0";
	Extent = "1024 768";
	MinExtent = "8 8";
	canSave = "1";
	Visible = "1";
	bitmap = "~/data/images/Black Background";
	useVariable = "0";
	tile = "1";
	
	new t2dSceneWindow(backdropWindow2D) {
		canSaveDynamicFields = "0";
		Profile = "GuiContentProfile";
		HorizSizing = "width";
		VertSizing = "height";
		Position = "0 0";
		Extent = "1024 768";
		MinExtent = "8 8";
		canSave = "1";
		Visible = "1";
		lockMouse = "0";
		useWindowMouseEvents = "0";
		useObjectMouseEvents = "0";
	};

	new t2dSceneWindow(playWindow2D) {
		canSaveDynamicFields = "0";
		Profile = "GuiContentProfile";
		HorizSizing = "width";
		VertSizing = "height";
		Position = "0 0";
		Extent = "1024 768";
		MinExtent = "8 8";
		canSave = "1";
		Visible = "1";
		lockMouse = "0";
		useWindowMouseEvents = "0";
		useObjectMouseEvents = "0";
	};

	new t2dSceneWindow(sceneWindow2D) {
		canSaveDynamicFields = "0";
		Profile = "GuiContentProfile";
		HorizSizing = "width";
		VertSizing = "height";
		Position = "0 0";
		Extent = "1024 768";
		MinExtent = "8 8";
		canSave = "1";
		Visible = "1";
		lockMouse = "0";
		useWindowMouseEvents = "0";
		useObjectMouseEvents = "0";
	};

	new t2dSceneWindow(mouseWindow2D) {
		canSaveDynamicFields = "0";
		Profile = "GuiContentProfile";
		HorizSizing = "width";
		VertSizing = "height";
		Position = "0 0";
		Extent = "1024 768";
		MinExtent = "8 8";
		canSave = "1";
		Visible = "1";
		lockMouse = "0";
		useWindowMouseEvents = "1";
		useObjectMouseEvents = "0";
	};
};
//--- OBJECT WRITE END ---

And also here are some code snippets:

(Note that window.setExtent and window.setPosition both use screen pixels and not world coordinates.)

new t2dSceneGraph(t2dSceneBack) { class = t2dSceneBackClass; };

	backdropWindow2D.setSceneGraph( t2dSceneBack );
	backdropWindow2D.setCurrentCameraPosition (2000, 1500, 4000, 3000);

	new t2dSceneGraph(t2dScenePlay);
	playWindow2D.setSceneGraph( t2dScenePlay );
	playWindow2D.setCurrentCameraPosition (400, 400, 800, 800);
	playWindow2D.setExtent(200, 200);
	playWindow2D.setPosition(50, 50);

	sprite = new t2dStaticSprite() { scenegraph = t2dScenePlay; };

Hope that helps.
#3
08/20/2006 (1:08 am)
Thanks Ken! That won't provide the ability to clip against some non-rectangular object (like some curved sprite), but it's great to know you can do that for rectangular clipping.