Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  {lwjgl} "lag" problem  (Read 1087 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-03 01:21:52 »

EDIT:
i edit the title, just to make it look more like a problem than a discussion
Hello,
i create this topic to show you how i decompose my project classes and see if am doing it the "right" way or not .
but then it seems that i couldn't fix the flickering issue, even when i run the examples from lwjgl wiki it doesn't run smoothly it always have that kinda of lag

and there is my codes :
 
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  
/**
 * This class is the main class
 *
 */


import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Game {

   // game width and height
   private final int WIDTH = 800, HEIGHT = 600;

   // variables of different classes
   Board board = new Board();       // the game board
   Loop loop = new Loop();       // the loop class
   MyOpenGL ogl = new MyOpenGL();   // openGl initialization

   public void start() {
      try {
         Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
         Display.create();

      } catch (LWJGLException e) {

         e.printStackTrace();
      }
     
      //initialize  open Gl
      ogl.initGL();
     
      //initialize  the game board
      board.init();
      while (!Display.isCloseRequested()) {
         
         //calling Board main update method
         board.myBoardLoop();

         Display.update();
         Display.sync(60);
      }
      Display.destroy();
   }

   public static void main(String[] args) {

      Game game = new Game();
      game.start();
   }

}


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  
/**
 * This class will handle the game logic
 *
 */


import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class Board {
   // varibable of other classes
   private Drawing draw = new Drawing(); // drawing class
   private Loop loop = new Loop(); // the loop class

   private double heroX, heroY, heroW, heroH, heroSpeed;
   private Player hero;

   int delta;

   public void init() {
      // initialize the loop
      loop.init();

      initHero();
      hero = new Player(heroX, heroY, heroW, heroH);

   }

   // the main game logic "caller"
   public void myBoardLoop() {
      delta = loop.getDelta();
      painting();
      update(delta);
   }

   // the update method
   private void update(int delta) {

      heroUpdate();

      loop.updateFPS();

   }

   private void painting() {
      // clear the screen and depth buffer
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

      // using pre-build draw method
      draw.coloring("blue");
      draw.drawRect(hero.getX(), hero.getY(), hero.getWidth(),
            hero.getHeight());

   }

   // initializing the player
   private void initHero() {
      heroX = 400;
      heroY = 300;
      heroW = 30;
      heroH = 30;
      heroSpeed = 0.5;
   }

   // updateing the player
   private void heroUpdate() {
      control(delta);
      hero.setX(heroX);
      hero.setY(heroY);
      hero.setWidth(heroW);
      hero.setHeight(heroH);

   }

   // controlling the player
   private void control(int delta) {
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
         heroX -= heroSpeed * delta;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
         heroX += heroSpeed * delta;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
         heroY += heroSpeed * delta;

      }
      if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
         heroY -= heroSpeed * delta;
      }
   }

}


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  
/**
 * This class will handle the game Frame rate
 * it's taken from the lwjgl wiki, "TimingExample"
 */


import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;

public class Loop {

   
   public static long lastFrame;
   
   public static int fps;

   public static long lastFPS;

   public void init(){
      getDelta();

      lastFPS = getTime();
   }
   
   public int getDelta() {

      long time = getTime();
      int delta = (int) (time - lastFrame);
      lastFrame = time;

      return delta;
   }

   public long getTime() {
      return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }

   public void updateFPS() {
      if (getTime() - lastFPS > 1000) {
         Display.setTitle("FPS: " + fps);
         fps = 0;
         lastFPS += 1000;
      }
      fps++;
   }
}


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  
/**
 * This class will handle basic OpenGl painting
 *
 */


import java.util.Random;

import org.lwjgl.opengl.GL11;

public class Drawing {

   // fill a rectangle
   public void fillRect(double x, double y, double w, double h) {
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2d(x, y); // up left corner
      GL11.glVertex2d(x + w, y);// up right corner
      GL11.glVertex2d(x + w, y + h);// down right corner
      GL11.glVertex2d(x, y + h);// down left corner
      GL11.glEnd();
   }

   // draw a rectangle
   public void drawRect(double x, double y, double w, double h) {
      GL11.glBegin(GL11.GL_LINES);

      GL11.glVertex2d(x, y);// x,y
      GL11.glVertex2d(x + w, y);// w,y

      GL11.glVertex2d(x + w, y);// w,y
      GL11.glVertex2d(x + w, y + h);// w,h

      GL11.glVertex2d(x + w, y + h);// w,h
      GL11.glVertex2d(x, y + h);// x,h

      GL11.glVertex2d(x, y + h);// x,h
      GL11.glVertex2d(x, y);// x,y
      GL11.glEnd();
   }

   // draw a line
   public void drawLine(double x1, double y1, double x2, double y2) {
      GL11.glBegin(GL11.GL_LINES);
      GL11.glVertex2d(x1, y1);
      GL11.glVertex2d(x2, y2);
      GL11.glEnd();
   }

