Sign in to follow this  
YetAnotherMatt

I Made Tetris

Recommended Posts

It wont lemme post in Drew Bentons' original thread... DreW: http://www.gamedev.net/community/forums/profile.asp?mode=display&id=64367 His Thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=364087&forum_id=11&gforum_id=0 But, I made tetris. Finally, and wanted someone to know about it. Here's the answers to Drew's Questions: -If you have made Tetris: I did! How did you go about it, did you start and work though it or did you use tutorials to help along the way? I used ALOT of tutorials, like it's all technically 3-D, cause I like Nehe's tutorials. I don't exercise alot of 3-D functionality, but it's the frame work. How did you implement it, most notably how did you store the board and the pieces and how did you handle those interactions? This was a messy pain in the butt, I just pliowed through it. My board is an array, with interger values denoting what type of square goes there. Some numbers are special and denote borders of the game board, etc. How long did it take? About 4 days, 6 hours a day. Would you reccommend beginner game programmers that are just starting out to make this game? I dunno, I guess in the end I had alot of fun. It wasn't easy. Is Tetris the kind of game you would want to make? Why? I made it, I didn't want to - but I read yer post and it WAS a challenge. HEre's some CODE! It's dirty, and nasty and FUNKY.... and I had a few beers to celebrate. My fancy board array:
int board[MAXX][MAXY] =
{
                     {9,0,0,0,0,0,0,0,0,0,0,0,0,9},
                     {9,0,0,0,0,0,0,0,0,0,0,0,0,9},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,0,0,0,0,0,0,0,0,0,0,0,0,8},
                     {8,8,8,8,8,8,8,8,8,8,8,8,8,8}
};

HEre's my DrawSquares function:
void Square(int x, int y, int color)
{
	glLoadIdentity();									// Reset The Current Modelview Matrix
    glTranslatef(-6.0f, 12.0f, trans_z);
    glTranslatef(float(y), -float(x), 0);

          // Pick color
          if (color == 1) glColor3f(1.0f,0.0f,0.0f);
          if (color == 2) glColor3f(0.0f,1.0f,0.0f);
          if (color == 3) glColor3f(0.0f,0.0f,1.0f);
          if (color == 4) glColor3f(1.0f,1.0f,0.0f);
          if (color == 5) glColor3f(0.0f,1.0f,1.0f);
          if (color == 6) glColor3f(1.0f,0.0f,1.0f);
          if (color == 7) glColor3f(0.45f,0.0f,0.75f);
          if (color == 8) glColor3f(0.50f,0.50f,0.50f); // Special border color
          if (color == 9) glColor3f(0.1f,0.05f,0.1f); // Special BLANK marker
          if (color == 10) glColor3f(1.0f,1.0f,1.0f); // Special EMPTY ME marker

    glBegin(GL_QUADS);						            // Draw A Quad
          glVertex3f( 0.0f, 0.0f, 0.0f);				// Top Left
          glVertex3f( 1.0f, 0.0f, 0.0f);				// Top Right
	      glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
          glVertex3f( 0.0f,-1.0f, 0.0f);				// Bottom Left
    glEnd();							// Done Drawing The Quad
}


