Package view

Source Code of view.InterfaceHandler

package view;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import game.GameSystem;

import java.io.IOException;
import java.nio.FloatBuffer;
import java.util.HashSet;
import java.util.Map;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.Pbuffer;
import org.lwjgl.opengl.PixelFormat;

import com.google.common.collect.HashMultimap;

import ctrl.InputHandler;

public class InterfaceHandler {

  private static final float VIEW_ANGLE = 70, VIEW_MIN_DISTANCE = 1f,
      VIEW_MAX_DISTANCE = 1000000, FOG_START_DISTANCE = 1.4f,
      FOG_END_DISTANCE = 3;

  private float displayRatio = 1;
  private HashMultimap<Integer, InputHandler> keyPressedHandlers = HashMultimap
      .create(),
      keyReleasedHandlers = HashMultimap.create(),
      keyActiveHandlers = HashMultimap.create();
  private HashSet<InputHandler> keyHandlers = new HashSet<InputHandler>();

  public void run(GameSystem system) {
    try {
      //Display.setDisplayMode(new DisplayMode(600, 400));
      Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
      Display.create(new PixelFormat(8, 24, 0, 4));
      Display.setVSyncEnabled(true);
    } catch (LWJGLException e) {
      e.printStackTrace();
      System.exit(0);
    }
    initOpenGL();
    try {
      system.init();
    } catch (IOException e) {
      e.printStackTrace();
    }

    while (!Display.isCloseRequested()) {
      glLoadIdentity();
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      system.render();
      handleInput();
      Display.update();
    }
    Display.destroy();
  }

  public void register(InputHandler handler) {
    for (int key : handler.pressedKeys) {
      keyPressedHandlers.put(key, handler);
      keyHandlers.add(handler);
    }
    for (int key : handler.releasedKeys) {
      keyReleasedHandlers.put(key, handler);
      keyHandlers.add(handler);
    }
    for (int key : handler.activeKeys) {
      keyActiveHandlers.put(key, handler);
      keyHandlers.add(handler);
    }
  }

  private void render() {

  }

  private void handleInput() {
    for (Map.Entry<Integer, InputHandler> entry : keyActiveHandlers
        .entries()) {
      if ((entry.getKey() >= 0) ? Keyboard.isKeyDown(entry.getKey())
          : Mouse.isButtonDown(-1 - entry.getKey())) {
        entry.getValue().set();
      }
    }
    while (Mouse.next()) {
      int button = -1 - Mouse.getEventButton();
      if (Mouse.getEventButtonState()
          && keyPressedHandlers.containsKey(button)) {
        for (InputHandler handler : keyPressedHandlers.get(button)) {
          handler.set();
        }
      }
      if (!Mouse.getEventButtonState()
          && keyReleasedHandlers.containsKey(button)) {
        for (InputHandler handler : keyReleasedHandlers.get(button)) {
          handler.set();
        }
      }
    }
    while (Keyboard.next()) {
      int key = Keyboard.getEventKey();
      if (Keyboard.getEventKeyState()
          && keyPressedHandlers.containsKey(key)) {
        for (InputHandler handler : keyPressedHandlers.get(key)) {
          handler.set();
        }
      }
      if (!Keyboard.getEventKeyState()
          && keyReleasedHandlers.containsKey(key)) {
        for (InputHandler handler : keyReleasedHandlers.get(key)) {
          handler.set();
        }
      }
    }
    for (InputHandler handler : keyHandlers) {
      handler.clear();
    }
  }

  private void initOpenGL() {
    glClearColor(0, 0, 0, 1);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    FloatBuffer matSpecular = BufferUtils.createFloatBuffer(4).put(
        new float[] { 1, 1, 1, 1 });
    FloatBuffer matOcean = BufferUtils.createFloatBuffer(4).put(
        new float[] { 0.3f, 0.3f, 1, 1 });
    FloatBuffer lightPosition = BufferUtils.createFloatBuffer(4).put(
        new float[] { 1, 1, -40000, 0 });
    FloatBuffer whiteLight = BufferUtils.createFloatBuffer(4).put(
        new float[] { 1, 1, 1, 1 });
    FloatBuffer sunLight = BufferUtils.createFloatBuffer(4).put(
        new float[] { 1, 1, 0.9f, 1 });
    FloatBuffer lModelAmbient = BufferUtils.createFloatBuffer(4).put(
        new float[] { 0.1f, 0.1f, 0.1f, 1 });
    matSpecular.flip();
    matOcean.flip();
    lightPosition.flip();
    whiteLight.flip();
    sunLight.flip();
    lModelAmbient.flip();
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glMaterial(GL_FRONT, GL_SPECULAR, matSpecular);
    glMaterialf(GL_FRONT, GL_SHININESS, 10.0f);
    glLight(GL_LIGHT0, GL_POSITION, lightPosition);
    glLight(GL_LIGHT0, GL_SPECULAR, sunLight);
    glLight(GL_LIGHT0, GL_DIFFUSE, sunLight);
    glLightModel(GL_LIGHT_MODEL_AMBIENT, lModelAmbient);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(VIEW_ANGLE, Display.getDisplayMode().getWidth()
        / (float) Display.getDisplayMode().getHeight(),
        VIEW_MIN_DISTANCE, VIEW_MAX_DISTANCE);
    glMatrixMode(GL_MODELVIEW);
   
    int vPhong = Shaders.buildVertexShader("res/phong.vert");
    int fPhong = Shaders.buildFragmentShader("res/phong.frag");
    //int vMulti = Shaders.buildVertexShader("res/multitexture.vert");
    //int fMulti = Shaders.buildFragmentShader("res/multitexture.frag");
    int program = Shaders.buildProgram(new int[] {vPhong, fPhong});
    GL20.glUseProgram(program);

  }

  public int getMaxAA() throws LWJGLException {
    int result = 0;
    PixelFormat format = new PixelFormat(0, 24, 0, 0);
    Pbuffer pb = new Pbuffer(10, 10, format, null);
    pb.makeCurrent();
    if (GLContext.getCapabilities().GL_ARB_multisample) {
      result = glGetInteger(GL30.GL_MAX_SAMPLES);
    }
    pb.destroy();
    return result;
  }

}
TOP

Related Classes of view.InterfaceHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.