[SOLVED] Texture is flickering, help?

My texture is flickering, it keeps showing up normal and after 5 seconds it starts to flicker again and then it shows up normal again. Is it my game loop or what is it?

Source code:


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

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Main {

	public boolean finished = false;
	Texture texture;
	float x = 50.0f;
	float y = 50.0f;

	public Main() {

	}

	private void init(boolean fullscreen) {
		try {

			Display.setTitle("Test LWJGL");
			Display.setFullscreen(fullscreen);
			Display.setDisplayMode(new DisplayMode(480, 600));
			Display.setVSyncEnabled(true);
			Display.create();

			texture = getTexture("texture");

			GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
			GL11.glMatrixMode(GL11.GL_MODELVIEW);

			GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glLoadIdentity();
			GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glEnable(GL11.GL_TEXTURE_2D);
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}

	private Texture getTexture(String key) {
		try {
			return TextureLoader.getTexture("PNG", new FileInputStream(
					new File("res/img/" + key + ".png")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void start() {
		init(false);
		run();
	}

	public void run() {
		while (!finished) {
			Display.update();
			if (Display.isCloseRequested()) {
				finished = true;
			} else {
				tick();
				render();
			}
		}
	}

	private void render() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

		draw();

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

	private void draw() {
		texture.bind();

		GL11.glBegin(GL11.GL_QUADS);
		GL11.glTexCoord2f(0, 0);
		GL11.glVertex2f(x, y);
		GL11.glTexCoord2f(1, 0);
		GL11.glVertex2f(x + texture.getTextureWidth(), y);
		GL11.glTexCoord2f(1, 1);
		GL11.glVertex2f(x + texture.getTextureWidth(),
				y + texture.getTextureHeight());
		GL11.glTexCoord2f(0, 1);
		GL11.glVertex2f(x, y + texture.getTextureHeight());
		GL11.glEnd();
	}

	private void tick() {

	}

	public static void main(String args[]) {
		Main main = new Main();
		main.start();
	}

}

For one thing, you should use tris and not quads, graphics cards prefer those. Also, if I remember correctly, you only bind a texture once, though I could be wrong, but I think that’s your problem.

This is the only texture I use. And Triangles just split it up. Or did you mean something else with tri?

pxiv6mqfrqs

Triangle strip, basically.

I think I just realized what could be wrong, it’s not the texture flickering but the entire scene. It’s been so long since I coded raw GL that I forgot how to fix this, but I do remember having that same problem. There’s a wait method for something that will fix it.

Read the tutorials and wiki. There are a few things wrong:

  • You call Display.update at the beginning of your loop, and then again during render(). You should only call this once at the end of your loop (before sync), and probably not inside your render() method. This is likely the cause of the flickering.
  • You aren’t using proper texcoords. See here.
  • You are loading from a file, which won’t work if you try packaging your resources into a JAR. You should be using MyGame.class.getResource().

Regarding triangles; true that the GPU will prefer triangles (since that’s what it will be in the end), but that’s the least of your worries. Right now you’re using old-school immediate mode and fixed function; ideally you should be learning more modern OpenGL through shaders and VBOs. Understandably though, the “hello world” quad is a good starting point for newbies.

If you find these topics difficult to grasp, then you would be better off using a library like LibGDX which will cover the lower-level stuff for you.

Thank you! It was the Display.update() which made this fault. I just started with OpenGL, but I have alot of experience in making 2D and 3D games with the built-in graphics class. Thanks for this file tip as well, I just made this messy piece of code in about 15 mins. And I know, it’s messy.