The point of this code is to display two lines that start from the center of the screen and reach outwards. When first run, they display properly, but when the window is resized, the center point is supposed to be updated, but isn't.
Here is the relevant code:
Code: Select all
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import GraphicsSettings;
import WindowTitleList;
public class GLWindow {
private int fps_cap = 60;
private int width = 800, height = 600;
private GLRenderable content;
public GLWindow() {
if(System.getProperty("os.name").startsWith("Windows"))
System.setProperty("org.lwjgl.librarypath", System.getProperty("user.dir") + "\\lib\\native\\windows");
else if(System.getProperty("os.name").startsWith("Linux"))
System.setProperty("org.lwjgl.librarypath", System.getProperty("user.dir") + "/lib/native/linux");
else {
System.out.println("No native OpenGL support!");
System.exit(0);
}
width = GraphicsSettings.getInstance().getWindowSize().width;
height = GraphicsSettings.getInstance().getWindowSize().height;
}
public void show() {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(WindowTitleList.getRandomWindowTitle());
if(GraphicsSettings.getInstance().isResizable())
Display.setResizable(true);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
while (!Display.isCloseRequested()) {
width = Display.getWidth();
height = Display.getHeight();
Display.setTitle(width + ":" + height);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT |GL11.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
set2DMode(0, width, 0, height);
if(content!=null) content.render();
Display.update();
Display.sync(fps_cap);
}
Display.destroy();
}
public static void set2DMode(float x, float width, float y, float height) {
//GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glPushMatrix(); // Store The Projection Matrix
GL11.glLoadIdentity(); // Reset The Projection Matrix
GL11.glOrtho(x, width, y, height, -1, 1); // Set Up An Ortho Screen
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glPushMatrix(); // Store The Modelview Matrix
GL11.glLoadIdentity(); // Reset The Modelview Matrix
}
public static void set3DMode() {
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glPopMatrix(); // Restore The Old Projection Matrix
GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
GL11.glPopMatrix(); // Restore The Old Projection Matrix
//GL11.glEnable(GL11.GL_DEPTH_TEST);
}
public void setContents(GLRenderable c) {
this.content = c;
this.show();
}
}
Code: Select all
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import GraphicsSettings;
public class GLIntroScreen implements GLRenderable {
private int width, height;
private int length_x, length_y;
public GLIntroScreen() {
width = GraphicsSettings.getInstance().getWindowSize().width;
height = GraphicsSettings.getInstance().getWindowSize().height;
}
@Override
public void update(int delta) {
// TODO Auto-generated method stub
}
@Override
public void render() {
width = Display.getWidth();
height = Display.getHeight();
// TODO Auto-generated method stub
// Clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
// set the color of the quad (R,G,B,A)
GL11.glColor3f(0.0f,0.0f,1.0f);
GL11.glBegin(GL11.GL_LINE);
GL11.glVertex2i(width/2, height/2);
GL11.glVertex2i(width/2-length_x, height/2);
GL11.glEnd();
GL11.glBegin(GL11.GL_LINE);
GL11.glVertex2i(width/2, height/2);
GL11.glVertex2i(width/2+length_x, height/2);
GL11.glEnd();
GL11.glBegin(GL11.GL_LINE);
GL11.glVertex2i(width/2, height/2);
GL11.glVertex2i(width/2, height/2-length_y);
GL11.glEnd();
GL11.glBegin(GL11.GL_LINE);
GL11.glVertex2i(width/2, height/2);
GL11.glVertex2i(width/2, height/2+length_y);
GL11.glEnd();
if(length_x < width/3) length_x++;
if(length_y < height/5) length_y++;
}
}
Code: Select all
interface GLRenderable {
void update(int delta);
void render();
}
Any hints/explanations on how to make it work are welcome.
Advertising
