Slick Forums

Discuss the Slick 2D Library
It is currently Sat Nov 01, 2014 1:02 pm

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sat Feb 19, 2011 3:35 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
Hi there!

First of all thanks for providing a great 2d java library. I am using it to create a transport simulation game called P1SIM. Here is a screenshot showing the current status:

http://www.tt-forums.net/download/file.php?id=141074

I am generating the terrain by using tile-based sprites. The render method of the class terrain directly draws each sprite onto the screen. Here is the corresponding code:

Code:
public void render()
   {
      int spriteID = 0;
      int spriteID_x, spriteID_y;
      int baseHeight = 0;
      
      for (int iy = 0; iy < SIZE_Y; iy++)
      {
         for (int ix = 0; ix < SIZE_X; ix++)
         {
            int x = origin_x + ix * 32 - iy * 32;
            int y = origin_y + ix * 16 + iy * 16;
            
            (...)
            
            spriteID = getSpriteIDforCornerHeights(heightmap[iy + 1][ix], heightmap[iy][ix], heightmap[iy][ix + 1], heightmap[iy + 1][ix + 1]);
            
            spriteID_y = (int)Math.ceil((int)(spriteID / 16));
            spriteID_x = spriteID % 16;                     
            
            texture.draw(
                  x, y - baseHeight * 15, x + 64, y + 64 - baseHeight * 15,
                  spriteID_x * 64, spriteID_y * 64,
                  spriteID_x * 64 + 64, spriteID_y * 64 + 64);            
         }
      }
   }


That works for terrain sizes up to about 64x64 tiles. (Still having about 60 frames per seconds then). But I want to provide maps being about 2000x2000 tiles big (!). Currently thats not possible. To many image.draw()'s inside the terrain loop.

DirectX provides something like surfaces where you can draw your images on to buffer the whole terrain. Only when the terrain changes, you redraw the new terrain parts. That way there is only one draw image call per frame (if nothing changes).

How to realize that using SLICK? What i want as pseudo code:

Code:
Image surface;

init()
{
surface = new Image(10000, 1000);
}

update()
{
if (terrainHasChanged)
   foreach (terrainTile)
       terrainTile.draw(on surface at pos x/y);
}

render()
{
surface.draw()
}

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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2011 4:51 pm 
Offline
Regular

Joined: Tue Mar 03, 2009 11:53 pm
Posts: 123
Hi.
Try Image.getGraphics() in order to draw on an Image.

However, if I'm not misunderstood, you are not drawing 2000x2000 tiles on the same screen. So, a simpler and better way would be to determine which tiles actually fit the screen and cull the others, so that you have roughly the same number of tiles to be drawn at each frame.

Ciao!

_________________
Carotinho


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2011 5:05 pm 
Offline
Game Developer
User avatar

Joined: Tue Nov 21, 2006 4:46 am
Posts: 620
Location: Iceland
That's a bad way to do tile based rendering.

All you need is a 2d array, tiles[x][y], where each element represents a tile in the map. And only render the tiles that are visible on the screen.

Slick has support for "Tiled" maps. Check out the Tiled map editor.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2011 10:53 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
stupid me... of course i have to draw the visible tiles only. so i give you another example:

i have coded my own gui system. the windows and buttons are drawn by drawing many many single sprites. since the window and its content doesnt change every frame AND the whole window is visible completely (no culling needed) it would be better (performancewise) to buffer the window when rendered one time and then only draw the whole buffered image/surface (call it as you like) every frame.

you (carotinho) suggested to use image.getgraphics(). could you provide a more detailed example showing how to use it? i tried something like

Code:
Image surface = new Image(800, 600); // buffer image to save assembled window
Graphics g = image.getGraphics();
g.drawImage(anyPartOfTheWindow);
g.drawImage(anotherPartOfTheWindow);


but now i need a command to draw this "g" like i draw other images. other images can be drawn using
image.draw()
but there is no
graphics.draw()
to send the graphics content to the opengl renderer.[/code]

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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2011 12:11 pm 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1325
Location: Italy
I guess.. if you will use QuadTree instead of ArrayList you can be really more fast when you decide what tile you can draw. This can help you?

_________________
Blog | Last game Gravity Duck tribute | In progress Gravity Duck tribute


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2011 2:20 pm 
Offline
Regular

Joined: Tue Mar 03, 2009 11:53 pm
Posts: 123
Hi

Gornova81 wrote:
I guess.. if you will use QuadTree instead of ArrayList you can be really more fast when you decide what tile you can draw. This can help you?


Isn't that a bit overkill? After all, if it's a rectangular portion of a bigger rectangle it's a matter of a pair of divides to discover from which tiles starting to render and how many of them you need to fill the screen.

ciao!

_________________
Carotinho


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 20, 2011 6:03 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
My problem isnt how to calculate which tiles to draw. I want to know how to draw an image onto another image to be able to save a i.e. a window built up by drawing many sprites. I want to save that window as one image.

Example:

Image

The image shows the window. The real content of the window has the size of the yellow rectangle. The content that should be displayed must fit in the red rectangle.

I need a method that draws the content onto an image (yellow rectangle) so that another method can draw the red rectangle by drawing only a part of the saved yellow rectangle.

I tried something like:

Code:
java.awt.image.BufferedImage bimage = new java.awt.image.BufferedImage(100, 100, java.awt.image.BufferedImage.TYPE_INT_ARGB);
java.awt.Graphics2D gTmp = bimage.createGraphics();
gTmp.drawImage(texture, 0, 0, null);
gTmp.dispose();


But the line "gTmp.drawImage(texture, 0, 0, null);" is not compileable since "texture" has the type org.newdawn.slick.Image while the type java.awt.image is needed.

How to cast a org.newdawn.slick.Image into a java.awt.image? Please help me :)

edit: this was helpful:
http://slick.javaunlimited.net/viewtopic.php?t=2898

leading me to:
https://bob.newdawnsoftware.com/repos/slick/trunk/Slick/src/org/newdawn/slick/tests/ImageGraphicsTest.java

trying to adapt the stuff now...

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


Top
 Profile  
 
 Post subject: Yeehaw! That works.
PostPosted: Mon Feb 21, 2011 4:16 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
This code example is quite stupid, but it shows how to draw on another image and then draw the final image (=surface) to the screen.

Code:
public class CTerrain
{
   Image texture = null;      
   Image surface;
   Graphics gSurface;
         
   public CTerrain() throws SlickException
   {
      texture = Mngr.Sprites.loadTexture("misc/ground_tiles.png");
      surface = new Image(400,300);
      gSurface = surface.getGraphics();
   }
      
   public void render(GameContainer gc, Graphics g) throws SlickException
   {      
      /* redraw surface (normally these two lines should be placed in another method, since it makes no sense to redraw the surface every frame. then you wouldnt need a buffer surface to draw on ;) */

      gSurface.drawImage(texture,100,150);
      gSurface.flush();
      
      surface.draw(100, 100);
   }   
}

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2011 8:56 pm 
Offline

Joined: Sat Feb 19, 2011 2:59 pm
Posts: 52
just wanted to show you the progress. thanks for your help so far

http://www.tt-forums.net/download/file.php?id=141966

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2011 9:08 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Really nice!

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2011 10:22 pm 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1325
Location: Italy
smallfly wrote:
just wanted to show you the progress. thanks for your help so far

http://www.tt-forums.net/download/file.php?id=141966


super cool!

_________________
Blog | Last game Gravity Duck tribute | In progress Gravity Duck tribute


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

All times are UTC


Who is online

Users browsing this forum: HariboTer 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