1 / 14

3D Game Programming Lab2-2D game example

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).

gerik
Download Presentation

3D Game Programming Lab2-2D game example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3D Game ProgrammingLab2-2D game example

  2. Goal • Familiar with GLUT game process • Load images • I/O control • Animation

  3. Image examples

  4. Read Image • RGBApixmappic; • pic.readBMPFile(“filename.bmp");

  5. Draw image • In display() • glRasterPos2i(x, y); • pic.blend(); (w,h) (0,0) (x,y)

  6. Change State RGBApixmappic[3]; Pic[2].readBMPFile(“walk.bmp"); Jump Stand Walk Pic[0].readBMPFile(“stand.bmp"); Pic[1].readBMPFile(“walk.bmp");

  7. 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

  8. 設定透明色去除圖片背景 • 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)為左圖 箭頭指向的淡藍色背景

  9. 去被圖製作範例 • 首先找一張將背景透明的圖片(檔案格式為 .gif 或 .png),接著用Photoshop等繪圖軟體把該圖片與我們選的特定顏色之背景做結合,以下我們用淡藍色(232, 248, 248)舉例:

  10. Time Function • void glutTimerFunc(unsigned int msecs, void (*func)(int value), int value); 時間間隔 (milliseconds) 要呼叫的函式名稱 要傳入函式的變數

  11. 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(); • }

  12. Practice • Back and Forth n/2 steps n steps change DirectState n/2 steps

  13. Challenge • Fly H y = H – c(x - W)2 W

  14. Super Challenge • Fly Randomly sin(x) or cos(x) + rand() % maxChange

More Related