Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
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  
  Slick2D Most efficient way to draw a pixel array  (Read 1834 times)
0 Members and 1 Guest are viewing this topic.
Offline DDDBOMBER

Junior Member


Projects: 1



« Posted 2013-03-02 00:48:06 »

I have a int[] cols, and want to draw that in a grid across the screen, I currently have each color fill a 4x4 area, but I want to have it 1 pixel instead of 4. Doing that seems to cause tremendous lag. I am looking for a better way to do this.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void renderTiles(Graphics g, int xScroll, int yScroll, int width, int height){
      for(int x = -1; x < width; x++){
         for(int y = -1; y < height; y++){
            int xt = (x+xScroll/tileSize)%size;
            int yt = (y+yScroll/tileSize)%size;
            if(xt < 0)xt += size;
            if(yt < 0)yt += size;
            g.setColor(new Color(tiles[xt+yt*size]));
            g.fillRect(x*tileSize-xScroll%tileSize, y*tileSize-yScroll%tileSize, tileSize, tileSize);
         }
      }
   }
Offline jonjava
« Reply #1 - Posted 2013-03-02 00:59:49 »

Hmm, why don't you just stretch the image into 4 times its size?

1  
g.drawImage( image, 0, 0, width * 4, height * 4, null );

Offline DDDBOMBER

Junior Member


Projects: 1



« Reply #2 - Posted 2013-03-02 01:03:06 »

There isn't actually an image, I'm using it for the background and the cars are leaving tracks in it, so the array is constantly changing
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-03-02 02:21:15 »

If you have a pixel array, you've got an image.

You're not using 'int[] cols' in your code. Is it actually called int[] tiles?

1  
2  
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] tiles = ((DataBufferInt)bimg.getRaster().getDataBuffer()).getData();


Now changes made to int[] tiles will be reflected in the BufferedImage.

Or you could copy them into another BufferedImage whenever int[] tiles changes.

1  
2  
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bimg.setRGB(0, 0, width, height, tiles, 0, width);

Offline davedes
« Reply #4 - Posted 2013-03-02 02:49:18 »

jonjava - he says he is using Slick2D, not Java2D.

In OpenGL you generally don't modify texture data per-pixel in real time; instead, you modify pixels as they are rasterized by GL. The former requires copying data between CPU and GPU (very slow), whereas the latter is done all on GPU (very fast).

Instead of modifying textures per-pixel, you might get faster speeds by drawing rectangles. The problem is that Slick doesn't batch calls to g.fillRect, so using it many times per frame can create a bottleneck. Instead, you might have better luck with using an Image to create an opaque coloured rectangle. So it might look like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
//pack a small opaque white area into your sprite sheet, 
//e.g. 1x1 white pixel with nearest filtering
//or a larger area with bilinear filtering

//get the sub-image of the white pixel
Image whitePixel = spriteSheet.getSubImage(...);

...

//now in your render loop, draw many rectangles like so:
spriteSheet.startUse();

//... draw your other sprites from this sheet using drawEmbedded ... //


//now bind the color you want your filled rectangles to be, e.g. black or whatever
rectColor.bind();

for ( each filled rectangle ) {
    //draw the filled rectangle
   whitePixel.drawEmbedded(x, y, width, height);
}

//end the sprite sheet
spriteSheet.endUse();


I also described the technique here (for a different library):
https://github.com/mattdesl/lwjgl-basics/wiki/Batching-Rectangles-and-Lines

For more info on drawEmbedded:
http://slick.cokeandcode.com/wiki/doku.php?id=performance_memory_tips

If you want to try modifying a texture per-pixel (and deal with the performance problems of copying data from CPU to GPU), check out this page:
http://slick.cokeandcode.com/wiki/doku.php?id=per-pixel_manipulation:pixeldata_utility

If you are more interested in the GPU approach, you might have some luck from shaders:
http://slick.cokeandcode.com/wiki/doku.php?id=shaders
https://github.com/mattdesl/lwjgl-basics/wiki/Shaders

Offline DDDBOMBER

Junior Member


Projects: 1



« Reply #5 - Posted 2013-03-02 10:35:36 »

Thanks very much, your method is a lot faster than what I was doing, I am going to look into shaders as they would probably be the most efficient. I still get a bit of lag when alot of the screen has been changed.
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 (11 views)
2014-07-30 21:08:39

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

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

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

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

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

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

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

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

Riven (30 views)
2014-07-23 20:56:16
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

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!