Using the SWT binding

Started by Matzon, April 05, 2004, 22:41:30

Previous topic - Next topic

Matzon

Hmm, I am having some weird issues. I grabbed the swt binding from fbi. Added org.lwjgl.opengl.GLCanvas to eclipse. Added org_lwjgl_opengl_GLCanvas.cpp to visual studio. Grabbed newest SWT dll & jar - compile. Success!

Then I made a test app. Failure!

The canvas is getting cleared, so something is working. I just copied from Game.java to my SWTTest. Granted I am a bit tired, but I really can't see whats wrong - anyone?

/*
 * Created on Apr 5, 2004
 * 
 * To change the template for this generated file go to Window - Preferences -
 * Java - Code Generation - Code and Comments
 */
package org.lwjgl.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.lwjgl.Sys;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLCanvas;
import org.lwjgl.opengl.GLContext;

/**
 * @author Brian
 * 
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class SWTTest {
	/** Shell instance */
	private Shell shell;
  
	/** Canvas instance */
	private GLCanvas canvas;
  
	/** Whether the test is closing */
	public boolean closing = false;

  /** A rotating square! */
  private static float  angle;  
  
	/**
	 * Initializes OpenGL
	 *  
	 */
	private void initializeOpenGL() {
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0.0, (double) 640, 0.0, (double) 480, -1.0, 1.0);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glViewport(0, 0, 640, 480);    
	}
	/**
	 * Executes the actual test
	 */
	public void executeTest() {
		initialize();
		runTest();
		destroy();
	}
  
	/**
	 * 
	 */
	private void destroy() {
    shell.dispose();
	}
  
	/**
	 *  
	 */
	private void initialize() {
		
    shell = new Shell();
		shell.setSize(640, 480);
		
    canvas = new GLCanvas(shell, 0, 0, 0);
		canvas.setSize(640, 480);
    
    // install listener
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				closing = true;
			}
		});
		
    shell.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
			}
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == SWT.ESC) {
					closing = true;
				}
			}
		});
	}
  
	/**
	 * Runs the test
	 */
	private void runTest() {
    shell.open();
    Display display = shell.getDisplay();
    canvas.setCurrent();
    GLContext.useContext(canvas);
    
    while (!closing) {
      display.readAndDispatch();
      
      if(shell.isDisposed()) {
       return; 
      }

      logic();
      render();
      canvas.paint();
      
      try {
      	Thread.sleep(33); // 30 fps
      } catch (InterruptedException ie) {
      }
    } 
	}  

	/**
	 * Does the "model logic"
	 */
	private void logic() {
    angle += 1f;
    if (angle > 360.0f) { 
      angle = 0.0f;    
    }
	}
  
	/**
	 * Render our triangles
	 */
	private void render() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    
    GL11.glPushMatrix();
      GL11.glTranslatef(canvas.getSize().x, canvas.getSize().y / 2, 0.0f);
      GL11.glRotatef(angle, 0, 0, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2i(-50, -50);
        GL11.glVertex2i(50, -50);
        GL11.glVertex2i(50, 50);
        GL11.glVertex2i(-50, 50);
      GL11.glEnd();
    GL11.glPopMatrix();
    
    System.out.println("Render " + Sys.getTime());
	}
  
	public static void main(String[] args) {
		new SWTTest().executeTest();
	}
}

fbi

That's not so bad as it might seem: you forgot to call your own initializeOpenGL method  :wink:
By the way...I'm sorry for having forgotten once again my code at home (this month I'm in the middle of an house transfer with my fiancé) but tomorrow I will bring it with me...anyway what do you think about the problems with the multiple versions (the thread title is SWT or something like that) ?

Matzon

Quote from: "fbi"That's not so bad as it might seem: you forgot to call your own initializeOpenGL method  :wink:
DOH! yeah - works fine now - thanks