Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Need help with my drawBackground function
Forum Updated to NodeBB v4.3 + New Features

Need help with my drawBackground function

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arector
    wrote on last edited by
    #1

    Hey guys,

    this is my first post here and i hope im in the right forum :D

    Currently im working on a level editor for a game that i want to create in the future. Most of it is done but the perfomance is bad. I overrode the drawBackground function from my graphicsscene to draw the tiles:

    @
    int x = 0, y = 0;
    for(int a = 0; a < layers.size(); a++)
    {
    for(auto it : getLayer(a+1)->getTiles())
    {
    painter->drawPixmap(x,y,it->getItem());

                x += tileSizeX;
    
                if(x == tileSizeX*mapSizeX)
                {
                    x = 0;
                    y += tileSizeY;
                }
            }
            y = 0;
        }
    

    @

    The problem is that when i create a map that is 1000x1000 large it is really hard to draw because all layers have to be redrawn. I know this is not the best method but i dont now anything better i tried everything :D

    I hope you guys can help me! :)

    PS: Im a student so my english is not the best but i think you can understand what i wrote :D

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, welcome to devnet.

      Have you tried... not drawing everything every time? :)
      Draw the tiles once to a pixmap and update them only when a tile changes. There's no need to redraw every tile every frame. Just update the ones that actually change and use the single large pixmap as a background.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Arector
        wrote on last edited by
        #3

        But how can i do that? If i implement this in the drawbackgrond function it only draws the tile when the function is called and the rest disappear.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Sorry, I don't follow. What disappears?

          Usually any game loop looks something like this:

          • Handle user input
          • Process game logic
          • Draw current state
          • Repeat

          So you create a large pixmap before you start anything.
          In the game logic step you check which tiles changed and need to be redrawn. You draw them on the big pixmap in this step.
          In the draw step you just draw the big pixmap as a background instead of each individual tile.
          Another thing - are all 1000x1000 tiles visible at the same time? If not then just update the ones that will actually be visible on the screen.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Arector
            wrote on last edited by
            #5

            Its hard to describe my problem sorry :D

            My draw code is in the drawbackground function but the problem is that when i just draw one tile there and nothing else the rest of the tiles are not redrawn. So i have to redraw everything because otherwise it isnt showed in the scene.

            But i will try it with a large pixmap, do you have any documentation about drawing on a pixmap? I cant find anything about it.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              [quote author="Arector" date="1415720419"]But i will try it with a large pixmap, do you have any documentation about drawing on a pixmap? I cant find anything about it.[/quote]

              It's just a matter of creating a painter:
              @
              //create a pixmap and a painter
              QPixmap pixmap(width, height);
              QPainter painter(&pixmap);

              //now you can draw on the pixmap with painter eg.
              painter->drawText(20,30, "Hello"); //draw some text
              painter->drawPixmap(40,50, someTilePixmap); //draw a pixmap
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Arector
                wrote on last edited by
                #7

                Thank you! It isnt working perfectly at the moment but your method works and i think the perfomance should be fine now :)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Arector
                  wrote on last edited by
                  #8

                  I have one last question ^^ If i draw a 1000x1000 Map now i get this error:

                  @QPainter::begin: Paint device returned engine == 0, type: 2@

                  I think it comes because Painter tries writing on a null pixmap. When i use a smaller size it works perfectly. Cant qpixmap handle this size?

                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What's the unit of this 1000x1000? Is it in pixels? Or tiles?

                    If it's tiles then do the simple math. Assuming a tile is 32x32 and each pixel takes 4 bytes(red, green, blue, alpha) you have this: 1000 * 32 * 1000 * 32 * 4 which is about 4GB of data. If your app is 32bit process then it's waaay beyond the available address space (roughly 2GB per process). On 64bit system you'd get away with it but it's a waste as well.

                    I doubt that you're displaying a 4GB worth of data every single frame so you need to plan it better. Divide the space into smaller areas (tiles of tiles) and only keep in memory what is actually showing.

                    To be honest a lot of that work Qt can do for you. Take a look at the "40000 Chips example":http://qt-project.org/doc/qt-5/qtwidgets-graphicsview-chip-example.html

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Arector
                      wrote on last edited by
                      #10

                      Okey i wil take a look thanks :)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved