tomsoft | j'ose meme pas balancer le code du tetris que j'ai codé l'an dernier avant d'apprendre la POO, avec glut, c'est assez immonde, mais juste pour le plaisir :
Code :
- // Tetris en OpenGL
- // dev par tomas, 12/2007
- #include <GL/glut.h>
- #include <iostream>
- #include <sstream>
- #include <math.h>
- using namespace std;
- #define COLS 10
- #define ROWS 19
- #define BLOCKSIZE 0.1
- #define OFFSET 0.01
- #define FALLSPEED 1
- int ** gameArray;
- int ** movingParts;
- int nxtPiece;
- int score(0);
- bool gameState(true);
- int speed(1000);
- int nxtLevel(0);
- bool mirror(false);
- void initGameArray();
- void initMovingParts();
- void affichage();
- void clavier(unsigned char touche,int x,int y);
- GLvoid fall(int val);
- void moveDownMovingParts();
- bool isMoveableDown(int x, int y);
- void moveLRMovingParts(char touche);
- bool isMoveableLR(char touche, int x, int y);
- void rotateMovingParts();
- int determinePivot();
- bool isBlockFree(int x, int y);
- int chooseNewPiece();
- bool initNewPiece();
- bool isGameOver();
- void drawContainer();
- void drawNxtPiece();
- void scanRows();
- void removeRow(int inxdRow);
- void afficheStr(const string &chaine, void* font, float x, float y);
- string makeStrScore();
- void pauseGame();
- typedef struct {
- float x; float y; float r; float g; float b;
- } block;
- typedef struct {
- float r; float g; float b;
- } color;
- color colorsBlock[8] = {
- {0.0, 0.0, 0.0},{0.0, 0.0, 1.0},{0.0, 1.0, 0.0},{0.0, 1.0, 1.0},
- {1.0, 0.0, 0.0},{1.0, 0.0, 1.0},{1.0, 1.0, 0.0},{1.0, 1.0, 1.0}
- };
- int main(int argc,char **argv){
- initGameArray();
- initMovingParts();
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
- glutInitWindowPosition(0,0);
- glutInitWindowSize(400,400);
- glutCreateWindow("TetrisCore v0.01" );
- glClearColor(0.0,0.0,0.0,0.0);
- glColor3f(1.0,1.0,1.0);
- glPointSize(2.0);
- glutDisplayFunc(affichage);
- glutKeyboardFunc(clavier);
- glutTimerFunc(speed, fall, 10);
- glutMainLoop();
- return 0;
- }
- void initGameArray(){
- gameArray = new int * [ROWS];
- for (int i = 0; i < ROWS; i++)
- gameArray[i] = new int[COLS];
- for (int i=0; i < ROWS; i++)
- for (int j=0; j < COLS; j++)
- gameArray[i][j] = 0;
- }
- void initMovingParts(){
- movingParts = new int * [4];
- for (int i = 0; i < 4; i++){
- movingParts[i] = new int[3]; // x, y, indxClr
- for (int j = 0; j < 3; j++)
- movingParts[i][j] = -1;
- }
- chooseNewPiece();
- initNewPiece();
- }
- void affichage(){
- glClear(GL_COLOR_BUFFER_BIT);
- drawContainer();
- drawNxtPiece();
- string strScore = makeStrScore();
- afficheStr(strScore, GLUT_BITMAP_HELVETICA_18, 0.25, -0.9);
- float yf, xf;
- int i(0), j(0), iplus(1), stopi(ROWS), stopj(COLS), colorXY;
- if (mirror){
- i = ROWS;
- stopi = 0;
- iplus = -1;
- }
- for (yf = -0.85 ; i < stopi; i+=iplus, yf+=0.1) {
- for (j=0, xf = -0.85; j < COLS; j++, xf+=0.1) {
- colorXY = gameArray[i][j];
- for (int k = 0; k < 4; k++)
- if (movingParts[k][0] == j && movingParts[k][1] == i)
- colorXY = movingParts[k][2];
- if (colorXY != 0){
- glBegin(GL_POLYGON);
- glColor3f(colorsBlock[colorXY].r,colorsBlock[colorXY].g,colorsBlock[colorXY].b);
- glVertex2f(xf - BLOCKSIZE / 2 + OFFSET / 2, yf - BLOCKSIZE / 2 + OFFSET / 2);
- glVertex2f(xf + BLOCKSIZE / 2 - OFFSET / 2, yf - BLOCKSIZE / 2 + OFFSET / 2);
- glVertex2f(xf + BLOCKSIZE / 2 - OFFSET / 2, yf + BLOCKSIZE / 2 - OFFSET / 2);
- glVertex2f(xf - BLOCKSIZE / 2 + OFFSET / 2, yf + BLOCKSIZE / 2 - OFFSET / 2);
- glEnd();
- }
- }
- }
- glutSwapBuffers();
- glFlush();
- }
- void clavier(unsigned char touche,int x,int y){
- switch (touche){
- case 'p':
- pauseGame();
- case 's':
- moveDownMovingParts();
- glutPostRedisplay();
- break;
- case 'a' : case 'd' :
- moveLRMovingParts(touche);
- glutPostRedisplay();
- break;
- case 'w' :
- rotateMovingParts();
- glutPostRedisplay();
- break;
- case 'm' :
- mirror = !mirror;
- glutPostRedisplay();
- }
- }
- void drawContainer(){
- glBegin(GL_LINE);
- glColor3f(1.0, 1.0, 1.0);
- glVertex2f(-0.95, -0.95);
- glVertex2f(-0.95, 0.75);
- glVertex2f(0.15, -0.95);
- glVertex2f(0.15, 0.75);
- glVertex2f(-0.95, -0.95);
- glVertex2f(0.15, -0.95);
- glEnd();
- }
- void drawNxtPiece(){
- int ** nxtPieceBlocks;
- nxtPieceBlocks = new int * [4];
- for (int i = 0; i < 4; i++){
- nxtPieceBlocks[i] = new int[3]; // x, y, indxClr
- for (int j = 0; j < 3; j++)
- nxtPieceBlocks[i][j] = -1;
- }
- switch (nxtPiece){
- case 0 :
- nxtPieceBlocks[0][0] = 5; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 4;
- nxtPieceBlocks[1][0] = 5; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 4;
- nxtPieceBlocks[2][0] = 5; nxtPieceBlocks[2][1] = ROWS-3; nxtPieceBlocks[2][2] = 4;
- nxtPieceBlocks[3][0] = 5; nxtPieceBlocks[3][1] = ROWS-4; nxtPieceBlocks[3][2] = 4;
- break;
- case 1 :
- nxtPieceBlocks[0][0] = 5; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 2;
- nxtPieceBlocks[1][0] = 5; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 2;
- nxtPieceBlocks[2][0] = 6; nxtPieceBlocks[2][1] = ROWS-1; nxtPieceBlocks[2][2] = 2;
- nxtPieceBlocks[3][0] = 6; nxtPieceBlocks[3][1] = ROWS-2; nxtPieceBlocks[3][2] = 2;
- break;
- case 2 :
- nxtPieceBlocks[0][0] = 5; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 6;
- nxtPieceBlocks[1][0] = 5; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 6;
- nxtPieceBlocks[2][0] = 6; nxtPieceBlocks[2][1] = ROWS-2; nxtPieceBlocks[2][2] = 6;
- nxtPieceBlocks[3][0] = 6; nxtPieceBlocks[3][1] = ROWS-3; nxtPieceBlocks[3][2] = 6;
- break;
- case 3 :
- nxtPieceBlocks[0][0] = 6; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 3;
- nxtPieceBlocks[1][0] = 6; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 3;
- nxtPieceBlocks[2][0] = 5; nxtPieceBlocks[2][1] = ROWS-2; nxtPieceBlocks[2][2] = 3;
- nxtPieceBlocks[3][0] = 5; nxtPieceBlocks[3][1] = ROWS-3; nxtPieceBlocks[3][2] = 3;
- break;
- case 4 :
- nxtPieceBlocks[0][0] = 5; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 7;
- nxtPieceBlocks[1][0] = 5; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 7;
- nxtPieceBlocks[2][0] = 5; nxtPieceBlocks[2][1] = ROWS-3; nxtPieceBlocks[2][2] = 7;
- nxtPieceBlocks[3][0] = 6; nxtPieceBlocks[3][1] = ROWS-3; nxtPieceBlocks[3][2] = 7;
- break;
- case 5 :
- nxtPieceBlocks[0][0] = 6; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 5;
- nxtPieceBlocks[1][0] = 6; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 5;
- nxtPieceBlocks[2][0] = 6; nxtPieceBlocks[2][1] = ROWS-3; nxtPieceBlocks[2][2] = 5;
- nxtPieceBlocks[3][0] = 5; nxtPieceBlocks[3][1] = ROWS-3; nxtPieceBlocks[3][2] = 5;
- break;
- case 6 :
- nxtPieceBlocks[0][0] = 5; nxtPieceBlocks[0][1] = ROWS-1; nxtPieceBlocks[0][2] = 1;
- nxtPieceBlocks[1][0] = 5; nxtPieceBlocks[1][1] = ROWS-2; nxtPieceBlocks[1][2] = 1;
- nxtPieceBlocks[2][0] = 5; nxtPieceBlocks[2][1] = ROWS-3; nxtPieceBlocks[2][2] = 1;
- nxtPieceBlocks[3][0] = 6; nxtPieceBlocks[3][1] = ROWS-2; nxtPieceBlocks[3][2] = 1;
- break;
- }
- float yf, xf;
- int i, j, colorXY;
- for (i = ROWS-4, yf = -0.65 ; i < ROWS; i++, yf+=0.1) {
- for (j = 5, xf = 0.55; j < 7; j++, xf+=0.1) {
- colorXY = 0;
- for (int k = 0; k < 4; k++)
- if (nxtPieceBlocks[k][0] == j && nxtPieceBlocks[k][1] == i)
- colorXY = nxtPieceBlocks[k][2];
- if (colorXY != 0){
- glBegin(GL_POLYGON);
- glColor3f(colorsBlock[colorXY].r,colorsBlock[colorXY].g,colorsBlock[colorXY].b);
- glVertex2f(xf - BLOCKSIZE / 2 + OFFSET / 2, yf - BLOCKSIZE / 2 + OFFSET / 2);
- glVertex2f(xf + BLOCKSIZE / 2 - OFFSET / 2, yf - BLOCKSIZE / 2 + OFFSET / 2);
- glVertex2f(xf + BLOCKSIZE / 2 - OFFSET / 2, yf + BLOCKSIZE / 2 - OFFSET / 2);
- glVertex2f(xf - BLOCKSIZE / 2 + OFFSET / 2, yf + BLOCKSIZE / 2 - OFFSET / 2);
- glEnd();
- }
- }
- }
- }
- void moveDownMovingParts(){
- bool canMoveDown (true);
- for (int i = 0; i < 4; i++){
- canMoveDown = isMoveableDown(movingParts[i][0],movingParts[i][1]);
- if (!canMoveDown){
- scanRows();
- break;
- }
- }
- if (canMoveDown)
- for (int i = 0; i < 4; i++)
- movingParts[i][1]--;
- }
- bool isMoveableDown(int x, int y){
- if (y-1 < 0 || gameArray[y-1][x] != 0){
- for (int i = 0; i < 4; i++){
- gameArray[movingParts[i][1]][movingParts[i][0]] = movingParts[i][2];
- }
- initNewPiece();
- return false;
- }
- return true;
- }
- void moveLRMovingParts(char touche){
- bool canMoveLR (true);
- for (int i = 0; i < 4; i++){
- canMoveLR = isMoveableLR(touche,movingParts[i][0],movingParts[i][1]);
- if (!canMoveLR)
- break;
- }
- if (canMoveLR)
- for (int i = 0; i < 4; i++){
- if (touche == 'a')
- movingParts[i][0]--;
- else
- movingParts[i][0]++;
- }
- }
- bool isMoveableLR(char touche, int x, int y){
- if (touche == 'a'){
- if (x-1 < 0 || gameArray[y][x-1] != 0)
- return false;
- }
- else {
- if (x+1 > COLS-1 || gameArray[y][x+1] != 0)
- return false;
- }
- return true;
- }
- void rotateMovingParts(){
- int indxPivot = determinePivot();
- bool isOk = true;
- for (int i = 0; i < 4; i++){
- if (i == indxPivot)
- continue;
- int x = movingParts[indxPivot][0] - ( movingParts[i][1] - movingParts[indxPivot][1] );
- int y = movingParts[indxPivot][1] + ( movingParts[i][0] - movingParts[indxPivot][0] );
- isOk = isBlockFree(x,y);
- if (!isOk)
- break;
- }
- if (isOk){
- for (int i = 0; i < 4; i++){
- if (i == indxPivot)
- continue;
- int x = movingParts[indxPivot][0] - ( movingParts[i][1] - movingParts[indxPivot][1] );
- int y = movingParts[indxPivot][1] + ( movingParts[i][0] - movingParts[indxPivot][0] );
- movingParts[i][0] = x;
- movingParts[i][1] = y;
- }
- }
- }
- int determinePivot(){
- float x(0);
- float y(0);
- for (int i = 0; i < 4; i++){
- x += movingParts[i][0];
- y += movingParts[i][1];
- }
- x = static_cast<int> (round(x/4));
- y = static_cast<int> (round(y/4));
- for (int i = 0; i < 4; i++)
- if (movingParts[i][0] == x && movingParts[i][1] == y)
- return i;
- }
- bool isBlockFree(int x, int y){
- if (y < 0 || x < 0 || x > COLS-1 || gameArray[y][x] != 0)
- return false;
- return true;
- }
- bool isGameOver(){
- bool gameOver = false;
- for (int i = 0; i < COLS; i++){
- if (gameArray[ROWS-5][i] != 0){
- gameOver = true;
- break;
- }
- }
- return gameOver;
- }
- GLvoid fall(int value){
- moveDownMovingParts();
- glutPostRedisplay();
- glutTimerFunc(speed, fall, 10);
- }
- void scanRows(){
- bool isComplete;
- int nb_removed(0);
- for (int i = 0; i < ROWS; i++){
- isComplete = true;
- for (int j = 0; j < COLS; j++)
- if (gameArray[i][j] == 0){
- isComplete = false;
- break;
- }
- if (isComplete){
- removeRow(i);
- nb_removed++;
- nxtLevel += nb_removed;
- i--;
- if (nxtLevel >= 10){
- speed -= 100;
- nxtLevel -= 10;
- }
- }
- }
- score += 100 * nb_removed * nb_removed;
- }
- void removeRow(int indxRow){
- for (int i = indxRow; i < ROWS-1; i++)
- for (int j = 0; j < COLS; j++)
- gameArray[i][j] = gameArray[i+1][j];
- }
- int chooseNewPiece(){
- srand(time(NULL));
- nxtPiece = rand() % 7;
- }
- bool initNewPiece(){
- if (isGameOver())
- return false;
- switch (nxtPiece){
- case 0 :
- movingParts[0][0] = 5; movingParts[0][1] = ROWS-1; movingParts[0][2] = 4;
- movingParts[1][0] = 5; movingParts[1][1] = ROWS-2; movingParts[1][2] = 4;
- movingParts[2][0] = 5; movingParts[2][1] = ROWS-3; movingParts[2][2] = 4;
- movingParts[3][0] = 5; movingParts[3][1] = ROWS-4; movingParts[3][2] = 4;
- break;
- case 1 :
- movingParts[0][0] = 5; movingParts[0][1] = ROWS-1; movingParts[0][2] = 2;
- movingParts[1][0] = 5; movingParts[1][1] = ROWS-2; movingParts[1][2] = 2;
- movingParts[2][0] = 6; movingParts[2][1] = ROWS-1; movingParts[2][2] = 2;
- movingParts[3][0] = 6; movingParts[3][1] = ROWS-2; movingParts[3][2] = 2;
- break;
- case 2 :
- movingParts[0][0] = 5; movingParts[0][1] = ROWS-1; movingParts[0][2] = 6;
- movingParts[1][0] = 5; movingParts[1][1] = ROWS-2; movingParts[1][2] = 6;
- movingParts[2][0] = 6; movingParts[2][1] = ROWS-2; movingParts[2][2] = 6;
- movingParts[3][0] = 6; movingParts[3][1] = ROWS-3; movingParts[3][2] = 6;
- break;
- case 3 :
- movingParts[0][0] = 6; movingParts[0][1] = ROWS-1; movingParts[0][2] = 3;
- movingParts[1][0] = 6; movingParts[1][1] = ROWS-2; movingParts[1][2] = 3;
- movingParts[2][0] = 5; movingParts[2][1] = ROWS-2; movingParts[2][2] = 3;
- movingParts[3][0] = 5; movingParts[3][1] = ROWS-3; movingParts[3][2] = 3;
- break;
- case 4 :
- movingParts[0][0] = 5; movingParts[0][1] = ROWS-1; movingParts[0][2] = 7;
- movingParts[1][0] = 5; movingParts[1][1] = ROWS-2; movingParts[1][2] = 7;
- movingParts[2][0] = 5; movingParts[2][1] = ROWS-3; movingParts[2][2] = 7;
- movingParts[3][0] = 6; movingParts[3][1] = ROWS-3; movingParts[3][2] = 7;
- break;
- case 5 :
- movingParts[0][0] = 6; movingParts[0][1] = ROWS-1; movingParts[0][2] = 5;
- movingParts[1][0] = 6; movingParts[1][1] = ROWS-2; movingParts[1][2] = 5;
- movingParts[2][0] = 6; movingParts[2][1] = ROWS-3; movingParts[2][2] = 5;
- movingParts[3][0] = 5; movingParts[3][1] = ROWS-3; movingParts[3][2] = 5;
- break;
- case 6 :
- movingParts[0][0] = 5; movingParts[0][1] = ROWS-1; movingParts[0][2] = 1;
- movingParts[1][0] = 5; movingParts[1][1] = ROWS-2; movingParts[1][2] = 1;
- movingParts[2][0] = 5; movingParts[2][1] = ROWS-3; movingParts[2][2] = 1;
- movingParts[3][0] = 6; movingParts[3][1] = ROWS-2; movingParts[3][2] = 1;
- break;
- }
- chooseNewPiece();
- return true;
- }
- void afficheStr(const string &chaine, void* font, float x, float y){
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_DEPTH_TEST);
- glRasterPos2f(x, y);
- glColor3f(1.,1.,1.);
- for (int i=0; i < chaine.length(); i++)
- glutBitmapCharacter(font, chaine[i]);
- }
- string makeStrScore(){
- ostringstream oss;
- oss << "Score : " << score;
- return oss.str();
- }
- void pauseGame(){
- }
|
|