Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  rendering speeds  (Read 1581 times)
0 Members and 1 Guest are viewing this topic.
Offline javatypo

Junior Member





« Posted 2003-04-14 20:41:09 »

i made an lwjgl app, which isnt complete yet.. but i made like a paddle and a "ball", the ball moves around the screen and the paddle is controled with arrows. i have not yet added collision detection..

but i was wondering how to speed up the rendering process?

The ball sort of jerks around.. and i am a newbie at this so any help would be cool..

heres my code if u want to c what im doing.

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  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.input.*;

public class Test {
     
      static GL gl;
      static GLU glu;
     
      // for paddle      
     static float xPos;
     
      // for bouncing thing
     static int x,xspeed = 2;
      static int y,yspeed = 2;
      static int width;
      static int height;
     
      public static void main(String argv[]) throws Exception {
            // Choose a Display Mode
           DisplayMode[] modes = Display.getAvailableDisplayModes();
            for (int i=0; i<modes.length; i++) {
                  if (modes[i].width == 800 &&
                        modes[i].height == 600 &&
                        modes[i].bpp == 32) {
                             
                        Display.create(modes[i],true);
                        break;
                  }
            }
           
            xPos = Display.getWidth()/2;
           
            x = (Display.getWidth()-5)/2;
            y = (Display.getHeight()-5)/2;
            width = 10;
            height = 10;
           
            init();
            render();
      }
     
      public static void init() throws Exception {
            // init keyboard
           Keyboard.create();
           
            // init gl and glu
           gl = new GL();
            glu = new GLU(gl);
            gl.create();
           
            // set timers
           Sys.setTime(0);
            Sys.setProcessPriority(Sys.HIGH_PRIORITY);
           
            // init matrices
           gl.matrixMode(GL.PROJECTION);
            gl.loadIdentity();
            glu.ortho2D(0, Display.getWidth(), 0, Display.getHeight());
            gl.matrixMode(GL.MODELVIEW);
            gl.loadIdentity();
            gl.viewport(0,0, Display.getWidth(),Display.getHeight());
      }
     
      public static void render() {
            gl.clearColor(0,0,0,0);
            for (;;) {
                  gl.clear(GL.COLOR_BUFFER_BIT);
                  gl.pushMatrix();
                  gl.translatef(xPos, 10.0f, 0.0f);
                  gl.rotatef(5.0f,0.0f,1.0f,0.0f);
                  gl.begin(GL.QUADS);
                  {
                        gl.color3f(1.0f,0.0f,0.0f);
                        gl.vertex2i(-50,5);
                        gl.vertex2i(-50,30);
                        gl.color3f(0.0f,0.0f,1.0f);
                        gl.vertex2i(50,30);
                        gl.vertex2i(50,5);
                  }
                  gl.end();
                  gl.popMatrix();
                 
                  gl.pushMatrix();
                  gl.translatef(0, 0, 0);
                  gl.begin(GL.QUADS);
                  {
                        gl.color3f(0,149,219);
                        gl.vertex2i(x,y);
                        gl.vertex2i(x,(y+10));
                        gl.color3f(219,149,0);
                        gl.vertex2i((x+10),(y+10));
                        gl.vertex2i((x+10),y);
                  }
                  gl.end();
                  gl.popMatrix();
                       
                  gl.swapBuffers();
                 
                  updateBall();
                  checkKeys();
            }      
      }
     
      public static void updateBall() {
            if (((x+xspeed) > Display.getWidth())) xspeed *= -1;
            if (((x+xspeed) < 0)) xspeed *= -1;
            if (((y+yspeed) > Display.getHeight())) yspeed *= -1;
            if (((y+yspeed) < 0)) yspeed *= -1;
           
            x += xspeed;
            y += yspeed;
      }
           
     
      public static void checkKeys() {
            Keyboard.poll();
           
            if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
                  Keyboard.destroy();
                  gl.destroy();
                  Display.destroy();
                  System.exit(0);
            }
           
            if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
                  if ((xPos-50.0f) - 5.0f < 0) return;
                  xPos-=5.0f;
            }
           
            if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
                  if ((xPos+50.0f) + 5.0f > Display.getWidth()) return;
                  xPos+=5.0f;
            }
      }
}


Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #1 - Posted 2003-04-16 19:57:04 »

