void repaint( list<Displayable *> dList, XInfo &xinfo) { list<Displayable *>::const_iterator begin = dList.begin(); list<Displayable *>::const_iterator end = dList.end(); //XClearWindow( xinfo.display, xinfo.window ); move to event loop while( begin != end ) { Displayable *d = *begin; d->paint(xinfo); begin++; } //XFlush( xinfo.display ); same here }
//repaint function void repaint( XInfo &xinfo) { list<Displayable *>::const_iterator begin = listofd.begin(); list<Displayable *>::const_iterator end = listofd.end(); // draw into the buffer // note that a window and a pixmap are “drawables” XFillRectangle(xinfo.display, xinfo.pixmap, xinfo.gc[0], 0, 0, xinfo.width, xinfo.height); while( begin != end ) { Displayable *d = *begin; d->paint(xinfo); // the displayables know about the pixmap begin++; } // copy buffer to window XCopyArea(xinfo.display, xinfo.pixmap, xinfo.window, xinfo.gc[0], 0, 0, xinfo.width, xinfo.height, // region of pixmap to copy 0, 0); // position to put top left corner of pixmap in window XFlush( xinfo.display ); }
/* function to repaint a display list Checking collisions with mountain NOTE: all the conditions for mountains are different cannot use helper function to reduce the code */ int repaint(XInfo &xinfo) { list<Displayable *>::const_iterator begin = dList.begin(); list<Displayable *>::const_iterator end = dList.end(); int status = 1; //1: nothing happened 0:landed 2: crushed float slop; int ship_point; int i; float length; float height; float expecty; int extra; int real_y; vector<int> x; vector<int> y; //big black rectangle to clear background XFillRectangle(xinfo.display, xinfo.pixmap, xinfo.gc[0], 0 , 0 , xinfo.width, xinfo.height); //draw display list while (begin != end) { Displayable *d = *begin; d->paint(xinfo); begin++; } //check if the ship landed if ( (ship.getY() >= lefty && pad.getx() <= ship.getX()+40 && ship.getX() <= leftx ) || (ship.getY() >= righty && rightx <= ship.getX()+40 && ship.getX() < rightx+pad2.getl()) ) { if(ship.getspeed() > 2) { status = 2; } else { status = 0; } } if(ship.getX() < pad.getx() ) { /*mountain 1*/ x = mountain1.getx(); y = mountain1.gety(); vector<int> indexs = findPoints(ship.getX(), ship.getX()+40, x); vector<int> points; for(int j = 0; j < indexs.size(); j ++ ) { points.push_back(y[indexs[j]]); } real_y = ship.getY(); // right point if(ship.getX() +40 < pad.getx()) { ship_point = ship.getX()+40; i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - 0; height = y[i] - 500; slop = height / length; extra = 500; } else if( i == x.size() ) { length = pad.getx() - x[i-1]; height = pad.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } // left point if( status == 1) { ship_point = ship.getX(); i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - 0; height = y[i] - 500; slop = height / length; extra = 500; } else if( i == x.size() ) { length = pad.getx() - x[i-1]; height = pad.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } //line if(lower(points, ship.getY()) && status == 1) { status = 2; } } else if( ship.getX() < pad2.getx() && ship.getX()+40 > pad.getx() + pad.getl() ) { //mountain 3 x = mountain3.getx(); y = mountain3.gety(); vector<int> indexs = findPoints(ship.getX(), ship.getX()+40, x); vector<int> points; for(int j = 0; j < indexs.size(); j ++ ) { points.push_back(y[indexs[j]]); } real_y = ship.getY(); //right point if(ship.getX() +40 > pad.getx() +pad.getl() && ship.getX() +40 < pad2.getx()) { ship_point = ship.getX()+40; i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad.getx()+pad.getl(); height = y[i] - pad.gety(); slop = height / length; extra = y[i] - x[i] *slop; } else if( i == x.size() ) { length = pad2.getx() - x[i-1]; height = pad2.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } //left point if(ship.getX() < pad2.getx() && ship.getX() > pad.getx() + pad.getl()) { if(status == 1) { ship_point = ship.getX(); i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad.getx()+pad.getl(); height = y[i] - pad.gety(); slop = height / length; extra = pad.gety() - (pad.getx()+pad.getl()) *slop; } else if( i == x.size() ) { length = pad2.getx() - x[i-1]; height = pad2.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; cout << "real_y " << real_y << endl; cout << "expecty" << expecty << endl; if ( real_y >= expecty) { status = 2; } } } //line if(lower(points,ship.getY()) && status == 1) { status = 2; } } else if( ship.getX() +40< 800 && ship.getX()+40 > pad2.getx() + pad2.getl() ) { x = mountain2.getx(); y = mountain2.gety(); vector<int> indexs = findPoints(ship.getX(), ship.getX()+40, x); vector<int> points; for(int j = 0; j < indexs.size(); j ++ ) { points.push_back(y[indexs[j]]); } real_y = ship.getY(); //right point ship_point = ship.getX()+40; i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad2.getx()+pad.getl(); height = y[i] - pad2.gety(); slop = height / length; extra = y[i] - x[i] *slop; } else if( i == x.size() ) { length = 0 - x[i-1]; height = 800 - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } //left point if(ship.getX() > pad2.getx() &&ship.getX() < 800) { if(status == 1) { ship_point = ship.getX(); i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad2.getx()+pad2.getl(); height = y[i] - pad2.gety(); slop = height / length; extra = pad2.gety() - (pad2.getx()+pad2.getl()) *slop; } else if( i == x.size() ) { length = 0 - x[i-1]; height = 800 - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } } //line if(lower(points, ship.getY()) && status == 1) { status = 2; } } XCopyArea(xinfo.display , xinfo.pixmap, xinfo.window , xinfo.gc[0], 0,0,xinfo.width,xinfo.height,(xinfo.width-800)/2,(xinfo.height-600)/2); XFlush(xinfo.display); return status; }