3d game programming lab2-2d game example

14
3D Game Programming Lab2-2D game example

Upload: gerik

Post on 05-Jan-2016

69 views

Category:

Documents


0 download

DESCRIPTION

3D Game Programming Lab2-2D game example. Goal. Familiar with GLUT game process Load images I/O control Animation. Image examples. Read Image. RGBApixmap pic; pic.readBMPFile(“filename.bmp");. Draw image. In display() glRasterPos2i(x, y); pic.blend();. (w,h). (0,0). (x,y). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 3D Game Programming Lab2-2D game example

3D Game ProgrammingLab2-2D game example3D Game ProgrammingLab2-2D game example

Page 2: 3D Game Programming Lab2-2D game example

GoalGoal

Familiar with GLUT game processLoad imagesI/O controlAnimation

Page 3: 3D Game Programming Lab2-2D game example

Image examplesImage examples

stand walk jump sun1 sun2

flyup flydown cat1 cat2

Page 4: 3D Game Programming Lab2-2D game example

Read ImageRead Image

RGBApixmap pic;pic.readBMPFile(“filename.bmp");

Page 5: 3D Game Programming Lab2-2D game example

Draw imageDraw image

In display()

glRasterPos2i(x, y);pic.blend();

(x,y)(0,0)

(w,h)

Page 6: 3D Game Programming Lab2-2D game example

Change StateChange State

Stand Walk

Jump

RGBApixmap pic[3];

Pic[0].readBMPFile(“stand.bmp"); Pic[1].readBMPFile(“walk.bmp");

Pic[2].readBMPFile(“walk.bmp");

Page 7: 3D Game Programming Lab2-2D game example

Change StateChange Statevoid SpecialKeys(int key, int x, int y){

switch(key) {case GLUT_KEY_LEFT:

picX -= 5;if (whichPic==0)

whichPic=1;else

whichPic=0;

DirectState=1;break;

case GLUT_KEY_RIGHT:…

}}

* glutSpecialFunc Description

void display() {

// 改變圖片位置glRasterPos2i(PicX, PicY);

// 藉由縮放讓圖片改變方向if(DirectState==0) {

glPixelZoom(-1.0, 1.0);

// 切換不同狀態pic[whichPic].blend();

}

Page 8: 3D Game Programming Lab2-2D game example

設定透明色去除圖片背景設定透明色去除圖片背景cout<<"Reading sprite";

// 匯入角色圖片pic[0].readBMPFile("pokemon/stand.bmp"); cout<<'.'; pic[1].readBMPFile("pokemon/walk.bmp"); cout<<'.';pic[2].readBMPFile("pokemon/jump.bmp"); cout<<'.'<<endl;

// 選取要去掉的背景顏色 (232, 248, 248)for (int i=0; i<3; i++) pic[i].setChromaKey(232, 248, 248);

// 顏色 (232, 248, 248) 為左圖 箭頭指向的淡藍色背景

Page 9: 3D Game Programming Lab2-2D game example

去被圖製作範例去被圖製作範例首先找一張將背景透明的圖片 ( 檔案格式為 .gif

或 .png ) ,接著用 Photoshop 等繪圖軟體把該圖片與我們選的特定顏色之背景做結合,以下我們用淡藍色 (232, 248, 248) 舉例:

Page 10: 3D Game Programming Lab2-2D game example

Time FunctionTime Functionvoid glutTimerFunc(unsigned int msecs, void (*func)(int value), int value);

時間間隔 (milliseconds)

要呼叫的函式名稱

要傳入函式的變數

Page 11: 3D Game Programming Lab2-2D game example

Time FunctionTime Functionvoid jump(int i){ whichPic=2;

if (i<15) picY+=14; else picY-=14; if(i<30) {

i++;glutTimerFunc( 50, jump, i);

}else {whichPic=0;jumpState=0;picY=150;

} glutPostRedisplay();}

glutTimerFunc( A, B, C );(1)A = 時間間隔 (milliseconds)

(2)B = 要呼叫的Function(3)C = 要傳入的變數GLUT 每隔 A msecs 就會呼叫一次 B(C) 函式

以 jump function 為例:glutTimerFunc(50,jump,i); 表示每隔 50 單位時間,呼叫一次 jump (i)

Page 12: 3D Game Programming Lab2-2D game example

PracticePractice

Back and Forth

n steps

Page 13: 3D Game Programming Lab2-2D game example

ChallengeChallenge

Fly

W

H y = H – c(x - W)2

Page 14: 3D Game Programming Lab2-2D game example

Super ChallengeSuper Challenge

Fly Randomly

sin(x) or cos(x)

+ rand() % maxChange