Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How would you go about designing levels from set colours from a .png file ?  (Read 977 times)
0 Members and 1 Guest are viewing this topic.
Offline GlennBrann
« Posted 2013-02-18 22:07:42 »

Hello, I don't know wheather my title was clear enough but i was wondering. How would i go about setting certain colours such as "0xff00ff" to lets say a grass block. and when ever there is that colour "0xff00ff" on a .png file it places it in the world. Is this complicated to. PS i am making a 2D game so its not a 3d game. Also another example would be this would be from Notch's Metagun game from the ludum dare competition:

I hope you can help thanks.

- GlennBrann
Offline jonjava
« Reply #1 - Posted 2013-02-18 22:32:44 »

Well, a pixel is generally made up of 4 bytes. The bytes are usually ordered like this: ARGB. Which stands for Alpha, Red, Green, Blue.

1 byte is reserved for each of these values. Together they make up 1 of over 16 million possible combinations/colours (disregarding the Alpha value)

values like "0xff00ff" are Hex values. This is identified by the "0x" that is used by convention to identify Hex values.

Data can be represented in many ways (decimal, hexa, binary, octal etc etc).

This means that the data "0000 0000 0000 0000 0000 0000 0000 0001" (represented here in binary) can be looked upon from Hexal, Decimal or Octal eyeglasses.

In Hex this data/value would be "0x00000001" (1 Hex character is made up of 4 bits (1 byte is made up of 8 bits, 1 bit is either a 1 or a 0 (binary)))

Sooo, basically each pixel in the PNG is made up of 4 bytes (an int is made up of 4 bytes).

So you read these pixels from the image, and then interpret them in some way and convert them into objects in your game.


Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #2 - Posted 2013-02-18 22:40:46 »

That's a good explanation, jonjava, but Java makes our lives so easy, why complicate it ?

You can use BufferedImage.getRGB() to get the int value for each color at its coordinate.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
 BufferedImage img = null;
 try {
  img = ImageIO.read(new File("path of your image"));
      } catch (IOException e) {
    e.printStackTrace();
    }
 
 int w = img.getWidth();
 int h = img.getHeight();  

 for (int i=0;i<w;i++)
      for (int k=0;k<h;k++)
    {
      int color = img.getRGB(i, k);
          //store color in an array for future use
         //...
         //...

    }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline vbrain
« Reply #3 - Posted 2013-02-18 22:45:46 »

That's a good explanation, jonjava, but Java makes our lives so easy, why complicate it ?

You can use BufferedImage.getRGB() to get the int value for each color at its coordinate.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
 BufferedImage img = null;
 try {
  img = ImageIO.read(new File("path of your image"));
      } catch (IOException e) {
    e.printStackTrace();
    }
 
 int w = img.getWidth();
 int h = img.getHeight();  

 for (int i=0;i<w;i++)
      for (int k=0;k<h;k++)
    {
      int color = img.getRGB(i, k);
          //store color in an array for future use
         //...
         //...

    }


According to THIS stackoverflow question, the getRGB() function is slower than other methods.
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #4 - Posted 2013-02-18 22:46:34 »

That's a good explanation, jonjava, but Java makes our lives so easy, why complicate it ?

You can use BufferedImage.getRGB() to get the int value for each color at its coordinate.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
 BufferedImage img = null;
 try {
  img = ImageIO.read(new File("path of your image"));
      } catch (IOException e) {
    e.printStackTrace();
    }
 
 int w = img.getWidth();
 int h = img.getHeight();  

 for (int i=0;i<w;i++)
      for (int k=0;k<h;k++)
    {
      int color = img.getRGB(i, k);
          //store color in an array for future use
         //...
         //...

    }


According to THIS stackoverflow question, the getRGB() function is slower than other methods.

that's why I said "store color in an array for future use", so you only have to read it once from the image.

Offline vbrain
« Reply #5 - Posted 2013-02-18 22:48:52 »

that's why I said "store color in an array for future use", so you only have to read it once from the image.

I know. I would like to read data as fast as possible, that's why I pointed it out.
Offline davedes
« Reply #6 - Posted 2013-02-18 23:10:27 »

For faster access, you have a couple options. One is to grab the raw int[] array backed by the BufferedImage, which is the fastest:
1  
int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();


The problem is, sometimes ImageIO will load an image using a type other than TYPE_INT_ARGB. So it might be backed by a byte[] array, or an int[] array of TYPE_INT_RGB. A simple solution is to create a new image of the necessary type. The benefit of this method is that you can modify the pixel array to change how the image is rendered.

In your case, since you don't need to render the buffered image, you might rather use this (which will do the type conversion for you):
1  
int[] pixels = img.getRGB(0, 0, imgWidth, imgHeight, null, 0, imgWidth);


As for how to implement something like this.. there are many ways. I like using enums for small projects. You could also use EnumMap or HashMap to query the colors. Here is one possible implementation:
http://www.java-gaming.org/?action=pastebin&id=432


Offline ReBirth
« Reply #7 - Posted 2013-02-19 11:35:34 »

And I think it's suitable for large world map. If you only have relative normal sized map, pick alternative solution that make your life easier.

Offline krasse
« Reply #8 - Posted 2013-02-19 12:31:17 »

You will feel limited when you want to add objects. Especially if you want to have parameterized objects in your map.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (25 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!