Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
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 642 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

JGO Knight


Medals: 32



« 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!
Try the Free Demo of Droid Assault
Offline jonjava

JGO Knight


Medals: 32



« 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  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (62 views)
2013-05-17 21:29:12

alaslipknot (71 views)
2013-05-16 21:24:48

gouessej (102 views)
2013-05-16 00:53:38

gouessej (99 views)
2013-05-16 00:17:58

theagentd (108 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.088 seconds with 20 queries.