using namespace std; #include #include "graphics.h" #include #include #include "astar.cpp" #define WIDTH 28 ////////////// Utilities: // make colorgc #define GGC Glib::RefPtr Glib::RefPtr darea::makecolorgc(short r, short g, short b) { Glib::RefPtr win = get_window(); Glib::RefPtr some_gc = Gdk::GC::create(win); Gdk::Color some_color; Glib::RefPtr some_colormap = get_default_colormap(); some_color.set_red(r); some_color.set_green(g); some_color.set_blue(b); some_colormap->alloc_color(some_color); some_gc->set_foreground(some_color); return some_gc; } void darea::displaytext(const char *c, Glib::RefPtr colorgc, int x, int y ) { Glib::RefPtr win = get_window(); Glib::RefPtr pangolayout = create_pango_layout(c); win->draw_layout(colorgc, x, y, pangolayout); } void delayforsecs(double s) { int x = 3; Glib::Timer t; t.start(); while (t.elapsed() win = get_window(); Glib::RefPtr some_gc = Gdk::GC::create(win); Glib::RefPtr blackgc = get_style()->get_black_gc(); Glib::RefPtr whitegc = get_style()->get_white_gc(); GGC mygc; GGC rgc; GGC bgc; GGC ggc; Gdk::Color some_color; Glib::RefPtr some_colormap = get_default_colormap(); // yellow: some_color.set_red(65535); some_color.set_green(65535); some_color.set_blue(0); some_colormap->alloc_color(some_color); some_gc->set_foreground(some_color); rgc = makecolorgc(65535,0,0); ggc = makecolorgc(0,65535,0); bgc = makecolorgc(0,0,65535); int i, j; playground PG; PG.genmap(); for (i=0;iterrain) { case WATER : mygc = bgc; break; case DESERT : mygc = some_gc; break; default: mygc = ggc; break; } // switch win->draw_rectangle(mygc,true,j*WIDTH,i*WIDTH,WIDTH,WIDTH); } // for each cell // generate initial positions of professor and goblin: int gobx, goby, profx, profy; do { gobx = random() % COLS; goby = random() % ROWS; } while (PG.M[goby][gobx]->terrain!=OPEN); do { profx = random() % COLS; profy = random() % ROWS; } while (PG.M[profy][profx]->terrain!=OPEN); // draw professor and goblin: Glib::RefPtr profimage = Gdk::Pixbuf::create_from_file("man15.gif"); Glib::RefPtr gobimage = Gdk::Pixbuf::create_from_file("gem1.gif"); gobimage->render_to_drawable(get_window(), get_style()->get_black_gc(), 0, 0, gobx*WIDTH,goby*WIDTH,gobimage->get_width(), gobimage->get_height(),Gdk::RGB_DITHER_NONE, 0, 0); profimage->render_to_drawable(get_window(), get_style()->get_black_gc(), 0, 0, profx*WIDTH,profy*WIDTH,profimage->get_width(), profimage->get_height(),Gdk::RGB_DITHER_NONE, 0, 0); // Call A* PG.Astar(goby,gobx,profy,profx); cout << "done A*\n"; cout << "gobinit: "<x!=gobx || current->y!=goby)) { // delayforsecs(0.1); y = current->y; x = current->x; profimage->render_to_drawable(get_window(), get_style()->get_black_gc(), 0, 0, x*WIDTH,y*WIDTH,profimage->get_width(), profimage->get_height(),Gdk::RGB_DITHER_NONE, 0, 0); current = current->prev; } return true; // stops other handlers } // onexposeevent dwindow::dwindow() : canvas(WIDTH*COLS,WIDTH*ROWS) { add(canvas); show_all_children(); } int main(int argc, char **argv) { if (argc>1) srandom(atoi(argv[1])); Gtk::Main mr(0, NULL); dwindow thewindow; mr.run(thewindow); return 0; } /* compile: g++ pathfinder.cpp -o pathfinder `pkg-config gtkmm-2.0 --cflags --libs` */