AND, now - prepare to barf as I show ya just how nasty I did some piece rotation: (took graphpaper and stuff)
                       switch (color)
                        {
                            case 1:
                                switch (orient)
                                {
                                    case 1:
                                        if(board[piece[0][0]-2][piece[1][0]+0]==0 &&
                                           board[piece[2][0]-1][piece[3][0]-1]==0 &&
                                           board[piece[0][1]+0][piece[1][1]+0]==0 &&
                                           board[piece[2][1]+1][piece[3][1]+1]==0)
                                        {
                                            piece[0][0]-=2;
                                            piece[1][0]+=0;
                                            piece[2][0]-=1;
                                            piece[3][0]-=1;
                                            piece[0][1]+=0;
                                            piece[1][1]+=0;
                                            piece[2][1]+=1;
                                            piece[3][1]+=1;
                                        }
                                        else orient--;
                                        break;
                                    case 2:
                                        if(board[piece[0][0]+0][piece[1][0]+2]==0 &&
                                           board[piece[2][0]-1][piece[3][0]+1]==0 &&
                                           board[piece[0][1]+0][piece[1][1]+0]==0 &&
                                           board[piece[2][1]+1][piece[3][1]-1]==0)
                                        {
                                            piece[0][0]+=0;
                                            piece[1][0]+=2;
                                            piece[2][0]-=1;
                                            piece[3][0]+=1;
                                            piece[0][1]+=0;
                                            piece[1][1]+=0;
                                            piece[2][1]+=1;
                                            piece[3][1]-=1;
                                        }
                                        else orient--;
                                        break;
                                    case 3:
                                        if(board[piece[0][0]+2][piece[1][0]+0]==0 &&
                                           board[piece[2][0]+1][piece[3][0]+1]==0 &&
                                           board[piece[0][1]+0][piece[1][1]+0]==0 &&
                                           board[piece[2][1]-1][piece[3][1]-1]==0)
                                        {
                                            piece[0][0]+=2;
                                            piece[1][0]+=0;
         

ETC.... Yep. Nasty, but it all works. :) Glad I could share some pieces of my nasty monstrosity. - Me

Share this post


Link to post
Share on other sites
I e-mailed the source, header, and Dev C++ *.dev file to you. :)

Bet yer sorry you asked now!

Note: PLEASE by all means compile it and look it over, but DONT use it as an example of fine programmuing. :) It WORKS. That's all I can say. It ain't pretty. For other folks:


main.cpp:

#include "main.h"

bool up;
bool lp;
bool rp;

bool HAVE_ACTIVE_PIECE = FALSE;
bool GAME_OVER = FALSE;

const int MAXX = 24;
const int MAXY = 14;

float trans_z = -30.0;
int n, m, color;
int orient = 1;

int score = 0;
int score_multiplier = 0;
int completed_lines = 0;
int level = 1;

int counter = 0;
int counter_down = 0;

int piece[4][2] =
{
{0,0},
{0,0},
{0,0},
{0,0}
};

int board[MAXX][MAXY] =
{
{9,0,0,0,0,0,0,0,0,0,0,0,0,9},
{9,0,0,0,0,0,0,0,0,0,0,0,0,9},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,0,0,0,0,0,0,0,0,0,0,0,0,8},
{8,8,8,8,8,8,8,8,8,8,8,8,8,8}
};

void DrawScore(void)
{

if (!GAME_OVER)
{
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-6.0f, 12.0f, trans_z);
// Pulsing Colors Based On Text Position
glColor3f( 1.0f * float(cos(cnt1)), 1.0f * float(sin(cnt2)), 1.0 + 1.0f * float(cos(cnt1+cnt2)));
// Position The Text On The Screen
glRasterPos2f(-9.0f + 1.25f * float(cos(cnt1)), -3.35f + 1.0f * float(sin(cnt2)));
// Print GL Text To The Screen
glPrint("Level: %d", level);

glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-6.0f, 12.0f, trans_z);

// Pulsing Colors Based On Text Position
glColor3f( 0.75f * float(cos(cnt1)), 0.75f * float(sin(cnt2)), 1.0 + 0.75f * float(cos(cnt1+cnt2)));
// Position The Text On The Screen
glRasterPos2f(
-9.0f + 0.5f * float(cos(cnt1)),
-6.35f + 0.25f * float(sin(cnt2)));
// Print GL Text To The Screen
glPrint("Lines Completed: %d", completed_lines);

glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-6.0f, 12.0f, trans_z);

