Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.willchill.game;
- import me.willchill.game.entity.Entity;
- import me.willchill.game.input.GameKeyboard;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- import org.lwjgl.opengl.GL11;
- public class Game{
- private GameKeyboard keyboard;
- private String gameStats = " ";
- private Entity player;
- public Game(){
- keyboard = new GameKeyboard();
- }
- private void start(){
- try{
- Display.setDisplayMode(new DisplayMode(800,600));
- Display.create();
- Display.setTitle("Game");
- Display.setVSyncEnabled(true);
- } catch (LWJGLException e){
- e.printStackTrace();
- System.exit(0);
- }
- initGL();
- player = new Entity("/res/player.png", 400, 300);
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- double ns = 1000000000.0 / 60.0;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- while(!Display.isCloseRequested()){
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while(delta >= 1){
- update();
- updates++;
- delta--;
- }
- frames++;
- if(System.currentTimeMillis() - timer > 1000){
- timer += 1000;
- gameStats = "FPS: " + frames + ", UPS: " + updates;
- Display.setTitle("Game " + gameStats);
- System.out.println(gameStats);
- updates = 0;
- frames = 0;
- }
- render();
- Display.update();
- }
- Display.destroy();
- }
- private void render(){
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
- GL11.glBegin(GL11.GL_QUADS);
- GL11.glVertex2f(player.x, player.y ); //Top-left
- GL11.glVertex2f(player.x + player.texture.getTextureWidth(), player.y);//Top-right
- GL11.glVertex2f(player.x + player.texture.getTextureWidth(), player.y + player.texture.getTextureHeight());//Bottom-right
- GL11.glVertex2f(player.x, player.y + player.texture.getTextureHeight());//Bottom-left
- GL11.glEnd();
- }
- private void update() {
- keyboard.pollInput();
- }
- public void initGL() {
- GL11.glMatrixMode(GL11.GL_PROJECTION);
- GL11.glLoadIdentity();
- GL11.glOrtho(0, 800, 0, 600, 1, -1);
- GL11.glMatrixMode(GL11.GL_MODELVIEW);
- GL11.glEnable(GL11.GL_TEXTURE_2D);
- }
- public static void main(String args[]){
- Game game = new Game();
- game.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment