I'm having the exact same problem with the same tutorial. Here is all of my code.
Based on
http://gpwiki.org/index.php/OpenGL:Tuto ... troductionImports Class and vars
Code:
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class Game
{
/*** Game title */
public static final String GAME_TITLE = "My Game";
/** Desired frame time */
private static final int FRAMERATE = 60;
/** Exit the game */
private static boolean finished;
/** Angle of rotating square */
private static float angle;
...
...
}
The Main Function
Code:
public static void main(String[] args)
{
boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
try
{
init(fullscreen);
run();
}
catch (Exception e)
{
e.printStackTrace(System.err);
Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
}
finally
{
cleanup();
}
System.exit(0);
}
The Init Function
Code:
private static void init(boolean fullscreen) throws Exception
{
// Create a fullscreen window with 1:1 orthographic 2D projection (default)
Display.setTitle(GAME_TITLE);
Display.setFullscreen(fullscreen);
// Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
Display.setVSyncEnabled(true);
// Create default display of 640x480
Display.create();
glViewport(0, 0, 800, 600);
}
The Run Function
Code:
private static void run()
{
while (!finished)
{
// Always call Window.update(), all the time - it does some behind the
// scenes work, and also displays the rendered output
Display.update();
// Check for close requests
if (Display.isCloseRequested())
{
finished = true;
}
// The window is in the foreground, so we should play the game
else if (Display.isActive())
{
logic();
render();
Display.sync(FRAMERATE);
}
// The window is not in the foreground, so we can allow other stuff to run and
// infrequently update
else
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
logic();
// Only bother rendering if the window is visible or dirty
if (Display.isVisible() || Display.isDirty())
{
render();
}
}
}
}
The Render Function
Code:
private static void render()
{
// clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// center square according to screen size
glClearColor(0f, 0f, 0.5f, 1.0f);
glPushMatrix();
glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);
// rotate square according to angle
glRotatef(angle, 0, 0, 1.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2i(-50, -50);
glVertex2i(50, -50);
glVertex2i(50, 50);
glVertex2i(-50, 50);
glEnd();
glPopMatrix();
}
Logic
Code:
private static void logic()
{
// Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
finished = true;
}
// Rotate the square
angle += 2.0f % 360;
}
Cleanup
Code:
private static void cleanup()
{
// Close the window
Display.destroy();
}
When I run all this I should get a rotating square in the center of my screen. Instead I get a blank blue screen.
I changed the background from black to blue to make sure the square wasn't the same color as the background. I adjusted the imports a bit from the tutorial trying to get it to work, but it didn't work when copied exactly from the tutorial either.
Any ideas?