⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blocks.cpp.in

📁 学习 open inventor 的例子
💻 IN
📖 第 1 页 / 共 3 页
字号:
/**************************************************************************\ * *  This file is part of a set of example programs for the Coin library. *  Copyright (C) 2000-2003 by Systems in Motion. All rights reserved. * *                   <URL:http://www.coin3d.org> * *  This sourcecode can be redistributed and/or modified under the *  terms of the GNU General Public License version 2 as published by *  the Free Software Foundation. See the file COPYING at the root *  directory of the distribution for more details. * *  As a special exception, all sourcecode of the demo examples can be *  used for any purpose for licensees of the Coin Professional *  Edition License, without the restrictions of the GNU GPL. See our *  web pages for information about how to acquire a Professional Edition *  License. * *  Systems in Motion, <URL:http://www.sim.no>, <mailto:support@sim.no> *\**************************************************************************/// This game is an implementation of a 3D tetris that I// played myself for the first time around 1990.// This version is rather lacking as a professional// game. No topscore, no introductional screen,// no possibility to change options, whatever those// should be.// Feel free to make it better, whoever wants to try.// I doubt I'll ever do anything more on this game// again.// If somebody wants to know about some of the stranger// undocumented things i've done, please mail// me at larsivi@sim.no#include <Inventor/@Gui@/So@Gui@.h>#include <Inventor/@Gui@/viewers/So@Gui@ExaminerViewer.h>#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoDirectionalLight.h>#include <Inventor/nodes/SoSpotLight.h>#include <Inventor/nodes/SoPerspectiveCamera.h>#include <Inventor/nodes/SoEventCallback.h>#include <Inventor/nodes/SoCube.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/nodes/SoCallback.h>#include <Inventor/events/SoKeyboardEvent.h>#include <Inventor/nodes/SoText2.h>#include <Inventor/elements/SoViewportRegionElement.h>#include <Inventor/sensors/SoTimerSensor.h>#include <Inventor/fields/SoSFString.h>#include <stdlib.h>SbBool room[4][4][14];SoSeparator * roomblocks[4][4][14];SoMaterial * roommaterials[4][4][14];short piece[4][3];short piecet;SoMaterial * piecematerial;int lines1 = 0;SbString lines2 ;SoSFString * lines3;SoSeparator * create_room(){  piecematerial = new SoMaterial;  piecematerial->ref();  lines3 = new SoSFString;  lines2 = SbString(lines1);  lines3->setValue(lines2.getString());  SoSeparator * roomsep = new SoSeparator;  SoMaterial *mat = new SoMaterial;  mat->diffuseColor.setValue(0.9,0.5,0.3);  mat->specularColor.setValue(0.51,0.53,0.08);  mat->ambientColor.setValue(0.0, 1.0, 0.1);  roomsep->addChild(mat);  SoCube *floor = new SoCube;  floor->height.setValue(0.2);  floor->width.setValue(4);  floor->depth.setValue(0.5);  roomsep->addChild(floor);  SbVec3f move;  SoTranslation *trans = new SoTranslation;  move.setValue(0,0,4.5);  trans->translation.setValue(move);  roomsep->addChild(trans);  roomsep->addChild(floor);  move.setValue(-2.25, 0, -2.25);  trans = new SoTranslation;  trans->translation.setValue(move);  floor = new SoCube;  floor->height.setValue(0.2);  floor->width.setValue(0.5);  floor->depth.setValue(4);  roomsep->addChild(trans);  roomsep->addChild(floor);  move.setValue(4.5, 0, 0);  trans = new SoTranslation;  trans->translation.setValue(move);  roomsep->addChild(trans);  roomsep->addChild(floor);  move.setValue(-3.75, 0, -1.5);  floor = new SoCube;  floor->height.setValue(0.2);  floor->width.setValue(1);  floor->depth.setValue(1);  for (int j=0;j<4;j++) {    for (int i=0;i<4;i++) {      trans = new SoTranslation;      trans->translation.setValue(move);      roomsep->addChild(trans);      roomsep->addChild(floor);      move.setValue(1,0,0);    }    move.setValue(-3,0,1);  }  move.setValue(0.625, 7.5, 0.625);  trans = new SoTranslation;  trans->translation.setValue(move);  roomsep->addChild(trans);  mat = new SoMaterial;  mat->diffuseColor.setValue(0.2,0.2,0.2);  mat->specularColor.setValue(0.2,0.53,0.78);  mat->ambientColor.setValue(0.0, 0.0, 1.0);  roomsep->addChild(mat);  SoCube *corner = new SoCube;  corner->height.setValue(15.2);  corner->width.setValue(0.25);  corner->depth.setValue(0.25);  roomsep->addChild(corner);  move.setValue(0, 0, -4.25);  trans = new SoTranslation;  trans->translation.setValue(move);  roomsep->addChild(trans);  roomsep->addChild(corner);  move.setValue(-4.25, 0, 0);  trans = new SoTranslation;  trans->translation.setValue(move);  roomsep->addChild(trans);  roomsep->addChild(corner);  move.setValue(0, 0, 4.25);  trans = new SoTranslation;  trans->translation.setValue(move);  roomsep->addChild(trans);  roomsep->addChild(corner);  return roomsep;}void setViewportCB(void *data, SoAction *action){  if (action->isOfType(SoGLRenderAction::getClassTypeId())) {    SbViewportRegion curVP, newVP;    curVP = ((SoGLRenderAction*)action)->getViewportRegion();    newVP = curVP;    newVP.setViewport( -0.07, 0.015, 0.45, 0.35);    SoState *state = action->getState();    SoViewportRegionElement::set( state, newVP );  }}SoSeparator *init_game(){  SoCube *block = new SoCube;  block->height.setValue(1);  block->width.setValue(1);  block->depth.setValue(1);  SoTranslation *trans;  SbVec3f move;  SoSeparator *blocksep = new SoSeparator;  for(int i = 0; i < 14; i++){    for(int j = 0; j < 4; j++){      for(int k = 0; k < 4; k++){        room[k][j][i] = FALSE;        move.setValue(-1.5 + k, 0.5 + i, 0.75 + j);        trans = new SoTranslation;        trans->translation.setValue(move);        roommaterials[k][j][i] = new SoMaterial;        roommaterials[k][j][i]->diffuseColor.setValue(0.2,0.2,0.2);        roommaterials[k][j][i]->specularColor.setValue(0.2,0.53,0.78);        roommaterials[k][j][i]->transparency.setValue(1.0);        roomblocks[k][j][i] = new SoSeparator;        roomblocks[k][j][i]->addChild(roommaterials[k][j][i]);        roomblocks[k][j][i]->addChild(trans);        roomblocks[k][j][i]->addChild(block);        blocksep->addChild(roomblocks[k][j][i]);      }    }  }  return blocksep;}SbBoollower_piece(){  SbBool ok = TRUE;  for(int i = 0; i < 4; i++){    short x = piece[i][0];    short y = piece[i][1];    short z = piece[i][2];    if(room[x][y][z-1] || z < 1){      ok = FALSE;      break;    }  }  if(ok){    int i;    for(i = 0; i < 4; i++){      roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(1.0);    }    for(i = 0; i < 4; i++){      roommaterials[piece[i][0]][piece[i][1]][piece[i][2]-1]->ambientColor = piecematerial->ambientColor;      roommaterials[piece[i][0]][piece[i][1]][piece[i][2]-1]->transparency.setValue(0.0);      piece[i][2] -= 1;    }  }  else{    for(int i = 0; i < 4; i++){      room[piece[i][0]][piece[i][1]][piece[i][2]] = TRUE;    }  }  return ok;}SbBooltranslate_piece(short way){  SbBool ok = TRUE;  int i = 0;  switch(way){  case 0:    for(i = 0; i < 4; i++){      if((ok = (piece[i][0]-1 > -1))){        if(!(ok = !room[piece[i][0] - 1][piece[i][1]][piece[i][2]])) break;      }      if(!ok) break;    }    if(ok){      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(1.0);        piece[i][0] -= 1;      }      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->ambientColor = piecematerial->ambientColor;        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(0.0);      }    }    break;  case 1:    for(i = 0; i < 4; i++){      if((ok = (piece[i][0]+1 < 4))){        if(!(ok = !room[piece[i][0] + 1][piece[i][1]][piece[i][2]])) break;      }      if(!ok) break;    }    if(ok){      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(1.0);        piece[i][0] += 1;      }      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->ambientColor = piecematerial->ambientColor;        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(0.0);      }    }    break;  case 2:    for(i = 0; i < 4; i++){      if((ok = (piece[i][1]-1 > -1))){        if(!(ok = !room[piece[i][0]][piece[i][1] - 1][piece[i][2]])) break;      }      if(!ok) break;    }    if(ok){      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(1.0);        piece[i][1] -= 1;      }      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->ambientColor = piecematerial->ambientColor;        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(0.0);      }    }    break;  case 3:    for(i = 0; i < 4; i++){      if((ok = (piece[i][1]+1 < 4))){        if(!(ok = !room[piece[i][0]][piece[i][1] + 1][piece[i][2]])) break;      }      if(!ok) break;    }    if(ok){      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(1.0);        piece[i][1] += 1;      }      for(i = 0; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->ambientColor = piecematerial->ambientColor;        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(0.0);      }    }    break;  }  return ok;}SbBoolrotate_piece(short way){  SbBool ok = TRUE;  int i = 0;  int d1 = 0;  int c[] = {0, 0, 0};  switch(way){  case 0:    for(i = 1; i < 4; i++){      if(piece[i][2] > piece[0][2]){ // z        if(piece[i][0] > piece[0][0]){ // x          c[i-1] = 1;          d1 = 2*(piece[i][2] - piece[0][2]);          if((ok = !(piece[i][2] - d1 < 0))){            if(!(ok = !room[piece[i][0]][piece[i][1]][piece[i][2] - d1])) break;          }          if(!ok) break;        }        else if(piece[i][0] < piece[0][0]){          c[i-1] = 2;          d1 = 2*(piece[0][0] - piece[i][0]);          if((ok = !(piece[i][0] + d1 > 3))){            if(!(ok = !room[piece[i][0] + d1][piece[i][1]][piece[i][2]])) break;          }          if(!ok) break;        }        else{          c[i-1] = 3;          d1 = piece[i][2] - piece[0][2];          if((ok = !(piece[i][0] + d1 > 3))){            if((ok = !(piece[i][2] - d1 < 0))){              if(!(ok = !room[piece[i][0] + d1][piece[i][1]][piece[i][2] - d1])) break;            }          }          if(!ok) break;        }      }      else if(piece[i][2] < piece[0][2]){ // z        if(piece[i][0] > piece[0][0]){ // x          c[i-1] = 4;          d1 = 2*(piece[0][2] - piece[i][2]);          if((ok = !(piece[i][0] - d1 < 0))){            if(!(ok = !room[piece[i][0] - d1][piece[i][1]][piece[i][2]])) break;          }          if(!ok) break;        }        else if(piece[i][0] < piece[0][0]){          c[i-1] = 5;          d1 = 2*(piece[0][0] - piece[i][0]);          if((ok = !(piece[i][2] + d1 > 13))){            if(!(ok = !room[piece[i][0]][piece[i][1]][piece[i][2] + d1])) break;          }          if(!ok) break;        }        else{          c[i-1] = 6;          d1 = piece[0][2] - piece[i][2];          if((ok = !(piece[i][2] + d1 > 13))){            if((ok = !(piece[i][0] - d1 < 0))){              if(!(ok = !room[piece[i][0] - d1][piece[i][1]][piece[i][2] + d1])) break;            }          }          if(!ok) break;        }      }      else{        if(piece[i][0] > piece[0][0]){ // x          c[i-1] = 7;          d1 = piece[i][0] - piece[0][0];          if((ok = !(piece[i][0] - d1 < 0))){            if((ok = !(piece[i][2] - d1 < 0))){              if(!(ok = !room[piece[i][0] - d1][piece[i][1]][piece[i][2] - d1])) break;            }          }          if(!ok) break;        }        else if(piece[i][0] < piece[0][0]){          c[i-1] = 8;          d1 = piece[0][0] - piece[i][0];          if((ok = !(piece[i][2] + d1 > 13))){            if((ok = !(piece[i][0] + d1 > 3))){              if(!(ok = !room[piece[i][0] + d1][piece[i][1]][piece[i][2] + d1])) break;            }          }          if(!ok) break;        }      }    }    if(ok){      for(i = 1; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(1.0);        switch(c[i-1]){        case 1:          d1 = 2*(piece[i][2] - piece[0][2]);          piece[i][2] -= d1;          break;        case 2:          d1 = 2*(piece[0][0] - piece[i][0]);          piece[i][0] += d1;          break;        case 3:          d1 = piece[i][2] - piece[0][2];          piece[i][0] += d1;          piece[i][2] -= d1;          break;        case 4:          d1 = 2*(piece[0][2] - piece[i][2]);          piece[i][0] -= d1;          break;        case 5:          d1 = 2*(piece[0][0] - piece[i][0]);          piece[i][2] += d1;          break;        case 6:          d1 = piece[0][2] - piece[i][2];          piece[i][0] -= d1;          piece[i][2] += d1;          break;        case 7:          d1 = piece[i][0] - piece[0][0];          piece[i][0] -= d1;          piece[i][2] -= d1;          break;        case 8:          d1 = piece[0][0] - piece[i][0];          piece[i][0] += d1;          piece[i][2] += d1;          break;        }      }      for(i = 1; i < 4; i++){        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->ambientColor = piecematerial->ambientColor;        roommaterials[piece[i][0]][piece[i][1]][piece[i][2]]->transparency.setValue(0.0);      }    }    break;  case 1:    for(i = 1; i < 4; i++){      if(piece[i][2] > piece[0][2]){ // z        if(piece[i][0] > piece[0][0]){ // x          c[i-1] = 1;          d1 = 2*(piece[i][0] - piece[0][0]);          if((ok = !(piece[i][0] - d1 < 0))){            if(!(ok = !room[piece[i][0] - d1][piece[i][1]][piece[i][2]])) break;          }          if(!ok) break;        }        else if(piece[i][0] < piece[0][0]){          c[i-1] = 2;          d1 = 2*(piece[i][2] - piece[0][2]);          if((ok = !(piece[i][2] - d1 < 0))){            if(!(ok = !room[piece[i][0]][piece[i][1]][piece[i][2] - d1])) break;          }          if(!ok) break;        }        else{          c[i-1] = 3;          d1 = piece[i][2] - piece[0][2];          if((ok = !(piece[i][0] - d1 < 0))){            if((ok = !(piece[i][2] - d1 < 0))){              if(!(ok = !room[piece[i][0] - d1][piece[i][1]][piece[i][2] - d1])) break;            }          }          if(!ok) break;        }      }      else if(piece[i][2] < piece[0][2]){ // z        if(piece[i][0] > piece[0][0]){ // x          c[i-1] = 4;          d1 = 2*(piece[0][2] - piece[i][2]);          if((ok = !(piece[i][2] + d1 > 13))){            if(!(ok = !room[piece[i][0]][piece[i][1]][piece[i][2] + d1])) break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -