GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


broken reversi board by Icetigris
#include <stdio.h>
#include <stdlib.h>
#include <math.h>    // Header File For floor() and ceil()
#include <windows.h>  // Header File For Windows
#include <gl\gl.h>      // Header File For The OpenGL32 Library
#include <gl\glu.h>      // Header File For The GLU32 Library
#include <gl\glut.h>  // Header File For The GLUT32 Library

//height and width of window
GLsizei winHeight = 500, winWidth = 500;

//initialise board dimensions
GLfloat boardX = 200, boardY = 200;

static GLfloat spin = 0.0;
GLfloat pieceR = 0.0, pieceG = 0.0, pieceB = 0.0;
GLfloat cellSizeX = (GLfloat)boardX/9;
GLfloat cellSizeY = (GLfloat)boardY/9;

bool lineTest(int mousePosX, int mousePosY)
{
    bool lineTest;
    
    float floatTestX = mousePosX/cellSizeX;
    int floatTestIntX = (int)floatTestX;

    float floatTestY = mousePosY/cellSizeY;
    int floatTestIntY = (int)floatTestY;

    if(floatTestX == floatTestIntX || floatTestY == floatTestIntY)
    {
        lineTest = true;
    }
    else
    {
        lineTest = false;
    }

    return lineTest;
     printf("Done with lineTest\n");
}

void drawBoard(void)//(boardX, boardY)?
{
    //BEGIN
    glBegin(GL_QUADS);

    //need a square the size of the whole game board
    glVertex2f(-boardX, boardY);                // Top Left
    glVertex2f(boardX, boardY);                 // Top Right
    glVertex2f(boardX, -boardY);                // Bottom Right
    glVertex2f(-boardX, -boardY);                // Bottom Left

    //maek it PURPLE, hexRGB=7F00FF
    glColor3f(0.5, 0.0, 1.0);
    
    //END
    glEnd();
    glFlush();
 printf("Done with drawBoard\n");

}

void drawGridLines(void)
{
    //variables
    int i,j;

    //BEGIN
    glBegin(GL_LINES);

    //need 9x9 lines to make an 8x8 cell grid
    for(i = 0; i < (boardX/9); i = i + (boardX/9))
    {
        for(j = 0; j < (boardY/9); j = j + (boardY/9))
        {
            //make a grid of lines
            glVertex2i(i+1, j+1);                                    // Top Left
            glVertex2i((i+(boardX/9)), j+1);                        // Top Right
            glVertex2i((i+(boardX/9)), (j+(boardY/9)));            // Bottom Right
            glVertex2i(i+1, (j+(boardY/9)));                        // Bottom Left

            //set colour of lines to BLACK
            glColor3f(0.0, 0.0, 0.0);

        }
    }

    //END
    glEnd();
    glFlush();
 printf("Done with drawGridLines\n");
}


void drawPiece(float pieceR, float pieceG, float pieceB, GLfloat cellX, GLfloat cellY)
{
    //variables
    int a;
    int sections = 20; //number of triangles to use to estimate a circle(a higher number yields a more perfect circle)
    GLfloat radius = (GLfloat)boardX/18; //radius = (board width)/(number of grid lines) * 0.5
    GLfloat twoPi = 2.0f * 3.14159f;
    GLfloat circleCenterX, circleCenterY;

    //BEGIN
    glBegin(GL_TRIANGLE_FAN);

    //figure out x and y coordinates of the piece's center
    bool onTheLine;
    float ceilingX = ceil(cellX/cellSizeX);
    float ceilingY = ceil(cellY/cellSizeY);
    float floorX = floor(cellX/cellSizeX);
    float floorY = floor(cellY/cellSizeY);
    onTheLine = lineTest(cellX, cellY);

    //if we didn't click right on a grid line
    if(!onTheLine)
    {
        //if mouse x coordinate is negative
        if(cellX < 0)
        {
            circleCenterX = (cellSizeX * ceilingX - 1) - (cellSizeX/2);
        }
        else
        {
            circleCenterX = (cellSizeX * floorX - 1) - (cellSizeX/2);
        }

        //if mouse y coordinate is negative
        if(cellY < 0)
        {
            circleCenterY = (cellSizeY * ceilingY - 1) - (cellSizeY/2);
        }
        else
        {
            circleCenterY = (cellSizeY * floorY - 1) - (cellSizeY/2);
        }
    }
    else
    {
        printf("You clicked directly on a grid line, wtf are we supposed to put your piece?\n");
    }

    //centre of cell in which mouse clicked, origin for the trifan
    glVertex2f(circleCenterX, circleCenterY);

    //horrible approximation of a circle in area mouse clicked
    for(a = 0; a <= sections; a++)
    {
        // make $section number of circles
        glVertex2f(radius * cos(a *  twoPi / sections), radius* sin(a * twoPi / sections));

        //set colour to input RGB values
        glColor3f(pieceR, pieceG, pieceB);
    }
    
    //END
    glEnd();
    glFlush();

    printf("Done with drawPiece\n");

}