// Pulsing Colors Based On Text Position
glColor3f( 0.5f * float(cos(cnt1)), 0.5f * float(sin(cnt2)), 1.0 + 0.5f * float(cos(cnt1+cnt2)));
// Position The Text On The Screen
glRasterPos2f(
-8.0f + 0.75f * float(cos(cnt1)),
-20.35f + 0.5f * float(sin(cnt2)));
// Print GL Text To The Screen
glPrint("Score: %i", score);
cnt1 += 0.050f; // Increase The First Counter
cnt2 += 0.005f; // Increase The Second Counter
}
else
{

glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, trans_z+0.5f);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-5.0f, -3.5f, 0.0f); // Top Left
glVertex3f( 5.0f, -3.5f, 0.0f); // Top Right
glVertex3f( 5.0f, +2.5f, 0.0f); // Bottom Right
glVertex3f(-5.0f, +2.5f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad

glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, trans_z+0.75f);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-4.8f, -3.3f, 0.0f); // Top Left
glVertex3f( 4.8f, -3.3f, 0.0f); // Top Right
glVertex3f( 4.8f, +2.3f, 0.0f); // Bottom Right
glVertex3f(-4.8f, +2.3f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad

glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, trans_z+1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glRasterPos2f(-2.0f, 1.0f);
glPrint("GAME OVER");
glRasterPos2f(-2.0f, -1.0f);
glPrint("SCORE: %d", score);
glRasterPos2f(-2.0f, -3.0f);
glPrint("LEVEL: %d", level);
}
}

void CheckForLines(void)
{
for (n = MAXX-2; n > 0; n--)
{
int bar = 0;

for (m = 1; m <= MAXY-2; m++)
{
if(board[n][m] != 0) bar++;
}
if (bar == MAXY-2)
{
for (int p = 1; p <= MAXY-2; p++)
board[n][p] = 10;
}
}
// Compact Board 4 times //
for(int z=0; z<4; z++)
{
// Cycle through lines stating at 1
for(n = MAXX-2; n > 0; n--)
{
// Does this line start with a 10, meaning it's complete?
if(board[n][1]==10)
{
completed_lines++;
if(completed_lines%10 == 0) level++;
score_multiplier++;
// It's a complete line, so compact it.
for(int p = n; p > 0; p--)
{
for(m = 1; m <= MAXY-2; m++)
{
board[p][m] = board[p-1][m];
}
}
}
}
}
switch(score_multiplier)
{
case 1:
score+=100;
break;
case 2:
score+=300;
break;
case 3:
score+=700;
break;
case 4:
score+=1000;
break;
}
score_multiplier=0;
}

void StepPieceDown(void)
{
board[piece[0][0]][piece[1][0]]=0;
board[piece[2][0]][piece[3][0]]=0;
board[piece[0][1]][piece[1][1]]=0;
board[piece[2][1]][piece[3][1]]=0;

if(board[piece[0][0]+1][piece[1][0]]==0 &&
board[piece[2][0]+1][piece[3][0]]==0 &&
board[piece[0][1]+1][piece[1][1]]==0 &&
board[piece[2][1]+1][piece[3][1]]==0)
{
piece[0][0]++;
piece[2][0]++;
piece[0][1]++;
piece[2][1]++;
}
else HAVE_ACTIVE_PIECE = FALSE;

board[piece[0][0]][piece[1][0]]=color;
board[piece[2][0]][piece[3][0]]=color;
board[piece[0][1]][piece[1][1]]=color;
board[piece[2][1]][piece[3][1]]=color;
}

void MakePiece(void)
{
orient = 1;
color = (rand()%7)+1;
switch (color)
{
case 1:
piece[0][0]= 0;
piece[1][0]= 6;
piece[2][0]= 1;
piece[3][0]= 6;
piece[0][1]= 1;
piece[1][1]= 7;
piece[2][1]= 1;
piece[3][1]= 8;
break;
case 2:
// L in offsets
piece[0][0]= 0;
piece[1][0]= 8;
piece[2][0]= 1;
piece[3][0]= 8;
piece[0][1]= 1;
piece[1][1]= 7;
piece[2][1]= 1;
piece[3][1]= 6;
break;
case 3:
// Op Z in offsets
piece[0][0]= 1;
piece[1][0]= 6;
piece[2][0]= 1;
piece[3][0]= 7;
piece[0][1]= 0;
piece[1][1]= 7;
piece[2][1]= 0;
piece[3][1]= 8;
break;
case 4:
// Z in offsets
piece[0][0]= 0;
piece[1][0]= 6;
piece[2][0]= 0;
piece[3][0]= 7;
piece[0][1]= 1;
piece[1][1]= 7;
piece[2][1]= 1;
piece[3][1]= 8;
break;
case 5:
// pipe in offsets
piece[0][0]= 0;
piece[1][0]= 5;
piece[2][0]= 0;
piece[3][0]= 6;
piece[0][1]= 0;
piece[1][1]= 7;
piece[2][1]= 0;
piece[3][1]= 8;
break;
case 6:
//square in offsets
piece[0][0]= 0;
piece[1][0]= 6;
piece[2][0]= 0;
piece[3][0]= 7;
piece[0][1]= 1;
piece[1][1]= 6;
piece[2][1]= 1;
piece[3][1]= 7;
break;
case 7:
// T in offsets
piece[0][0]= 0;
piece[1][0]= 7;
piece[2][0]= 1;
piece[3][0]= 6;
piece[0][1]= 1;
piece[1][1]= 7;
piece[2][1]= 1;
piece[3][1]= 8;
break;
default:
break;
}
if(board[piece[0][0]][piece[1][0]]==0 &&
board[piece[2][0]][piece[3][0]]==0 &&
board[piece[0][1]][piece[1][1]]==0 &&
board[piece[2][1]][piece[3][1]]==0)
{
board[piece[0][0]][piece[1][0]]=color;
board[piece[2][0]][piece[3][0]]=color;
board[piece[0][1]][piece[1][1]]=color;
board[piece[2][1]][piece[3][1]]=color;
}
else GAME_OVER = TRUE;
}

