📄 blocks.cpp.in
字号:
} if(!ok) break; } else if(piece[i][1] < piece[0][1]){ c[i-1] = 5; d1 = 2*(piece[0][1] - piece[i][1]); if((ok = !(piece[i][1] + d1 > 3))){ if(!(ok = !room[piece[i][0]][piece[i][1] + d1][piece[i][2]])) 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][1] + d1 > 3))){ if(!(ok = !room[piece[i][0]][piece[i][1] + d1][piece[i][2] + d1])) break; } } if(!ok) break; } } else{ if(piece[i][1] > piece[0][1]){ // x c[i-1] = 7; d1 = piece[i][1] - piece[0][1]; if((ok = !(piece[i][1] - d1 < 0))){ if((ok = !(piece[i][2] + d1 > 13))){ if(!(ok = !room[piece[i][0]][piece[i][1] - d1][piece[i][2] + d1])) break; } } if(!ok) break; } else if(piece[i][1] < piece[0][1]){ c[i-1] = 8; d1 = piece[0][1] - piece[i][1]; if((ok = !(piece[i][2] - d1 < 0))){ if((ok = !(piece[i][1] + d1 > 3))){ if(!(ok = !room[piece[i][0]][piece[i][1] + d1][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][1] - piece[0][1]); piece[i][1] -= d1; break; case 2: d1 = 2*(piece[i][2] - piece[0][2]); piece[i][2] -= d1; break; case 3: d1 = piece[i][2] - piece[0][2]; piece[i][1] -= d1; piece[i][2] -= d1; break; case 4: d1 = 2*(piece[0][2] - piece[i][2]); piece[i][2] += d1; break; case 5: d1 = 2*(piece[0][1] - piece[i][1]); piece[i][1] += d1; break; case 6: d1 = piece[0][2] - piece[i][2]; piece[i][1] += d1; piece[i][2] += d1; break; case 7: d1 = piece[i][1] - piece[0][1]; piece[i][1] -= d1; piece[i][2] += d1; break; case 8: d1 = piece[0][1] - piece[i][1]; piece[i][1] += 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; } return ok;}voidremove_planes(){ short planes[] = {-1,-1,-1,-1}; short planes2[] = {-1,-1,-1,-1}; short movers[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}; short numplanes = 0; SoMaterial * tempmat = new SoMaterial; tempmat->ref(); SbBool full; full = TRUE; int i; for(i = 0; i < 14; i++){ full = TRUE; for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ if(!room[k][j][i]){ full = FALSE; break; } } if(!full){ break; } } if(full){ planes[numplanes] = i; numplanes++; if(numplanes == 4) break; } } if(numplanes > 0){ numplanes = 1; planes2[0] = planes[0]; for(i = 1; i < 4; i++){ if(planes[i] == -1) break; if(planes[i] == planes[i-1]) continue; planes2[numplanes] = planes[i]; numplanes++; } for(i = 0; i < numplanes; i++){ for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ roommaterials[k][j][planes2[i]]->transparency.setValue(1.0); room[k][j][planes2[i]] = FALSE; } } } short plnum = 1; short temp = 1; int j = 1; SbBool t = TRUE; (void)printf("1 \n"); if(planes2[1] < 0){ for(i = planes2[0] + 1; i < 14; i++){ for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ if(room[k][j][i]){ room[k][j][i] = FALSE; roommaterials[k][j][i]->transparency.setValue(1.0); tempmat->ambientColor = roommaterials[k][j][i]->ambientColor; roommaterials[k][j][i - plnum]->ambientColor = tempmat->ambientColor; room[k][j][i - plnum] = TRUE; roommaterials[k][j][i - plnum]->transparency.setValue(0.0); } } } } } else{ for(i = planes2[0] + 1; i < 14; i++){ if(j < numplanes & planes2[j] == i){ j++; plnum++; } else{ for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ if(room[k][j][i]){ room[k][j][i] = FALSE; roommaterials[k][j][i]->transparency.setValue(1.0); tempmat->ambientColor = roommaterials[k][j][i]->ambientColor; roommaterials[k][j][i - plnum]->ambientColor = tempmat->ambientColor; room[k][j][i - plnum] = TRUE; roommaterials[k][j][i - plnum]->transparency.setValue(0.0); } } } } (void)printf("numplanes = %d, j = %d, plnum = %d\n", numplanes, j, plnum); } } lines1 += numplanes; lines2 = SbString(lines1); lines3->setValue(lines2.getString()); } tempmat->unref();}SbBoolcreate_piece(){ SbBool ok = TRUE; SbTime *time = new SbTime; time->setToTimeOfDay(); unsigned long piecetype = int(fmod(time->getValue(), 1.0) * 1000)%5; int i = 0; //int piecetype = 2; switch(piecetype){ case 0: piecet = 0; piece[0][0] = 1; piece[0][1] = 1; piece[0][2] = 11; piece[1][0] = 1; piece[1][1] = 1; piece[1][2] = 12; piece[2][0] = 1; piece[2][1] = 1; piece[2][2] = 13; piece[3][0] = 1; piece[3][1] = 1; piece[3][2] = 10; piecematerial->ambientColor.setValue(0,1,0); break; case 1: piecet = 1; piece[0][0] = 1; piece[0][1] = 1; piece[0][2] = 13; piece[1][0] = 1; piece[1][1] = 2; piece[1][2] = 13; piece[2][0] = 1; piece[2][1] = 1; piece[2][2] = 12; piece[3][0] = 1; piece[3][1] = 2; piece[3][2] = 12; piecematerial->ambientColor.setValue(1,0,0); break; case 2: piecet = 2; piece[0][0] = 1; piece[0][1] = 1; piece[0][2] = 12; piece[1][0] = 2; piece[1][1] = 1; piece[1][2] = 13; piece[2][0] = 1; piece[2][1] = 1; piece[2][2] = 13; piece[3][0] = 1; piece[3][1] = 1; piece[3][2] = 11; piecematerial->ambientColor.setValue(1,1,1); break; case 3: piecet = 3; piece[0][0] = 1; piece[0][1] = 1; piece[0][2] = 12; piece[1][0] = 1; piece[1][1] = 1; piece[1][2] = 13; piece[2][0] = 2; piece[2][1] = 1; piece[2][2] = 12; piece[3][0] = 0; piece[3][1] = 1; piece[3][2] = 12; piecematerial->ambientColor.setValue(0,0,1); break; case 4: piecet = 4; piece[0][0] = 1; piece[0][1] = 1; piece[0][2] = 12; piece[1][0] = 1; piece[1][1] = 1; piece[1][2] = 13; piece[2][0] = 2; piece[2][1] = 1; piece[2][2] = 12; piece[3][0] = 2; piece[3][1] = 1; piece[3][2] = 11; piecematerial->ambientColor.setValue(1,0,1); break; } for (; i < 4; i++){ if(!room[piece[i][0]][piece[i][1]][piece[i][2]]){ 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); } else{ ok = FALSE; break; } } return ok;}voidevent_cb(void *userdata, SoEventCallback *node){ const SoEvent *event = node->getEvent(); //move left if (SO_KEY_PRESS_EVENT(event, LEFT_ARROW)) { translate_piece(0); } //move right else if (SO_KEY_PRESS_EVENT(event, RIGHT_ARROW)) { translate_piece(1); } //move forward else if (SO_KEY_PRESS_EVENT(event, UP_ARROW)) { translate_piece(2); } //move backward else if (SO_KEY_PRESS_EVENT(event, DOWN_ARROW)) { translate_piece(3); } //drop else if (SO_KEY_PRESS_EVENT(event, SPACE)) { if(!lower_piece()){ remove_planes(); if(!create_piece()) exit(1); } } //yaw forward else if (SO_KEY_PRESS_EVENT(event, W)) { if(!rotate_piece(0)); } //yaw backward else if (SO_KEY_PRESS_EVENT(event, S)) { if(!rotate_piece(1)); } //rotate left else if (SO_KEY_PRESS_EVENT(event, A)) { if(!rotate_piece(2)); } //rotate right else if (SO_KEY_PRESS_EVENT(event, D)) { if(!rotate_piece(3)); } //roll left else if (SO_KEY_PRESS_EVENT(event, Q)) { if(!rotate_piece(4)); } //roll right else if (SO_KEY_PRESS_EVENT(event, E)) { if(!rotate_piece(5)); } else if (SO_KEY_PRESS_EVENT(event, X)) { exit(1); }}voidtime_cb(void *userdata, SoSensor *){ if(!lower_piece()){ remove_planes(); if(!create_piece()) exit(1); }}intmain(int argc, char **argv){ @WIDGET@ window = So@Gui@::init(argv[0]); if (window == NULL) exit(1); SoSeparator * root = new SoSeparator; SoSeparator * gameroot = new SoSeparator; root->ref(); gameroot->ref(); SoPerspectiveCamera * gamecam = new SoPerspectiveCamera; gameroot->addChild(gamecam); SoDirectionalLight *light= new SoDirectionalLight; light->direction.setValue(-1,-1,-1); gameroot->addChild(light); SoEventCallback *cb = new SoEventCallback; cb->addEventCallback(SoKeyboardEvent::getClassTypeId(), event_cb, NULL); gameroot->insertChild(cb, 0); gameroot->addChild(create_room()); gameroot->addChild(init_game()); SbViewportRegion reg(500,500); SbVec3f pa(0,0,0); gamecam->position.setValue(0,14,2); gamecam->pointAt(pa); gamecam->viewAll(gameroot, reg); root->addChild(gameroot); // New camera for the instructions SoSeparator * instruction = new SoSeparator; instruction->ref(); root->addChild(instruction); SoPerspectiveCamera * instrcam = new SoPerspectiveCamera; instruction->addChild(instrcam); // To make it render in a custom viewport SoCallback *vpcb = new SoCallback(); vpcb->setCallback(setViewportCB, NULL); instruction->addChild(vpcb); SoTranslation * instrtrans = new SoTranslation; instrtrans->translation.setValue(7,-5.25,0); //instruction->addChild(instrtrans); SoText2 * text = new SoText2; SoText2 * score = new SoText2; const char * str[] = { "","SPACE drops current piece", "Arrows move piece left, right,", "forward and backward", "(not adjusted to eventual rotation", "by the user)", "W and S for forward and backward roll", "Q and E for left and right roll", "A and D for left and right rotation", "Press X to end Blocks","", "Planes completed =" }; score->string.connectFrom(lines3); text->string.setValues(0, sizeof(str) / sizeof(char *), str); text->justification = SoText2::LEFT; instruction->addChild(text); instruction->addChild(instrtrans); instruction->addChild(score); root->addChild(instruction); SbViewportRegion reg2(245,250); instrcam->viewAll(instruction,reg2); create_piece(); // Sets up the examiner viewer So@Gui@ExaminerViewer * viewer = new So@Gui@ExaminerViewer(window); viewer->setSceneGraph(root); viewer->setTitle("Blocks"); viewer->setViewing(FALSE); SoTimerSensor * ts = new SoTimerSensor(time_cb, NULL); ts->setInterval(SbTime(1.0)); ts->schedule(); viewer->show(); So@Gui@::show(window); So@Gui@::mainLoop(); delete viewer; piecematerial->unref(); root->unref(); gameroot->unref(); instruction->unref(); delete lines3; delete ts; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -