I’ve been working on a tile map that draws 40x40 tiles onto the canvas evenly using my own logic. I have tho, used kev glass’ SpriteStore and Sprite class so that I can store duplicate tiles in one memory spot, im actually not sure if i used the class totailly correctly, of wheather or no its really in one memory space.
Well anyway, I have drawn the tiles on the canvas with sucess :). But the scrolling part is bothering me. Yes, it scrolls, BUT it renders all of the tiles AT ONCE, and if i make a map even slightly big, it will lag like nuts or not load at all! So its bad because im rendering a huge image now.
Then, to count this, i added another array that keeps track of visibility. I made final int variabled HIDDEN and VISIBLE, it works well, and it will only render tiles within your screen on any resolution. BUT i dont know how to make them HIDDEN, then VISIBLE as you scroll through the screen, so basically my map is only what fits in the screen, and as you scroll around, the rest is the black background. but the HIDDEN does work, because i can have any map size and it wont lag, but the problem is that i dont know how to scroll then draw, then make hidden.
some snippets:
import java.awt.Graphics;
import java.util.Random;
/****************************
* WorldGenerator v1.0
* + TileMap Class
* @author X
****************************/
public class TileMap {
/** The value indicating a clear cell */
private static final int LIGHT_GRASS = 1;
private static final int DARK_GRASS = 2;
private static final int DIRT = 3;
private static final int SAND = 4;
private static final int WATER = 5;
/** The value indicating a blocked cell */
//private static final int STATIC = 1;
/** The sprite that represents this entity */
protected Sprite tileGraphic;
/** The current x location of this entity */
protected int xPos;
/** The current y location of this entity */
protected int yPos;
Random rand = new Random();
private int random;
private final int VISIBLE = 0;
private final int HIDDEN = 1;
private String mapName;
/** The width in grid cells of the map */
private int MAP_WIDTH;
/** The height in grid cells of the map */
private int MAP_HEIGHT;
/** The rendered size of the tile (in pixels) */
private int TILE_SIZE;
/** The actual data for our map */
private int[][] tile = new int[0][0];
private int[][] tileVis = new int[0][0];
public TileMap(String ref, int width, int height, int x, int y, int size) {
this.mapName = ref;
this.MAP_WIDTH = width;
this.MAP_HEIGHT = height;
this.xPos = x;
this.yPos = y;
this.TILE_SIZE = size;
this.tile = new int[MAP_WIDTH][MAP_HEIGHT];
this.tileVis = new int[MAP_WIDTH][MAP_HEIGHT];
/** DIRECT CONTROL */
/*
this.tile[0][0] = LIGHT_GRASS; this.tile[1][0] = LIGHT_GRASS; this.tile[2][0] = LIGHT_GRASS; this.tile[3][0] = LIGHT_GRASS;
this.tile[0][1] = LIGHT_GRASS; this.tile[1][1] = LIGHT_GRASS; this.tile[2][1] = LIGHT_GRASS; this.tile[3][1] = LIGHT_GRASS;
this.tile[0][2] = LIGHT_GRASS; this.tile[1][2] = LIGHT_GRASS; this.tile[2][2] = LIGHT_GRASS; this.tile[3][2] = LIGHT_GRASS;
*/
for (int i = 0; i < this.MAP_WIDTH; i++) {
for (int j = 0; j < this.MAP_HEIGHT; j++) {
random = rand.nextInt(2)+1;
//if (random==0) { this.tile[i][j] = LIGHT_GRASS; }
if (random==1) { this.tile[i][j] = DARK_GRASS; }
if (random==2) { this.tile[i][j] = DIRT; }
//if (random==3) { this.tile[i][j] = SAND; }
//if (random==4) { this.tile[i][j] = WATER; }
this.tileVis[i][j] = HIDDEN;
}
}
for (int i = 0; i < (800 / TILE_SIZE); i++) {
for (int j = 0; j < (600 / TILE_SIZE); j++) {
this.tileVis[i][j] = VISIBLE;
}
}
}
public void setX(int amount) {
this.xPos += amount;
}
public void setY(int amount) {
this.yPos += amount;
}
/**
* Draw this tile to the graphics context provided
*
* @param g The graphics context on which to draw
*/
public void draw(Graphics g) {
for (int i = 0; i < this.MAP_WIDTH; i++) {
for (int j = 0; j < this.MAP_HEIGHT; j++) {
if (this.tileVis[i][j] == VISIBLE) {
if (this.tile[i][j] == LIGHT_GRASS) { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\lightGrass.png"); }
if (this.tile[i][j] == DARK_GRASS) { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\darkGrass.png"); }
if (this.tile[i][j] == DIRT) { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\dirt.png"); }
if (this.tile[i][j] == SAND) { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\sand.png"); }
if (this.tile[i][j] == WATER) { this.tileGraphic = SpriteStore.get().getSprite("\\tile_textures\\water.png"); }
this.tileGraphic.draw(g, this.xPos+(this.tileGraphic.getWidth()*i), this.yPos+(this.tileGraphic.getHeight()*j));
}
}
}
}
}
public void programScroll(TileMap map) {
TileMap mapped = map;
if (leftPressed) { mapped.setX(4); }
if (rightPressed) { mapped.setX(-4); }
if (downPressed) { mapped.setY(-4); }
if (upPressed) { mapped.setY(4); }
}
/** BEGIN PAINT **/
g.setColor(Color.black);
g.fillRect(0,0,SCREENWIDTH,SCREENHEIGHT);
defaultMap.draw(g);
Next time i proabably will not use normal arrays for a map, for this reason:
Well I was wondering if arrayLists are better since they handel objects and classes. You see when I get to making an RPG, each tile much keep track of:
so what is the best way to make a 2D tile map for an rpg?
ty for help!