void Square(int x, int y, int color)
{
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-6.0f, 12.0f, trans_z);
glTranslatef(float(y), -float(x), 0);

// Pick color
if (color == 1) glColor3f(1.0f,0.0f,0.0f);
if (color == 2) glColor3f(0.0f,1.0f,0.0f);
if (color == 3) glColor3f(0.0f,0.0f,1.0f);
if (color == 4) glColor3f(1.0f,1.0f,0.0f);
if (color == 5) glColor3f(0.0f,1.0f,1.0f);
if (color == 6) glColor3f(1.0f,0.0f,1.0f);
if (color == 7) glColor3f(0.45f,0.0f,0.75f);
if (color == 8) glColor3f(0.50f,0.50f,0.50f); // Special border color
if (color == 9) glColor3f(0.1f,0.05f,0.1f); // Special BLANK marker
if (color == 10) glColor3f(1.0f,1.0f,1.0f); // Special EMPTY ME marker

glBegin(GL_QUADS); // Draw A Quad
glVertex3f( 0.0f, 0.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 0.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f( 0.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, trans_z-1.0f);
for (n=0; n < MAXX; n++)
{
for (m=0; m < MAXY; m++)
{
if (board[n][m] != 0)
{
Square(n, m, board[n][m]);
}
}
}
DrawScore();
return TRUE; // Everything Went OK
}

int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop
fullscreen=TRUE;

srand(time(NULL));