   // set a color
   public void coloring(String color) {
      if (color == "white") {
         GL11.glColor3f(1.0f, 1.0f, 1.0f);
      } else if (color == "black") {
         GL11.glColor3f(0.0f, 0.0f, 0.0f);
      } else if (color == "red") {
         GL11.glColor3f(1.0f, 0.0f, 0.0f);
      } else if (color == "green") {
         GL11.glColor3f(0.0f, 1.0f, 0.0f);
      } else if (color == "blue") {
         GL11.glColor3f(0.0f, 0.0f, 1.0f);
      } else if (color == "random") {
         Random randomColor = new Random();
         float r = randomColor.nextFloat();
         float g = randomColor.nextFloat();
         float b = randomColor.nextFloat();
         GL11.glColor3f(r, g, b);

      }
   }

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
/**
 * This class will handle basic OpenGl initialisation
 *
 */



import org.lwjgl.opengl.GL11;


public class MyOpenGL {

   // initializing openGl
   public void initGL() {
     
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, 800, 0, 600, 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);
      GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   }
   
}


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  
/**
 * this class will hanle the player properties
 */


public class Player {

   private double x, y, width, height;

   public Player(double x, double y, double width, double height) {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
   }

   // setting values
   public void setX(double x) {
      this.x = x;
   }

   public void setY(double y) {
      this.y = y;
   }

   public void setWidth(double width) {
      this.width = width;
   }

   public void setHeight(double height) {
      this.height = height;
   }

   // getting values
   public double getX() {
      return this.x;
   }

   public double getY() {
      return this.y;
   }

   public double getWidth() {
      return this.width;
   }

   public double getHeight() {
      return this.height;
   }

}

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-05-03 01:38:08 »

What do you mean by 'lagging'?

There are a few problems with the code (unused variables etc.) but I'll leave you to find those.

Offline alaslipknot
« Reply #2 - Posted 2013-05-03 02:08:55 »

What do you mean by 'lagging'?

when i move the player, it doesn't really lag, but, it did that "effect", it's like i saw 2 squares or something like that and it hurt my eyes  Tongue


"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-05-03 02:43:35 »

Something to do with your movement code?

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #4 - Posted 2013-05-03 02:48:36 »

when i move the player, it doesn't really lag, but, it did that "effect", it's like i saw 2 squares or something like that and it hurt my eyes  Tongue
It's flickering?

Offline HeroesGraveDev

JGO Kernel


Medals: 310
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-05-03 03:06:48 »

1  
Display.setVSyncEnabled(true);


Maybe?

Offline alaslipknot
« Reply #6 - Posted 2013-05-03 12:33:43 »

Quote
It's flickering?
yes, that's the word

Quote
1  
Display.setVSyncEnabled(true);

Maybe?
nope, it didn't solve it
here is the .jar file maybe you can test it and tell me if you get the same problem 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline NegativeZero

JGO Knight


Medals: 41
Projects: 2


Zero but not.


« Reply #7 - Posted 2013-05-04 09:35:19 »

It's working fine for me
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #8 - Posted 2013-05-04 12:24:25 »

Take note your jar isn't working on 64-bit platform. Error:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\Iulian\AppDa
ta\Local\Temp\natives-656819368\lwjgl.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary1(Unknown Source)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at org.lwjgl.Sys$1.run(Sys.java:73)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
        at org.lwjgl.Sys.loadLibrary(Sys.java:95)
        at org.lwjgl.Sys.<clinit>(Sys.java:112)
        at Loop.getTime(Loop.java:34)
        at Loop.getDelta(Loop.java:26)
        at Board.<init>(Board.java:14)
        at Game.<init>(Game.java:16)
        at Game.main(Game.java:47)

Getting a project done is by far the most hard thing in game development.
Offline alaslipknot
« Reply #9 - Posted 2013-05-04 13:38:00 »

@NegativeZero
okay ...
@marcuiulian13
yes that's because i didn't add the native 64bit files

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online trollwarrior1
« Reply #10 - Posted 2013-05-30 17:32:33 »

It's working fine for me

I have this same flickering problem too. I believe it was you who said it was fine for you. It's either your pc is special or you should get your eyes tested. This problem is getting annoying. I don't see any problem in my code yet I still get this lag on my machine and on other machines.
Offline Phibedy

Senior Devvie


Medals: 9



« Reply #11 - Posted 2013-05-30 17:42:47 »

it's working fine for me too. You might download: http://mineforce.de/dhany.php
if it works fine, I will look up the differences  Smiley
(sry don't have any uptodate projects except that)
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.

rwatson462 (35 views)
2014-12-15 09:26:44

Mr.CodeIt (26 views)
2014-12-14 19:50:38

BurntPizza (55 views)
2014-12-09 22:41:13

BurntPizza (88 views)
2014-12-08 04:46:31

JscottyBieshaar (50 views)
2014-12-05 12:39:02

SHC (65 views)
2014-12-03 16:27:13

CopyableCougar4 (68 views)
2014-11-29 21:32:03

toopeicgaming1999 (128 views)
2014-11-26 15:22:04

toopeicgaming1999 (119 views)
2014-11-26 15:20:36

toopeicgaming1999 (34 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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