void init(void)
{
    //set shit up
    glClearColor (1.0, 1.0, 1.0, 0.0);   //  Display-window color = white.
    glShadeModel (GL_FLAT);
    drawBoard();
    drawGridLines();
    printf("Done with init\n");
}

void winReshapeFcn (int newWidth, int newHeight)
{
    //resize the window
   winWidth = newWidth;
   winHeight = newHeight;

   glViewport (0, 0, (GLsizei) newWidth, (GLsizei) newHeight);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-(winHeight/2), (winWidth/2), -(winHeight/2), (winWidth/2), -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   printf("Done with winReshapeFcn\n");
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~Mouseshit variable declarations~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GLfloat winX, winY, winZ;                // Holds Our X, Y and Z Coordinates
GLdouble posX, posY, posZ;                // Hold The Final Values

void mouseFunc(GLint button, GLint action, int x, int y)
{
    //single click: draw piece in square
    GLdouble modelview[16];                //Where The 16 Doubles Of The Modelview Matrix Are To Be Stored
    GLdouble projection[16];            //Where The 16 Doubles Of The Projection Matrix Are To Be Stored
    GLint viewport[4];                    //Where The Viewport Values Will Be Stored

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);    //Retrieve The Modelview Matrix
    glGetDoublev(GL_PROJECTION_MATRIX, projection);    //Retrieve The Projection Matrix
    glGetIntegerv(GL_VIEWPORT, viewport);            //Retrieves The Viewport Values (X, Y, Width, Height)

    winX = (float)x;                            //Mouse x coordinate
    winY = (float)viewport[3] - (float)y;        //Mouse y coordinate moved from top left to bottom left

    //buh
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

    //make posX, posY, posZ global so gluUnProject can dump into them and anything can use them
    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

    /*
    * We need to see if the input mouse coordinates are on a grid line or not
    * so that if they are, we don't put a piece in an arbitrary place.
    * If you click on a grid line, casting the float to an int won't matter because they
    * will be equal
    */
    bool didYouClickTheLine;
    didYouClickTheLine = lineTest(posX,posY);

    if(!didYouClickTheLine)
    {
        //if you didn't click right on a grid line, we know where to draw a piece
        drawPiece(0.0,0.0,0.0, posX, posY);
    }

printf#t2#7--);
}

void keyboardFunc(unsigned char key, int x, int y)
{
    //arrow keys move board around
    //R button rotates board by 5 degrees
    //H is scale height up
    //N is scale height down
    //W is scale width up
    //S is scale width down
    //P is pass turn
    //Spacebar is reset the game
    printf#t2#8--);
}

void rotateBoard(void)
{
    //calculate new angle of orientation for the board's coordinate system
    spin = spin + 5.0;
    if (spin > 360.0)
    {
        spin = spin - 360.0;
    }

    //actually rotate game board
    glPushMatrix();
    glRotatef(spin, 0.0, 0.0, 1.0);
    glPopMatrix();

    //tell the display function to redraw it's shit
    glutPostRedisplay();
    printf#t2#9--);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~DISPLAY FUNCTION IS A BIG DEAL~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void display(void)
{
    //HOLY SHIT WE NEED TO REDRAW:
    glClear (GL_COLOR_BUFFER_BIT);

    drawBoard();
    drawGridLines();

    //the board when it's moved
    //the board when it's rotated
    //the board when it's scaled
    
    //new game pieces
    glFlush();
    printf("Done with display\n");

}

/******************************************************************************
 ******************************************************************************
 *~~~~~~~~~~~~~~~~~~~~~~~MAIN FUNCTION IS MAIN~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ******************************************************************************
 ******************************************************************************
*/
void main (int argc, char** argv)
{
   //the usual crap
   printf("Main start\n");
   glutInit(&argc, argv);
   printf("glutInit done\n");
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
   printf("glutInitDisplayMode done\n");
   glutInitWindowSize (500, 500); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow ("Reversi");
   init ();
   glutDisplayFunc(display); 
   glutReshapeFunc(winReshapeFcn); 
   glutMouseFunc(mouseFunc);
   glutKeyboardFunc(keyboardFunc);
   glutMainLoop();
}