I'm currently attempting to change my map loading/rendering code so that only the area within x number of blocks in all directions of the sprites' location are rendered. I know how this should work but I'm not sure how to change the code for it to work in the way that I want it to work.The way that I'm planning to have it work is that, because the maps are tile-based, it will render all the tiles that you can see on screen and 1 tile in each direction off screen so that when the player is moving it will have enough space and time to render the new tiles so that you don't start seeing a black background.

Below is the code that I use to load the map. Each pixel on the image corresponds to a tile that will be loaded; when the tiles are being rendered there is a switch statement that, depending on the value at a certain (X,Y) location in the 2D map[][] array, will render a specific tile. This code is based off of another piece of code that I managed to find while googling one day.I need to somehow change it so that it will only load a certain area of the image based upon four sets of (X, Y) coordinates) instead of loading the entire image pixel by pixel by line by line.

Edit: All code is in the third post

This snippit of code is what I use to render the map. It takes the map[][] array that is loaded from the method above and then goes through every single position in the map[][] array and, depending on the value at that point, renders a specific tile.

Edit: All code is in the third post.

Thanks in advance to anyone who can help me figure out how to solve my problem.

Ack! While your case statement there will work (And you'll have to do probably do something like it somewhere) it's not the best way to handle that. This is more about your switch statement than anything. You should pull out that g.draw command and put it into a method that you call:

Of course, translating the pixel values into indexes into a bounded array or something would result in better efficiency... But ah, getting off track there, hah.

Whew! That said for your problem about painting the only a sub-section of the map, you first need to decide how you're going to do it? Are you centering the displayed section on the player? And if so, when he gets to an edge of your map will the map stop following him?

Thanks for the reply. I've attempted to add something similar to what you wrote into the code (I've pasted the part I'm talking about below) but I can't seem to figure out the variables due to how I've written my code. I probably should have posted all of the code to give anyone a better understanding of how it works, Anyways...

Do the playerY & playerX variables represent the current area in the map[][] array where the player is?Do the halfHeight & haldWidth variables represent the amount of tiles from where the player is standing on to the edge of the screen?What do tileX and tile Y represent?

Ah, next time use the pastebin (Link below the text area) for a large chunk of code. :3

Alright, I was making some assumptions based on the code you had posted, here they are:1) The player's location is represented by a screen pixel location (In this case, the center of their sprite).2) You know the dimensions of your screen.

The first assumption leads to playerX being the player's X pixel location and that playerY is the player's Y pixel location. (I was hand typing things I try to keep the length of my variable names short, those two values are playerXLocation and playerYLocation respectively. Assuming that it's pixel data).The second assumption allows us to find the displayed bounds of the screen by tiles. Let's say that the player's location is (400,500) and that the display is (480,320), and that tiles are 40 pixels wide/high. Looking at we can substitute and getint startX = (playerX - halfWidth) / tileX - 1;startX = (400 - 240) / 40 - 1;startX = (160) / 40 - 1;startX = 4 - 1;startX = 3.This means that to only call paint for tiles on the screen, we'd start drawing at 3 and end at some endX (Which we'd find in the same way).

Anyway, your paint loop would compute those four values and use them as a bounds for painting. Ah, I can post some code (My aborted platformer project) which has some code for doing the bound finding and paint loop.

Hmm... I can't see if it's working or not but I've altered the map rendering code. I tried using a very large map (400x400 pixels in size which is a lot of tiles) and you could easily see that the game was slower and lagging just as it did before I edited the rendering code. I'm going to try and mess around with the code to see if I can check if it's working somehow.

Edit: Derp, it looks like I forgot to add the startY, startX, endX, endY to the for loop that renders the map... Back to testing!

Edit 2:I'm getting a lot of ArrayIndexOutOfBounds errors. Attempting to figure out a fix for them now...

Edit 3: After testing out your code a little bit I managed to create a variant of it that works somewhat the same although I still get ArrayIndexOutOfBounds errors. I'm working on fixing them now.

Edit 4: Managed to get it working properly. All that's left now is to figure out how to optimize map loading so that it doesn't spend a few minutes loading a 1000x1000 pixel image and then error out because it's using 100% CPU. Everything except for the map loading is working amazingly compared to how it was before, thanks a ton!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org