Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Roquen (posted 2014-01-24 14:24:11, viewed 231 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241  
package wiki.roquen;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

/**
 * The basic framework for use with my lwjgl tutorials.
 * 
 * @author Sri Harsha Chilakapati
 */
public class Game {
  // Single instance is allowed
  private static Game instance;

  /**
   * A basic game.
   */
  public Game() {
    try {
      instance = this;
      Display.create();
      setDisplayMode(800, 600);
      Display.setVSyncEnabled(true);
      Display.setResizable(true);
      gameLoop();
    } catch (LWJGLException e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }

  // The gameloop. Runs at 60 fps
  private void gameLoop() {
    long lastFrame = getCurrentTime();
    long thisFrame;

    init();

    while (!Display.isCloseRequested()) {
      thisFrame = getCurrentTime();

      update(thisFrame - lastFrame);
      render();

      Display.update();

      if (Display.wasResized()) {
        resized();
      }

      Display.sync(60);

      lastFrame = thisFrame;
    }

    end();
  }

  /**
   * Switch the fullscreen state.
   */
  public static void switchFullscreen() {
    setFullscreen(!Display.isFullscreen());
  }

  /**
   * Sets the fullscreen state.
   */
  public static void setFullscreen(boolean fullscreen) {
    setDisplayMode(Display.getDisplayMode(), fullscreen);
  }

  /**
   * Sets a DisplayMode.
   * 
   * @param mode
   *          The DisplayMode.
   * @param fullscreen
   *          The fullscreen state.
   */
  public static boolean setDisplayMode(DisplayMode mode, boolean fullscreen) {
    return setDisplayMode(mode.getWidth(), mode.getHeight(), fullscreen);
  }

  /**
   * Sets a DisplayMode.
   */
  public static boolean setDisplayMode(DisplayMode mode) {
    return setDisplayMode(mode, false);
  }

  /**
   * Sets a windowed DisplayMode.
   * 
   * @param width
   *          The width of the display.
   * @param height
   *          The height of the display.
   */
  public static boolean setDisplayMode(int width, int height) {
    return setDisplayMode(width, height, false);
  }

  /**
   * Sets a DisplayMode after selecting for a better one.
   * 
   * @param width
   *          The width of the display.
   * @param height
   *          The height of the display.
   * @param fullscreen
   *          The fullscreen mode.
   * 
   * @return True if switching is successful. Else false.
   */
  public static boolean setDisplayMode(int width, int height, boolean fullscreen) {
    // return if requested DisplayMode is already set
    if ((Display.getDisplayMode().getWidth() == width)
        && (Display.getDisplayMode().getHeight() == height)
        && (Display.isFullscreen() == fullscreen))
      return true;

    try {
      // The target DisplayMode
      DisplayMode targetDisplayMode = null;

      if (fullscreen) {
        // Gather all the DisplayModes available at fullscreen
        DisplayMode[] modes = Display.getAvailableDisplayModes();
        int freq = 0;

        // Iterate through all of them
        for (DisplayMode current : modes) {
          // Make sure that the width and height matches
          if ((current.getWidth() == width) && (current.getHeight() == height)) {
            // Select the one with greater frequency
            if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
              // Select the one with greater bits per pixel
              if ((targetDisplayMode == null)
                  || (current.getBitsPerPixel() > targetDisplayMode
                      .getBitsPerPixel())) {
                targetDisplayMode = current;
                freq = targetDisplayMode.getFrequency();
              }
            }

            // if we've found a match for bpp and frequency against the
            // original display mode then it's probably best to go for this one
            // since it's most likely compatible with the monitor
            if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode()
                .getBitsPerPixel())
                && (current.getFrequency() == Display.getDesktopDisplayMode()
                    .getFrequency())) {
              targetDisplayMode = current;
              break;
            }
          }
        }
      } else {
        // No need to query for windowed mode
        targetDisplayMode = new DisplayMode(width, height);
      }

      if (targetDisplayMode == null) {
        System.out.println("Failed to find value mode: " + width + "x" + height
            + " fs=" + fullscreen);
        return false;
      }

      // Set the DisplayMode we've found
      Display.setDisplayMode(targetDisplayMode);
      Display.setFullscreen(fullscreen);

      System.out.println("Selected DisplayMode: "
          + targetDisplayMode.toString());

      // Generate a resized event
      instance.resized();

      return true;
    } catch (LWJGLException e) {
      System.out.println("Unable to setup mode " + width + "x" + height
          + " fullscreen=" + fullscreen + e);
    }

    return false;
  }

  /**
   * @return Current time in milliseconds.
   */
  public static long getCurrentTime() {
    return Sys.getTime() * 1000 / Sys.getTimerResolution();
  }

  /**
   * Properly terminate the game.
   */
  public static void end() {
    instance.dispose();
    instance = null;
    Display.destroy();
    System.exit(0);
  }

  /**
   * Load any resources here.
   */
  public void init() {
  }

  /**
   * Update the logic of the game.
   * 
   * @param elapsedTime
   *          Time elapsed since last frame.
   */
  public void update(long elapsedTime) {
  }

  /**
   * Render to screen.
   */
  public void render() {
  }

  /**
   * Display is resized
   */
  public void resized() {
  }

  /**
   * Dispose created resources.
   */
  public void dispose() {
  }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
EgonOlsen (364 views)
2018-06-10 19:43:48

EgonOlsen (364 views)
2018-06-10 19:43:44

EgonOlsen (304 views)
2018-06-10 19:43:20

DesertCoockie (540 views)
2018-05-13 18:23:11

nelsongames (871 views)
2018-04-24 18:15:36

nelsongames (854 views)
2018-04-24 18:14:32

ivj94 (1306 views)
2018-03-24 14:47:39

ivj94 (426 views)
2018-03-24 14:46:31

ivj94 (1089 views)
2018-03-24 14:43:53

Solater (445 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!