Your bat is probably not going to change its geometry over the course of a game, so you could place it into a display list.  If you change the ball drawing routine to translate to (x, y) first that could be in a display list too.

Aside from that, there's nothing much more to optimize.  At least, nothing that would have much effect.  Run the game with -Xprof on the command line and pipe the output to a file.  Where is the code spending most of it's time?


A couple of comments:

You've probably fixed these already, but you're got a do-nothing translate in there, and a couple of color3f calls that need their arguments fixed.

You're using Display.getWidth/getHeight() several times per frame.  I don't know if the JVM can tell those settings aren't going to change and optimise them, but you can just copy them out into private static final variables to help it along.  Probably won't affect the speed, but hey.

Your checkKeys() methods needs a few changes.  At the moment, when one of the sanity checks fail it returns - you really want to just not execute the update instead.

Hellomynameis Charlie Dobbie.
Offline vrm

Junior Member




where I should sign ?


« Reply #2 - Posted 2003-04-19 11:24:46 »

I think you hit the 200 FPS and bnurn oall the CPU time, the jerk is prolly due to the garbage collector.

try to limit 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  
      /** How many frames per second we want */
      public static final int DESIRED_FPS = 60;
       /** The duration of one frame, in seconds */
      public static final float FRAME_TIME = 1.0f / (float) DESIRED_FPS;


// in the loop :
   public boolean loop(float frameTime) {  
        /* if drawGLScene(...) fails, stop the loop */
            long then = Sys.getTime();
             

     call your render method here
           
           
        // in place of swaping the buffer by : _gl.swapBuffers();            
           if (!GL.WGL_EXT_swap_control) {
                  float elapsed = 0.0f;
                  frameTime = 0.0f;
                  do {
                        long now = Sys.getTime();
                        elapsed = (float)(now - then) / (float)Sys.getTimerResolution();
                        if (frameTime == 0.0f)
                              frameTime = elapsed;
                        if (elapsed < FRAME_TIME)
                        {
                              try{
                                    Thread.sleep(1);
                              }catch(InterruptedException ie)
                              {
                                    ie.printStackTrace();
                              }
                        }
                  }
                  while (elapsed < FRAME_TIME);
                _gl.swapBuffers();
          } else {
                 long now = Sys.getTime();
                 frameTime = (float)(now - then) / (float)Sys.getTimerResolution();
                 _gl.swapBuffers();
          }

        input(frameTime);
        return _done;

    }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #3 - Posted 2003-04-19 14:39:58 »

limiting the fps would do the trick... well... it would look that way but the problem itself wont disapear this way.

the problem here is that u r using tick based animation (move x units to the left each frame) and that doesnt really work :]

take a look at the ported gametuturials.com tuturial i fixed recently u can find it (and tons of other nice tuts) at chman's page:

http://chman-area.tuxfamily.org/

http://chman-area.tuxfamily.org/gt/GT10.ZIP

hf Smiley

edit:

mh... it still looks like the old one ._o

弾幕 ☆ @mahonnaiseblog
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #4 - Posted 2003-04-19 16:35:32 »

ah well... here u go Wink

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  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.input.*;

public class Test {

 static GL gl;
 static GLU glu;

 // for paddle
static float xPos, paddleSpeed;

 // for bouncing thing
static float x,xspeed = 2;
 static float y,yspeed = 2;
 static int width;
 static int height;

 static float delta;
 static float ballUPS=150.0f;
 static float paddleUPS=350.0f;

 static long currentTime,frameTime;
 static float g_FrameInterval;

 public static void main(String argv[]) throws Exception {
  // Choose a Display Mode
 DisplayMode[] modes = Display.getAvailableDisplayModes();
  for (int i=0; i<modes.length; i++) {
   if (modes[i].width == 800 &&
    modes[i].height == 600 &&
    modes[i].bpp == 32) {

    Display.create(modes[i],true,"blub");
    break;
   }
  }

  xPos = Display.getWidth()/2;

  x = (Display.getWidth()-5)/2;
  y = (Display.getHeight()-5)/2;
  width = 10;
  height = 10;

  init();
  render();
 }

