Hiding a sprite behind the background

General Programming help in C, C++ or ASM, Compiling / Debugging, and R3000A Central Processing Unit (CPU) information
Post Reply
jman
Rookie Programmer
Rookie Programmer
Posts: 110
Joined: August 13th, 2013, 7:58 am

Hiding a sprite behind the background

Post by jman » July 6th, 2014, 8:31 am

Hello,

I'm trying to figure out this: let's say I have a sprite moving. When it goes a certain coordinate it should gradually disappear behind the backgroud.
Imagine a sprite representing the hero entering a house. I'd like this sprite to get lower drawing priority as it moves through the door: I'd like to "see" only the portion of the hero that it's not yet inside the house.

Is it possible to do that? Any hint? Consider the case in which I only have two sprites: the background and the hero and I cannot break them down to smaller parts (because it would be too much work).

Thanks

User avatar
Shadow
Admin / PSXDEV
Admin / PSXDEV
Posts: 2393
Joined: December 31st, 2012, 5:37 pm
PlayStation Model: H2000/5502

Re: Hiding a sprite behind the background

Post by Shadow » July 6th, 2014, 6:18 pm

If you're using a polygon as your sprite, you can use a transparent polygon.
This is how you can draw a transparent polygon (note that it is old and unoptimised code):

Code: Select all

///////////////////////////////////////////////////////////
// Draw a transparent rectangle.
// The transparency 'colour' is calculated from the foreground
// and background pixels.
//
// EG: DrawTransRect(10,0, 20,0, 10,39, 20,39, 25,25,25, 0);
///////////////////////////////////////////////////////////
void DrawTransRect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int r, int g, int b, int ot)
{
	POLY_F4 f4; // f4 = 4 sided polygon

	SetPolyF4(&f4); // initialise the primitive
	setRGB0(&f4, r, g, b); // set the foreground fill colour
	setSemiTrans(&f4, 1); // semi-transparency (0=off | 1=on)
	setXY4(&f4, x1,y1, x2,y2, x3,y3, x4,y4); // draw 4 lines (4 sets of x/y coordinates)
	//DrawPrim(&f4); // copy shape to frame buffer
	GsSortPoly(&f4, &WorldOrderingTable[CurrentBuffer], ot); // put the polygon into the ordering table
}
The easiest method however is to lower all RGB value down equally in a loop.
Here is some old and crude code I wrote a while ago to do that.
NOTE: This code fades it out, and then back in again:

Code: Select all

// display the license.tim image until the timer reaches 245, which will fade the
// license.tim image out. when it has, continue to titleFadeI(); to fade in the
// license.tim image back in.
void timerFadeLicense()
{
  int i=0, timerA=130;

  for (i=0; i<timerA; i++)
  {
    if (DEBUG_FADE) printf("Fading LICENSE.TIM out=%d\n",i);
    
    // if the luminosity is 0...
    // otherwise, decrease the luminosity by a multiple of 2
    if (LicenseSprite[0].r=LicenseSprite[0].g=LicenseSprite[0].b==0 || i>=64)
    {
      if (DEBUG_FADE) printf("LicenseSprite[0]=%d\n",LicenseSprite[0].r=LicenseSprite[0].g=LicenseSprite[0].b);
      break;
    }
    else
    {
      LicenseSprite[1].r=LicenseSprite[1].g=LicenseSprite[1].b-=2; // luminosity -2
      LicenseSprite[0].r=LicenseSprite[0].g=LicenseSprite[0].b-=2; // luminosity -2
    }
	
	if(!DTL_H2000) CheckCD();
	if(cderror==1) display(0);
	else display(1);
  }

  timerA=130; // reset the timer
  LicenseSprite[0].r=LicenseSprite[0].g=LicenseSprite[0].b=0;
}
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest