Animated image sequences, fonts and image transforms

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

4.1. G RAPHICS II Animated image sequences, fonts and image transforms.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Section 3-1 to 3-2, 3-5 Drawing Lines Some of the material in these slides may have been adapted from university of Virginia, MIT, and Åbo Akademi University.
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Macromedia Director 8 Foundation Level Course. What is Director? Director is a challenging program for creating animation and multimedia productions for.
3.1. G RAPHICS I The use of images within games. Reflections and advice on the games proposed in the Week 2 Hand-in.
Week 1 - Friday.  What did we talk about last time?  C#  SharpDX.
Working with Graphics. Objectives Understand bitmap and vector graphics Place a graphic into a frame Work with the content indicator Transform frame contents.
Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
3.2. G RAPHICS I Alpha blending within games. An exploration of the use of alpha blending within games.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
2.1. T HE G AME L OOP Central game update and render processes.
PowerPoint Practice Exercise 1 1.Save this file in your AV-TECH Folder as PowerPoint Practice Exercise 1. 2.Open this file in PowerPoint. 3.Edit each slide.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
2.3. A RCHITECTURAL D ESIGN I Example approach for game screen management.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
XNA ● Proprietary Microsoft framework ● C#. Interface.
Review Expressions and operators Iteration – while-loop – for-loop.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
What is Binary Code? Computers use a special code of their own to express the digital information they process. It's called the binary code because it.
2D drawing basics For CSE 3902 By: Matt Boggus. Overview 2D coordinate systems Raster images Sprite drawing in XNA.
Simple Sprite Sheets with Inkscape & Gimp David Cline.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Main characteristics of Vector graphics  Vector graphics provide an elegant way of constructing digital images (diagrams, technical illustration and.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
CDM105 Session 8 Macromedia FLASH MX 2004 Part 1 : The Interface, Drawing graphics, Symbols, Layers and Frame-by-frame animation 1 CDM105 Session 8.
Lecture 4 Using SpriteFonts
Graphical Output Basic Images.
Animation Frame Animation.
Week 2 - Monday CS361.
Using and Creating Sprites
2D drawing basics For CSE 3902 By: Matt Boggus.
Scientific Visualization V106.04
Introduction to Microsoft publisher
Tools and techniques.
Review.
Flash Interface, Commands and Functions
“Animation Through the Ages”
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Java Programming: Guided Learning with Early Objects
ФІЗИКА Кількість завдань - 34 Час на виконання – 180 хв.
Alpha blending within games
Barb Ericson Georgia Institute of Technology April 2006
More programming with "Processing"
Multidimensional Arrays and Image Manipulation
Game Loop Update & Draw.
2.3. Architectural Design I
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Multidimensional Arrays
OpenGL program.
Development of a particle system
PowerPoint Practice Exercise
Presentation transcript:

Animated image sequences, fonts and image transforms 4.1. Graphics II Animated image sequences, fonts and image transforms

Animations Animated image sequences; playback speed and image source

Image Sequences An image sequence is simply a series of images, displayed one after the other. If the playback is fast a series of progressive images will appear as continuous, unbroken animation. Typically, how many frame/s are needed to give the appearance of unbroken animation? A – 50 frame/s B – 30 frame/s C – 20 frame/s D – 10 frame/s E – 5 frame/s

Image Sequences ● TV provides either 25 or 30 frame/s (PAL = 30) ● Consider the set of 6 images: ○ At 2 frame/s it is not smooth ○ At 10 frame/s it is reasonably smooth ● A typical animated cartoon run at 12 frame/s

Sequence Source Images to be played back as a sequence may reside within a number of different file structures, including: ● A series of files (1 image per file) ● A continuous image strip (1 file) ● An image sheet (1 file) Aside: For best performance, all images should be stored in an image sheet, with all animations using that sheet rendered together.

Image Sequence Playback Animating a sequence of images

Image sequence playback Playback of an image sequence can be integrated into the update/draw loop as follows: During the update phase determine when the next image in the sequence should be selected to provide the target number of frames/second During the draw phase, render the current selected image Aside: The following example is based on the ImageAssetSequence within the Java code repository

Image sequence playback (example implementation) Assuming the images are stored as an array (or an array of rectangles into a single image sheet) the following parameters can be used to offer different playback options: playCount- number of times to play the animation (-1 = loop forever, 0 = pause) homeFrame – frame to display when not animating currentFrame – current frame to display animationPeriodms – number of ms which a single playback should take animationStartTime – start time of the animation (if playback has commenced) private BufferedImage[] images; private int playCount; private int homeFrame; private int currentFrame; private long animationPeriodms; private long animationStartTime; For example, an exploding barrel animation might be paused on the homeFrame (say first frame) until playback is started

The update() method determines the current frame that should be displayed in the animation. In turn the draw() method simply draws the current frame. public void update() { determineCurrentFrame(); } public void draw(Graphics2D graphics2D, int drawX, int drawY) { graphics2D.drawImage( images[currentFrame], drawX, drawY, null);

private int determineCurrentFrame() { long currentTime = GetCurrentTime(); if (animinationStartTime == -1) animinationStartTime = currentTime; if (playCount > 0) { if (currentTime - animinationStartTime > (long) playCount * animationPeriodms) { playCount = 0; } if (playCount == 0) { currentFrame = homeFrame; } else { long timeIntoAniminationPeriod = (currentTime – animinationStartTime) % animationPeriodms; currentFrame = (int) ( (timeIntoAniminationPeriod * (long)images.length ) / animationPeriodms ); return currentFrame; A check is made to see if the animation has completed, in which case the playCount is reset to zero. Based on the current time, target animation period and playCount value the current frame is calculated and returned. The start time will be reset to -1 anytime the play count is changed. Aside: This is a slightly different version of the ImageAssetSequence implementation – the later ensures that the end frame of the animation is always drawn at least once.

In-Game Fonts Displaying textual information within games

The expense of drawing a font… Early fonts were bitmap based. Most modern fonts (e.g. TrueType) are outline based, providing a set of line and curve equations describing the shape of each ‘glyph’ (character). When drawing a font, the outline is rendered and then filled by an ink colour. Generating text by doing this 50-60 times per second is needlessly expensive. How can we improve performance? Render text once into a bitmap and use bitmap until text changes Revert to using a bitmap font

In-game fonts (using Java) Load an image asset sequence holding the bitmap font characters. Create a base TextElement, holding the image sequence and a corresponding text-to- image mapping. Use the base TextElement to create text strings that can be drawn. TextElement guiText = new TextElement(this, (ImageAssetSequence) assetManager.retrieveAsset( "GUIFont" ), 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ“ + "abcdefghijklmnopqrstuvwxyz" + "1234567890`!\"$%^&*()-+={}[];:'@#~,.<>/\\? ", ""); TextElement someText = guiText.getMatchingElement(“Hello!"); someText.draw(graphics2D, x, y);

In-game fonts (using XNA) Specify and add a SpriteFont content asset (the SpriteFont converts any installed TrueType font into an XNB sprite sheet) Load in the SpriteFont within the game. Use the SpriteBatch. DrawString() method to render text. <Asset Type="Graphics:FontDescription"> <FontName>Courier New</FontName> <Size>10</Size> <Spacing>0</Spacing> <UseKerning>true</UseKerning> <Style>Regular</Style> SpriteFont spriteFont; SpriteBatch spriteBatch; spriteFont = content.Load<SpriteFont>("SpriteFont"); spriteBatch.Begin(); spriteBatch.DrawString( spriteFont, “Hello! ", position, Color.White); spriteBatch.End(); Aside: For more information on importing fonts, see: http://blogs.msdn.com/shawnhar/archive/2007/04/26/bitmap-fonts-in-xna.aspx

Exercise: Using digit arrays Assume you have a series of images representing the digits 0 to 9 stored in an array Develop an approach that will take a positive integer score and graphically illustrate it using the image sequence. ImageAsset[] digitImage = new ImageAsset[10]; Start 1 min 2 mins Finished 3 mins 30 sec 6 mins 9 mins 10 mins 8 mins 7 mins 5 mins 4 mins

Exercise: Using digit arrays By using the modulus (%) operator the last digit in the score can be extracted and printed. Using integer division, the last digit can then be removed and the process iterated until no more digits remain. void drawScore( int targetScore ) { int scoreFragment = targetScore; do { int digit = scoreFragment % 10; digitImages[digit].Draw( ... ) scoreFragment = scoreFragment / 10; } while(scoreFragment > 9 ); } Aside: When drawing the images, it will be necessary to ensure the draw location of each image is determined. Additionally, the score will need to be drawn right-to-left.

Image Manipulation Basic forms of image transform

Consider game applicability To do: Consider game applicability Basic forms of image transform A number of affine image transforms can be usefully employed within games, including: Resizing Rotating Fadding Flipping Depth of view effects Object rotation Appear / disappear effects Left / right / up / down sprite reuse

Image Transforms (Resizing) For Java use: newWidth newHeight drawX, drawY newDrawX, newDrawY drawResizedImage( Graphics2D graphics2D, BufferedImage image, int drawX, int drawY, double scaleX, double scaleY ) { int newWidth = (int) (image.getWidth() * scaleX ); int newHeight = (int) (image.getHeight() * scaleY ); // Optional… if desired int newDrawX = drawX + image.getWidth()/2 - newWidth/2; int newDrawY = drawY + image.getHeight()/2 - newHeight/2; graphics2D.drawImage( image, newDrawX, newDrawY, newWidth, newHeight, null ); } For XNA use: The source texture will be scaled to fit the destination rectangle. SpriteBatch.Draw( Texture2D texture, Rectangle destinationRectangle, Color color )

Image Transforms (Rotation) For Java use: drawRotatedImage( Graphics2D graphics2D, BufferedImage image, int drawX, int drawY, double rotationDeg ) { AffineTransform origAT = graphics2D.getTransform(); AffineTransform rotation = new AffineTransform(); rotation.rotate( Math.toRadians(rotationDeg), drawX + image.getWidth()/2, drawY + image.getHeight()/2 ); graphics2D.setTransform( rotation ); graphics2D.drawImage( image, drawX, drawY, null ); graphics2D.setTransform(origAT); } drawX, drawY For XNA use: SpriteBatch.Draw( Texture2D texture, Rectangle destinationRectangle, Nullable<Rectangle> sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth ) The source texture will be rotated around the specified origin

Image Transforms (Fading) drawX, drawY Image Transforms (Fading) For Java use: drawFadedImage( Graphics2D graphics2D, BufferedImage image, int drawX, int drawY, float alpha ) { Composite origComposite = graphics2D.getComposite(); graphics2D.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha) ); graphics2d.drawImage( image, drawX, drawY, null); graphics2D.setComposite( origComposite ); } drawX, drawY For XNA use: Specify a colour with an alpha component < 256, e.g. for 50% fading new Color( ???, ???, ???, 128 ); SpriteBatch.Draw( Texture2D texture, Rectangle destinationRectangle, Color color )

Image Transforms (Flipping) For Java use: drawX, drawY drawHorizFlip(Graphics2D graphics2D, BufferedImage image, int drawX, int drawY ) { int width = image.getWidth(); int height = image.getHeight(); graphics2d.drawImage( image, drawX, drawY+height, drawX+width, drawY, 0, 0, width, height, null); } drawImage( Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver obs) For XNA use: SpriteBatch.Draw( Texture2D texture, Rectangle destinationRectangle, Nullable<Rectangle> sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth ) SpriteEffects includes FlipVertically and FlipHorizontally

To do: Summary Today we explored: The basics behind image sequence playback How in-game fonts can be efficiently rendered Some useful image transforms To do: Continue to explore image repository and select graphics Think about the types of image animation/transform and font usage in your game. Write code snippets to load / display and animate / transform images