I am going through several tutorials and managed to create a display with 2 rendered squares. The problem I have is that when I move one square, the other moves at the same time. I only want one of the squares to move. I did a search here and used Google, but could not locate an article on this specific issue. Below is the code I am using, any ideas? I know I have to add a timer (ticks) and more, but I am trying to get a prototype done.
package worldofadventure;
/**
*
* @author Stoofus
*/
// Start imports
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
// End imports
// Start class Main
public class Main
{
/**
* @param args the command line arguments
*/
// Start variables
static String TITLE = "World of Adventure - PreAlpha version 0.0.1";
static int S_WIDTH = 800; // Screen Width
static int S_HEIGHT = 600; // Screen Height
static final Logger LOGGER = Logger.getLogger( Main.class.getName() );
int squareSize; // Size of the square
int squareX; // Horizontal location of the square
int squareY; // Vertical location of the square
int squareZ; // Depth of the square relative to the viewer
int rectSize; // Size of rectangle
int rectX; // Horizontal location of rectangle
int rectY; // Vertical location of rectangle
int rectZ; // Depth of rectangle relative to the viewer
// End variables
// Add a log handler and create a log file
static
{
try
{
LOGGER.addHandler( new FileHandler("errors.log", true));
}
catch( IOException ex )
{
LOGGER.log( Level.WARNING, ex.toString(), ex );
}
} // End log handler
// Start main program
public static void main(String[] args)
{
// Start the main GUI
mainGUI myGUI = new mainGUI(); // Create myGUI object
myGUI.setTitle( TITLE ); // Set title
myGUI.setVisible( true ); // Set visable to true
} // End main program
// Start class startGame
public void startGame()
{
Main main = null; // Create a null object
try
{
// Print out mapped keys
System.out.println( "Keys:" );
System.out.println( "Q = Shrink" );
System.out.println( "E = Grow" );
System.out.println( "W = Up" );
System.out.println( "S = Down" );
System.out.println( "A = Left" );
System.out.println( "D = Right" );
System.out.println( "Esc = Exit" );
main = new Main(); // Create an instance of the object
main.create(); // Create the display
main.run(); // Run the display
}
// Ooops, record what went wrong
catch( Exception ex )
{
LOGGER.log( Level.SEVERE, ex.toString(), ex );
}
finally
{
if (main != null )
{
main.destroy(); // Destroy the instance of the object
}
}
} // End class startGame
// Start Main constructor
public Main()
{
// Initialize variables
squareSize = 100; // Initial size of square
squareX = 55; // Initial X (Horizontal) position of square
squareY = 50; // Initial Y (Vertical) position of square
squareZ = 0; // Initial Z position of square
rectSize = 100; // Initial size of rectangle
rectX = 200; // Initial X (Horizontal) position of rectangle
rectY = 50; // Initial vertical position of rectangle
rectZ = 0; // Initial Z position of rectangle
} // End Main constructor
// Start class create
public void create() throws LWJGLException
{
// Display
// S_WIDTH = Display Width
// S_HEIGHT = Display Heigh
Display.setDisplayMode( new DisplayMode( S_WIDTH, S_HEIGHT ) );
Display.setFullscreen( false );
Display.setTitle( TITLE ); // Same title of mainGUI
Display.create(); // Create the display object
// Create keyboard
Keyboard.create();
// Create and ungrab mouse
Mouse.setGrabbed( false );
Mouse.create();
// Initialize OpenGL
initGL();
resizeGL();
} // End class create
// Start class destroy
// This method already checks if they have been created
// before destroying them
public void destroy()
{
Mouse.destroy();
Keyboard.destroy();
Display.destroy();
} // End class destroy
// Start class initGL
// Creates the background
public void initGL()
{
// 2D Initialization
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );
} // End class initGL
// Start class processKeyboard
public void processKeyboard()
{
// Square's X axis
if( Keyboard.isKeyDown( Keyboard.KEY_W ) )
{
// Move square up
++squareY;
}
if ( Keyboard.isKeyDown( Keyboard.KEY_S ) )
{
// Move square down
--squareY;
}
// Square's Z axis
if ( Keyboard.isKeyDown( Keyboard.KEY_A ) )
{
// Move square left
--squareX;
}
if ( Keyboard.isKeyDown( Keyboard.KEY_D ) )
{
// Move square right
++squareX;
}
// Shrink or Grow square
if ( Keyboard.isKeyDown( Keyboard.KEY_Q ) )
{
// Shrink square
--squareSize;
}
if ( Keyboard.isKeyDown( Keyboard.KEY_E ) )
{
// Grow square
++squareSize;
}
} // End class processKeyboard
// Start class processMouse
public void processMouse()
{
// Enter mouse code here
} // End class processMouse
// Start class render
public void render()
{
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
// Translate into view for square
glTranslatef( squareX, squareY, 0.0f );
glRotatef( squareZ, 0.0f, 0.0f, 1.0f );
glTranslatef( -( squareSize >> 1 ), -( squareSize >> 1 ), 0.0f );
glColor3f( 0.0f, 0.5f, 0.5f );
// Square
glBegin( GL_QUADS );
glTexCoord2f( 0.0f, 0.0f ); glVertex2f( 0.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f ); glVertex2f( squareSize, 0.0f );
glTexCoord2f( 1.0f, 1.0f ); glVertex2f( squareSize, squareSize );
glTexCoord2f( 0.0f, 1.0f ); glVertex2f( 0.0f, squareSize );
glEnd();
// Translate into view for rectangle
glTranslatef( rectX, rectY, 0.0f );
glRotatef( rectZ, 0.0f, 0.0f, 1.0f );
glTranslatef( -( rectSize >> 1 ), -( rectSize >> 1 ), 0.0f );
glColor3f( 0.0f, 0.5f, 0.5f );
glBegin( GL_QUADS );
glTexCoord2f( 0.0f, 0.0f ); glVertex2f( 0.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f ); glVertex2f( rectSize, 0.0f );
glTexCoord2f( 1.0f, 1.0f ); glVertex2f( rectSize, rectSize );
glTexCoord2f( 0.0f, 1.0f ); glVertex2f( 0.0f, rectSize );
glEnd();
} // End class render
// Start class resizeGL
public void resizeGL()
{
// 2D Scene
glViewport( 0,0, S_WIDTH, S_HEIGHT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0f, S_WIDTH, 0.0f, S_HEIGHT );
glPushMatrix();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
} // End class resizeGL
// Start class run
public void run()
{
while( !Display.isCloseRequested() &&
!Keyboard.isKeyDown( Keyboard.KEY_ESCAPE ) )
{
if ( Display.isVisible() )
{
processKeyboard();
processMouse();
update();
render();
}
else
{
if( Display.isDirty() )
{
render();
}
try
{
Thread.sleep( 100 );
}
catch( InterruptedException ex )
{
}
}
Display.update();
Display.sync( 60 );
}
} // End class run
// Start class update
public void update()
{
int squareEdge = (squareSize / 2 ); // Find the edge via its size
int rectEdge = ( rectSize / 2 ); // Ditto
// Don't go offscreen from bottom
if ( squareY < 50 )
{
squareY = 50;
}
// Don't go offscreen from top
if ( squareY > S_HEIGHT - squareEdge )
{
squareY = S_HEIGHT - squareEdge;
}
// Don't go offscreen from left
if ( squareX < squareEdge)
{
squareX = squareEdge;
}
// Don't go offscreen from right
if ( squareX > S_WIDTH - squareEdge )
{
squareX = S_WIDTH - squareEdge;
}
// Don't get too small to see
if ( squareSize < 5 )
{
squareSize = 5;
}
// Don't get bigger than the screen
if ( squareSize > S_HEIGHT )
{
squareSize = S_HEIGHT;
}
} // End class update
} // End class Main
Thanks to Troubleshoots for the code tag suggestion! I kept comments to assist with the troubleshooting.