Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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: Zerali (posted 2012-07-20 16:50:20, viewed 111 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   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311  
// Window.java

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

import static org.lwjgl.opengl.GL11.*;
 
public class Window {
 
   private String Title = "Prelude to Dawn, Indev, Build: 0";
   private int initScreenHeight = 640;
   private int initScreenWidth  = 1080;
   private State state = State.INTRO;
   
   private int fps; 
   private long lastFrame;
   private long lastFPS;
   private long startTime;
    
   private static enum State {
      INTRO, MAIN_MENU, GAME;
   }
   
   public void start() {
      try {
         Display.setDisplayMode(new DisplayMode(initScreenWidth, initScreenHeight));
         Display.setTitle(Title);
         Display.setResizable(true);
         Display.create();
      } catch (LWJGLException e) {
         e.printStackTrace();
         System.exit(0);
      }
 
      initGL(); // init OpenGL
 
      menu();
      
      Display.destroy();
   }
 
   public void initGL() {
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, initScreenWidth, 0, initScreenHeight, 1, -1);
      glMatrixMode(GL_MODELVIEW);
      glEnable(GL_TEXTURE_2D);
   }
   
   private void menu() {
      lastFPS = getTime(); // call before loop to initialise fps timer
      startTime = lastFPS;

      while (!Display.isCloseRequested()) {
         int delta = getDelta();
         
         // Clear The Screen And The Depth Buffer
         glClear(GL_COLOR_BUFFER_BIT);
         
         switch(state) {
            case INTRO:
               glColor3f(1.0f, 0.0f, 0.0f);
               glRectf(0, 0, initScreenWidth, initScreenHeight);
               
               if((getTime() - startTime) > 1000) {
                  state = State.MAIN_MENU;
               }
               break;
            case MAIN_MENU:
               glColor3f(0.0f, 1.0f, 0.0f);
               glRectf(0, 0, initScreenWidth, initScreenHeight);
               
               if((getTime() - startTime) > 2000) {
                  state = State.GAME;
               }
               break;
            case GAME:
               glColor3f(0.0f, 0.0f, 1.0f);
               glRectf(0, 0, initScreenWidth, initScreenHeight);
               
               
               try {
                  Display.releaseContext();
               } catch (LWJGLException e) {
                  e.printStackTrace();
               }
               
               Game game = new Game();
               game.play();
               
               break;
         }
         
         Display.update();
         Display.sync(60); // cap fps to 60fps
         updateFPS();
      }
   }
   
   /** 
    * Calculate how many milliseconds have passed 
    * since last frame.
    * 
    * @return milliseconds passed since last frame 
    */
   public int getDelta() {
       long time = getTime();
       int delta = (int) (time - lastFrame);
       lastFrame = time;
 
       return delta;
   }
 
   /**
    * Get the accurate system time
    * 
    * @return The system time in milliseconds
    */
   public long getTime() {
       return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }
 
   /**
    * Calculate the FPS and set it in the title bar
    */
   public void updateFPS() {
      if (getTime() - lastFPS > 1000) {
         Display.setTitle(Title + " FPS: " + fps);
         fps = 0;
         lastFPS += 1000;
      }
      fps++;
   }

   public static void main(String[] argv) {
      Window display = new Window();
      display.start();
   }
}


// Game.java

public class Game {
   private RenderLoop render;
   private GameLoop update;
   
   Game() {
      render = new RenderLoop();
      update = new GameLoop();
   }
   
   public void play () {
      render.start();
      update.start();
      
      try {
         render.join();
         update.join();
      }
      catch(InterruptedException e) { }
      
   }
}

// RenderLoop.java

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glRectf;

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

public class RenderLoop extends Thread {
   private String Title = "Prelude to Dawn, Indev, Build: 0";
   private int windowHeight = 640;
   private int windowWidth = 1080;
   
   private int fps; 
   private long lastFrame;
   private long lastFPS;
   
   private long startTime;
   
   // Constructor
   RenderLoop() {

   }
   
   // This is what the thread runs when started
   public void run(){

      try {
         Display.makeCurrent();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
      
      lastFPS = getTime(); // call before loop to initialise fps timer
      startTime = lastFPS;
      
      while (!Display.isCloseRequested()) {
         int delta = getDelta();
         
         System.out.println("Render Loop");
               
         // Clear The Screen Buffer
         glClear(GL_COLOR_BUFFER_BIT);
         
         glColor3f(0.5f, 0.5f, 0.5f);
         glRectf(0, 0, windowWidth, windowHeight);
      
         
         Display.update();
         Display.sync(60); // cap fps to 60fps
         //updateFPS();
      }
   }
   
   public int getDelta() {
       long time = getTime();
       int delta = (int) (time - lastFrame);
       lastFrame = time;
 
       return delta;
   }
 
   public long getTime() {
       return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }
 
   public void updateFPS() {
      if (getTime() - lastFPS > 1000) {
         Display.setTitle(Title + " FPS: " + fps);
         fps = 0;
         lastFPS += 1000;
      }
      fps++;
   }
   
   public void checkScreenSize() {
      if (windowHeight != Display.getHeight()) {
         windowHeight = Display.getHeight();
         System.out.println("Height = " + windowHeight);
      }
      
      if(windowWidth != Display.getWidth()) {
         windowWidth = Display.getWidth();
         System.out.println("Widht = " + windowWidth);
      }
   }
}


// GameLoop.java

import org.lwjgl.Sys;

public class GameLoop extends Thread {
   
   private boolean gameRunning = true;
   private long tickRate = 30;
   private long lastTick;
   private long startTime;
   
   public void run() {
      lastTick = getTime();
      
      while(gameRunning) {
         int tick = getTicks();
         
         System.out.println("Game Loop is running");
         
         // Run the loop for 60 seconds for testing purposes
         if(getTime() - startTime > 60000) {
            gameRunning = false;
         }
         
         wait(tick);
      }
   }
   
   public int getTicks() {
       long tick = getTime();
       int ticks = (int) (tick - lastTick);
       lastTick = tick;
 
       return ticks;
   }
   
   /**
    * Get the accurate system time
    * 
    * @return The system time in milliseconds
    */
   public long getTime() {
       return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }
   
   public void wait(int tick) {
      if(tick < (1000/tickRate)) {
         try { Thread.sleep((1000/tickRate) - tick); } 
         catch (Exception e) {}
      }
   }
}





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
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (37 views)
2014-12-09 22:41:13

BurntPizza (72 views)
2014-12-08 04:46:31

JscottyBieshaar (34 views)
2014-12-05 12:39:02

SHC (46 views)
2014-12-03 16:27:13

CopyableCougar4 (42 views)
2014-11-29 21:32:03

toopeicgaming1999 (110 views)
2014-11-26 15:22:04

toopeicgaming1999 (96 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!