Original Post
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. Could someone help me? Thanks, Brian //--------------------------------------------------------------------------- source tags added, all the < and > will now be wrong however so you might want to fix that [Edited by - phantom on October 24, 2005 8:22:56 PM]
#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 ){
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void textxy(int x, int y, char* p){
static int i, size=0;
for(;;){
if( *(p+size)=='\0') break;
else size++;
}
for(i=0; i<size; i++){
glRasterPos2i(x+(i*8),y);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *(p+i) );
}
}
void line1(int x1, int y1, int x2, int y2){
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
}
// start of pathfinder functions
int set_back(int xs,int ys){
return (xs*100)+ ys;
}//set_back
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;
continue;
}
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;
}//if
}//if
}//for
xs= xs+holdi;
ys= ys+holdj;
xn[xs][ys].who = 2;
}//set8
// end of pathfinder functions
//draw path from start to finish
void paintgh(){
static int i,j;
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<screenHeight;i++){
for(j=0;j<screenWidth;j++){
switch(xn[j].who){
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
}//case
}//j
}//i
}
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[j].g = xn[j].h = xn[j].f = 0;
xn[j].back = 0;
xn[j].who = 0; }//for-ij
//pathnode=2,node=1,null=0,Start=-1,Dest=-2,Barrier=-3
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){
xn[oldx][oldy].who=-1;
xn[newx][newy].who=-2;
oldx=newx;oldy=newy;
newx = x; newy = screenHeight - y; // flip y
glPointSize(3.0);
glBegin(GL_POINTS);
glVertex2i(x,screenHeight-y);
glEnd();
if(count){
glPointSize(1.0);
while(!yet){
xs=x; // x start = x
ys=y; // y start = y
set8(xs,ys,xd,yd);
//paintgh();
}//while
//count=0;
}
else count=1;
glFlush();
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(0);
}
void doit ( void ){
glClear(GL_COLOR_BUFFER_BIT);
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.");
glFlush();
}
int main ( int argc, char** argv ){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600,400);
glutInitWindowPosition(100, 150);
glutCreateWindow ( argv[0] );
init ( );
glutMouseFunc ( doMouse );
glutDisplayFunc( doit );
glutMainLoop ( );
return 0;
}