The Following is a example of a openGL program. Click here to download the source
main.cpp
/* Copyright (c)Christopher L. Pelech, 2014. */ /* This program is freely distributable without licensing fees and is provided without guarantee or warrantee expressed or implied. This program is -not- in the public domain. */ #include#include #include #include #include "Tetris.h" using namespace std; /******************************************/ /* Global Variables */ /******************************************/ //Tetris Test Tetris myGame; //Light vars GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* Red diffuse light. */ GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */ //Demo Cube GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */ {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} }; GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */ {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4}, {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} }; GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */ //Camera values float azimth = 0; float elevation = 0; float zoomDst = 75; const GLfloat NEAR_VIEWING_PLANE = 1.0; const GLfloat FAR_VIEWING_PLANE = 5000.0; const GLfloat FOVY = 60.0; //Time values int oldTimeSinceStart = 0; #define FRAMES_PER_SECOND 60; const int TIMER_FUNC_DELAY=1000/FRAMES_PER_SECOND; //Keyboard vars bool* keyStates = new bool[256]; bool* keySpecialStates = new bool[256]; //Window Vars static int win_width, win_height; int windowWidth = 800; int windowHeight = 800; //Other Vars float speed = 0.25f; float Rot_y =0.0f; /******************************************/ /* Ligt vars */ /******************************************/ void initLightEnvironment( void ) { float light0_position[] = { 0.0, 0.0, 0.0, 1.0 }; float light0_diffuse[] = { 0.1, 0.1, 0.4, 1.0 }; //Light Colour, maybe attempt to create a light/day effect? float light0_specular[] = { 1.0, 1.0, 1.0, 1.0 }; float light0_ambient[] = { 0.1, 0.1, 0.1, 1.0 }; glLightfv( GL_LIGHT0, GL_POSITION, light0_position ); glLightfv( GL_LIGHT0, GL_DIFFUSE, light0_diffuse ); glLightfv( GL_LIGHT0, GL_SPECULAR, light0_specular ); glLightfv( GL_LIGHT0, GL_AMBIENT, light0_ambient ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_COLOR_MATERIAL ); glEnable( GL_DEPTH_TEST ); glCullFace( GL_BACK ); glEnable( GL_CULL_FACE ); } /******************************************/ /* Window Rez */ /******************************************/ void reshape (int w, int h) { glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications glMatrixMode (GL_PROJECTION); //set the matrix to projection glLoadIdentity (); gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 500.0); //set the perspective (angle of sight, width, height, , depth) glMatrixMode (GL_MODELVIEW); //set the matrix back to model windowWidth = w; windowHeight = h; win_width = w; win_height = h; } /******************************************/ /* Keyboard */ /******************************************/ void keyboard (unsigned char key, int x, int y) { keyStates[key] = true; if (key=='1'){ ShowWindow( GetConsoleWindow(), SW_HIDE ); }else if(key=='2'){ ShowWindow( GetConsoleWindow(), SW_RESTORE ); printf("Window Open"); } } void processSpecialKeys(int key, int x, int y) { keySpecialStates[key] = true; switch(key) { case GLUT_KEY_UP : //printf("up"); break; case GLUT_KEY_DOWN: //printf("down"); break; case GLUT_KEY_LEFT: myGame.MoveLeft(); break; case GLUT_KEY_RIGHT: myGame.MoveRight(); break; } } void keyUp (unsigned char key, int x, int y) { keyStates[key] = false; } void keySpecialUp (int key, int x, int y) { keySpecialStates[key] = false; } /******************************************/ /* Mouse Functions */ /******************************************/ void motion( int mx, int my ){ } void mouse( int button, int state, int mx, int my ){ } void mouseMovement(int x, int y) { //if (x==0||x>(win_width/3)*2){ //glutWarpPointer(win_width/2, y); //} } /******************************************/ /* Other Functions */ /******************************************/ void drawBox(void) { int i; for (i = 0; i < 6; i++) { glBegin(GL_QUADS); glNormal3fv(&n[i][0]); glVertex3fv(&v[faces[i][0]][0]); glVertex3fv(&v[faces[i][1]][0]); glVertex3fv(&v[faces[i][2]][0]); glVertex3fv(&v[faces[i][3]][0]); glEnd(); } } /******************************************/ /* Update Functions */ /******************************************/ void update(void) { int timeSinceStart = glutGet(GLUT_ELAPSED_TIME); int deltaTime = timeSinceStart - oldTimeSinceStart; oldTimeSinceStart = timeSinceStart; Rot_y+= speed * deltaTime; //Update by time and constant speed if(Rot_y>360.0f)Rot_y=0.0f; //Send deltaT to any functions who need it myGame.Update_timer(deltaTime); } /******************************************/ /* Display */ /******************************************/ void display(void) { //update vars update(); //Clear whats currently rendered to screen/memory glClearColor (0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Apply transforms glPushMatrix(); //glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, myGame.line_count); //glRotatef(Rot_y,0.0f,1.0f,0.0f); //glutSolidCube(1.0f); //drawBox(); glPopMatrix(); //Tetris Test //view update func for time call myGame.UpdateGrid(); myGame.DrawGrid(); //Redraw the screen glutSwapBuffers(); } /******************************************/ /* Init Vars */ /******************************************/ void init(void) { /* Setup cube vertex data. */ v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1; v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1; v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1; v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1; v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1; v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1; /* Enable a single OpenGL light. */ initLightEnvironment(); /* Use depth buffering for hidden surface elimination. */ glEnable(GL_DEPTH_TEST); /* Setup the view of the cube. */ glMatrixMode(GL_PROJECTION); gluPerspective( /* field of view in degree */ 40.0, /* aspect ratio */ 1.0, /* Z near */ 1.0, /* Z far */ 10.0); glMatrixMode(GL_MODELVIEW); gluLookAt(5.5, 11.0, 25.0, /* eye is at (0,0,5) */ 5.5, 11.0, -25.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ /* Adjust cube position to be asthetic angle. */ //glTranslatef(0.0, 0.0, -1.0); //glRotatef(60, 1.0, 0.0, 0.0); //glRotatef(-20, 0.0, 0.0, 1.0); } /******************************************/ /* Main */ /******************************************/ int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (windowWidth, windowHeight); //set the window size glutInitWindowPosition (0, 0); //set the position of the window glutCreateWindow("red 3D lighted cube"); init(); glutDisplayFunc(display); glutIdleFunc (display); //update any variables in display, display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++; glutReshapeFunc (reshape); //reshape the window accordingly glutKeyboardFunc (keyboard); //check the keyboard glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events glutSpecialFunc(processSpecialKeys); glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keySpecialUp" for special up key events glutMouseFunc( mouse ); glutMotionFunc( motion); glutPassiveMotionFunc(mouseMovement); //ShowWindow( GetConsoleWindow(), SW_HIDE ); //Hide the Console glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
Tetris.h
#include "vector3d.h" #include/* printf, scanf, puts, NULL */ #include /* srand, rand */ #include #include #include using namespace std; #pragma once class Tetris { public: Tetris(void); ~Tetris(void); enum tet_piece { Block, Line, L_piece, Three_piece}; tet_piece current_piece; struct block_group{ // Declare PERSON struct type int x_1, x_2, x_3, x_4; int y_1, y_2, y_3, y_4; } current_block_info; int score; int line_count; bool dropingPeice; int currentHeight; float drop_duration; float delta_t; float drop_counter; bool Grid_index_filled[10][22]; vector3d Grid_Positions[10][22]; void Generate_Grid(); void Generate_Piece(); void CheckForLine(); void UpdateGrid(); void DrawGrid(); void MoveLeft(); void MoveRight(); void Update_timer(float temp_time); };
Tetris.cpp
#include "Tetris.h"
Tetris::Tetris(void)
{
Generate_Grid();
score = 0;
line_count = 0;
//Piece related vars
dropingPeice = true;
currentHeight = 0;
drop_duration = 250.0f; //in miliseconds
delta_t = 0.1f; //in miliseconds
drop_counter = 0.0f;
// setting up
int v1 = rand() % 2;
switch (v1)
{
case 1:
current_piece = tet_piece::Block;
printf("Case 1");
break;
case 2:
printf("Case 2");
current_piece = tet_piece::Line;
break;
default:
printf("Default case");
break;
}
}
Tetris::~Tetris(void)
{
}
void Tetris::Generate_Grid(){
for(int i=0;i<10;i++){
for(int j=0;j<22;j++){
Grid_index_filled[i][j] = false;
Grid_Positions[i][j].x = i;
Grid_Positions[i][j].y = j;
Grid_Positions[i][j].z = 0.0f;
}
}
Generate_Piece();
}
void Tetris::Generate_Piece(){
currentHeight = 0;
srand (time(NULL));
int v1 = rand() % 5;
switch (v1)
{
case 1:
current_piece = tet_piece::Block;
current_block_info.x_1 = 4; current_block_info.y_1 = 0;
current_block_info.x_2 = 4; current_block_info.y_2 = 1;
current_block_info.x_3 = 5; current_block_info.y_3 = 0;
current_block_info.x_4 = 5; current_block_info.y_4 = 1;
Grid_index_filled[4][0] = true;
Grid_index_filled[4][1] = true;
Grid_index_filled[5][0] = true;
Grid_index_filled[5][1] = true;
break;
case 2:
current_piece = tet_piece::Block;
current_block_info.x_1 = 4; current_block_info.y_1 = 0;
current_block_info.x_2 = 4; current_block_info.y_2 = 1;
current_block_info.x_3 = 5; current_block_info.y_3 = 0;
current_block_info.x_4 = 5; current_block_info.y_4 = 1;
Grid_index_filled[3][0] = true;
Grid_index_filled[4][1] = true;
Grid_index_filled[5][2] = true;
Grid_index_filled[6][3] = true;
break;
case 3:
current_piece = tet_piece::Block;
current_block_info.x_1 = 4; current_block_info.y_1 = 0;
current_block_info.x_2 = 4; current_block_info.y_2 = 1;
current_block_info.x_3 = 5; current_block_info.y_3 = 0;
current_block_info.x_4 = 5; current_block_info.y_4 = 1;
Grid_index_filled[1][0] = true;
Grid_index_filled[2][1] = true;
Grid_index_filled[3][0] = true;
Grid_index_filled[4][1] = true;
break;
case 4:
current_piece = tet_piece::Block;
current_block_info.x_1 = 4; current_block_info.y_1 = 0;
current_block_info.x_2 = 4; current_block_info.y_2 = 1;
current_block_info.x_3 = 5; current_block_info.y_3 = 0;
current_block_info.x_4 = 5; current_block_info.y_4 = 1;
Grid_index_filled[6][0] = true;
Grid_index_filled[7][1] = true;
Grid_index_filled[8][2] = true;
Grid_index_filled[9][3] = true;
break;
default:
current_block_info.x_1 = 4; current_block_info.y_1 = 0;
current_block_info.x_2 = 4; current_block_info.y_2 = 1;
current_block_info.x_3 = 5; current_block_info.y_3 = 0;
current_block_info.x_4 = 5; current_block_info.y_4 = 1;
Grid_index_filled[3][0] = true;
Grid_index_filled[4][0] = true;
Grid_index_filled[5][0] = true;
Grid_index_filled[6][0] = true;
break;
}
}
void Tetris::DrawGrid(){
for(int i=0;i<10;i++){
for(int j=0;j<22;j++){
if(Grid_index_filled[i][j]){
glPushMatrix();
int v1 = rand() % 2;
int v2 = rand() % 2;
int v3 = rand() % 2;
glColor3f(v1,v2,3);
glTranslatef(Grid_Positions[i][j].x,Grid_Positions[i][j].y,Grid_Positions[i][j].z);
glutSolidCube(1.0f);
glPopMatrix();
}else if(!Grid_index_filled[i][j]){
glPushMatrix();
glColor3f(0,0,1);
glTranslatef(Grid_Positions[i][j].x,Grid_Positions[i][j].y,Grid_Positions[i][j].z);
glutWireCube(1.0f);
glPopMatrix();
}
}
}
}
void Tetris::UpdateGrid(){
drop_counter += delta_t/drop_duration;
//cout <<"counting: " << drop_counter << "\n";
if(drop_counter >= 1.0f){
drop_counter = 0.0f;
//cout <<"reset: " << drop_counter << "\n";
bool Grid_index_just_moved[10][22];
for(int i=0;i<10;i++){
for(int j=0;j<22;j++){
Grid_index_just_moved[i][j] = false;
}
}
for(int i=0;i<10;i++){
for(int j=0;j<22;j++){
if(Grid_index_filled[i][j] && j != 21 && !Grid_index_just_moved[i][j]){
if(!Grid_index_filled[i][j+1]){
Grid_index_filled[i][j+1] = true;
Grid_index_filled[i][j] = false;
Grid_index_just_moved[i][j+1] = true;
}
}
}
}
currentHeight +=1;
if(currentHeight >= 22){
CheckForLine();
Generate_Piece();
}
}
}
void Tetris::CheckForLine(){
int block_count = 0;
for(int j=0;j<10;j++){
if(Grid_index_filled[j][21]){
block_count +=1;
}
}
if(block_count>=9){
for(int j=0;j<10;j++){
if(Grid_index_filled[j][21]){
Grid_index_filled[j][21] = false;
}
}
line_count +=1;
cout<<"Current Line Count: " << line_count <<"\n";
}
}
void Tetris::Update_timer(float temp_time){
delta_t = temp_time;
}
void Tetris::MoveLeft(){
for(int i=0;i<10;i++){
for(int j=0;j<22;j++){
if(Grid_index_filled[i][j] && i != 0){
if(!Grid_index_filled[i-1][j]){
Grid_index_filled[i-1][j] = true;
Grid_index_filled[i][j] = false;
//Grid_index_just_moved[i][j+1] = true;
}
}
}
}
}
void Tetris::MoveRight(){
for(int i=9;i>=0;i--){
for(int j=0;j<22;j++){
if(Grid_index_filled[i][j] && i < 9){
if(!Grid_index_filled[i+1][j]){
Grid_index_filled[i+1][j] = true;
Grid_index_filled[i][j] = false;
//Grid_index_just_moved[i][j+1] = true;
}
}
}
}
}
Hoped this refresher helped! Be sure to leave feedback