コード例 #1
0
void TextSprite::RenderImage(uint32 gameTime)
{
    if(textColorSource != null) textColor = textColorSource->GetColor(gameTime);
    if(borderColorSource != null) borderColor = borderColorSource->GetColor(gameTime);
    if(backgroundColorSource != null) backgroundColor = backgroundColorSource->GetColor(gameTime);
    if(marginXSource != null) margin = SCREEN_TO_SUBPIXEL(CIwVec2(marginXSource->GetInt(gameTime), marginYSource->GetInt(gameTime)));
    if(paddingXSource != null) padding = SCREEN_TO_SUBPIXEL(CIwVec2(paddingXSource->GetInt(gameTime), paddingYSource->GetInt(gameTime)));
    if(borderXSource != null) borderThickness = SCREEN_TO_SUBPIXEL(CIwVec2(borderXSource->GetInt(gameTime), borderYSource->GetInt(gameTime)));

    if(marginXSource != null || paddingXSource != null || borderXSource != null)
    {
        UpdatePositionData();
    }

    CIw2DFont *prevFont = null;
    CIwColour prevColor = Iw2DGetColour();

    if(textFont != null)
    {
        prevFont = Iw2DGetFont();
        Iw2DSetFont(textFont);
    }

    if(backgroundColor.a > 0)
    {
        Iw2DSetColour(backgroundColor);
        Iw2DFillRect(CIwSVec2(backgroundPosition), CIwSVec2(backgroundSize));
    }

    if(borderColor.a > 0)
    {
        Iw2DSetColour(borderColor);
        Iw2DFillRect(CIwSVec2(borderPosition), CIwSVec2(borderSize));

        Iw2DSetColour(backgroundColor);
        Iw2DFillRect(CIwSVec2(innerBorderPosition), CIwSVec2(innerBorderSize));
    }

    Iw2DSetColour(textColor);
    
    const char *textPtr = text.c_str();

    Iw2DDrawString(textPtr, CIwSVec2(textPosition), CIwSVec2(textSize), horizontalAlignment, IW_2D_FONT_ALIGN_TOP);

    Iw2DSetColour(prevColor);

    if(prevFont != null)
    {
        Iw2DSetFont(prevFont);
    }
}
コード例 #2
0
void Sprite::Render()
{
	// set the values for interpolating translation
	if( interpolateTranslation == true )
	{
		// debug
		// printf("interpolating...\n");
		bool interpolateX = true;
		bool interpolateY = true;

		
		// increment position
		if( position != endingPosition )
		{			
			position += velocity;

			/* debug
			printf("position is %d, %d \n", position.x, position.y );
			printf("velocity is %d, %d \n", velocity.x, velocity.y );
			printf("acceleration is %d, %d \n", acceleration.x, acceleration.y );
			*/

			if( mode == DECELERATING )
			{
				if( interpolateX == true && (sqrt( pow( endingPosition.x-position.x, 2) + pow( endingPosition.y-position.y, 2)) < decelerationLimit ) )
				{				
					velocity.x += acceleration.x;
				}

				if( interpolateY == true && (sqrt( pow( endingPosition.x-position.x, 2) + pow( endingPosition.y-position.y, 2)) < decelerationLimit ) )
				{
					velocity.y += acceleration.y;
				}
			}
			else if( mode == ACCELERATING )
			{
				velocity += acceleration;
				/*
				if( sqrt( pow( endingPosition.x-position.x, 2) + pow( endingPosition.y-position.y, 2)) < accelerationLimit  )
				{
					// do nothing if limit is reached
				}
				else
				{
					velocity += acceleration;
				}*/
			}
		}

		// Clamp position if endingPosition reached
		if( velocity.x <= 0 )
		{
			if( position.x <= endingPosition.x ) // if final x position reached
			{
				position.x = endingPosition.x;
				interpolateX = false;
			}
		}
		else if( velocity.x > 0 )
		{
			if( position.x >= endingPosition.x )
			{
				position.x = endingPosition.x;
				interpolateX = false;
			}
		}

		if( velocity.y <= 0 )
		{
			if( position.y <= endingPosition.y ) // if final y position reached
			{
				position.y = endingPosition.y;
				interpolateY = false;
			}
		}
		else if( velocity.y > 0 )
		{
			if( position.y >= endingPosition.y )
			{
				position.y = endingPosition.y;
				interpolateY = false;
			}
		}

		// shuts off interpolation
		if( interpolateX == false && interpolateY == false )
		{
			interpolateTranslation = false;
			// debug
			// printf("done interpolating. \n");
		}
	}

	if( interpolateAlpha == true )
	{
		// do nothing if alpha limit reached
		currentAlpha += deltaAlpha;
		if( endingAlpha > startingAlpha )
		{
			if( currentAlpha >= endingAlpha )
			{
				currentAlpha = endingAlpha;
			}
		}
		else if( startingAlpha > endingAlpha )
		{
			if( currentAlpha <= endingAlpha )
			{
				currentAlpha = endingAlpha;
			}
		}

		externalColor = Iw2DGetColour();
		CIwColour color = { 255, 255, 255, currentAlpha };
		Iw2DSetColour( color );
	}


	if( playAnimation == true )
	{
		// Set the topleft coordinate based on Sprite's center position and size to be drawn
		topLeft.x = position.x - size.x / 2;
		topLeft.y = position.y - size.y / 2;

		// just in case uHeight was not set, it means it's a square
		if( uHeight == 0 )
			uHeight = uWidth;

		// set the region size to be drawn on the screen. uWidth and uHeight are # of pixels
		regionSize.x = uWidth;
		regionSize.y = uHeight;

		// Calculate the pixel offset for region to be drawn
		int16 totalPixelWidth = image->GetWidth(); // the pixel width of the original image
		int16 totalPixelHeight = image->GetHeight(); // the pixel height of the original image
		int16 totalColumns = totalPixelWidth / uWidth; // the number of columns of entire image based on uwidth
		int16 totalRows = totalPixelHeight / uHeight; // the number of rows of entire image based on uheight

		int16 columnNumber = frameCounter % totalColumns; // column number is f % c. This works. Where framecounter starts at zero. x/y offsets also start at zero
		int16 rowNumber = frameCounter / totalColumns; // row number is f / c. This also works. Where frameCounter starts at zero. x/y offsets also start at zero 

		regionOffset.x = columnNumber * uWidth;
		regionOffset.y = rowNumber * uHeight;

		// DRAW THE IMAGE
		if( repeatCount != 0 ) // if there's a repeat count set
		{
			if( loopCount < repeatCount )
			{
				Iw2DDrawImageRegion( image, topLeft, size, regionOffset, regionSize );
			}
		}
		else // if no repeat count, render and animate indefinitely
		{
			Iw2DDrawImageRegion( image, topLeft, size, regionOffset, regionSize );
		}

		// allows for slowing fps.
		if( fpsDelayTime == 0 ) // if a delay time was not set, the frame does not increment and sprite does not animate
		{
			// no animation
			frameCounter = 0;
		}
		else if( fpsDelayTime == 1 ) // if delay time is 1, that means it animates at 1 frame per iteration. 
		{
			frameCounter++; // this is incrementing what frame is being rendered

			// WRAP the frame counter. END OF ANIMATION LOOP
			if( totalFrames != 0 )
			{
				if( frameCounter == totalFrames )
				{
					frameCounter = 0;
					loopCount++;
				}
			}		
			else if( frameCounter == totalColumns * totalRows ) // if frame counter reaches the lastFrame+1, set equal to first frame; lastFrame+1 = (totalC * totalR) because frameCounter starts at Zero													   
			{
				frameCounter = 0;
				loopCount++;
			}
			
		}
		else if( fpsDelayCounter % fpsDelayTime == 0 && fpsDelayCounter != 0 ) // if the loop has been delayed long enough to equal the delay time, increment frame
		{
			frameCounter++; // this is incrementing what frame is being rendered
		
			// WRAP the frame counter. END OF ANIMATION LOOP
			if( totalFrames != 0 )
			{
				if( frameCounter == totalFrames )
				{
					frameCounter = 0;
					loopCount++;
				}
			}		
			else if( frameCounter == totalColumns * totalRows ) // if frame counter reaches the lastFrame+1, set equal to first frame; lastFrame+1 = (totalC * totalR) because frameCounter starts at Zero													   
			{
				frameCounter = 0;
				loopCount++;
			}

			// WRAP the delay counter to prevent overflow
			fpsDelayCounter = 0;

			// CLAMP the loop count to prevent overflow
			if( loopCount >= 15000 )
			{
				loopCount = 15000;
			}

		}

		// SKIP A FRAME
		fpsDelayCounter++; // this ONLY affects the 3rd condition of the above if-statement
	} // end of if playAnimation == true

	if( interpolateAlpha == true )
	{
		Iw2DSetColour( externalColor );
	}
}