Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:10:40+00:00 2026-05-27T14:10:40+00:00

I have my game which, on every render loop, loops through all the blocks

  • 0

I have my game which, on every render loop, loops through all the blocks in my map (128×128) which as you can probably tell, causes a lot of lag. When I first made it, I had to make it render only the blocks on the screen, or it would crash instantly. Now I only render the blocks on the screen, but still loop through all the blocks to see if they are on the screen, which makes my fps about 2.

    for (int y = 0; y < 128; y++) {
        for (int x = 0; x < 128; x++) {
            Block b = LevelGen.getBlockAt(x, y);
            if (Forgotten.isInsideGameWindow(x * 30, y * 30)) {
                arg1.drawImage(b.getTexture(), x * 30, y * 30);
            }
        }
    }

Is there a way to make it so doesn’t loop through all of them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:10:40+00:00Added an answer on May 27, 2026 at 2:10 pm

    Figure out the size of your display window, and only iterate blocks that are within it.

    final int tile_size = 30;
    
    final RectangleType displayWindow = Forgotten.getDisplayWindow ();
    
    final int left = displayWindow.getLeft () / tile_size - 1;
    final int right = displayWindow.getRight () / tile_size + 1;
    final int top = displayWindow.getTop () / tile_size - 1;
    final int bottom = displayWindow.getBottom () / tile_size + 1;
    
    for (int y = top; y < bottom; ++y)
       for (int x = left; x < right; ++x)
           canvas.drawImage (LevelGen.getBlockAt (x,y).getTexture (),
                             x * tile_size, y * tile_size);
    

    You may also want to figure out which area(s) of the canvas actually want to be drawn, and instead keep a “dirty rectangle” list of areas to be redrawn. Whenever a tile changes, or a sprite/particle/whatever passes through its space, add that tile to the rectangle. Even if you just use a single dirty rectangle that enlarges to encompass all updates during a frame, if your game doesn’t actually have “stuff happening” at all points on the display at all times, your frame rate will be higher on average (but suffer from large-scale effects)

    expanding upon that:

          public class Map {
                private Rectangle dirtyRect = null;
    
                public void dirty (Rectangle areaAffected) {
                       if (null == dirtyRect) {
                            dirtyRect = areaAffected; return;
                       }
                       dirtyRect = new Rectangle ( Math.min (dirtyRect.getLeft (),
                                                             areaAffected.getLeft () ),
                                                   Math.min (dirtyRect.getTop (),
                                                             areaAffected.getTop () ),
                                                   Math.max (dirtyRect.getRight (),
                                                             areaAffected.getRight () ),
                                                   Math.max (dirtyRect.getBottom (),
                                                             areaAffected.getBottom () ));
                }
    

    Then use the dirty rectangle in place of displayWindow for normal draws, and you can test if (null == getDirtyRectangle ()) return; to skip drawing at all if nothing’s changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I have that functionality in my game through which the players can
I have a game in which you can score from -40 to +40 on
You have an input a n games. Each game has a fame (which can
I have made a game which is nearly identical to Popcap's Atomica. ( http://www.popcap.com/gamepopup.php?theGame=atomica
I am developing an iPhone game in which birds bounce. I have set up
I am making a card game in ruby. I have the Game class, which
I have Tile s which represent the tiles in a game's 2-dimensional world. The
I have a JSlider which shows bet sizes (for a poker game) I am
I have a games table which holds the data about a game. Then another
I made shoot em up like game.But I have only one ememy which fallows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.