Jump to content
  • Advertisement
Sign in to follow this  
RiseAgainst

Newbie To OpenGL With A Problem...

This topic is 4904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys, im a new openGL programmer and im working my way through some usual activities in getting to know a language and im making a simple slider puzzle, its currently only around half done and ive come up with a problem. It checks for a win by looping through the array making sure each element is equal to its position in the array (i.e array[1] = 1 and so on) and if its as it should be the int win is set to 1 and a message displayed. however it doesnt work even though ive set the array to be as it should be in a winning state. If i change it when i declare it to be set to 1 then the message is correctly displayed...help! *ps how do you post code properly? ill add it in just copy/paste form but know there is a better way just dont know myself :/
/*
 *  Martins Game
 *  
 */

#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>           
#include <GL/glut.h>            
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(linker,"/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") //Removes console window

// ************ Function Prototypes ************

void display(void);
void mouse(int, int ,int , int );
void Board();
int checkWin();
void Square(int,int);

// ************ Global Variables ******************
int sqsize = 50;
//int off_array[] = {1,3,2,6,4,5,9,7,8,10,12,11,13,15,14,16}; 
int off_array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int MAX_PIECES = 16;
int MAX_ROW = 4;
int MAX_COL = 4;
int win = 0;
int Blank = 1;
int valid = 0;

// ************ Function Implementation ************

//Used to print Text
void Text_Display( int x, int y, char *st)
{
	int l,i;

	l=strlen( st ); // see how many characters are in text string.
	glRasterPos2i( x, y); // location to start printing text
	for( i=0; i < l; i++)  // loop until i is greater then l
		{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, st); // Print a character on the screen
	}

}

void Square(int row, int col) // Draw a single square
{
	int sqx, sqy ;
	int startx=50, starty=50  ;
	
	Board();						//Reset board first
	glColor3f(1.0, 1.0, 1.0);		//Use Pink as colour
	sqx = startx + (col*sqsize);	//Calculate screen x-coord
	sqy = starty + (row*sqsize);	//Calculate screen y-coord
	/*glBegin(GL_POLYGON);			//Draw square
        glVertex2i(sqx , sqy);
		glVertex2i(sqx + sqsize, sqy);
		glVertex2i(sqx+ sqsize, sqy+ sqsize);
		glVertex2i(sqx, sqy+ sqsize);
    glEnd();*/
	glFlush();
}

void Board()				//Draw chess board
{
	int row, col ;
	int startx=150, starty=150  ;
    
	for (row=0 ; row<4 ;row++)
	{
		for (col=0 ; col<4 ; col++)
		{
			if(row+col == 0) glColor3f(1.0,0.0,0.0);

			else if (((row+col) % 2) == 0) glColor3f(0.0, 0.0, 0.0);
			    
				else glColor3f(0.0, 0.0, 0.0);			
			
				glBegin(GL_POLYGON);	//Draw square
                glVertex2i(startx , starty);
				glVertex2i(startx + sqsize, starty);
				glVertex2i(startx+ sqsize, starty+ sqsize);
				glVertex2i(startx, starty+ sqsize);
    		glEnd();
			startx += sqsize;
			glColor3f(1.0, 0.0, 1.0);
			Text_Display(0, 0, "Martins Slider Game");
		}
		starty+=sqsize;
		startx = 150 ;
		
		if (win==1)
	{
		glColor3f(1.0, 1.0, 1.0);
		Text_Display(0, 0, "Winner");
	}
	}

		glFlush();
}


void display(void)
{
/* set clear color to black and clear window */
		glMatrixMode (GL_MODELVIEW);
        glClearColor (0.0, 1.0, 1.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);

		Board();
/* flush GL buffers */

        glFlush();

}

