Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game;
- //LWJGL
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.Sys;
- import org.lwjgl.input.Keyboard;
- public class CL2
- {
- private Player player;
- private long lastLoopTime = getTime();
- private static long timerTicksPerSecond = Sys.getTimerResolution();
- public static long delta;
- private long lastFPS;
- private int fps;
- private boolean vsync = false;
- public CL2()
- {
- lastFPS = getTime();
- openGLInit();
- player = new Player(100,475);
- gameLoop();
- }
- private void openGLInit()
- {
- try
- {
- Display.setDisplayMode(new DisplayMode(800,600));
- Display.setFullscreen(false);
- Display.create();
- Display.setVSyncEnabled(false);
- GL11.glEnable(GL11.GL_BLEND);
- GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- GL11.glDisable(GL11.GL_DEPTH_TEST);
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GL11.glOrtho(0, 800, 600, 0, -1, 1);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- GL11.glViewport(0, 0, 800, 600);
- GL11.glClearColor(.3f, .3f, .3f, 0f);
- }
- catch (LWJGLException le)
- {
- le.printStackTrace();
- }
- }
- private void gameLoop()
- {
- while(!Display.isCloseRequested())
- {
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glLoadIdentity();
- delta = getTime() - lastLoopTime;
- lastLoopTime = getTime();
- if (getTime() - lastFPS > 1000)
- {
- Display.setTitle("FPS: " + fps);
- fps = 0;
- lastFPS += 1000;
- }
- fps++;
- applyGameControls(); //Take input from keyboard
- moveEntities(); //Move entities
- render(); //Draw entities
- Display.update();
- Display.sync(60);
- }
- Display.destroy();
- System.exit(0);
- }
- private void moveEntities()
- {
- player.move();
- }
- private void render()
- {
- player.draw();
- }
- private void applyGameControls()
- {
- while (Keyboard.next())
- {
- if(Keyboard.getEventKeyState()) //KEY PRESSED
- {
- if (Keyboard.getEventKey() == Keyboard.KEY_LEFT)
- {
- player.left(true);
- }
- else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT)
- {
- player.right(true);
- }
- else if(Keyboard.getEventKey() == Keyboard.KEY_V)
- {
- if(vsync)
- {
- vsync = false;
- }
- else
- {
- vsync = true;
- }
- Display.setVSyncEnabled(vsync);
- }
- }
- else //KEY RELEASED
- {
- if (Keyboard.getEventKey() == Keyboard.KEY_LEFT)
- {
- player.left(false);
- }
- else if (Keyboard.getEventKey() == Keyboard.KEY_RIGHT)
- {
- player.right(false);
- }
- }
- }
- }
- public static long getTime()
- {
- return (Sys.getTime() * 1000) / timerTicksPerSecond;
- }
- public static void main(String[] args)
- {
- new CL2();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment