Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  After using slick for textures, game lags too much  (Read 421 times)
0 Members and 1 Guest are viewing this topic.
Offline Ablaze

Junior Newbie





« Posted 2013-12-08 10:22:58 »

Hi!
    I made a small test game. It uses textures using slick. The textures cause ENORMOUS lag. Can someone help me?

Main.java
1  
2  
3  
4  
5  
6  
7  
package example;

public class Main {
   public static void main(String[] args) {
      new LWJGLInput();
   }
}


LWJGLInput.java
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  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
package example;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


public class LWJGLInput {
   
   List<Box> boxes = new ArrayList<Box>(16);
   boolean select = false;
   
   
   public Texture loadTexture(String key){
      try {
         return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/" + key + ".png")));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      return null;
   }
   public LWJGLInput(){
      try {
         Display.setDisplayMode(new DisplayMode(640, 480));
         Display.setTitle("Puzzle");
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      //65
      boxes.add(new Box(15, 15));
      boxes.add(new Box(200, 150));
      boxes.add(new Box(15, 115));
     
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, 640, 480, 0, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_2D);
     
     
      while (!Display.isCloseRequested()){
         glClear(GL_COLOR_BUFFER_BIT);
         
         
         
         if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            Display.destroy();
            System.exit(0);
         }
         
         for(Box box : boxes){
            if(Mouse.isButtonDown(0) && box.isInBounds(Mouse.getX(), 480 - Mouse.getY()) && !select && (boxes.indexOf(box)==1)){
               box.selected = true;
               select = true;
            }
           
            if(Mouse.isButtonDown(1)){
               box.selected = false;
               select = false;
            }
           
            if (box.selected){
               box.update(Mouse.getDX(), -Mouse.getDY());
            }
           
            if(boxes.indexOf(box) == 1 && (box.x == 15) && (box.y == 65)){

               
               if(!box.selected){
                  while(true){
                  System.out.println("You win the game!");
                  System.out.println("Thanks for playing! :)");
                  System.exit(0);
                 
               }
               }
            }
           
            box.draw();
           
         }
     
         
         
         Display.update();
         Display.sync(60);
      }
     
      Display.destroy();
     
     
      }
   

   
}
   


Box.java
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  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
package example;

import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glVertex2f;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Box {
      Texture wood;
      int x,y;
      float cRed, cBlue, cGreen;
      public boolean selected = false;
     
      Box(int x, int y){
         this.x = x;
         this.y = y;
         
         Random ran = new Random();
         
         cRed = ran.nextFloat();
         cBlue = ran.nextFloat();
         cGreen = ran.nextFloat();
      }
     
      void randomize(){
         Random ran = new Random();
         cRed = ran.nextFloat();
         cBlue = ran.nextFloat();
         cGreen = ran.nextFloat();
      }
     
      void draw(){
         wood = loadTexture("wood");
         wood.bind();
         
         
         int a = x;
         int b = y;
         int c = x+50;
         int d = y + 50;
         
         glBegin(GL_QUADS);
            glTexCoord2f(0,0);
            glVertex2f(a, b);
            glTexCoord2f(1,0);
            glVertex2f(c, b);
            glTexCoord2f(1,1);
            glVertex2f(c, d);
            glTexCoord2f(0,1);
            glVertex2f(a, d);
         glEnd();
         
      }
     
      void update(int dx, int dy){
         x += dx;
         y += dy;
      }
     
      boolean isInBounds(int mouseX, int mouseY) {
            return mouseX > x && mouseX < x + 50 && mouseY > y && mouseY < y + 50;
        }
     
      public Texture loadTexture(String key){
         try {
            return TextureLoader.getTexture("PNG", this.getClass().getClassLoader().getResourceAsStream("res/" + key + ".png"));
         } catch (FileNotFoundException e) {
           
         } catch (IOException e) {
           
         }
         return null;
      }
   

}


Its probably because of the enormous loop going on in LWJGLInput. How can I reduce the lag?

Regards,
Ablaze.
Online SHC
« Reply #1 - Posted 2013-12-08 11:14:39 »

You are loading the texture every time you draw.

1  
2  
3  
void draw(){
-    wood = loadTexture("wood");
     wood.bind();

Move the highlighted line into the constructor.

Offline Ablaze

Junior Newbie





« Reply #2 - Posted 2013-12-08 11:53:14 »

You are loading the texture every time you draw.

1  
2  
3  
void draw(){
-    wood = loadTexture("wood");
     wood.bind();

Move the highlighted line into the constructor.

OMG, HOW COULD I NOT THINK OF THAT? I FEEL WEIRD - HAPPY FOR MY ISSUE BEING SOLVED AND SILLY FOR BEING SO *silly* (lol xD)
Here is a brief description of how I feel:-

IHDSFOSFHDSFHIODSFHIODSFHDFIOSHDSFIOSIDFOHFDSIOODSFIHIDOSHADSIOHIODSFAHIOSFADHDFIOSHDSFOIHADSIOHFDI OHDSFIOSFAHIODSFHIOFDSHIOFHDOFDSHIODSFHIOF

Thanks you so much!

Regards,
Ablaze

Edit: I request mods to close this.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EmanP

Junior Devvie


Projects: 2
Exp: 3 years


Satisfaction never guaranteed.


« Reply #3 - Posted 2013-12-08 15:38:19 »

I had the same problem a while back, but I would advise against closing this thread.. The first thing someone does when he/she has a problem in code, they often google it. If this thread were closed, then one cannot find this resource, and so would waste hours of their time if there is nothing else of the same problem!

Glad you fixed it though :-)

Signed- EmanP
Offline opiop65

JGO Kernel


Medals: 159
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #4 - Posted 2013-12-08 15:39:39 »

The mods don't actually close threads unless they are spam, but you can't reply to them if they are very old.

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.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
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!