Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 2:17 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon May 21, 2012 9:31 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
Hello together,

I am developing the logistics game "P1SIM" (see http://www.p1sim.org) and I have a question about SLICK.

I would like to do something like this (pseudo code!):
Attachment:
this.png
this.png [ 2.47 KiB | Viewed 494 times ]


But there is no similar function. So how to modify the single pixels of an image? I want to use loops to modify all pixels after each other and this way create an overview map of the terrain (see website for screenshots). Having modified the pixels I want to draw them using g.draw(image, x, y).

Thanks in advance for your help! I already coded more than 6000 lines of code, but now I get a bit stuck due to the pixel modification issue :(

smallfly

//edit: i tried something like g.drawline(x, y, x, y) to draw pixels but drawing 400x400 pixels image that way really slows down the game

//edit: i am now using a buffer (see code). but its quite slow. how do you realize fast pixel modifications?

Code:
private ImageBuffer buffer;
private Image image;
...
buffer = new ImageBuffer(SIZE, SIZE);
image = buffer.getImage();
...
for (...)
    for (...)
        ...
        buffer.setRGBA(x, y, red, green, blue, 255);

_________________
http://www.p1sim.org - Economy/Logistics simulaton based on Java and Slick2D


Top
 Profile  
 
PostPosted: Mon May 21, 2012 10:24 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
Slick on its own isn't exactly suitable for per-pixel software rendering, which is what you're trying to do. Instead, you should look into glTexSubImage2D to upload pixel data yourself (and only upload it when the height map changes).

Another, perhaps more efficient solution would be to use shaders.

Both of these are relatively simple to add, and once you begin to grasp their concepts they will enable you to do a lot of neat things that would not be possible/efficient with plain old vanilla Slick2D rendering. Since these are commonly asked topics, I'll see if I can get some examples going.


Top
 Profile  
 
PostPosted: Tue May 22, 2012 6:20 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
Thanks so far :-) I will read some stuff about glTexSubImage2D and shaders.

But still one more question about the (in my case perhaps unsuitable) image buffers. After a short time (ca. 30 seconds) I get an out of memory error like this:

Code:
java.lang.OutOfMemoryError
   at sun.misc.Unsafe.allocateMemory(Native Method)
   at java.nio.DirectByteBuffer.<init>(Unknown Source)
   at java.nio.ByteBuffer.allocateDirect(Unknown Source)
   at org.lwjgl.BufferUtils.createByteBuffer(BufferUtils.java:60)
   at org.newdawn.slick.ImageBuffer.getImageBufferData(ImageBuffer.java:97)
   at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:376)
   at org.newdawn.slick.Image.<init>(Image.java:311)
   at org.newdawn.slick.Image.<init>(Image.java:289)
   at org.newdawn.slick.Image.<init>(Image.java:278)
   at org.newdawn.slick.ImageBuffer.getImage(ImageBuffer.java:140)
   at org.p1sim.game.gui.content.map.CMap.redraw(CMap.java:153)
   at org.p1sim.game.gui.CContent.redraw(CContent.java:85)
   at org.p1sim.game.gui.CPanel.redraw(CPanel.java:74)
   at org.p1sim.game.gui.CPanel.render(CPanel.java:65)
   at org.p1sim.game.gui.CWindow.render(CWindow.java:98)
   at org.p1sim.game.managers.CWinMngr.render(CWinMngr.java:188)
   at org.p1sim.game.objects.CGuiLayer.render(CGuiLayer.java:92)
   at org.p1sim.game.Mngr.render(Mngr.java:210)
   at org.p1sim.game.CGame.render(CGame.java:52)
   at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:681)
   at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
   at org.p1sim.game.CGame.main(CGame.java:63)
Tue May 22 20:16:13 CEST 2012 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
   at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:684)
   at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
   at org.p1sim.game.CGame.main(CGame.java:63)
java.lang.OutOfMemoryError


It happens here in the code (see below) in the line g.drawImage(buffer.getImage(), 0, 0);. What general mistake do I make? I think Im using the image buffer in a totally wrong way, do I?

Code:
(...)

public class CMap extends CContent
{   
   (...)   
   private ImageBuffer buffer;
        (...)
   
   public CMap()
   {
      (...)
      buffer = new ImageBuffer(SIZE, SIZE);
      (...)
   }
   
   public void setOriginMapCoords(int sx, int sy)
   {
      (...)
      clearBuffer();
   }
   
