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 (553)
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  
  Tile Rendering ?!  (Read 1911 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelDeBurner

Junior Member


Medals: 1



« Posted 2012-08-13 12:41:13 »

Hi guys.

I was searching online and could not finding the answer to this. I'm programming a little tile based game, just to learn some stuff. And I'm using mojang's Catacomb Snatch to learn from it. I tried to use Slick and managed to do the map rendering, but it was weird. ( lines occure here and there ).

Now, I want to ask you, in plain java, how would you render tiles. Would you just use drawImage method ?

Or something that I saw, using pixels array with databuffer from bufferedImage, and than inserting pixels into pixels array and rendering that ?
I don't understand this pixels array stuff, so could you explain it to me ?
Offline jonjava
« Reply #1 - Posted 2012-08-13 13:13:34 »

Well, any image one way or another is made up of pixels. A pixel is simply a bucket of 3 colours. 3 fundamental colours that can represent any colour the eye can see. I.e, Red, Green and Blue. If you put a magnifying glass on your monitor and inspect you can see that each tiny little pixel on your monitor is actually made up of 3 even smaller lights. A Red light, a Green light and a Blue light. By varying how intensely these 3 actual mini light bulbs shine you can make it appear as any colour. Contemporary monitors have such high resolutions (more pixels over area (even smaller pixels)) it might be hard to even see the 3 tiny lights that make up every pixel.



SO. A pixel is simply a bucket of 3 values. 3 values that can each have a value from 0 to 255. ( Also known as a byte ( A byte can have values form 0-255 )). The higher the value, the brighter the light. Black is when all the values are 0 and White is when all the values are 255. Together these 3 values that can have 255 values each can represent (255 * 255 * 255 = 16,581,375 ) ~16 million different colours.

Most of the time, a pixel can be placed in an int. An int is made up of 4 bytes and therefore the 3 bytes that represent a pixel can fit in there fine. There's an extra byte that is unused an int however, and that's where we can put an extra value called the "Alpha". It represents the transparency of the given pixel. (Transparency = how much this pixel blends in with another pixel. )

By default java and the majority of anything else represents pixels as a 4 byte value made up of an Alpha, Red, Green and Blue value (I.e, ARGB). These are also how the bytes are ordered.

So if we have an ARGB pixel and we would like to grab the Red colour of the pixel, we would grab the third byte from the right.

We can do this by using bitwise operators. Here's a few ways: ( 1 byte = 8 bits )

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
byte red = ARGB >> 16; 
/* shift to the right 16 bits = 2 bytes (essentially deleting the first 2 bytes (I.e GB in ARGB))
and save the value in the byte called 'red' ( we save the value AR, which is 2 bytes into the
variable red, but the variable red can only hold 1 byte, so only the first byte (starting from the right)
gets saved and the rest (the A byte in ARGB) gets skipped */


byte red = ARGB & 0x00FF0000;
/* Grab the third byte from the right, ignore the others. This is called "bitmasking".
It looks weird like this if you don't know what hex values are, but in actual bits this
hex value (0x00FF0000) looks like this [00000000111111110000000000000000] so we
surgically grab the 8 bits that represents the colour red */

http://en.wikipedia.org/wiki/Mask_%28computing%29
http://en.wikipedia.org/wiki/Bitwise_operation

Java's BufferedImages are abstractions from all this bit manipulation. Juggling with the pixels yourself or using BufferedImages is all preference. There's no particular significance using one over the other.

The writableRaster you're talking about is a special pixel array that is connected with the BufferedImage it was taken from. Special in the sense that they both (the raster and the bufferedImage) both point into the same pixel array in memory. So a change in either of them is reflected on the other.

You can get a copy of any pixel array from a BufferedImage in java by using the BufferedImage's builtin getRGB() method. This gives you a copy of that array, and changing that array won't alter the BufferedImage it was copied from.

As an example, this applet draws all its tiles with g.drawImage() and does no manual pixel manipulation whatsoever.

http://heartpirates.com/hearts/games/isoapplet.html
source code here: http://jonjavaweb.orgfree.com/IsoWorldSource.zip

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #2 - Posted 2012-08-13 13:22:03 »

I thought maybe it has some perfomance difference. That's why I was asking.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2012-08-13 13:35:29 »

TL;DR - No don't worry about it! Deal with pixels themselves only if you want to!:}

Well if you want to deal with pixels you have greater control over what's going on, but you also have a lot more responsibility.

Performance differences between the two fundamentally are negligible. If you want high graphics and performance, both of these are shit compared to LWJGL.

In other words, use what you feel more comfortable with, performance shouldn't be an issue.

Why people want to juggle around with the pixels themselves is probably because it can be considered as fun. I.e, it's fun to know how it works and deal with it yourself - but it does take a more involved mind. I.e, only geeks and nerds do it for their own pleasure.

This tendency to do things the hard way because it's fun can be detrimental, however. So be wary. Over optimizing can be fun but also can be a huge waste of time.

Time wasted having fun != time wasted - well, tell that to the overall progress and health of your 2+ year projects that still isn't finished.

[EDIT]: I just want to clarify that I only speak on the behalf of myself and the time I've wasted having fun dealing with pixels and optimizing unnecessarily, this post comes off a lot more cynical than intended :]

Offline davedes
« Reply #4 - Posted 2012-08-13 13:44:08 »

Your map will likely be made up of images/sprites, so you will just be using drawImage. You don't need to worry about pixels or RGB data just to draw a simple tiled map.

Java2D is slow and your performance will likely not be suitable after you start adding more sprites/features/particles/etc. You should stick with LibGDX or Slick2D (or anything else based on OpenGL) if you want good performance.

Regarding the "weird lines" issue when using Slick: this is because you were using GL_LINEAR as your filtering mode. As you said yourself in the slick forums, nearest neighbour fixed the issue. I explain in more detail why this occurs here:
http://www.java-gaming.org/topics/bleeding/26823/msg/237162/view.html#msg237162

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #5 - Posted 2012-08-13 13:46:06 »

I used the nearest filter, but it fixed the issue I had on hole tiles, but then, ( maybe it's just my pc performance issue ) it seems like some tiles lag on some places, and empty 1px wide background lines started to occur on some places.
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #6 - Posted 2012-08-13 14:00:49 »

@davedes :

Here is what I'm talking about :



The black lines on floor tiles, occur only on some places when I'm at specific location... do you know what could it be ?

EDIT: I'm using g.translate(x, y) to scroll the map. Could that be issue ?
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #7 - Posted 2012-08-13 22:20:40 »

Well, it totally depends on the width and the height of the tiles, and how you are placing the tiles. If you are using division to find out where to place tiles when you move around your map, sometimes you will not get an even number and it'll cause some spots to have a 1px wide gap in between the tiles.

To fix it, you should make sure your beginning map has all the tiles spread uniform (evenly using pixels). Use translate (x, y) to move around the map. If you have zoom, it can complicate matters. Please tell me if that is the case.

Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #8 - Posted 2012-08-13 22:28:51 »

I'm using 32 * 32 tiles, using 4 for loops, that loop through level's width and height, and based on that set the tiles. Then I initialize them by multiplying the x and y values by tile size.

Then I'm using slick's g.translate to move them around.
Offline davedes
« Reply #9 - Posted 2012-08-13 23:55:59 »

Try using integer coordinates when rendering/translating to see if that resolves the issue. Half-pixels don't always play nicely in OpenGL.

Regarding stuttering/performance -- if you are still getting issues with the latest LWJGL (2.8.4 or greater) and the latest Slick code (from dev branch) then you may have found a bug in Slick or LWJGL.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PixelDeBurner

Junior Member


Medals: 1



« Reply #10 - Posted 2012-08-14 12:29:01 »

Casting my xScroll and yScroll as int fixed my problem. Now I'm just gonna try to recreate this stuff in pure java ( just for fun Cheesy ).
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.

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

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

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

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

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

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

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

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

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (38 views)
2014-08-08 02:01:56
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!