FPS is lower than it should be/Shuttering

Started by trollwarrior1, May 30, 2013, 16:42:04

Previous topic - Next topic

trollwarrior1

I don't know how my is problem called.

My game runs normally and smoothly. Sometimes my game screen starts to look very low FPS. My game still renders 60 FPS but it doesn't seem that way on the screen. The problem lasts for 5-10 seconds and goes away. After half a minute it comes again and disappears. It keeps doing this shit forever.

I use immediate mode to render everything. glStart, quads, color, texture, vertices, glEnd. This is for rendering my 2d sprites.

I have tried 2 different game loops: in the first one, I calculate the time passed since last update and render and if it is bigger than delta value which is 1/fps I update my game again. Second loop is just Display.sync(60); and update, render. Both of these loops encounter the same problem so I'm guessing problem is not there.

My render method consists of:
glClear(color_bits); render geometry using immediate mode, Display.update();

I have experimented adding glPush/Pop matrices with no luck. I also tried to add glFlush at the end of every frame. None of these seemed to fix the problem. My game seems to run at normal speed except with low frame rate, although my game loop still calculates that it rendered 60 FPS.

The problem seems to go away once I stop limiting my FPS. But then my updates/second go down by a few which make the game loop a bit slow. My FPS problem doesn't make the game look slow, rather low FPS.

From what I can see this is the problem if Display or my rendering mode. Anybody experienced something like this?

EDIT
-----------------------------------------------------------------------------------------------------------------------------------
Ok I guess my problem isn't clear enough. I will try to make myself clear. Here is my simple "game". Move using ASDW. Move for a little and see the square getting thorn apart or flashing don't know how to describe it.

http://www.mediafire.com/?4msmu4q48np9m9m

This is all the code:

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;
public class LWJGL {

	public LWJGL(){
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
			
			glMatrixMode(GL_PROJECTION);
			
			glLoadIdentity();
			glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
			
			glMatrixMode(GL_MODELVIEW);
			
			glLoadIdentity();
			
			loop();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private float xpos, ypos;
	
	private int dir = 1;
	
	public void loop(){
		
		
		while(!Display.isCloseRequested()){
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			
			
			if(Keyboard.isKeyDown(Keyboard.KEY_A)) xpos-=3;
			if(Keyboard.isKeyDown(Keyboard.KEY_D)) xpos+=3;
			if(Keyboard.isKeyDown(Keyboard.KEY_S)) ypos+=3;
			if(Keyboard.isKeyDown(Keyboard.KEY_W)) ypos-=3;
			
			
			drawCube(xpos, ypos);
			
			Display.update();
			Display.sync(60);
		}
	}
	
	public void drawCube(float xpos, float ypos){
		
		glBegin(GL_QUADS);
		
		glColor3f(1, 0, 0);
		
		glVertex2f(xpos, ypos);
		glVertex2f(xpos + 50, ypos);
		glVertex2f(xpos + 50, ypos + 50);
		glVertex2f(xpos, ypos + 50);
		
		glEnd();
	}
	
	public static void main(String args[]){
		new LWJGL();
	}
}

trollwarrior1

Seriously? Nobody here has ever encountered lags?

I have made yet another program yet this time using VBO as my rendering method, because I though it was my rendering mode.

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

import static org.lwjgl.opengl.GL11.*;

public class Main {

	public Main(){
		
		try{
			
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
			
			
			glMatrixMode(GL_PROJECTION);
			glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
			
			glMatrixMode(GL_MODELVIEW);
			
			Render.init();
			
			loop();
			
		}catch(Exception e){
			System.out.println("Exiting");
		}
		
	}
	
	int x = 0;
	int y = 0;
	
	public void loop(){
		while(!Display.isCloseRequested()){
			
			boolean up = Keyboard.isKeyDown(Keyboard.KEY_W);
			boolean down = Keyboard.isKeyDown(Keyboard.KEY_S);
			boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
			boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);
			
			
			int amt = 2;
			
			if(up) y-=amt;
			if(down) y+=amt;
			if(left) x-=amt;
			if(right) x+=amt;
			
			
			
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();
		
			Render.render(x, y);
			
			Display.update();
			
			
			Display.sync(60);
		}
		Display.destroy();
	}
	
	
	
	
	public static void main(String args[]){
		new Main();
	}
	
}


import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL11.*;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL15;


public class Render {

	
	
	private static int vertexHandle, colorHandle;
	
	public static void init(){
		
		vertexHandle = glGenBuffers();
		colorHandle = glGenBuffers();
		
		int size = 100;
		
		int[] vertexData = {0, 0,
							size, 0,
							size, size,
							0, size};
		IntBuffer vertexBuffer = BufferUtils.createIntBuffer(8);
		vertexBuffer.put(vertexData);
		vertexBuffer.flip();
		
		
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexHandle);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
		
		float c = 0.5f;
		float[] colorData = {c, c, c,
							 c, c ,c,
							 c, c, c,
							 c, c, c};
		
		FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(12);
		colorBuffer.put(colorData);
		colorBuffer.flip();
		
		glBindBuffer(GL_ARRAY_BUFFER, colorHandle);
		glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
		
	}	
	
	
	public static void render(int xpos, int ypos){
		
		glTranslatef(xpos, ypos, 0);
		
		glEnableClientState(GL_VERTEX_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER, vertexHandle);
		glVertexPointer(2, GL_INT, 0, 0);
		
		glEnableClientState(GL_COLOR_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER, colorHandle);
		glColorPointer(3, GL_FLOAT, 0, 0);
		
		
		glDrawArrays(GL_QUADS, 0, 8);
		
		
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_COLOR_ARRAY);
		
		
	}
	
}


Am I to blame myself for this or am I to blame my hardware?

Fool Running

My best guess is that you aren't doing enough in your loop which is causing your CPU to switch into a low-power mode or something that lowers the clock-rate. That might be causing the timer used in LWJGL to not update the 1000 times per second (although I thought the timer used by LWJGL didn't have this problem). You can test this by going back to your loop that calculates the time since the last frame and see if it is ever zero (i.e. even though another frame was drawn no time on the timer passed).
I've heard of similar things happening before, but I'm not sure what to do about it (maybe put in a low-priority infinite loop on another thread that keeps the CPU busy?).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

trollwarrior1

Quote from: Fool Running on June 10, 2013, 13:41:01
My best guess is that you aren't doing enough in your loop which is causing your CPU to switch into a low-power mode or something that lowers the clock-rate. That might be causing the timer used in LWJGL to not update the 1000 times per second (although I thought the timer used by LWJGL didn't have this problem). You can test this by going back to your loop that calculates the time since the last frame and see if it is ever zero (i.e. even though another frame was drawn no time on the timer passed).
I've heard of similar things happening before, but I'm not sure what to do about it (maybe put in a low-priority infinite loop on another thread that keeps the CPU busy?).

I don't think you understand. This is shit you're talking. I'm doing everything at 10/10 mark. LWJGL can't handle my monitor refresh rate properly. Once I use full screen displaymode no lags there. I have tried playing my game on other machines and they have lag on full screen mode.