Top Banner
Game Programming I Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1 Timing, Direct Input, and Animation and Sprites 7 th Week, 2007 Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1 Downloading Sample Program
23

Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Mar 14, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Game Programming I

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Timing, Direct Input, and Animation and Sprites

7th Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Downloading Sample Program

Page 2: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Microsoft Visual Studio .Net

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

File New Project…

Visual C++ Win32Win32 Project

Page 3: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Application Settings

Empty project

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

‘Chap05’ Solution

Page 4: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Graphics Stats Demo

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (1)

Copying the source code

Page 5: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (2)

Project Add Existing Item

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (3)

Page 6: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Project Properties

Configuration Properties Linker InputAdditional Dependencies: d3d9.lib d3dx9d.lib d3d9.lib d3dx9d.lib dxguid.libdxguid.lib dxerr9.lib dinput8.libdxerr9.lib dinput8.lib

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Result – Graphics Stats Demo

Page 7: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sample Code: GfxStatsDemo

d3dApp.h

d3dApp.cpp

d3dUtil.h

GfxStatsDemo.cpp

: useful utility code

: the core Direct3D application class code

: our application code

derivation

GfxStats.h

GfxStats.cpp

: calculating graphics stats

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Graphics Stats Demo (1)

Calculating and drawing the FPS and milliseconds per frame

#class GfxStats{public:

GfxStats();~GfxStats();

void onLostDevice();void onResetDevice();

void addVertices(DWORD n);void subVertices(DWORD n);void addTriangles(DWORD n);void subTriangles(DWORD n);

void setTriCount(DWORD n);void setVertexCount(DWORD n);

void update(float dt);void display();

Page 8: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Graphics Stats Demo (2)

Calculating and drawing the FPS and milliseconds per frame (cont’)

private:// Prevent copyingGfxStats(const GfxStats& rhs);GfxStats& operator=(const GfxStats& rhs);

private:ID3DXFont* mFont;float mFPS;float mMilliSecPerFrame;DWORD mNumTris;DWORD mNumVertices;

};

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Graphics Stats Demo (3)

Counting the number of frames processed over some specified time period

void GfxStats::update(float dt){

static float numFrames = 0.0f;static float timeElapsed = 0.0f;

numFrames += 1.0f;

timeElapsed += dt;

if( timeElapsed >= 1.0f ){

mFPS = numFrames;

mMilliSecPerFrame = 1000.0f / mFPS;

timeElapsed = 0.0f;numFrames = 0.0f;

}}

Page 9: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Graphics Stats Demo (4)

Outputting the informationvoid GfxStats::display(){

static char buffer[256];

sprintf(buffer, "Frames Per Second = %.2f\n""Milliseconds Per Frame = %.4f\n""Triangle Count = %d\n""Vertex Count = %d", mFPS, mMilliSecPerFrame,

mNumTris, mNumVertices);

RECT R = {5, 5, 0, 0};HR(mFont->DrawText(0, buffer, -1, &R, DT_NOCLIP,

D3DCOLOR_XRGB(0,0,0)));}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Graphics Stats Demo (5)

Keeping track of the overall scene triangle and vertex countvoid GfxStats::addVertices(DWORD n) { mNumVertices += n; }

void GfxStats::subVertices(DWORD n) { mNumVertices -= n; }

void GfxStats::addTriangles(DWORD n) { mNumTris += n; }

void GfxStats::subTriangles(DWORD n) { mNumTris -= n; }

void GfxStats::setTriCount(DWORD n) { mNumTris = n; }

void GfxStats::setVertexCount(DWORD n){ mNumVertices = n; }

Page 10: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

The Sprite Demo

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (1)

Copying the source code

Page 11: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (2)

Project Add Existing Item

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (3)

Page 12: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Result – The Sprite Demo

FF: Full-Screen modeEscEsc: Window mode

AA, DD: to rotate the shipWW, SS: to accelerate or decelerate the shipSpacebarSpacebar: to fire bullets

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sample Code: SpriteDemo

d3dApp.h

d3dApp.cpp

d3dUtil.h

SpriteDemo.cpp

: useful utility code

: the core Direct3D application class code

: our application code

derivation

GfxStats.h

DirectInput.h

: calculating graphics stats

: working with keyboard and mouse

GfxStats.cpp

DirectInput.cpp

Page 13: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Bullet Structure

Containing the properties of a bullet

pos: position of a bullet

rotation: rotation angle (the direction the bullet is traveling)life: the amount of time the bullet has existed

struct BulletInfo{

D3DXVECTOR3 pos;float rotation;float life;

};

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sprite Demo Class (1)class SpriteDemo : public D3DApp{public:

SpriteDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);~SpriteDemo();

bool checkDeviceCaps();void onLostDevice();void onResetDevice();void updateScene(float dt);void drawScene();

// Helper functions.void updateShip(float dt);void updateBullets(float dt);void drawBkgd();void drawShip();void drawBullets();

Page 14: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sprite Demo Class (2)private:

GfxStats* mGfxStats;

ID3DXSprite* mSprite;

IDirect3DTexture9* mBkgdTex;D3DXVECTOR3 mBkgdCenter;