// Create Our OpenGL Window
if (!CreateGLWindow("Graphics Programming",1024,768,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received?
{
done=TRUE; // ESC or DrawGLScene Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
SwapBuffers(hDC); // Swap Buffers (Double Buffering)

if(!GAME_OVER)
{
if (++counter == 51-level)
{
counter = 0;
StepPieceDown();
}
// Is a new piece needed? If so, make piece
if (HAVE_ACTIVE_PIECE == FALSE)
{
CheckForLines();
HAVE_ACTIVE_PIECE = TRUE;
MakePiece();
}
// Move active piece
if (keys[VK_UP] && !up) // Is Up Arrow Being Pressed?
{
// Rotate The piece orient =1, 2,3,4 clockwise - color = type of block
up=TRUE;
if (orient != 4) orient++;
else orient = 1;

board[piece[0][0]][piece[1][0]]=0;
board[piece[2][0]][piece[3][0]]=0;
board[piece[0][1]][piece[1][1]]=0;
board[piece[2][1]][piece[3][1]]=0;

switch (color)
{
case 1:
switch (orient)
{
case 1:
if(board[piece[0][0]-2][piece[1][0]+0]==0 &&
board[piece[2][0]-1][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+1][piece[3][1]+1]==0)
{
piece[0][0]-=2;
piece[1][0]+=0;
piece[2][0]-=1;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=1;
piece[3][1]+=1;
}
else orient--;
break;
case 2:
if(board[piece[0][0]+0][piece[1][0]+2]==0 &&
board[piece[2][0]-1][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+1][piece[3][1]-1]==0)
{
piece[0][0]+=0;
piece[1][0]+=2;
piece[2][0]-=1;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=1;
piece[3][1]-=1;
}
else orient--;
break;
case 3:
if(board[piece[0][0]+2][piece[1][0]+0]==0 &&
board[piece[2][0]+1][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]-1][piece[3][1]-1]==0)
{
piece[0][0]+=2;
piece[1][0]+=0;
piece[2][0]+=1;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]-=1;
piece[3][1]-=1;
}
else orient--;
break;
case 4:
if(board[piece[0][0]+0][piece[1][0]-2]==0 &&
board[piece[2][0]+1][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]-1][piece[3][1]+1]==0)
{
piece[0][0]+=0;
piece[1][0]-=2;
piece[2][0]+=1;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]-=1;
piece[3][1]+=1;
}
else orient--;
break;
}
break;
case 2:
switch (orient)
{
case 1:
if(board[piece[0][0]+0][piece[1][0]+2]==0 &&
board[piece[2][0]+1][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]-1][piece[3][1]-1]==0)
{
piece[0][0]+=0;
piece[1][0]+=2;
piece[2][0]+=1;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]-=1;
piece[3][1]-=1;
}
else orient--;
break;
case 2:
if(board[piece[0][0]+2][piece[1][0]+0]==0 &&
board[piece[2][0]+1][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]-1][piece[3][1]+1]==0)
{
piece[0][0]+=2;
piece[1][0]+=0;
piece[2][0]+=1;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]-=1;
piece[3][1]+=1;
}
else orient--;
break;
case 3:
if(board[piece[0][0]+0][piece[1][0]-2]==0 &&
board[piece[2][0]-1][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+1][piece[3][1]+1]==0)
{
piece[0][0]+=0;
piece[1][0]-=2;
piece[2][0]-=1;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=1;
piece[3][1]+=1;
}
else orient--;
break;
case 4:
if(board[piece[0][0]-2][piece[1][0]+0]==0 &&
board[piece[2][0]-1][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+1][piece[3][1]-1]==0)
{
piece[0][0]-=2;
piece[1][0]+=0;
piece[2][0]-=1;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=1;
piece[3][1]-=1;
}
else orient--;
break;
}
break;
case 3:
switch (orient)
{
case 1:
case 3:
if(board[piece[0][0]+2][piece[1][0]-1]==0 &&
board[piece[2][0]+0][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+0][piece[3][1]+0]==0)
{
piece[0][0]+=2;
piece[1][0]-=1;
piece[2][0]+=0;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=0;
piece[3][1]+=0;
}
else orient--;
break;
case 2:
case 4:
if(board[piece[0][0]-2][piece[1][0]+1]==0 &&
board[piece[2][0]+0][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+0][piece[3][1]+0]==0)
{
piece[0][0]-=2;
piece[1][0]+=1;
piece[2][0]+=0;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=0;
piece[3][1]+=0;
}
else orient--;
break;
}
break;
case 4:
switch (orient)
{
case 1:
case 3:
if(board[piece[0][0]-2][piece[1][0]+0]==0 &&
board[piece[2][0]+0][piece[3][0]+0]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+0][piece[3][1]+2]==0)
{
piece[0][0]-=2;
piece[1][0]+=0;
piece[2][0]+=0;
piece[3][0]+=0;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=0;
piece[3][1]+=2;
}
else orient--;
break;
case 2:
case 4:
if(board[piece[0][0]+2][piece[1][0]+0]==0 &&
board[piece[2][0]+0][piece[3][0]+0]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+0][piece[3][1]-2]==0)
{
piece[0][0]+=2;
piece[1][0]+=0;
piece[2][0]+=0;
piece[3][0]+=0;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=0;
piece[3][1]-=2;
}
else orient--;
break;
}
break;
case 5:
switch (orient)
{
case 1:
case 3:
if(board[piece[0][0]+2][piece[1][0]-2]==0 &&
board[piece[2][0]+1][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]-1][piece[3][1]+1]==0)
{
piece[0][0]+=2;
piece[1][0]-=2;
piece[2][0]+=1;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]-=1;
piece[3][1]+=1;
}
else orient--;
break;
case 2:
case 4:
if(board[piece[0][0]-2][piece[1][0]+2]==0 &&
board[piece[2][0]-1][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+1][piece[3][1]-1]==0)
{
piece[0][0]-=2;
piece[1][0]+=2;
piece[2][0]-=1;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=1;
piece[3][1]-=1;
}
else orient--;
break;
}
break;
case 6:
break;
case 7:
switch (orient)
{
case 1:
if(board[piece[0][0]-1][piece[1][0]+1]==0 &&
board[piece[2][0]-1][piece[3][0]-1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+1][piece[3][1]+1]==0)
{
piece[0][0]-=1;
piece[1][0]+=1;
piece[2][0]-=1;
piece[3][0]-=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=1;
piece[3][1]+=1;
}
else orient--;
break;
case 2:
if(board[piece[0][0]+0][piece[1][0]+0]==0 &&
board[piece[2][0]+1][piece[3][0]+1]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+0][piece[3][1]+0]==0)
{
piece[0][0]+=0;
piece[1][0]+=0;
piece[2][0]+=1;
piece[3][0]+=1;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=0;
piece[3][1]+=0;
}
else orient--;
break;
case 3:
if(board[piece[0][0]+1][piece[1][0]-1]==0 &&
board[piece[2][0]+0][piece[3][0]+0]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]+0][piece[3][1]+0]==0)
{
piece[0][0]+=1;
piece[1][0]-=1;
piece[2][0]+=0;
piece[3][0]+=0;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]+=0;
piece[3][1]+=0;
}
else orient--;
break;
case 4:
if(board[piece[0][0]+0][piece[1][0]+0]==0 &&
board[piece[2][0]+0][piece[3][0]+0]==0 &&
board[piece[0][1]+0][piece[1][1]+0]==0 &&
board[piece[2][1]-1][piece[3][1]-1]==0)
{
piece[0][0]+=0;
piece[1][0]+=0;
piece[2][0]+=0;
piece[3][0]+=0;
piece[0][1]+=0;
piece[1][1]+=0;
piece[2][1]-=1;
piece[3][1]-=1;
}
else orient--;
break;
}
break;
}
board[piece[0][0]][piece[1][0]]=color;
board[piece[2][0]][piece[3][0]]=color;
board[piece[0][1]][piece[1][1]]=color;
board[piece[2][1]][piece[3][1]]=color;
}
if (!keys[VK_UP]) up=FALSE;
if (keys[VK_DOWN])
{
if(counter_down == 3)
{
//move piece down
board[piece[0][0]][piece[1][0]]=0;
board[piece[2][0]][piece[3][0]]=0;
board[piece[0][1]][piece[1][1]]=0;
board[piece[2][1]][piece[3][1]]=0;

if(board[piece[0][0]+1][piece[1][0]]==0 &&
board[piece[2][0]+1][piece[3][0]]==0 &&
board[piece[0][1]+1][piece[1][1]]==0 &&
board[piece[2][1]+1][piece[3][1]]==0)
{
piece[0][0]++;
piece[2][0]++;
piece[0][1]++;
piece[2][1]++;
}
else HAVE_ACTIVE_PIECE = FALSE;
board[piece[0][0]][piece[1][0]]=color;
board[piece[2][0]][piece[3][0]]=color;
board[piece[0][1]][piece[1][1]]=color;
board[piece[2][1]][piece[3][1]]=color;
counter_down = 0;
}
else counter_down++;
}
if (keys[VK_RIGHT] && !rp)
{
rp=TRUE;
board[piece[0][0]][piece[1][0]]=0;
board[piece[2][0]][piece[3][0]]=0;
board[piece[0][1]][piece[1][1]]=0;
board[piece[2][1]][piece[3][1]]=0;
if(board[piece[0][0]][piece[1][0]+1]==0 &&
board[piece[2][0]][piece[3][0]+1]==0 &&
board[piece[0][1]][piece[1][1]+1]==0 &&
board[piece[2][1]][piece[3][1]+1]==0)
{
piece[1][0]++;
piece[3][0]++;
piece[1][1]++;
piece[3][1]++;
}
board[piece[0][0]][piece[1][0]]=color;
board[piece[2][0]][piece[3][0]]=color;
board[piece[0][1]][piece[1][1]]=color;
board[piece[2][1]][piece[3][1]]=color;
}
if (!keys[VK_RIGHT]) rp=FALSE;
if (keys[VK_LEFT] && !lp)
{
lp=TRUE;
board[piece[0][0]][piece[1][0]]=0;
board[piece[2][0]][piece[3][0]]=0;
board[piece[0][1]][piece[1][1]]=0;
board[piece[2][1]][piece[3][1]]=0;
if(board[piece[0][0]][piece[1][0]-1]==0 &&
board[piece[2][0]][piece[3][0]-1]==0 &&
board[piece[0][1]][piece[1][1]-1]==0 &&
board[piece[2][1]][piece[3][1]-1]==0)
{
piece[1][0]--;
piece[3][0]--;
piece[1][1]--;
piece[3][1]--;
}
board[piece[0][0]][piece[1][0]]=color;
board[piece[2][0]][piece[3][0]]=color;
board[piece[0][1]][piece[1][1]]=color;
board[piece[2][1]][piece[3][1]]=color;
}
if (!keys[VK_LEFT]) lp=FALSE;
}
if (keys[VK_PRIOR]) trans_z +=0.5;
if (keys[VK_NEXT]) trans_z -=0.5;
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("Graphics Programming",1024,768,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}
}

// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}



main.h:

#include <windows.h> // Header File For Windows
#include <math.h> // Header File For Math (cos and sin)
#include <time.h>
#include <stdarg.h> // Header File For Variable Argument Routines ( ADD )
#include <stdio.h> // Header File For Standard Input/Output
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default

GLuint base; // Base Display List For The Font Set
GLfloat cnt1; // 1st Counter Used To Move Text & For Coloring
GLfloat cnt2; // 2nd Counter Used To Move Text & For Coloring

//GLuint texture[1]; // preserve space for 1 texture

//int LoadGLTextures(void);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

GLvoid BuildFont(GLvoid) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
HFONT oldfont; // Used For Good House Keeping

base = glGenLists(96); // Storage For 96 Characters

font = CreateFont( -24, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Courier New"); // Font Name

oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
SelectObject(hDC, oldfont); // Selects The Font We Want
DeleteObject(font); // Delete The Font
}

GLvoid KillFont(GLvoid) // Delete The Font List
{
glDeleteLists(base, 96); // Delete All 96 Characters
}

GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments

if (fmt == NULL) // If There's No Text
return; // Do Nothing

va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text

glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}

File=fopen(Filename,"r"); // Check To See If The File Exists

if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}

return NULL; // If Load Failed Return NULL
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
//LoadGLTextures();

