Unable to Update the Window

Started by irongiant, June 17, 2008, 14:30:12

Previous topic - Next topic

irongiant

Hi,

The following class creates a LWJGL window. Display.update() is called regularly but it doesn't update the window.

public class GLPreview implements Runnable {

    private static final HashMap<Integer, Bool> KEYS;
    private Thread thread;
    private Camera camera;
    private long storedDelta;
    private long lastFrame;
    private long minimumLogicInterval;
    private long maximumLogicInterval;
    private int gLWidth;
    private int gLHeight;
    private int gridList;
    private boolean running;
    float a = 45.0f, b = -3.0f;

    static {
        KEYS = new HashMap<Integer, Bool>();
        GLPreview.addKey(Keyboard.KEY_W);
        GLPreview.addKey(Keyboard.KEY_D);
        GLPreview.addKey(Keyboard.KEY_S);
        GLPreview.addKey(Keyboard.KEY_A);
    }

    public GLPreview() {
        this.thread = new Thread(this, "GLPreview");
        this.thread.setDaemon(true);
        this.camera = new Camera();
        this.storedDelta = 0L;
        this.lastFrame = 0L;
        this.minimumLogicInterval = 24L;
        this.maximumLogicInterval = 32L;
        this.running = false;
    }

    public void run() {
        try {
            initGL();
            getDelta();

            while (running) {
                updateAndRender(getDelta());
            }

            Display.destroy();
        } catch (LWJGLException ex) {
            ex.printStackTrace();
        }
    }

    public Camera getCamera() {
        return camera;
    }

    public int getGLHeight() {
        return gLHeight;
    }

    public int getGLWidth() {
        return gLWidth;
    }

    /**
     * Starts the thread and initializes LWJGL, eventually.
     */
    public void create() {
        running = true;
        thread.start();
    }

    public void destroy() {
        running = false;
    }

    public static void addKey(int key) {
        KEYS.put(key, new Bool());
    }

    /**
     * Initializes LWJGL related stuff.
     * @throws org.lwjgl.LWJGLException
     */
    private void initGL() throws LWJGLException {
        Display.setVSyncEnabled(true);
        Display.setDisplayMode(new org.lwjgl.opengl.DisplayMode(640, 480));
        Display.create();
        //Display.sync(24);

        gLWidth = Display.getDisplayMode().getWidth();
        gLHeight = Display.getDisplayMode().getHeight();

        GL11.glViewport(0, 0, gLWidth, gLHeight);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glClearDepth(1.0f);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GLU.gluPerspective(45.0f, (float) gLWidth / (float) gLHeight, 0.1f, 1000.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        gridList = new Grid(10.0f).createList();
    }

    /**
     * Updates the context and renders.
     * @param delta
     * @throws org.lwjgl.LWJGLException
     */
    private void updateAndRender(long delta) throws LWJGLException {
        storedDelta += delta;
        pollInput();

        if (storedDelta >= minimumLogicInterval) {

            if (maximumLogicInterval != 0L) {
                long cycles = storedDelta / maximumLogicInterval;

                for (long index = 0L; index < cycles; index++) {
                    update(maximumLogicInterval);
                }

                update(delta % maximumLogicInterval);
            } else {
                update(storedDelta);
            }
            storedDelta = 0L;

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            //GL11.glMatrixMode(GL11.GL_MODELVIEW_MATRIX);
            GL11.glLoadIdentity();
            render();
            Display.update();
        }
    }

    /**
     * Updates the context.
     * @param delta
     */
    private void update(long delta) {
        if (KEYS.get(Keyboard.KEY_W).value) {
            a += 2.0f;
            System.out.println(a);
        }
        if (KEYS.get(Keyboard.KEY_S).value) {
            b += 0.2f;
            System.out.println(b);
        }
    }

    /**
     * Renders to the window.
     */
    private void render() {
        GL11.glTranslatef(0.0f, b, 0.0f);
        GL11.glRotatef(a, 1.0f, 0.0f, 0.0f);
        
        // Draw something here...
    }

    /**
     * Updates the keys.
     */
    private void pollInput() {
        Keyboard.poll();
        int key;

        while (Keyboard.next()) {
            key = Keyboard.getEventKey();

            if (KEYS.containsKey(key)) {
                KEYS.get(key).value = Keyboard.getEventKeyState();
            }
        }
    }

    /**
     * Returns the elapsed time since the last call.
     * @return
     */
    private long getDelta() {
        long time = Sys.getTime();
        long delta = time - lastFrame;
        lastFrame = time;

        return delta;
    }
    
    public static class Bool {

        public boolean value = false;
    }
}


updateAndRender() does the actual call to update the window.

I'd appreciate if someone could test the code and tell me what's wrong (call create() to create the window).
Pressing W or S rotates the view around the x-axis and translates along the y-axis, respectively.

Thanks.

Fool Running

QuoteDisplay.update() is called regularly but it doesn't update the window.
What exactly do you mean that it doesn't update? Do you see your clear color (0.3f, 0.3f, 0.3f, 1.0f)? If you drag a window over top of the display, does it repaint?
If you see your clear color, or it repaints, then the Display.update() is working. This probably means that what ever you are drawing is off the screen or too close to see (less then 0.1f depth for the code there).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

irongiant

Fool Running, your post made me look at some part of my code (not included in my previous) and I think I found the problem.
I was drawing a grid using glBegin(GL_LINES) but I wasn't making any call to glEnd().

Pretty stupid fault of mine... Anyway, thank you for your time!  ;)