Map Editor Optimization

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Map Editor Optimization

Post 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
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Map Editor Optimization

Post 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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: Map Editor Optimization

Post 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).
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Map Editor Optimization

Post 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?
JesseGuarascia
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 70
Joined: Mon Dec 13, 2010 10:55 pm

Re: Map Editor Optimization

Post 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.
-- Jesse Guarascia

I like C/++, SDL, SFML, OpenGL and Lua. If you don't like those, then gtfo my sig pl0x (jk trollololololol)
Post Reply