본문 바로가기
프로그래밍

이미지에 애니메이션 붙이기

by 건우아빠유리남편 2009. 7. 9.
반응형

지난번에 이미지 그리기에 성공했으니 이젠 움직이는 그림을 해볼까요..

 

그림파일이 필요하고.. 리소스 추가하는 법은 저번과 동일합니다.

 

그럼 애니메이션효과를 주기위해 필요한 클래스와 몇가지 설명을 하자면..

 

Game Class

게임자체의 정보를 가진다.

 

TimeSpan InactiveSleepTime

활성화되지 않았을 때의 sleep시간

bool IsActive

활성화되었는가 아닌가를 알려줌.

bool IsFixedTimeStep

정기적으로Update()Draw() call여부.

bool IsMouseVisible

마우스를 보이게 하는가 여부.

GameServiceContainer Services

서비스ContentManager생성에 이용.

TimeSpan TargetElapsedTime

정기적으로Update()Draw()call할 시간설정

void Exit()

application종료

void LoadGraphicsContent()

그래픽스 컨텐츠를 Load

void UnloadGraphicsContent()

그래픽스 컨텐츠를 Unload

void Update()

Update

void Draw()

Draw

 

ContentManager 클래스

리소스를 읽어 들일 때 이용한다.

ContentManager(IServiceProvider)

constructor

T Load<T>(String)

리소스를 읽어옴.

 

GraphicsDeviceManager클래스

그래픽스장치를 관리한다.

GraphicsDeviceManager(Game)

constructor

GraphicsDevice GraphicsDevice

그래픽스 장치

bool IsFullScreen

fullscreen여부

void ToggleFullScreen()

fullscreen 변경

 

GraphicsDevice 클래스

그래픽스장치의 정보를 가지고 있다.

void Clear(Color)

화면 클리어

 

TimeSpan구조체

시간간격을 가지고 있다.

TimeSpan(long)

constructor.  1/1000milisec 단위로 지정

 

SpriteBatch클래스

spritebatch의 정보를 가지고 있다.

SpriteBatch(GraphicsDevice)

Constructor

void Begine()

그리기 시작

void Begine(SpriteBlendMode)

그리기 시작. Blend모드 지정.

void End()

그리기 종료

void Draw(texture,drawArea,color)

texture : Texture2D
drawArea
화면영역:Rectangle
color
컬러채널(default : white) : Color

void Draw(texture,drawArea,sourceArea,color)

texture : Texture2D
drawArea
화면영역:Rectangle <그리는 곳>
sourceArea : Nullable<Rectangle>
color
컬러채널(default : white):Color

void Draw(texture,drawArea,sourceArea,color,
rotation,origin,effects,depth)

texture : Texture2D
drawArea
화면영역:Rectangle
sourceArea : Nullable<Rectangle>
color
컬러채널(default : white):Color
rotation
회전각도(라디안단위):float
origin
회전각도:Vector2
effects
반전효과(SpriteEffects로 갖는 정수)
:SpriteEffects
depth sprite
의 심도(01):float

void Draw(texture,pos,color)

texture:Texture2D
pos
위치:Vector2
color
컬러채널(default : white):Color

void Draw(texture,pos,sourceArea,color)

texture:Texture2D
pos
위치:Vector2
sourceArea : Nullable<Rectangle>
color
컬러채널(default : white):Color

void Draw(texture,pos,sourceArea,color,
rotation,origin,scale,effects,depth)

texture:Texture2D
pos
위치:Vector2
sourceArea:Nullable<Rectangle>
color
컬러채널(default : white):Color
rotation
회전각도(라디안단위):float
origin
회전원점:Vector2
scale
: float
effects
반전효과(SpriteEffects를 갖는 정수)
:SpriteEffects
depth sprite
의 심도(01):float

void Draw(texture,pos,sourceArea,color,
rotation,origin,scale,effects,depth)

