Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  [SOLVED] Simple Tilemap from Image file noob question :D  (Read 583 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-05-24 03:07:58 »

Hello JGO, bringing forth a rather nooby question tonight but I'm in need of help Grin

Lets say I have a Image that's 2x2 and each pixel is colored green.

I want to scan the picture and detect if the pixels green. (completed)
I'd than like to render a tile which corresponds with the data in the image.

The problem I'm having is actually rendering on my map based off of the read data.

At the moment this is how I'm loading the map:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
   public void preInit(double currentPercentage) {
      if (!loaded) {
         /* ...... */
         /* Map Loading */
         int[] pixels;
         try {
            final BufferedImage img = ImageIO.read(new File("res/textures/terrain/map_1.png"));
            final int width = mapImgW = img.getWidth(null);
            final int height = mapImgH = img.getHeight(null);
            tiles = new int[width * height];
            pixels = ImageUtils.getPixelsFromBufferedImage(img); // Correctly obtains pixels[]
           final int green = -14503604; // Green ^_^
           for (int i = 0; i < pixels.length; i++) {
               if (pixels[i] == green) {
                  tiles[i] = 1; // Assign the tile type, 1 = green(grass)
              }
               //System.out.println("Number at slot " + i + " is: " + pixels[i]);
           }
         } catch (IOException e) {
            e.printStackTrace();
         }
         loaded = true;
      }
   }


If anybody could advise me on how I'd go about rendering the tiles please let me know ^_^, I've never done this before and I'm kinda stuck ATM ;o

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #1 - Posted 2013-05-24 03:25:59 »

The way I'm reading it, you're loading an image and rendering all of the green pixels on to the screen... Do I understand your code right? Is that what's happening now?

If yes, what did you want to do again, if not that? Smiley

I'm not familiar with ImageUtils.getPixelsFromBufferedImage() btw... where did you get that?

Offline GabrielBailey74
« Reply #2 - Posted 2013-05-24 03:36:20 »

The way I'm reading it, you're loading an image and rendering all of the green pixels on to the screen... Do I understand your code right? Is that what's happening now?

At the moment yea ^_^, I'll add other colors after green (Grass Texture) is working.

If yes, what did you want to do again, if not that? Smiley

I haven't found a way to implement it lol, that's the point of this thread =D

I'd like some pseudo code or advice on how to implement:
Reading a Images pixels, if one's a certain color render a Texture on the map that corresponds to the pixels color.

I'm not familiar with ImageUtils.getPixelsFromBufferedImage() btw... where did you get that?

It's from a little 'toolkit' I made Smiley
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   /**
    * Scans the BufferedImage provided to obtain the pixel[] data.
    * @param imageToScan The BufferedImage to scan for pixels.
    * @return The pixel[] array.
    */

   public static int[] getPixelsFromBufferedImage(BufferedImage imageToScan) {
      final int width = imageToScan.getWidth(null), height = imageToScan.getHeight(null);
      final int[] rgbs = new int[width * height];
      imageToScan.getRGB(0, 0, width, height, rgbs, 0, width);
      return rgbs;
   }


Notes:
Tiles are 32x32.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GabrielBailey74
« Reply #3 - Posted 2013-05-24 03:53:50 »

I got it >...<

I wasn't scaling the texture * 32 Angry
1  
2  
3  
4  
5  
6  
7  
      for (int x = 0; x < mapImgW; x++) {
         for (int y = 0; y < mapImgH; y++) {
            if (tiles[x][y] == 1) {
               g2d.drawImage(image_tiles[2].getImage(), x * 32 + mapX, y * 32 + mapY, tileW, tileH, null);
            }
         }
      }

Never mind, the above works but I'm still having issues with reading the map data properly and rendering it properly.

At the moment my map .PNG file looks like (A simple 4x2 .PNG):
[R][G][R][G]
[G][R][G][R]
http://i41.tinypic.com/33ayoaw.png

And I cannot manage to:
Load the data properly, and store it in my tile array.
-or it's-
Rendering the data incorrectly.

Here is what is rendering at the moment (incorrect):

Offline GabrielBailey74
« Reply #4 - Posted 2013-05-24 20:44:54 »

If somebody could direct me to a guide on how to load a tile map from a Image that would be great!!

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #5 - Posted 2013-05-24 23:38:33 »

I'd really want to help, but I cannot figure out how that 4x2 red + green image was rendered into what looks like grass and stone Huh

Offline GabrielBailey74
« Reply #6 - Posted 2013-05-25 01:00:13 »

I'd really want to help, but I cannot figure out how that 4x2 red + green image was rendered into what looks like grass and stone Huh

Wrong picture was uploaded my bad ^_^

Well thanks for the help guys, I was hoping to get a snippet / link reply (Thinking this is a common method like loading tiles from a text file)

I'm going to try loading tiles from a text file tonight :3

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.

Nickropheliac (10 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (27 views)
2014-08-22 19:31:30

atombrot (40 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (34 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!