void Iw2DSprite::Render(CIwSVec2 pPosition) { if(m_Angle > 0 || m_Angle < 0) { CIwMat2D RotationMatrix; RotationMatrix.SetRot(m_Angle, CIwVec2(pPosition.x + m_Center.x , pPosition.y + m_Center.y) ); Iw2DSetTransformMatrix(RotationMatrix); } if( m_Flip.x > 0 && m_Flip.y > 0) Iw2DSetImageTransform(IW_2D_IMAGE_TRANSFORM_ROT180); else if( m_Flip.x > 0 && m_Flip.y <= 0) Iw2DSetImageTransform(IW_2D_IMAGE_TRANSFORM_FLIP_X); else if( m_Flip.y > 0 && m_Flip.x <= 0) Iw2DSetImageTransform(IW_2D_IMAGE_TRANSFORM_FLIP_Y); Iw2DDrawImageRegion(m_SpriteSheet, pPosition, m_FrameSize, m_DrawRect, m_FrameSize); if(m_Angle > 0 || m_Angle < 0) Iw2DSetTransformMatrix(CIwMat2D::g_Identity); if( m_Flip.x > 0 || m_Flip.y > 0) Iw2DSetImageTransform(IW_2D_IMAGE_TRANSFORM_NONE); }
/** * Draws the enemy image */ void MeleeEnemy::draw() { if (mDrawDirection == 1) { Iw2DSetImageTransform(IW_2D_IMAGE_TRANSFORM_FLIP_X); } CIwSVec2 screenCentre = CIwSVec2((int16)Iw2DGetSurfaceWidth() >> 1, (int16)Iw2DGetSurfaceHeight() >> 1); b2Transform t = mBody->GetTransform(); CIwSVec2 pos = screenCentre + (CIwSVec2(int16(t.p.x*8), -int16(t.p.y*8))); float normalRegionHeight = (52.0f/100.0f) * ((mHealth/mMaxHealth)*100.0f); float hurtRegionHeight = 52.0f - normalRegionHeight; CIwColour col; col.Set(255, 50, 50); Iw2DSetColour(col); Iw2DDrawImageRegion(mImg, pos - CIwSVec2((mFrameWidth/1.5)*mSpriteScale, ((52/2)*mSpriteScale)), CIwSVec2(mFrameWidth*1.5, hurtRegionHeight*mSpriteScale), CIwSVec2(mFrame, mVerFrame), CIwSVec2(mFrameWidth, hurtRegionHeight)); col.Set(255, 255, 255); Iw2DSetColour(col); Iw2DDrawImageRegion(mImg, pos - CIwSVec2((mFrameWidth/1.5)*mSpriteScale, (((52/2)*mSpriteScale))-hurtRegionHeight), CIwSVec2(mFrameWidth*1.5, normalRegionHeight*mSpriteScale), CIwSVec2(mFrame, hurtRegionHeight+mVerFrame), CIwSVec2(mFrameWidth, normalRegionHeight)); if(mFrameDelay <= 0) { mFrame += (mFrameWidth); if (mFrame > (mFrameCount*mFrameWidth)) { //setIdle(); mFrame = 0; if(punching) { setIdle(); mPunchTimer = 10.0f; } } mFrameDelay = 5; } mFrameDelay--; Iw2DSetImageTransform(IW_2D_IMAGE_TRANSFORM_NONE); }
void Anima::Render() { if(timeElapsed>_aniSpeed) { _frameIndex++; if(_frameIndex>=_frameCount) _frameIndex=0; timeElapsed=0; } Iw2DDrawImageRegion(_image,_pos,_framePos[_frameIndex],_frameSize); //Iw2DDrawImage(_image, _pos); }
void TileSet::Render(int index,CIwSVec2 topLeft,int rotation) { int ind=index-m_firstGid; if(ind<0) return; int index_X=ind%_tilesPerRow; int index_Y=ind/_tilesPerRow; CIwSVec2 pos = CIwSVec2(index_X * _tilewidth,index_Y * _tileheight); if(rotation!=0) { iwangle angle =90*rotation;//90 degrees CIwSVec2 centre=CIwSVec2(iwsfixed(topLeft.x+(float)_tilewidth/2.0f),iwsfixed(topLeft.y+(float)_tileheight/2.0f)); CIwMat2D rot; rot.SetRot(IW_ANGLE_FROM_DEGREES(angle), CIwVec2(centre)); Iw2DSetTransformMatrix(rot); } Iw2DDrawImageRegion(_image, topLeft, pos, _tileSize); Iw2DSetTransformMatrix(CIwMat2D::g_Identity); }
/** * Draws and animates the effect */ void Effect::draw() { CIwSVec2 pos = CIwSVec2(mPos.x-((mImage->GetWidth()/mFrameCount)/2)*mScale, mPos.y-(mImage->GetHeight()/2)*mScale); Iw2DDrawImageRegion(mImage, pos, CIwSVec2((mImage->GetWidth()/mFrameCount)*mScale, mImage->GetHeight()), CIwSVec2(mCurFrame*(mImage->GetWidth()/mFrameCount), 0), CIwSVec2(mImage->GetWidth()/mFrameCount, mImage->GetHeight()) ); }
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 ); } }
int main() { // Initialise the 2D graphics system Iw2DInit(); // Create an image from a PNG file CIw2DImage* image = Iw2DCreateImage("textures/Tiles.png"); //Avatar bits AnimationData testData; testData.animationName = "Up"; testData.animationPosition = 0; testData.frameHeight = 64; testData.frameWidth = 32; testData.numberOfFrames = 4; AnimationData testData2; testData2.animationName = "Right"; testData2.animationPosition = 64; testData2.frameHeight = 64; testData2.frameWidth = 32; testData2.numberOfFrames = 4; Animation* animation = new Animation("textures/AvatarTest.png",testData); Animation* animation2 = new Animation("textures/AvatarTest.png",testData2); s3eFile* file = s3eFileOpen("tilemaps/tilemapdemo.json", "rb"); int len = s3eFileGetSize(file); char* rawTileJSON = new char[len]; if (file != NULL) { if (s3eFileRead(rawTileJSON, len, 1, file) != 1) { s3eFileGetError(); s3eDebugOutputString(s3eFileGetErrorString()); } s3eFileClose(file); } else { s3eFileGetError(); s3eDebugOutputString(s3eFileGetErrorString()); } cJSON *root = cJSON_Parse(rawTileJSON); int gridHeight = cJSON_GetObjectItem(root,"height")->valueint; int gridWidth = cJSON_GetObjectItem(root,"width")->valueint; int tileWidth = cJSON_GetObjectItem(root,"tileheight")->valueint; int tileHeight = cJSON_GetObjectItem(root,"tilewidth")->valueint; cJSON *layers = cJSON_GetObjectItem(root,"layers"); cJSON *tileData; int i; for (i = 0; i<cJSON_GetArraySize(layers); i++) { cJSON *layer = cJSON_GetArrayItem(layers,i); tileData = cJSON_GetObjectItem(layer,"data"); } // Loop forever, until the user or the OS performs some action to quit the app while (!s3eDeviceCheckQuitRequest()) { // Clear the drawing surface Iw2DSurfaceClear(0xff000000); int x,y,tileIndex,tileType; for( y = 0; y < gridHeight; y++) { for(x = 0; x < gridWidth; x++) { tileIndex = (y * gridWidth) + x; tileType = cJSON_GetArrayItem(tileData,tileIndex)->valueint -1; // Draw an image Iw2DDrawImageRegion(image, CIwFVec2(x*tileWidth,y*tileHeight),CIwFVec2(tileWidth,tileHeight),CIwFVec2(tileType*tileWidth,0),CIwFVec2(tileWidth,tileHeight)); } } animation->render(50,50); animation2->render(150,200); // Show the drawing surface Iw2DSurfaceShow(); // Yield to the OS s3eDeviceYield(0); } cJSON_Delete(root); // Clean-up delete rawTileJSON; delete image; delete animation; delete animation2; Iw2DTerminate(); return 0; }