Uncategorized

Lwjgl 3D Game Engine

Welcome to the second part .Today we are going make our window and an interface class that will make our engine reusable to make different type of games . Any class that will implement the interface will inherit all its methods . 

package gameLoop;

public interface Game{

void audio();

void dispose();

void render();

void update(float time);

void input();

void init()throws Exception;

}

OK now  we can create our window . So create a new class called Display.

Public class Display{

public static final into width = 800;

public static final int  height = 600;

private static long lastFrameTime ;

private static float delta;

private static int last frame;

private static int fps;

private static long lastFps;

public static boolean vsync;

We define some properties for our window ,the width and height . The other variables will help us calculate our fps at run time . Vsync  prevents screen tearing  but  we define it as a boolean because  we want to be able to change it at runtime .

public static void createDisplay(){

ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true);

try{

Display. setDisplayMode(new Display Mode(WIDTH,HEIGHT));

Display. create(new PixelFormat().withDepthBits(24),attribs);

Display. setTitle(“Game Engine”);

Display. setInitialBackground(1,1,1);

GL11.glEnable (GL13.GL_MULTISAMPLE);

}catch(LWJGLException e){

e.printStackTrace();

System.err.printing(“Couldn’t create display”);

System. exit(-1);

}

GL11.glViewport(0,0,WIDTH, HEIGHT);

lastFrameTime = getCurrentTime();

getDelta();

lastFps  = get time();

}

We create our display in this method and set its parameters . When you create the context you need to input the opengl version  supported by your GPU .In my case its 3,2 . 

public static float getCurrentTime(){

return Sys.get time()*1000/Sys.getTimerResolution();

}

public static void update display(){

Display. sync(GameEngine.TARGET_FPS);

Display.update ();

long current FrameTime = getCurrentTime();

delta = (current FrameTime – lastFrameTime)/1000f;

lastFrameTime = current FrameTime;

updateFps();

update(delta);

}

public static float getFrameTimeSeconds(){

return delta;}

public static void destroyDisplay(){

Display. destroy;}

public static void update (float delta){

while(Keyboard.next()){

if(keyboard.getEventKeyState()){ if(keyboard. getEventKey()==keyboard. KEY_F){

setDisplayMode(WIDTH, HEIGHT,! Display. isFullscreen());}

if(keyboard.get eventKey()==keyboard. KEY_V){

vsync =!vsync;

Display. setVSyncEnable(vsync);

}}}}

we sync the display to update at our target frame rate  . We also make it possible for us to change both the screen settings (between full screen and window mode) and vsync. The rest of the code is self explanatory . The finished Display class.



public static final int WIDTH = 800;

 public static final int HEIGHT = 600;

 

 private static long lastFrameTime;

 private static float delta;

 

    private static int lastFrame;

 

 private static int fps;

 

 private static long lastFps;

 

 public static boolean vsync;

 


 public static void createDisplay(){

  ContextAttribs attribs = new ContextAttribs(1,3)

  .withForwardCompatible(true);

 

  try {

   

   Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));

   Display.create(new PixelFormat().withDepthBits(24), attribs);

   Display.setTitle(“Game enginet”);

   Display.setInitialBackground(1, 1, 1);

   GL11.glEnable(GL13.GL_MULTISAMPLE);

   

  } catch (LWJGLException e) {

   e.printStackTrace();

   System.err.println(“Couldn’t create display!”);

   System.exit(-1);

  }

 

 

  GL11.glViewport(0,0,WIDTH,HEIGHT);

  lastFrameTime = getCurrentTime();

  getDelta();

  lastFps = getTime();

 }

 private static long getCurrentTime() {

  return Sys.getTime()*1000/Sys.getTimerResolution();

 }

 public static void updateDisplay(){

  Display.sync(GameEngine.TARGET_FPS);

  Display.update();

 

  long currentFrameTime = getCurrentTime();

  delta = (currentFrameTime – lastFrameTime)/1000f;

  lastFrameTime = currentFrameTime;

  updateFps();

  update(delta);

 }

 public static float getFrameTimeSeconds(){

  return delta;

 }

 public static void destroyDisplay(){

   Display.destroy();

 

 }

   public static void update(float delta){

 

  while(Keyboard.next()){

   if(Keyboard.getEventKeyState()){

    if(Keyboard.getEventKey()==Keyboard.KEY_F){

     setDisplayMode(WIDTH,HEIGHT,!Display.isFullscreen());

    }

    if(Keyboard.getEventKey() == Keyboard.KEY_V){

     

      vsync =!vsync;

      Display.setVSyncEnabled(vsync);

     

    }

   }

  }

 

 }

   

   public static void setDisplayMode(int width,int height,boolean fullscreen){

 

  if((Display.getDisplayMode().getWidth() == width)&&

    (Display.getDisplayMode().getHeight() == height)&&

    (Display.isFullscreen() == fullscreen)){

   return;

  }

 

  try{

   DisplayMode targetDisplayMode = null;

   

   if(fullscreen){

    DisplayMode[] modes = Display.getAvailableDisplayModes();

    int freq = 0;

   

    for(int i = 0;i<modes.length;i++){

     DisplayMode current = modes[i];

     

     if((current.getWidth() == width)&&(current.getHeight()==height)){

     

      if((targetDisplayMode ==null)||(current.getFrequency()>=freq)){

       

       if((targetDisplayMode ==null)||(current.getBitsPerPixel()>targetDisplayMode.getBitsPerPixel()))

       {

        targetDisplayMode = current;

        freq = targetDisplayMode.getFrequency();

       }

      }

      if((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel())&&

       

        (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())){

       targetDisplayMode = current;

       

       break;

      }

     }

    }

   }else{

    targetDisplayMode = new DisplayMode(width,height);

   }

   if(targetDisplayMode == null){

    System.out.println(“Failed to find value mode”+width+”x”+height+”fs=”+fullscreen);

    return;

   }

   Display.setDisplayMode(targetDisplayMode);

   Display.setFullscreen(fullscreen);

  }catch(LWJGLException e){

   System.out.println(“Unable to setup mode”+width+”x”+height+”fullscreen=”+fullscreen + e);

  }

 }

 public static int getDelta(){

  long time = getTime();

  int delta =(int)(time – lastFrame);

  lastFrame = (int) time;

 

  return delta;

 }

 public static long getTime(){

  return Sys.getTime() * 1000/ Sys.getTimerResolution();

 }

 

 public static void updateFps(){

  if(getTime() – lastFps > 1000){

   Display.setTitle(“FPS:”+ fps);

   fps = 0;

   lastFps += 1000;

  }

  fps++;

 }

Well that’s it for now .Feel free to leave a comment cheers!.



Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s