Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

  1. Grass
  2. Stone
  3. Water
  4. 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:

2*2 Pixels wide

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 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) {


     public void LoadMap()
         File file= new File("res/map.png");
        try {
            map =;
        } catch (IOException e) {
            // TODO Auto-generated catch block

            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;


            for(int i = 0;i<mapSize;i++)

        public void RenderMap()


What do I put in



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!!!

share|improve this question

closed as too localized by Byte56, Le Comte du Merde-fou, Tetrad Feb 25 '13 at 17:25

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Have you heard of loops? I recommend an introductory programming book. – Anko Feb 24 '13 at 13:00
I'm sure Griffin has heard of loops, as they are using them in the existing code. I do think this question is a "write my code for me" question. It's a problem that can easily be solved by thinking out loud about what you want to do. You even already have an example of how to loop through all your tiles. Convert your sentence ("This should make my program...") into code. Then find a way to make it generic for any array, no matter what's in it. Then find a way to extend that to make it work for any size array. – Byte56 Feb 24 '13 at 16:48
I don't think he has a problem with the map logic. Though I don't know why he put so much of it in the question. I think the question is more about how to render in OpenGL. Unfortunately there is no simple answer to this. If this doesn't make any sense, go through the tutorials one by one. – JohannesA Feb 24 '13 at 18:05
I get that I have to use loops to draw them and I know how to render them in openGL. I just need help working out where each tile goes! – Griffy Feb 25 '13 at 4:56

If the map is stored in a 1D array, you can calculate the position of the tile like this:

for (int i = 0; i < Tiles.size(); i++) {
  int x = (i  % mapWidth) * 32; //You said tiles are 32 pixels, should be put into a constant.
  int y = Math.floor(i / mapWidth) * 32;

  //Render the quad at (x, y)


Or change the array into a 2D array like you did, but 1D arrays can be cleaner and some times faster.

share|improve this answer
I dont want to hijack this thread but I am curious why you feel 1D arrays are faster? Do you mean faster in computer terms or something more significant than that? – RobCurr Feb 25 '13 at 7:41
Faster in computer terms and often results in cleaner code too, as in this case. At least in my opinion this single loop with some extra math is a lot cleaner than two nested loops. – JohannesA Feb 25 '13 at 7:45
Ok I will agree with that. Just didn't want people to come away with thinking they are hosed if they feel they have to use a 2D array. – RobCurr Feb 25 '13 at 7:49
Edited the post a bit to soften it down. Compilers often optimize it anyways so it doesn't matter what you use. – JohannesA Feb 25 '13 at 7:52
@JohannesA "faster" isn't exactly a valid description is it? It still comes down to the Big O measurement( And I really don't think simple loops like this will be any different eitherway). And whether it's cleaner is partially subjective. It really depends on the data structure you use and the algorithm that goes with it. Readability is also part of "clean" code. – Sidar Feb 25 '13 at 10:01
up vote 1 down vote accepted

I solved the problem by changing the Tiles[] array to a 2D array that i could save the colors X and Y positions as well as the color.

Then I just ran through them with 2 for(){} loops and drew openGL quads with the textures.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.