texture:Texture2D
pos
위치:Vector2
sourceArea:Nullable<Rectangle>
color
컬러채널(default : white):Color
rotation
회전각도(라디안 단위):float
origin
회전원점:Vector2
scale:Vector2
effects
반전효과(SpriteEffects를 갖는 정점)
:SpriteEffects
depth sprite
의 심도(01):float

 

SpriteBlendMode 열거체

sprite에 지정하는 blend효과를 표시한다.

Additive

가산

AlphaBlend

알파블렌드

None

없음

 

SpriteEffects 열거체

sprite에 지정하는 반전효과를 표시한다.

FlipHorizontally

수평반전

FlipVertically

수직반전

None

없음

 

소스코드의 편집

Game1.cs를 편집한다.

 

Game1.cs

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;

 

//애니메이션을 표시한다.

namespace XnaImage {

    public class Game1 : Microsoft.Xna.Framework.Game {

        //시스템

        private GraphicsDeviceManager graphics; //그래픽스

        private ContentManager        content; //컨텐츠

        private SpriteBatch           spBatch; //spriteBatch

        private Texture2D             texture; //텍스쳐

        private int                   width;   //

        private int                   height;  //높이

 

        //위치와 속도

        private int x = 100; //X좌표

        private int y = 100; //Y좌표

        private int dx =  1; //X방향

        private int dy =  1; //Y방향

 

        //constructor

        public Game1() {

            //그래픽스와 컨텐츠 지정

            graphics = new GraphicsDeviceManager(this);

            content  = new ContentManager(Services);

 

            //Update() 1초에 20 call하도록 설정한다.

            IsFixedTimeStep = true;

            TargetElapsedTime = new TimeSpan(500000); //50밀리 초당 1

        }

 

        //초기화

        protected override void Initialize() {

            width  = graphics.GraphicsDevice.Viewport.Width; //

            height = graphics.GraphicsDevice.Viewport.Height; //높이

            base.Initialize();

        }

 

        //그래픽스 컨텐트를 불러온다.

        protected override void LoadGraphicsContent(bool loadAllContent) {

            if (loadAllContent) {       

                spBatch = new SpriteBatch(graphics.GraphicsDevice);

                texture = content.Load<Texture2D>("xna"); //Assent Name을 지정한다.

            }

        }

 

        //그래픽스 컨텐트를 놓아준다.

        protected override void UnloadGraphicsContent(bool unloadAllContent) {

            if (unloadAllContent == true) {

                content.Unload();

            }

        }

 

        //갱신

        protected override void Update(GameTime gameTime) {

            //이동량(매초 400픽셀)

            int move = (int)(400 *

                (float)gameTime.ElapsedRealTime.TotalMilliseconds/1000.0f);

 

            //이동

            x += dx*move;

            y += dy*move;

            if (x < 0 || width  < x) dx = -dx;

            if (y < 0 || height < y) dy = -dy;

 

            //base update

            base.Update(gameTime);

        }

 

        //그리기

        protected override void Draw(GameTime gameTime) {

            //배경색 그리기

            graphics.GraphicsDevice.Clear(Color.White);

 

            //텍스쳐 그리기

            spBatch.Begin();

            Vector2 pos = new Vector2(x-90,y-90); //좌표지정

            spBatch.Draw(texture,pos,Color.White);

            spBatch.End();

 

            //base그리기

            base.Draw(gameTime);

        }

    }

}

 

하시면 이미지가 상하좌우 벽에 부딪히며 움직이는 모습을 보실 수 있습니다.


반응형

'프로그래밍' 카테고리의 다른 글

마우스 이벤트 처리하기  (0) 2009.07.09
키 이벤트 제어하기  (0) 2009.07.09
이미지 파일 읽어오기  (0) 2009.07.09
헬로 월드! in XNA  (0) 2009.07.09
XNA의 컨텐츠와 파이프라인  (0) 2009.07.09

댓글