Inefficient Background Drawing



  • I've been working on a very simple simulator of a robot. The maze for the robot is simply represented by an array of black or white square representing 1 mm squares. I am very new to graphics drawing and wrote the code so that it refreshes the full background every frame. This is clearly quite inefficient as the background is exactly the same every frame. I have to redraw at least part of the background because as the robot moves over the background the background needs to be redrawn where it changed but the vast majority of pixels won't have any need to be changed.

    I'm guessing that there is a way to do this properly with layers but I can't figure out where to look. If anybody could point me in the right direction of having reasonably efficient code that would be very helpful. I attached the current drawMaze function in case that helps people understand what I am doing or want to do.

    @
    void Dialog::paintEvent(QPaintEvent *e){
    drawMaze();
    drawRobot();
    }

    void Dialog::drawMaze(){
    QPainter painter(this);
    QPen whitePen(Qt::white);
    whitePen.setWidth(1);
    QPen blackPen(Qt::black);
    blackPen.setWidth(1);

     for(int i = 0; i < maze->getWidth() && i < this->width(); i++){
          for(int j = 0; j < maze->getHeight() && j < this->height(); j++){
               if(maze->isBlack(i,j)){
                    painter.setPen(blackPen);
               }
               else{
                    painter.setPen(
               }
               painter.drawPoint(i,j);
        }
     }
    

    }
    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can draw your original maze on a QPixmap, (update it when your widget is resized) then draw that pixmap. However, if you want to do some gaming, you might be also interested by the QGraphicsView framework.

    Hope it helps



  • Thanks, I've changed my system so I'm using both QGraphcisView and QPixmap and everything is going well.


  • Lifetime Qt Champion

    Good !

    Then since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.