IDirect3DTexture9* mShipTex;D3DXVECTOR3 mShipCenter;D3DXVECTOR3 mShipPos;float mShipSpeed;float mShipRotation;

IDirect3DTexture9* mBulletTex;D3DXVECTOR3 mBulletCenter;std::list<BulletInfo> mBulletList;

const float BULLET_SPEED;const float MAX_SHIP_SPEED;const float SHIP_ACCEL;const float SHIP_DRAG;

};

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

“SpriteDemo.cpp”

Page 15: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Alpha Channel

Fourth channel for alpha blendingWeights that specify how much color to use from the texture image (source color) and from the back buffer pixels (destination color)

White: 100% source color

Black: 100% destination color

The sprites and their alpha channels(32-bit BMP file)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Page Flipping Animation

Page 16: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (1)

Copying the source code

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (2)

Project Add Existing Item

Page 17: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (3)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Result – Page Flipping Animation

Page 18: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Page Flipping Animation (1)

Generating the animation as a sequence of images, and then displaying the frames in rapid succession

Useful for effects like explosions or fire

Each frame of animation is stored in the texture (including alpha info)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Page Flipping Animation (2)

Initialization:

Update:

// The current frame of animation to render.mCurrFrame = 0;

// Keep track of how much time has accumulated.static float timeAccum = 0.0f;timeAccum += dt;

// Play animation at 30 frames per second.if( timeAccum >= 1.0f / 30.0f ){

// After 1/30 seconds has passed, move on to the next frame.++mCurrFrame;timeAccum = 0.0f;

// This animation has have 30 frames indexed from // 0, 1, ..., 29, so start back at the beginning if // we go over.if(mCurrFrame > 29)

mCurrFrame = 0;}

Page 19: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Page Flipping Animation (3)

Rendering:// Compute rectangle on texture atlas of the current frame// we want to use.int i = mCurrFrame / 6; // Rowint j = mCurrFrame % 6; // ColumnRECT R = {j*64, i*64, (j+1)*64, (i+1)*64};

// Turn on alpha blending.HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true));

// Don't move explosion--set identity matrix.D3DXMATRIX M;D3DXMatrixIdentity(&M);HR(mSprite->SetTransform(&M));HR(mSprite->Draw(mFrames, &R, &mSpriteCenter, 0,

D3DCOLOR_XRGB(255, 255, 255)));HR(mSprite->End());

// Turn off alpha blending.HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false));

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

“PageFlipDemo.cpp”

Page 20: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Exercise – Assignment #1

2D Shooting GameGoal: shooting down your enemies

RaidenX(http://www.crazymonkeygames.com/RaidenX.html)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Defaults

Use different sprites (+15 points)Background, ship, and bullet

Fly enemies (+5 points)One more kind of enemy (each +5 points)

Shoot the enemies (+10 points)Collision detection

Add explosion by page flipping animation (+10 points)

Display scores (+10 points)

Page 21: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Options

Enemies may shoot bullets at your ship (+10 points)

Different kind of bullets or items (each +5 points)

Level design (+20 points)

Etc. (+α points)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 – Assignment #1 (1)

2차원슈팅게임목표: 적들을피하고총으로맞춤

RaidenX(http://www.crazymonkeygames.com/RaidenX.html)

Page 22: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 – Assignment #1 (2)

필수사항다른종류의 sprite 사용 (+15점): 배경, 비행기, 총알

적기 (+5 점): 여러종류의적 (각 +5점)

적기격추 (+10점): 충돌체크필수

격추된후폭파 (+10점): 교과서예제 page flipping animation 이용

점수계산후디스플레이 (+10점)

옵션사항적기가총을쏨 (+10점)

아이템, 아이템획득후총알종류가달라짐 (각 +5점)

게임레벨디자인 (+20점)

기타(+α 점)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 – Assignment #1 (3)

제출방법2007. 5. 3 수업시간전까지

수업시간에과제발표예정

프로그램 (소스코드만압축, Debug 폴더제외)

+ 스냅샷이미지 (여러장, 최소 2~3장)

+ shelf-check table (뒷장참조)

3개를압축하여 “학번.zip”을웹에 upload 할것

Page 23: Timing, Direct Input, and Animation and Spritesgraphics.hallym.ac.kr/teach/2007/gp1/src/12prac.pdf · 2008-09-16 · 다른종류의sprite 사용(+15점): 배경, 비행기, 총알

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Self-Check Table

스스로채점후제출하시오. (O, Δ, X 표기)

학번학번: : 이름이름: :

Options (+α점)

1. Enemy가총을쏘는가? (10점)

2. 다른종류의아이템발생, 다른종류의 bullet (각 5점)

3. 게임레벨디자인 (20점)

4. 기타 (+α점) :

2D Shooting Game (50점)

1. 새로운 Sprite를만들어사용했는가? (15점)

2. Enemy가있는가? (각 5점)

3. Enemy를격추시킬수있는가? (10점)

4. 격추후 Page Flipping Animation 폭파가능한가? (10점)

5. 점수계산과디스플레이가되는가? (10점)