 public static void init() throws Exception {
  // init keyboard
 Keyboard.create();

  // init gl and glu
 gl = new GL();
  glu = new GLU(gl);
  gl.create();

  // set timers
 Sys.setTime(0);
  Sys.setProcessPriority(Sys.HIGH_PRIORITY);

  // init matrices
 gl.matrixMode(GL.PROJECTION);
  gl.loadIdentity();
  glu.ortho2D(0, Display.getWidth(), 0, Display.getHeight());
  gl.matrixMode(GL.MODELVIEW);
  gl.loadIdentity();
  gl.viewport(0,0, Display.getWidth(),Display.getHeight());

  gl.wglSwapIntervalEXT(0);
 }

 public static void render() {
  gl.clearColor(0,0,0,0);
  for (;;) {
   currentTime = Sys.getTime();
   g_FrameInterval = ((float)(currentTime - frameTime))/Sys.getTimerResolution();
   frameTime = currentTime;

   delta = ballUPS*g_FrameInterval;
   paddleSpeed = paddleUPS * g_FrameInterval;

   gl.clear(GL.COLOR_BUFFER_BIT);
   gl.pushMatrix();
   gl.translatef(xPos, 10.0f, 0.0f);
   gl.rotatef(5.0f,0.0f,1.0f,0.0f);
   gl.begin(GL.QUADS);
   {
    gl.color3f(1.0f,0.0f,0.0f);
    gl.vertex2i(-50,5);
    gl.vertex2i(-50,30);
    gl.color3f(0.0f,0.0f,1.0f);
    gl.vertex2i(50,30);
    gl.vertex2i(50,5);
   }
   gl.end();
   gl.popMatrix();

   gl.pushMatrix();
   gl.translatef(0, 0, 0);
   gl.begin(GL.QUADS);
   {
    gl.color3f(0,149,219);
    gl.vertex2f(x,y);
    gl.vertex2f(x,(y+10));
    gl.color3f(219,149,0);
    gl.vertex2f((x+10),(y+10));
    gl.vertex2f((x+10),y);
   }
   gl.end();
   gl.popMatrix();

   gl.swapBuffers();

   updateBall();
   checkKeys();
  }
 }

 public static void updateBall() {
  if (((x+xspeed) > Display.getWidth())) xspeed *= -1.0f;
  if (((x+xspeed) < 0)) xspeed *= -1.0f;
  if (((y+yspeed) > Display.getHeight())) yspeed *= -1.0f;
  if (((y+yspeed) < 0)) yspeed *= -1.0f;

  x += xspeed * delta;
  y += yspeed * delta;
 }


 public static void checkKeys() {
  Keyboard.poll();

  if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
   Keyboard.destroy();
   gl.destroy();
   Display.destroy();
   System.exit(0);
  }

  if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
   if ((xPos-50.0f) - paddleSpeed < 0) return;
   xPos-=paddleSpeed;
  }

  if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
   if ((xPos+50.0f) + paddleSpeed > Display.getWidth()) return;
   xPos+=paddleSpeed;
  }
 }
}


-> Display.create(modes,true,"blub");

lwjgl 0.5... remove the "blub" part if u still use 0.4

-> gl.wglSwapIntervalEXT(0);

remove that line if your card doesnt support this extension or use 1 as argument (to enable vsync).

what i did isnt that complicated. instead of movin the stuff x units in each frame i check how many time elapsed since the last frame and multiply the time (in seconds) with the ups(units per second) value to get the distance wich it should have traveled Wink

弾幕 ☆ @mahonnaiseblog
Offline Captain-Goatse

Junior Member




I suck at teh 2D. XBOX IS BIG LOL!111


« Reply #5 - Posted 2003-04-19 21:02:36 »

In my terrain renderer, I found the problem was that I was running in a such a tight loop that the GC didn't have time to do its work. What helped me, was that I caught one of the system threads and made it yield. Actually, you can do it with any thread. I'm pretty sure it is sort of dangerous to catch the system thread and force it to yield. Anyway, it is a possibility if you are absolutely afraid of creating a thread,
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.

Dwinin (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!