Hiya everyone! I am new to using LWJGL and OpenGL, and I am having a problem with my time-based game loop.
When I run the program, the graphics just seem to stutter occasionally, and I have a bad feeling that it is some oversight in my code causing the problem.
If anyone could be nice enough to look over these portions of my code and tell me what my problem is, I would greatly appreciate it!
I will explain the system so maybe from my explanation you could find a flaw and not even have to look in the code:
I have two base classes, GameWindow and GameControl. GameWindow contains the main() method. Each class has a reference to the active instance of the other class.
GameWindow begins by intializing Display, setting the title, display modes and everything and initializes OpenGL. In then sets the boolean
programRunning to true and begins the game loop, contained in GameControl.
GameControl's game loop works like this:
1. set variable
beforeLoop to System.nanoTime()
2. Yield the thread
3. Poll the keyboard for input
4. run method
programLifeTime, passing the value of variable
delta, which is a method that keeps track of how long the program has been running.
5. run method
renderGame, which limits the frame rate to a preset value, according to a calculated time period based off of the preset value. For example, if the Desired FPS is 60, the period of time to wait before rendering again would be 16666666 ns. It only renders a maximum of once per loop, then resets the time counter (code example below).
6. run method
updateGame, which then passes the
delta value to the requisite areas for game updating.
7. Update the Display
8. calculate the
delta value according to System.nanotime() -
beforeLoopOpenGL Initialization1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| private void initGL() { GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glShadeModel(GL11.GL_SMOOTH); GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL11.glClearDepth(1.0); GL11.glDisable(GL11.GL_DEPTH_TEST); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); GL11.glOrtho(0, windowWidth, windowHeight, 0, -1, 1); } |
Game Loop1 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
| public void gameLoop() { beforeLoop = System.nanoTime(); Thread.yield(); Keyboard.poll(); processKeyboard(); programLifeTime(delta); renderGame(delta); updateGame(delta); Display.update(); delta = System.nanoTime() - beforeLoop; }
public void updateGame(long delta) { Display.setTitle(gameWindow.getWindowTitle() + " | Elapsed Time- " + lifeSpanMinutes + "m:" + lifeSpanSeconds + "s " + "FPS: " + currentFPS); for(int x = 0; x < 10; x++) { rect[x].updateEntity(delta); } } public void renderGame(long delta) { FPS_TIME_ELAPSED = FPS_TIME_ELAPSED + delta; fpsCounterTimer = fpsCounterTimer + delta; if(FPS_TIME_ELAPSED >= FPS_PERIOD) { FPS_TIME_ELAPSED = 0; try { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); } catch(Exception e) { e.printStackTrace(); System.exit(0); } render();
fpsCounter++; if(fpsCounterTimer > 1000000000) { currentFPS = fpsCounter; fpsCounter = 0; fpsCounterTimer = 0; } } } |
And here is my rendering code for rendering a quad (the code stutters even with just a single moving quad on the screen):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public void drawSprite(float x, float y) { GL11.glColor3f(1.0f, 0f, 0f); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2f(x - (width/2), y - (height/2)); GL11.glVertex2f(x - (width/2), y + (height/2)); GL11.glVertex2f(x + (width/2), y + (height/2)); GL11.glVertex2f(x + (width/2), y - (height/2)); GL11.glEnd(); } |
I know this is alot of code and a huge post to look over and maybe not even enough to find my problem, but if anyone could, I would be so happy!
I cannot figure out why my code stutters!
Thank you so much in advance!