Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Problem parsing tile set from a BufferedImage

Started by Megaman_22 Feb 27, 2007 at 8:49 PM 5 replies 1.4k views
Original Post
Megaman_22
Megaman_22
I'm writing a class to encapsulate a tile set for a hex-based game. I'm having some trouble parsing the one tile set .png file into individual tiles. This is my function thus far
public void load( String filename )
	{
		
		this.filename = filename;
		File file = new File( filename );
		BufferedImage whole_set = null;
		try
		{
			whole_set = ImageIO.read( file );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			System.exit( 1 );
		}

		final int CORNER = 0xFFFFFF00; // yellow
		final int ANCHOR_X = 0xFFFF00FF;// magenta
		final int ANCHOR_Y = 0xFF00FF00; // green
		
		int start_x[] = new int[ 256 ];
		int start_y[] = new int[ 256 ];
		int end_y[] = new int[ 256 ];
		int end_x[] = new int[ 256 ];
		int anchor_x[] = new int[ 256 ];
		int anchor_y[] = new int[ 256 ];
		int x_counter = 0;
		int y_counter = 0;
		int a_x_counter = 0;
		int a_y_counter = 0;
		
		if ( whole_set.getRGB( 0,0 ) == CORNER && whole_set.getRGB( 1,0 ) == ANCHOR_X
			&& whole_set.getRGB( 2,0 ) == ANCHOR_Y )
		{
			for ( int y = 1; y < whole_set.getHeight(); y++ )
				for ( int x = 1; x < whole_set.getWidth(); x++ )
				{
					if ( whole_set.getRGB( x, y ) == CORNER )
					{
						if ( start_x[ x_counter ] == 0 )
						{
							start_x[ x_counter ] = x;
						}
						else
						{
							end_x[ x_counter ] = x;
							start_x[ ++x_counter ] = x;
						}
						if ( start_y[ y_counter ] == 0 )
						{
							start_y[ y_counter ] = y;
						}
						else
						{
							end_y[ y_counter ] = y;
							start_y[ ++y_counter ] = y;
						}
						
						
					}// end if
					else if ( whole_set.getRGB( x, y ) == ANCHOR_X )
					{
						if ( anchor_x[ a_x_counter ] == 0 )
						{
							anchor_x[ a_x_counter++ ] = x;
						}
					}
					else if ( whole_set.getRGB( x, y ) == ANCHOR_Y )
					{
						if ( anchor_y[ a_y_counter ] == 0 )
						{
							anchor_y[ a_y_counter++ ] = y;
						}
					}// end else
					
				}// end for
				if ( ( ( x_counter != y_counter ) || ( a_x_counter != a_y_counter ) ) || x_counter == 0 )
					System.err.println( "it's fucked");
					
				for ( int i = 0; i < x_counter; i++ )
				{
					int width = 0;
					int height = 0;
					
					if ( end_x[ i ] != 0 )
						width = end_x[ i ] - start_x[ i ];
					else
						width = whole_set.getWidth() - start_x[ i ];
					if ( end_y[ i ] != 0 )
						height = end_y[ i ] - start_y[ i ];
					else
						height = whole_set.getHeight() - start_y[ i ];
						
					BufferedImage sub = whole_set.getSubimage( start_x[ i ], start_y[ i ], width, height );
				}
				System.err.println( "it's not fucked");
		
		}// end if
	}// end load()



This is my .png file I am using ( It's very basic, and extremely small ) simple tileset using my template This is a blown up version It seems that it doesn't work at all.
Eric Richards
Megaman_22
Megaman_22
Nevermind, I've fixed it
Eric Richards
OrangyTang
OrangyTang
You should be careful with the BufferedImage.getSubImage method - it returns an image which simply references a chunk of the original image data. Extracting a tile/sprite into a totally independant image (eg. by using getSubImage then drawing it onto a newly created BufferedImage) allows much faster image drawing (the image is more likely to be cached).
Megaman_22
Megaman_22
Thanks, I'll try that.
Eric Richards
zubatzu
zubatzu
lol "if ( ( ( x_counter != y_counter ) || ( a_x_counter != a_y_counter ) ) || x_counter == 0 )
System.err.println( "it's fucked");"
Son of Cain
Son of Cain
Quote:
Original post by zubatzu
lol "if ( ( ( x_counter != y_counter ) || ( a_x_counter != a_y_counter ) ) || x_counter == 0 )
System.err.println( "it's fucked");"


It's amazing what you can find working through other people's code =D

I suggest you take a look at worsethanfailure.com (former www.thedailywtf.com), for other laughs.

a.k.a javabeats at yahoo.ca
CaptainJester
CaptainJester
Quote:
Original post by Son of Cain
I suggest you take a look at worsethanfailure.com (former www.thedailywtf.com), for other laughs.



That's great one Rodrigo.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.