Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Game slow down

    Help
    6
    26
    1324
    Loading More Posts
    • 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.
    • Spacemario
      Spacemario F @waldron last edited by

      @waldron One trick which may or may not help you is to think of absolutely everything as having a cost; every loop through an array you have within your main game loop is chewing up CPU cycles. Modern computers like the Switch are enormously powerful for these types of simple 2d games we're making, but there's still a limit :)

      Something I've been employing is, "what kind of pre-processing can I do during level loading, to make the game loop as streamlined as possible?" Users won't care if the levels take an extra half second to load, if it means the framerate can be higher during gameplay.

      So, if you can somehow structure your data in an "uglier" way, but which means fewer "sub-loops" during your main loop, the better off you'll be.

      I learned this trick when doing STOS programming on the Atari ST-- even when compiled, virtually anything (relative to something like the Switch) in those programs' main loops would bring that Motorola 68000 to its knees :) In my racing game (which even had simple AI opponents), it was a struggle just to maintain something like 15-20 fps.

      Just my two cents anyway-- I'm not very experienced with game programming either in general, so others on the forum will probably have better advice!

      waldron 2 Replies Last reply Reply Quote 3
      • waldron
        waldron F last edited by

        That's great help iv shortened the array which has fixed it so il just add to it till it breaks again to test its limit anything after il load into the next level

        1 Reply Last reply Reply Quote 2
        • waldron
          waldron F @Spacemario last edited by waldron

          @Spacemario i noticed i placed some code in the wrong place causing slow down but all good now my level array now is nearly 200 columns long....

          1 Reply Last reply Reply Quote 0
          • waldron
            waldron F @Spacemario last edited by

            @Spacemario iv hit the limit switch just shuts down ....
            What's the best way to load into another loop?

            1 Reply Last reply Reply Quote 0
            • waldron
              waldron F last edited by

              can someone help and tell me ?

              at the moment my level array
              level = [( 0, 0, 0, 0 ),
              ( 0, 0, 0, 0 )
              ]

              but mine is 200 in length causing my switch to crash as it is drawing to much between update()
              so at the minute you get about 30 seconds gameplay till you reach where im up to ...
              im running 3 level arrays one for collision background and foreground so the switch is actually reading 3 lots of level arrays a total of 600+

              can i do this and will it work?

              level = [( 0, 0, 0, 0 ),
              ( 0, 0, 0, 0 ),
              update()
              ( 0, 0, 0, 0 ),
              ( 0, 0, 0, 0 )
              ]

              1 Reply Last reply Reply Quote 0
              • R
                Richard F last edited by Richard

                It sounds like you're drawing the whole level all the time. Could you adapt your code to only draw the columns that are on the screen, so it's not drawing too much between each update?

                waldron 1 Reply Last reply Reply Quote 1
                • waldron
                  waldron F @Richard last edited by

                  @Richard that would be a great fix ! but how would i accomplish this im still learning

                  1 Reply Last reply Reply Quote 0
                  • waldron
                    waldron F last edited by

                    i cant work on this till later but have an idea
                    if i structure my next part of my array as level.2 so on
                    i can add at the bottom of the for loop to then level.2 then level.3
                    ... please work

                    1 Reply Last reply Reply Quote 0
                    • R
                      Richard F last edited by Richard

                      You know where your player is in the level. So which column they're on, then you know how many columns can be seen each side of the player, for the purposes of an example well assume 5 to the left and 10 to the right. Then you change your loop that was something like

                      for i = 0 to len (level) loop
                         // Draw level
                      repeat
                      

                      With

                      for i = playerColumn - 5 to playerColumn + 10 loop
                      // Draw level
                      repeat 
                      
                      1 Reply Last reply Reply Quote 3
                      • waldron
                        waldron F last edited by

                        ooooh thankyou il give this a go fingers crossed

                        1 Reply Last reply Reply Quote 0
                        • pobtastic
                          pobtastic F last edited by

                          Ah perfect! This is what I was talking about earlier when I said about using a "window" in your array, rather than the whole array. I hope this works for you (apologies, I've not been able to look at your code).

                          waldron 1 Reply Last reply Reply Quote 1
                          • waldron
                            waldron F @pobtastic last edited by

                            @pobtastic no problem mate hoping this works should do the trick

                            1 Reply Last reply Reply Quote 0
                            • Martin
                              Martin Fuze Team last edited by

                              I would think that should work. At the end of the day you know that you can draw a screens worth because it's been working OK up until some point.

                              1 Reply Last reply Reply Quote 1
                              • waldron
                                waldron F last edited by

                                My code is

                                For row = 0 to len( level ) loop
                                For col = 0 to len ( level[0] ) loop
                                If level[row][col] >= 0 then
                                X = col * size
                                Y = ( row + levelOffset ) * tsize
                                drawSheet(..........

                                Confused on how to do this

                                Discostew 1 Reply Last reply Reply Quote 0
                                • waldron
                                  waldron F last edited by

                                  Think iv hit a serious bug switch keeps crashing on the coding screen so even if I do manage to fix the loop every time I try to extend the level array it will crash

                                  1 Reply Last reply Reply Quote 0
                                  • Discostew
                                    Discostew F @waldron last edited by

                                    @waldron You're trying to draw only what is going to be visible, yes? So what you need is to keep track of the position within the level, then work with that. If you're making a horizontal scroller where it scrolls from left to right (no vertical scrolling), the left-most position would be position 0. The width of what the camera displays extends from the left of the screen to the right of the screen. Assume a display dimension of 400x256 pixels. If your tiles are 16x16 pixels, then you can fill the display with 25x16 tiles (assume 26x16 for a horizontal scroller because when you scroll, you'll be moving in pixels where tiles can crop on the sides, so technically one extra column to render).

                                    So, the camera displays from position 0 to 400 (in pixels). Than means you'll be drawing the tiles columns from columns 0 to column 25 (divide positions by the width of the tile size). If you move right by 16 pixels, then it displays from position 16 to 416. If you convert that to tile position (divide by 16), that could make for columns 1 and columns 26. So you'd draw from column 1 to column 26. Now, what if you move right only an extra 8 pixels? Well, that is now position 24 to position 424. Dividing by 16 to get the tile range results in column 1.5 and 26.5.

                                    So how does one work with that? Simple. Take the whole number position (1 and 26), and draw from the left column to the right. This will result in the right side having an empty portion because you only compensated for 25 columns. This is why you should draw one additional column beyond the display range. This sort of design can allow you to have a large area, yet only have to deal with displaying a small section. You're basically cropping out everything except the area that you need to work with.

                                    waldron 1 Reply Last reply Reply Quote 0
                                    • waldron
                                      waldron F @Discostew last edited by

                                      @Discostew martins working on it, i understand what needs to happen but to make it happen im at a loss

                                      1 Reply Last reply Reply Quote 0
                                      • Martin
                                        Martin Fuze Team last edited by Martin

                                        basically arrtived at this:

                                        drawX = floor(playerX / tsize)
                                        
                                        drawLevelChunk(level, drawX, scale)
                                        drawLevelChunk(levelb, drawX, scale)
                                        
                                        /**
                                         * Draw a chunk of a level (nothing to do with draw
                                         * windows, this function exists because the levels
                                         * are so big the same code is called multiple times)
                                         * _l - level array
                                         * _x - current player position in tiles
                                         * _s - scale
                                         */
                                        function drawLevelChunk(_l, _x, _s)
                                            _low = _x - 7
                                            _high = _x + 22
                                            _max = len(_l[0])
                                        
                                            for row = 0 to len(_l) loop
                                                for col = clamp(_low, 0, _x) to clamp(_high, _x, _max) loop
                                                    if _l[row][col] >= 0 then
                                                        x = col * tsize    // tsize is defined outside
                                                        y = (row * levelOffset) * tsize
                                                        drawSheet(tileSheet, tiles[_l[row][cold]], x - screenX, y, _s)
                                                    endif
                                                repeat
                                            repeat
                                        return void
                                        

                                        The -7 and +22 are the values that are currently enough to smoothly draw "just enough"

                                        1 Reply Last reply Reply Quote 0
                                        • Martin
                                          Martin Fuze Team last edited by

                                          Oh, PS: Not overly surprisingly after this the game is back up to 60fps and CPU is down to around 4% so plenty of headroom.

                                          The editor is still choking when it comes to displaying a 200 x 9 array so I'll taise that as a big if it is not already.

                                          1 Reply Last reply Reply Quote 0
                                          • waldron
                                            waldron F last edited by

                                            After messing with a single array and extending it, it still crashes

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post