140 likes | 328 Views
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).
E N D
Goal • Familiar with GLUT game process • Load images • I/O control • Animation
Read Image • RGBApixmappic; • pic.readBMPFile(“filename.bmp");
Draw image • In display() • glRasterPos2i(x, y); • pic.blend(); (w,h) (0,0) (x,y)
Change State RGBApixmappic[3]; Pic[2].readBMPFile(“walk.bmp"); Jump Stand Walk Pic[0].readBMPFile(“stand.bmp"); Pic[1].readBMPFile(“walk.bmp");
Change State void display() { //改變圖片位置 glRasterPos2i(PicX, PicY); //藉由縮放讓圖片改變方向 if(DirectState==0) { glPixelZoom(-1.0, 1.0); //切換不同狀態 pic[whichPic].blend(); } void 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
設定透明色去除圖片背景 • 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)為左圖 箭頭指向的淡藍色背景
去被圖製作範例 • 首先找一張將背景透明的圖片(檔案格式為 .gif 或 .png),接著用Photoshop等繪圖軟體把該圖片與我們選的特定顏色之背景做結合,以下我們用淡藍色(232, 248, 248)舉例:
Time Function • void glutTimerFunc(unsigned int msecs, void (*func)(int value), int value); 時間間隔 (milliseconds) 要呼叫的函式名稱 要傳入函式的變數
Time Function • glutTimerFunc( A, B, C ); • A = 時間間隔 (milliseconds) • B= 要呼叫的Function • C= 要傳入的變數 • GLUT每隔Amsecs 就會呼叫一次B(C)函式 • 以jump function為例: • glutTimerFunc(50,jump,i); • 表示每隔50單位時間,呼叫一次 jump (i) • void 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(); • }
Practice • Back and Forth n/2 steps n steps change DirectState n/2 steps
Challenge • Fly H y = H – c(x - W)2 W
Super Challenge • Fly Randomly sin(x) or cos(x) + rand() % maxChange