void mouse(int btn, int state, int x, int y)
{
	int xc,yc,sqrow,sqcol;
	int row, col;
	int selected_index;
	int MAX_ROW = 4;
	int MAX_COL = 4;

	if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)  
	{
		xc=x-50;				//Express coord (xc) relative to board (lower left)
		yc=450-y;				//Express coord (yc) relative to board (lower left)
		sqrow = (yc / sqsize) ;	//Change to square number by dividing by square size
		sqcol = (xc / sqsize) ;
		
		row = Blank / MAX_ROW;
        col = Blank - (row * MAX_ROW);
		selected_index = col + (row * MAX_ROW);

        // Check the piece right above the blank piece.
        if (row > 0)
            if (selected_index == (Blank - MAX_ROW))
                valid = 1;
        // Check the piece right below the blank piece.
        if (row < (MAX_ROW - 1))
            if (selected_index == (Blank + MAX_ROW))
                valid = 1;
        // Check the piece to the left of the blank piece
        if (col > 0)
            if (selected_index == (Blank - 1))
                valid = 1;
        // Check the piece to the right of the blank piece
        if (col < (MAX_COL - 1))
            if (selected_index == (Blank + 1))
                valid = 1;

        valid = 0;

	if (valid == 1)
		{
			int temp = off_array [selected_index];
            off_array [selected_index] = off_array [Blank];
            off_array [Blank] = temp;
            Blank = selected_index;
		}

	checkWin();

	if ( ((xc<300) && (yc<300)) && ( (xc>100) && (yc>100) ) )
		{ // Don't do this if mouse isn't on the board
			Square(sqrow,sqcol);
		}
	}	
}


int checkWin()
{
		int i;
        int count = 0;
        for (i = 0; i < MAX_PIECES; i++)
        {
            if (off_array  == i) count ++;
        }
		if (count == MAX_PIECES) return win = 1;
			return win = 0;
	
}



int main(int argc, char** argv)
{

/* Initialize mode and open a window in upper left corner of screen */

        glutInit(&argc,argv);
        glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
        glutInitWindowSize(500,500);
        glutInitWindowPosition(100,100);
        glutCreateWindow("Martins Game");
        glutDisplayFunc(display);
		glutMouseFunc (mouse);
	
		glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
		//Specify orthographic projection view area
        gluOrtho2D(0.0, 500.0, 0.0, 500.0); 
		glMatrixMode (GL_MODELVIEW);
		//Enter event loop		
        glutMainLoop();


return 0 ;
}

Share this post


