Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Fast Image Rendering in Slick  (Read 2559 times)
0 Members and 1 Guest are viewing this topic.
Offline Miltage

Junior Newbie





« Posted 2011-03-20 21:52:48 »

Hi guys,

I recently started using Slick earlier today and I have found it extremely useful. I managed to get a working game prototype up and running in only a few hours (that includes the time it took me to draw and animate sprites).

The game uses a 20 x 20 grid of tiles. When the game initializes, it draws all the tiles onto one image, which takes about 2 - 3 seconds. During that time it displays a black screen, but that's fine I'm sure I can put a loading screen or something there.
During the game, it is very smooth and I have no problems because I only draw the one image with all the tiles drawn onto it.

When the player presses space bar, the tile under them changes. To make change visible I redrew all the tiles onto the image, but this freezes the game for about a second. I tried only drawing the changed tile onto the image, but it had the same effect.

I also tried to draw the tiles directly, instead of onto an image first. I get a FPS of only 2 or 3 then. So I tried drawing only those within 5 tiles of the character and got a FPS of 14 or 15. An improvement, but the game still wasn't smooth.

Is there a better way of drawing all the tiles onto the screen so that they can be changed on the fly without the game freezing?
Offline TheMatrix154

Junior Duke


Medals: 2



« Reply #1 - Posted 2011-03-20 21:59:43 »

A 20x20 grid shouldn't be a problem when your images aren't very big.
Could you post some code, especially the render and update method ( stripped down ) ?

Offline Miltage

Junior Newbie





« Reply #2 - Posted 2011-03-20 22:40:32 »

Alright, he's a very dumbed-down version of my code.
I tried to take out everything unnecessary.

The tiles are all in one png file, which I load into an Animation and turn auto update off so it doesn't cycle through.
Then when I change the tile, I simply change the frame of the animation.

When I recall drawTiles() in the grid class, that's when the game freezes for a bit.

Thanks for your help!

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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
/* Grid Class */
Image img;
Graphics g;
int width = 20;
int height = 20;
Animation[][] tiles = new Animation[height][width];

init(){
    img = new Image(width*60, height*60); //tile size is 60x60
    g = tiledImage.getGraphics();

    for(int i=0; i<height; i++){
   for(int j=0; j<width; j++){
            Image tileImg = new Image("tiles.png");
       SpriteSheet sheet = new SpriteSheet(tileImg, 60, 60);
            tiles[i][j] = new Animation(sheet, 1);
            tiles[i][j].setAutoUpdate(false);
        }
    }
}

drawTiles(){
    for(int i=0; i<height; i++){
   for(int j=0; j<width; j++){
       try{
           g.drawImage(tiles[i][j].getImage(), j*60, i*60);
       }catch(SlickException e){}
        }
    }
    g.flush();
}

/* Main game class */
Grid grid = new Grid();
update(){
   if(user presses spacebar){
        //tile changes
        grid.drawTiles();
   }
}

render(){
    grid.img.drawImage(0, 0);
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #3 - Posted 2011-03-30 19:43:55 »

My knowledge is very very limited but I think the problem is the lack of buffered images.(imagebuffer) (search slick and buffered
I think what might be happening is that its pulling from the image file each time.  I could be wrong but its my guess.



"Experience is what you get when you did not get what you wanted"
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2011-04-23 15:50:43 »

Slick only has Images, but that's fine. Smiley


I can't really figure out why you chose to store images in an animation, and other stuff.. I'm just going to give
you my way of drawing a tilemap that can be changed on the fly.

I usually create a tile object, holding it's own position and an image perhaps.
If I ALL the tiled, I use a Tile[][]-array, and load all the tiles into it on initialization.
When I need to change something about it I can just do something like this:
1  
2  
3  
4  
5  
public Tile getTile(x, y) {
     return tiles[x][y];
}

getTile(1,1).changeImage(Art.getBrick(4));


Where my art class contains this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
private static Image[] bricks = new Image[8];

    public static void loadBricks()
    {
        try {
            SpriteSheet sheet = new SpriteSheet(ART_PATH + "bricks.png", 20, 20);
            for (int i = 0; i < 8; i++)
            {
                bricks[i] = sheet.getSubImage(i, 0);
            }
        } catch (SlickException ex) {
            Logger.getLogger(Art.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Image getBrickImage(int id)
    {
        return bricks[id];
    }


This way images are only loaded once into different image-objects, saving that CPU usage.

It depends how much you need to change, and how often.. With the imformation you gave, this approach should be fast though Smiley

Good luck! 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.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (46 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!