Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Image sequences of an animated sprite are usually contained in a single sprite sheet. In this case, in the data structure AnimData given in the
Image sequences of an animated sprite are usually contained in a single sprite sheet. In this case, in the data structure AnimData given in the textbook, array ImageFile images[] becomes a single variable ImageFile image. Modify the pseudocode (see code below) of AnimData and AnimatedSprite to make the code work for a single sprite sheet instead of an array of images. Assume that images in the sprite sheet are stored on one horizontal dimension. You can update the provided code.
class Sprite ImageFile image int drawOrder int x, y function Draw() // Draw the image at the correct (x,y) ... end end SortedList spriteList // When creating a new sprite... Sprite newSprite = specify image and desired x/y newSprite.drawOrder = set desired draw order value // Add to sorted list based on draw order value spriteList.Add(newSprite.drawOrder, newSprite) // When it's time to draw... foreach Sprite s in spriteList s.Draw() loop struct AnimFrameData // The index of the first frame of an animation int startFrame // The total number of frames for said animation int numFrames end struct AnimData // Array of images for all the animations ImageFile images[] // The frame data for all the different animations AnimFrameData frameInfo[] end class AnimatedSprite inherits Sprite // All of the animation data (includes ImageFiles and FrameData) AnimData animData // The particular animation that is active int animNum // The frame number of the active animation that's being displayed int frameNum // Amount of time the current frame has been displayed float frameTime // The FPS the animation is running at (24FPS by default). float animFPS = 24.0f function Initialize(AnimData myData, int startingAnimNum) function UpdateAnim(float deltaTime) function ChangeAnim(int num) end function AnimatedSprite.Initialize(AnimData myData, int startingAnimNum) animData = myData ChangeAnim(startingAnimNum) end function AnimatedSprite.ChangeAnim(int num) animNum = num // The active animation is now at frame 0 and 0.0f time frameNum = 0 animTime = 0.0f // Set active image, which is just the starting frame. int imageNum = animData.frameInfo[animNum].startFrame image = animData.images[imageNum] end function AnimatedSprite.UpdateAnim(float deltaTime) // Update how long the current frame has been displayed frameTime += deltaTime // This check determines if it's time to change to the next frame. if frameTime > (1 / animFPS) // The number of frames to increment is the integral result of // frameTime / (1 / animFPS), which is frameTime * animFPS frameNum += frameTime * animFPS // Check if we've advanced past the last frame, and must wrap. if frameNum >= animData.frameInfo[animNum].numFrames // The modulus (%) makes sure we wrap correctly. // (Eg. If numFrames == 10 and frameNum == 11, frameNum would // wrap to 11 % 10 = 1). frameNum = frameNum % animData.frameInfo[animNum].numFrames end // Update the active image. // (startFrame is relative to all the images, while frameNum is // relative to the first frame of this particular animation). int imageNum = animData.frameInfo[animNum].startFrame + frameNum image = animData.images[imageNum] // We use fmod (floating point modulus) for the same reason // as the % above. frameTime = fmod(frameTime, 1 / animFPS) end end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started