Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  A simple slick2D game running at ~5 FPS. Why?  (Read 990 times)
0 Members and 1 Guest are viewing this topic.
Offline Llorn

Junior Newbie





« Posted 2014-10-12 21:39:17 »

Hi, I just started trying to code up a simple game to use a training project. The first milestone is a small field on which a player-controlled sheep will move around. I have been following thenewboston's tutorials and I'm using slick2D.

The idea behind the code is to represent the game world as a 2D array of Tiles in which all the internal logic will happen and from which all the information on what to display will be drawn. For now, I'm using a simple placeholder world generation algorithm and two test terrain types to see of the whole thing is working.

The game executes fine, but I noticed that for some reason or the other, a simple 20x20 grid of 16x16 sprites is playing merry hell on the FPS, dragging it down from ~2000(the performance on an empty window) to ~5. I'm sure it's the tiles graphics doing it because a 10x10 map will leave it at whopping ~20 and a 3x3 one at about ~200.

I'm sure I did some bummer there and I'm calculating something unnecessary over and over because I don't actually understand how all the stuff in the libraries works. I don't want to proceed any further until this is resolved, lest I build a game on faulty foundations and will have to redo it because it will run at 0.34 FPS on a good computer.

So, where is this extremely low FPS coming from? I'd be grateful for any answer.

Here's the code of the Play state:

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  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
package FFGame;

import java.util.Hashtable;
import java.util.Random;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState {
   
   Tile[][] world;
   int x, y;
   Random random;
   String[] terrain;
   Image steppe;
   Image grassland;
   
   public Play(int state) {      
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {  
     
      //Images
      terrain = new String[32];
      terrain[0] = "res/terrain/steppe.png";
      terrain[1] = "res/terrain/grassland.png";
     
     
      //TODO World generation. Provisional, revise this.
     
      Random random = new Random();
      x = 3;
      y = 3;
      world = new Tile[x][y];
      for (int i = 0; i < x; i++) {
         for (int j = 0; j < y; j++) {
            world[i][j] = new Tile(random.nextInt(2));            
         }      
      }
     
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {      
     
      for (int i = 0; i < x; i++) {
         for (int j = 0; j < y; j++) {
            Image toDraw = new Image(terrain[world[i][j].getTerrain()]);
            g.drawImage(toDraw, 16*i, 16*j);
         }
         
      }
     
      g.drawString("Play state", 150, 50);
     
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {      
   }
   
   public int getID() {
      return 1;
   }
}


And the Tile class, as of yet a mere shadow of things to come:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
package FFGame;

public class Tile {
   
   int terrain;
   int entities;  
   
   Tile(int terrain) {
      this.terrain = terrain;
   }
   
   int getTerrain() {
      return this.terrain;
   }
}


Image for better reference:
---
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #1 - Posted 2014-10-12 21:42:34 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {      
     
      for (int i = 0; i < x; i++) {
         for (int j = 0; j < y; j++) {
           Image toDraw = new Image(terrain[world[i][j].getTerrain()]);
            g.drawImage(toDraw, 16*i, 16*j);
         }
         
      }
     
      g.drawString("Play state", 150, 50);
     
   }


You seem to be reading an image file for each tile each frame.
terrain[] should be an array of images, not an array of paths to images.
Offline Llorn

Junior Newbie





« Reply #2 - Posted 2014-10-13 06:57:18 »

Seems to have worked, it's about 420 now. Thanks!
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (148 views)
2017-09-30 02:57:34

theagentd (213 views)
2017-09-26 18:23:31

cybrmynd (296 views)
2017-08-02 12:28:51

cybrmynd (285 views)
2017-08-02 12:19:43

cybrmynd (295 views)
2017-08-02 12:18:09

Sralse (288 views)
2017-07-25 17:13:48

Archive (967 views)
2017-04-27 17:45:51

buddyBro (1093 views)
2017-04-05 03:38:00

CopyableCougar4 (1665 views)
2017-03-24 15:39:42

theagentd (1426 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!