I have this pathfinder and I got it to work using c++... now i am trying to convert it to OpenGL and it is not working.
I wrote code to use a mouse to plot the start and destination points. but it won't work properly.
I have attatched the file to this thread.
Could someone help me?


#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <dos.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <GL/glut.h>
#include <math.h>

const int screenWidth = 600;
const int screenHeight = 400;
const int l_bottom = 50;
const int l_left = 50;
const int l_top  = screenHeight-100;
const int l_right = screenWidth-50;

static int yet=0;

typedef struct s{
  float  g;
  float  h;
  float  f;    //f=g+h   h-Mantattan D
  int  who;    //node,null,Start,Dest
  int back;    //previous node
} node;

node xn[screenWidth][screenHeight];

void init ( void ){
    glColor3f(0.0f, 0.0f, 0.0f);
    gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);

void textxy(int x, int y, char* p){
 static int i, size=0;
      if( *(p+size)=='\0') break;
      else size++;
 for(i=0; i<size; i++){
      glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *(p+i) );

void line1(int x1, int y1, int x2, int y2){
                glVertex2i(x1, y1);
                glVertex2i(x2, y2);

                            // start of pathfinder functions
int set_back(int xs,int ys){
    return (xs*100)+ ys;

int md( int startx, int starty, int endx, int endy )
{  return ( abs( (endx - startx) ) + abs( (endy - starty) ) );  }//md

void set8(int &xs, int &ys, int xd, int yd){
  static long minh=1000, maxg=0;
  static int holdi,holdj;
  static int i=0,j=0;

  for( i = -1; i < 2; i++ )  for( j = -1; j < 2; j++ )
     if((xs+i)<0 || (ys+j)<0) continue;
     if((xs+i)>=screenWidth || (ys+j)>=screenHeight) continue;     
     if(xn[xs+i][ys+j].who == -3) continue;
     if(xn[xs+i][ys+j].who == -2 ){ 
         yet = 1;
     if(xn[xs+i][ys+j].who == -1) continue;
     if(xn[xs+i][ys+j].who == 2) continue;
     xn[xs+i][ys+j].h = abs(md( xs+i, ys+j, xd, yd ));
        xn[xs+i][ys+j].g = sqrt( abs( i ) + abs( j ) ) + xn[xn[xs][ys].back/100][xn[xs][ys].back%100].g;
        xn[xs+i][ys+j].f = xn[xs+i][ys+j].g + xn[xs+i][ys+j].h;
        xn[xs+i][ys+j].who = 1; 
        xn[xs+i][ys+j].back = set_back(xs+i,ys+j);
          if(xn[xs+i][ys+j].h <= minh){
              if(xn[xs+i][ys+j].g >= maxg){
               maxg = xn[xs+i][ys+j].g;
               minh = xn[xs+i][ys+j].h;
               holdi = i;
               holdj = j;
xs= xs+holdi;
ys= ys+holdj;
xn[xs][ys].who = 2;

                             // end of pathfinder functions

                 //draw path from start to finish
void paintgh(){
  static int i,j;

           case  2: ;  //pathnode   draw node[x-1][y-1] to node[x][y]
           case  0: ;  //null node  blank output
           case -1: ;  //Starting position
           case -2: ;  // Destination position
           case -3: ;  // barrier

void doMouse(int button, int state, int x, int y){
    static int oldx,newx,oldy,newy;
    static int count=0;
    static int xs,ys,xd,yd;
    static int i,j;

  for(i=0;i<screenWidth;i++)   for(j=0;j<screenHeight;j++){
      xn[i][j].g = xn[i][j].h = xn[i][j].f = 0;
      xn[i][j].back = 0;
      xn[i][j].who  = 0; }//for-ij

    if(x>=l_right) x=l_right-1;
    else if(x<l_left) x=l_left+1;

    if((screenHeight-y)>=l_top) y=screenHeight-l_top+1;
    else if((screenHeight-y)<l_bottom) y=screenHeight-l_bottom-1;

    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){

              newx = x; newy = screenHeight - y; // flip y

                       xs=x;     // x start = x
                       ys=y;     // y start = y


                  else count=1;
    else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)


void doit ( void ){

   line1(l_left, l_top, l_right, l_top);
   line1(l_right, l_top, l_right, l_bottom);
   line1(l_right, l_bottom, l_left, l_bottom);
   line1(l_left, l_bottom, l_left, l_top);
   textxy(90,390,"Pathfinder GUI" );
   textxy(90,360,"Mouse: L-click to draw start and finish.  R-click to quit.");

int main ( int argc, char** argv ){
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
   glutInitWindowPosition(100, 150); 
   glutCreateWindow    ( argv[0]  );  
   init ( );
   glutMouseFunc  ( doMouse );
   glutDisplayFunc( doit );
   glutMainLoop ( );
   return 0;

I managed to get your code to compile and run, but I had to kill it after clicking twice. After looking at your code I noticed that you were trying to draw in the glut mouse function, that's a bad idea. There is a reason why you have a draw function, and there is a reason why you have a mouse function, don't mix the two up. Also, it seems to be getting caught in this infinite loop for me:

                       xs=x;     // x start = x
                       ys=y;     // y start = y



What? I'm sorry, but you really need to clean up the code and give some informative comments as to what you're doing in each function if you want anyone to understand this and to help you.


Well .... here I go.. this will be hard to describe so bare with me...

xs is x start position
ys is y start position
xd is x destination position
yd is y destination position
yet is a flag to stop the while loop
struct s is the node structure

- set8 function takes the xs,ys,xd,yd it find the g distance (distance from node to start position) and h distance (manhattan distance).
- md function finds the manhattan distance
- set_back function finds the last visited node
- paintgh function should print the start position, destination position and the line that connects to every visited node. // I need help on this one.

Number meanings:
-2: represents the destination node
-1: represents the start node
0: represents a null node
1: represents a node // no need to worry about output for this one
2: represents the path node
3: represents the barrier node

What I want to do is:
-Click once to create start node.
-Click a second time to create the destination node.
-After all of this, the set8 function should work and help find the path from the start node to the desitination node.
-The paintgh function should then step in and paint the graphics from the start node to the destination node.

I have made the code so that anyone can add a barrier. The code should find its way arround the barrier. Its really cool when I ran it with out Opengl.

If there are any more question please email me at brian63304@hotmail.com.

Thank you

This article has been dead for over six months. Start a new discussion instead.