glEnable(GL_TEXTURE_2D);
glClearColor(0.1f,0.05f,0.1f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
BuildFont();
return TRUE; // Initialization Went OK
}

GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}

if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}

if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}

if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
KillFont();
}

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height

fullscreen=fullscreenflag; // Set The Global Fullscreen Flag

hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name

if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=TRUE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
dwStyle=WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size

// Create The Window
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title, // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

ShowWindow(hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen

if (!InitGL()) // Initialize Our Newly Created GL Window
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

return TRUE; // Success
}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
active=TRUE; // Program Is Active
}
else
{
active=FALSE; // Program Is No Longer Active
}

return 0; // Return To The Message Loop
}

case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}

case WM_CLOSE: // Did We Receive A Close Message?
{
PostQuitMessage(0); // Send A Quit Message
return 0; // Jump Back
}

case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
return 0; // Jump Back
}

case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}

case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
}

// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
/*
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=TRUE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("tim.bmp"))
{
glGenTextures(1, &texture[0]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
} else Status=FALSE; // Failure to load image
if (TextureImage[0]) { // If Texture Exists
if (TextureImage[0]->data) { // If Texture Image Exists
free(TextureImage[0]->data); // Free The Texture Image Memory
} free(TextureImage[0]); // Free The Image Structure
}
return Status; // Return The Status
}
*/




Sorry it's so ugly! :) It's for windows, uses OpenGL, and works well in the Dev C++ programming IDE. :)

Thank you Nehe, and thank you GameDev!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this