   protected void clearBuffer()
   {
      for (int ix = 0; ix < SIZE; ix++)
         for (int iy = 0; iy < SIZE; iy++)
            buffer.setRGBA(ix, iy, 0, 0, 0, 0);
   }
   
   public void scroll(DIRECTION direction)
   {
      (...)
      clearBuffer();
                (...)
   }
   
   @Override
   public void update(int delta)
   {   
      (...)
      int r = 0; // red
      int g = 0; // green
      int b = 0; // blue
      
      int mx;
      int my;
      
      for (int updateCycles = 0; updateCycles < 3; updateCycles++)
      {      
         iterator = iterator + STEP;
         
         if (iterator >= SIZE)
            iterator -= SIZE - 1;      
   
         for (int ix = iterator; ix <= iterator; ix++)
         {
            for (int iy = 0; iy < SIZE; iy++)
            {
                                        (...)
               if (CTerrain.DEEP_WATER_LEVEL <= height  && height < CTerrain.SHALLOW_WATER_LEVEL)
               { r = 46; g = 85; b = 152; }
               else if (CTerrain.SHALLOW_WATER_LEVEL <= height  && height < CTerrain.SAND_LEVEL)
               { r = 45; g = 164; b = 153; }
               (...)
               buffer.setRGBA(ix, iy, r, g, b, 255);            
            }   
         }
      }
   }

   @Override
   protected void redraw(Graphics g)
   {
      g.drawImage(buffer.getImage(), 0, 0);
      
      (...)
   }

      
}

_________________
http://www.p1sim.org - Economy/Logistics simulaton based on Java and Slick2D


Top
 Profile  
 
PostPosted: Tue May 22, 2012 7:21 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
I had a look at this
http://pastebin.com/MMw1zgEa

You posted it in April this year :)

And it works fine. And how much faster are shaders? When editing 512^2 pixels all the time?
(But just for your info: I do not update all the pixels all the time since my map view algorithm loads single columns. See attached screenshot.)


Attachments:
2012-05-22T20-05-20.png
2012-05-22T20-05-20.png [ 87.94 KiB | Viewed 465 times ]

_________________
http://www.p1sim.org - Economy/Logistics simulaton based on Java and Slick2D
Top
 Profile  
 
PostPosted: Tue May 22, 2012 9:14 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
As long as it's used efficiently, shaders will be faster. They modify the pixel colors at the lowest level possible.

I'm working on a "PixelData" type utility to solve some per-pixel problems that many Slick users face. It may or may not be added to the official Slick library, depending on community/dev opinion. Here's an article describing it a bit: (still a WIP)
http://slick.cokeandcode.com/wiki/doku. ... ta_utility

And here is the code: (also a WIP)
http://pastebin.com/Bhss398V

And here's a more comprehensive test case using PixelData to mimic what you might do in shaders (i.e. fractal sets, radial gradients, etc).
http://pastebin.com/JwCGS1iW

Result:
Image

(Of course, if you're doing something as intensive as a fractal, you should be using hardware acceleration instead of writing pixel-by-pixel.)


Top
 Profile  
 
PostPosted: Tue May 22, 2012 9:23 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
the examples you posted show a technique i already implemented. do you have shader example for me? :)

_________________
http://www.p1sim.org - Economy/Logistics simulaton based on Java and Slick2D


Top
 Profile  
 
PostPosted: Wed May 23, 2012 12:29 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
In theory, shaders will be faster since they don't require synchronous texture uploads (which, when used many times per frame, can slow things down). But given your particular needs it probably wouldn't be easy to write a shader solution. And glTexSubImage2D already tends to be very fast when used occasionally, so any performance difference is likely to be negligible.

If you're finding a bottleneck with glTexSubImage2D, and you've already tried all the optimizations listed in the wiki article I linked, then you could also look into asynchronous texture uploads (using PBO) which will speed things along. I hope to add them into my PixelData utility at some point. :)

One thing, though, don't use glTexSubImage2D for each column; instead just send all of the mini-map data (512x512) at once with glTexSubImage2D. Obviously you should only do that once -- when the map is loaded. From then on, you only need to update sub-regions of your mini map depending on what's changed on the actual map. So if your user loads up a map and leaves it idling, you've only used a single texture upload.


Top
 Profile  
 
PostPosted: Wed May 23, 2012 4:48 am 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
the maps of my game are infinite. you can scroll as far as you want. thus the minimap changes all the map when a user has opened the minimap window and scrolls the map or the main view.

_________________
http://www.p1sim.org - Economy/Logistics simulaton based on Java and Slick2D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group