Page 1 of 1

Map Editor Optimization

Posted: Sat Jan 08, 2011 12:09 am
by JesseGuarascia
Hey err'body. I've been developing a map editor (C++ w/ Qt) for a project I'm doing at school (completely different language, Turing). The saving and, for the most part, displaying is completely optimized. I only display tiles that are actually in the viewable area of the map (the area scrolled to), and only tiles that are set to display.

The project is an RPG, because I wanted to be original and not make Pong like err'body else. I realized just yesterday, "Perhaps having layers would make it prettier! :D" So I implemented it, and as of right now, I have 2 layers displaying on screen. This is fine and dandy, but the map renders realllly slowly.

This is how I currently do it:

Code: Select all

void Init(){
    // Initialize the tiles
    Tile** tile;

    tile = new Tile*[mapWidth];

    for (int x = 0; x < mapWidth; x++){
        tile[x] = new Tile[mapHeight];
        for (int y = 0; y < maHeight; y++){
            // Set the positions of the tiles
            tile[x][y].SetPos(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
        }
    }
}

void paintEvent(QPaintEvent *event){
    QPainter paint (this);

    for (int x = 0; x < mapWidth; x++){
        for (int y = 0; y < mapHeight; y++){
            tile[x][y].Render(paint, tileSheet);
        }
    }
}
The paintEvent is just a small snippet, showing how I basically display things. I go through every item of the array, and attempt to display it. The "Tile" class' Render function just takes the QPainter, and a QPixmap tilesheet. The tile's have a boolean "display" flag, which holds whether or not they should be displayed. This is used by their Render member function to decide whether or not to draw. I tried doing something like a Linked List of tiles that should be displayed, and this didn't work out too well. I'm not sure what was wrong, but everything drew slower. I thought about a stack, but then I couldn't remove a randomly clicked tiles from the stack of tiles being checked.

Does anybody have a suggestion as to a better way of handling display of the tile maps with an array style like that? I'm stumped for ideas here. I just know that, say, the user makes a map 100 x 100 (in tiles), and each tile is 32 x 32 px. That would be drawing 10,000 tiles. If I have 2 + layers of that, it will get pretty slow, especially if there's a lot of stuff drawn on each layer.

So, if it makes any difference, I'm using QPainter to draw, and checking if each tile should be drawn, then drawing it if so. I'm just looking for a better approach then this, and not a linked list or stack. Unless I'm doing it wrong for one or both of those, in which case, any support in that manor would be greatly appreciated :).

-- Jesse Guarascia

Re: Map Editor Optimization

Posted: Sat Jan 08, 2011 8:37 am
by MrDeathNote
Are you checking if every tile is being displayed or every layer? Because doing a boolean check on every displyed tile is hella inefficient, like you said if you have 10000 tile drawing, then that's 10000 boolean checks.

Re: Map Editor Optimization

Posted: Sat Jan 08, 2011 10:34 am
by JesseGuarascia
Well, in Qt (as far as I know), each time something covers the window, or it's re-sized or something, the widget has to redraw everything. So when I click on the map to add a tile (once again, as far as I know), I have to redraw everything, which would mean redrawing the layers as well. Luckily, I only actually draw a small section of the map. I'm worried about when the user zooms out to view more of the map. This could ultimately result in a larger amount of tiles needing to be drawn, and most likely, result in a huuuge amount of lag (Edit: it will, lol).

Re: Map Editor Optimization

Posted: Sat Jan 08, 2011 12:57 pm
by Falco Girgis
So are you using the QGraphicsView and QGraphicsScene paradigm? I need a little more info, so that I know what QT is and is not doing for you as far as culling is concerned.

Are your tiles QGraphicsItems/Rects added a scene, or are you just painting QPixmaps manually to a scene?

Re: Map Editor Optimization

Posted: Sat Jan 08, 2011 1:00 pm
by JesseGuarascia
Oh, sorry, forgot about that part. I'm just displaying QPixmap's to a scene. Each tile contains a clipping rectangle, and a position rectangle (QRect). Whenever the widget is updated, it just uses the tile sheet for the map, and displays all of the tiles on screen using that tile sheet.