I have spent over 12 hours with no avail trying to successfully draw my map.
The map is stored in an array called Tiles[]
. Each value in Tiles can either be 4 numbers:
- Grass
- Stone
- Water
- Void (EMPTY BLOCK)
My map class reads an image and converts it into the 4 values in Tiles[]
.
For example: If I give the program this image:
The tiles array becomes this:
- 1
- 2
- 3
- 0
This should make my program to draw a 32*32 sprite of grass at 0,0, draw a 32*32 sprite of stone at 0,32, draw a 32*32 sprite of water at 32,0, and draw nothing at 32,32.
So far this is my code:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Map {
int MX;
int MY;
int mapWidth=2;
int mapSize=mapWidth*mapWidth;
int[] Tiles = new int[mapSize+1];
int Tile;
public Texture grass;
public Texture stone;
public Texture water;
BufferedImage map = null;
public void Init()
{
try {
grass = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Grass.png"));
stone = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Stone.png"));
water = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Water.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void LoadMap()
{
File file= new File("res/map.png");
try {
map = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int Y = 0;Y<map.getHeight();Y++){
for(int X = 0;X<map.getWidth();X++){
// Getting pixel color by position x=100 and y=40
int clr= map.getRGB(X,Y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
if(red==0&&green==0&&blue==0){
Tiles[Tile]=0;
}
if(red==255&&green==0&&blue==0){
Tiles[Tile]=1;
}
if(red==0&&green==255&&blue==0){
Tiles[Tile]=2;
}
if(red==0&&green==0&&blue==255){
Tiles[Tile]=3;
}
Tile++;
}
}
for(int i = 0;i<mapSize;i++)
{
System.out.println(Tiles[i]);
}
}
public void RenderMap()
{
}
}
What do I put in
`RenderMap()
{
}`
to make it draw all of the tiles at MX
and MY
?
I am using LWJGL (IF THAT HELPS AT ALL) and I would like to draw the tiles with openGL!
Thanks, Griffin!!!