Java2D Render Tiled Map

Hello everyone,

I’m pretty new here and have basic knowlegde about java. I have a LUA background, but know basic Java.

I’ve started playing around with generate maps with two dimensional array and i fell in love with it. I saw this: http://www.cokeandcode.com/info/showsrc/showsrc.php?src=../../collision/tilemaps/src/org/newdawn/tilemap/Map.java

My setup look almost like this, but i added a camera: http://www.cokeandcode.com/main/tutorials/tile-maps/

My question: Is this a good way to render tiles? And also how can i only render tiles only inside my camera view.

Thanks everyone.



     int camX = CameraXPosition - SCREENWIDTH / 2;
     int camX /= TILEWIDTH;
     int endX = SCREENWIDTH / TILEHEIGHT + camX;
     int camY = CameraYPosition - SCREENHEIGHT / 2;
     int camY /= TILEHEIGHT; 
     int endY = SCREENHEIGHT / TILEHEIGHT + camY;

     for(y=camY; y< endY; y++)
        for(x=camX; x<endX,x++)
           DrawTile(x, y);


Hope that helps. The code you show for the tile map rendering isn’t very good but it is a start, I would recommend the 2d array not be an int though, could easily be a simple byte.

Be wary of ints vs byte if you are using each byte as an ID to reference a type of tile! Eventually you may run out of IDs to be passed out. Don’t optimize until you know you need it :slight_smile:

8 bits in a byte ;D

Lua and Java forever!

I loved putting Lua into my projects whenever I can. I even wrote a module which allows me to run Java to run Lua scripts which bounce off each other.

Anyways, store your tile information in many chunks. Render 6 chunks at a time, depending on their size and whether or not you want to do calculations to determine when the next chunks need to be wrote into memory and displayed… as well as the 3 that will be loaded out of memory.

It did not work :frowning:

I want todo something like this. fromPositionX, toPositionX and fromPositionY, toPositionY

I need to see your code, could you zip and send me your project?

Sorry for the late answer, but i’ve uploaded my code here:

Since that is a separate issue, you may can help me with the render certain radius in this thread.