Link to post
Share on other sites
Advertisement
Use [ source ] [ /source ] tags (without spaces) to post code.
Have you tried to use the debugger? You can step through the code and see what goes wrong (I didn't read through it yet).

Share this post


Link to post
Share on other sites
have you tried stepping through your code in the debugger and watching the value of win?

also, to post your code put it in [*source][/source] brackets (without the * of course)

-me

Share this post


Link to post
Share on other sites
i tried stepping that and it doesnt seem to have a problem with it, maybe its how its called? im really out of ideas with this one now

Share this post


Link to post
Share on other sites
My first suggestion would be to change from single- to double-buffered:


void display(void)
{
/* set clear color to black and clear window */
glMatrixMode (GL_MODELVIEW);
/* load identity matrix, not necessary but clearer */
glLoadIdentity();

glClearColor (0.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

Board();

/* swap GL buffers */
glutSwapBuffers();

}

int main(int argc, char** argv)
{
/* Initialize mode and open a window in upper left corner of screen */
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
/* the rest stays the same */
}





This is just a shoot in the dark but might help.
[edit]
Also note that your win-state is only changed if you click into your board.
[/edit]

Share this post


Link to post
Share on other sites
Quote:
Original post by RiseAgainst
i tried stepping that and it doesnt seem to have a problem with it, maybe its how its called? im really out of ideas with this one now


You aren't stepping to see if it has a problem with stepping, you are stepping to watch the value of win as the program runs. Put a break-point in your checkWin() function, and then look to see what it does. If you don't hit that breakpoint, then your problem is that it isn't ever being called (see [edit] section of above post). If you do hit it, just trace through it to see what it does wrong.

-me

Share this post


Link to post
Share on other sites
ive decided to add in my own debug helper in the command window which shows the values of the variables during the process yet im still confused why its behaving like it is...thanks for your help so far, any help on getting past this hurdle and getting it to actually use the checkWin function?

 /*
* Martins Game
*
*/


#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#pragma comment(lib,"glu32.lib")
//#pragma comment(linker,"/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") //Removes console window

// ************ Function Prototypes ************

void display(void);
void mouse(int, int ,int , int );
void Board();
int checkWin();
void Square(int,int);

// ************ Global Variables ******************
int sqsize = 50;
//int off_array[] = {1,3,2,6,4,5,9,7,8,10,12,11,13,15,14,16};
int off_array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int MAX_PIECES = 16;
int MAX_ROW = 4;
int MAX_COL = 4;
int win = 0;
int Blank;
int valid = 0;

// ************ Function Implementation ************

//Used to print Text
void Text_Display( int x, int y, char *st)
{
int l,i;

l=strlen( st ); // see how many characters are in text string.
glRasterPos2i( x, y); // location to start printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, st); // Print a character on the screen
}

}

void Square(int row, int col) // Draw a single square
{
int sqx, sqy ;
int startx=50, starty=50 ;

Board(); //Reset board first
glColor3f(1.0, 1.0, 1.0); //Use Pink as colour
sqx = startx + (col*sqsize); //Calculate screen x-coord
sqy = starty + (row*sqsize); //Calculate screen y-coord
/*glBegin(GL_POLYGON); //Draw square
glVertex2i(sqx , sqy);
glVertex2i(sqx + sqsize, sqy);
glVertex2i(sqx+ sqsize, sqy+ sqsize);
glVertex2i(sqx, sqy+ sqsize);
glEnd();*/

glFlush();
}

void Board() //Draw chess board
{
int row, col ;
int startx=150, starty=150 ;
Blank = MAX_PIECES - (MAX_PIECES - 1);
for (row=0 ; row<4 ;row++)
{
for (col=0 ; col<4 ; col++)
{
if(row+col == 0) glColor3f(1.0,0.0,0.0);

else if (((row+col) % 2) == 0) glColor3f(0.0, 0.0, 0.0);

else glColor3f(0.0, 0.0, 0.0);

glBegin(GL_POLYGON); //Draw square
glVertex2i(startx , starty);
glVertex2i(startx + sqsize, starty);
glVertex2i(startx+ sqsize, starty+ sqsize);
glVertex2i(startx, starty+ sqsize);
glEnd();
startx += sqsize;
glColor3f(1.0, 0.0, 1.0);
Text_Display(0, 0, "Martins Slider Game");
}
starty+=sqsize;
startx = 150 ;

if (win==1)
{
glColor3f(1.0, 1.0, 1.0);
Text_Display(0, 0, "Winner");
}
}

glFlush();
}


void display(void)
{
/* set clear color to black and clear window */
glMatrixMode (GL_MODELVIEW);
/* load identity matrix, not necessary but clearer */
glLoadIdentity();

glClearColor (0.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

Board();

/* swap GL buffers */
glutSwapBuffers();

}


void mouse(int btn, int state, int x, int y)
{
int xc,yc;
int row, col;
int selected_index;
int MAX_ROW = 4;
int MAX_COL = 4;

if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
xc=x-50; //Express coord (xc) relative to board (lower left)
yc=450-y; //Express coord (yc) relative to board (lower left)
row = (yc / sqsize) - 1 ; //Change to square number by dividing by square size
col = (xc / sqsize) - 1;

selected_index = col + (row * MAX_ROW);

if ( ((xc<300) && (yc<300)) && ( (xc>100) && (yc>100) ) )
{ // Don't do this if mouse isn't on the board
Square(row,col);
if (row > 0)
// Check the piece right above the blank piece.
if (selected_index == (Blank - MAX_ROW))
valid = 1;
// Check the piece right below the blank piece.
if (row < (MAX_ROW - 1))
if (selected_index == (Blank + MAX_ROW))
valid = 1;
// Check the piece to the left of the blank piece
if (col > 0)
if (selected_index == (Blank - 1))
valid = 1;
// Check the piece to the right of the blank piece
if (col < (MAX_COL - 1))
if (selected_index == (Blank + 1))
valid = 1;

else valid = 0;
printf("win is %d and valid is %d\n",win,valid);
printf("Blank is %d\n",Blank);
printf("col is %d and row is %d\n",col,row);

if (valid == 1)
{
int temp = off_array [selected_index];
off_array [selected_index] = off_array [Blank];
off_array [Blank] = temp;
Blank = selected_index;
}

checkWin();
}
}
}


int checkWin()
{
int i;
int count = 0;
for (i = 0; i < MAX_PIECES; i++)
{
if (off_array == i) count ++;
}
if (count == MAX_PIECES) return win = 1;
else return win = 0;

}



int main(int argc, char** argv)
{

/* Initialize mode and open a window in upper left corner of screen */

glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Martins Game");
glutDisplayFunc(display);
glutMouseFunc (mouse);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//Specify orthographic projection view area
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode (GL_MODELVIEW);
//Enter event loop
glutMainLoop();


return 0 ;
}



** UPDATE : i now have it reporting the row & col variables correctly as they were being reported as zero.

Share this post


Link to post
Share on other sites
Sounds like an array "off by one" problem.

try this

int checkWin()
{
int i;
int count = 0;
win = 0;
for (i = 1; i <= MAX_PIECES; i++) // Changed
{
if (off_array [i-1] == i) count ++; //Changed
}
if (count == MAX_PIECES)
return win = 1;
}




edit: typo

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!