piexil

Untitled

Nov 12th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: Bobby
  4.  * Date: 11/9/13
  5.  * Time: 8:57 AM
  6.  * To change this template use File | Settings | File Templates.
  7.  */
  8.  
  9.  
  10.  
  11. import static org.lwjgl.opengl.GL11.*;
  12. import org.lwjgl.LWJGLException;
  13. import org.lwjgl.opengl.Display;
  14. import org.lwjgl.opengl.DisplayMode;
  15.  
  16.  
  17. /**
  18.  * A bare-bones implementation of a LWJGL application.
  19.  * @author davedes
  20.  */
  21. public class gameOfLife {
  22.  
  23.     // Whether to enable VSync in hardware.
  24.     public static final boolean VSYNC = true;
  25.  
  26.     // Width and height of our window
  27.     public static final int WIDTH = 800;
  28.     public static final int HEIGHT = 600;
  29.     public int generation;
  30.     // Whether to use fullscreen mode
  31.     public static final boolean FULLSCREEN = false;
  32.  
  33.     // Whether our game loop is running
  34.     protected boolean running = false;
  35.  
  36.     public static void main(String[] args) throws LWJGLException {
  37.        new gameOfLife().start();
  38.     }
  39.     public static int getDisplayHeight(){
  40.         return HEIGHT;
  41.     }
  42.     public static int getDisplayWidth(){
  43.         return WIDTH;
  44.     }
  45.     Cell Population [ ] [ ] = new Cell [ getDisplayWidth() ] [ getDisplayHeight() ];
  46.     public void initialize(){
  47.         int i, j;
  48.  
  49.         for ( i = 0 ; i < 20 ; i ++ )
  50.             for ( j = 0 ; j < 20 ; j ++ )
  51.                 Population [ i ] [ j ].initialize ( );
  52.     }
  53.     void next_generation ( ) {
  54.         int i, j;
  55.  
  56.         for ( i = 0; i < getDisplayWidth() ; i ++ )
  57.             for ( j = 0; j < getDisplayHeight() ; j++ )
  58.                 Population [ i ] [ j ].computeNextGen ( );
  59.  
  60.         for ( i = 0; i < getDisplayWidth() ; i ++ )
  61.             for ( j = 0; j < getDisplayHeight() ; j++ )
  62.                 Population [ i ] [ j ].updateGen ( );
  63.  
  64.  
  65.  
  66.     }
  67.     public void init ( ) {
  68.         int i, j, k, l;
  69.         int rand;
  70.  
  71.  
  72.         for ( i = 0 ; i < getDisplayWidth() ; i ++ )
  73.             for ( j = 0 ; j < getDisplayHeight() ; j ++ )
  74.                 Population [ i ] [ j ] = new Cell ( );
  75.  
  76.         for ( i = 0 ; i < getDisplayWidth() ; i ++ )
  77.             for ( j = 0 ; j < getDisplayHeight() ; j ++ ){
  78.                 for ( k = -1 ; k <= 1 ; k ++ )
  79.                     for ( l = -1 ; l <= 1 ; l ++ )
  80.                         if ( ( i + k >= 0 ) &&
  81.                                 ( i + k < getDisplayWidth() ) &&
  82.                                 ( j + l >= 0 ) &&
  83.                                 ( j + l < getDisplayHeight() ) &&
  84.                                 ( ( k != 0 ) || ( l != 0 ) ) ){
  85.                             Population [ i ] [ j ].setNeighbor (
  86.                                     Population [ i + k] [ j + l ] );
  87.                         }
  88.             }
  89.         initialize ( );
  90.  
  91.  
  92.     }
  93.     // Start our game
  94.     public void start() throws LWJGLException {
  95.         // Set up our display
  96.         Display.setTitle("Display example"); //title of our window
  97.         Display.setResizable(true); //whether our window is resizable
  98.         Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); //resolution of our display
  99.         Display.setVSyncEnabled(VSYNC); //whether hardware VSync is enabled
  100.         Display.setFullscreen(FULLSCREEN); //whether fullscreen is enabled
  101.  
  102.         //create and show our display
  103.         Display.create();
  104.         glPointSize(20);
  105.         // Create our OpenGL context and initialize any resources
  106.         create();
  107.  
  108.         // Call this before running to set up our initial size
  109.         resize();
  110.  
  111.         init();
  112.         glMatrixMode(GL_PROJECTION);
  113.         glLoadIdentity(); // Resets any previous projection matrices
  114.         glOrtho(0, HEIGHT, WIDTH, 0, 1, -1);
  115.         glMatrixMode(GL_MODELVIEW);
  116.         glClear(GL_COLOR_BUFFER_BIT);
  117.  
  118.         running = true;
  119.         // While we're still running and the user hasn't closed the window...
  120.         while (running && !Display.isCloseRequested()) {
  121.             // If the game was resized, we need to update our projection
  122.             if (Display.wasResized())
  123.                 resize();
  124.  
  125.             // Render the game
  126.             render();
  127.  
  128.             // Flip the buffers and sync to 60 FPS
  129.             Display.update();
  130.             Display.sync(60);
  131.             generation++;
  132.             System.out.println("Generation:" + generation);
  133.             next_generation();
  134.         }
  135.  
  136.  
  137.         // Dispose any resources and destroy our window
  138.         dispose();
  139.         Display.destroy();
  140.        }
  141.  
  142.     // Exit our game loop and close the window
  143.     public void exit() {
  144.         running = false;
  145.     }
  146.  
  147.     // Called to setup our game and context
  148.     protected void create() {
  149.         // 2D games generally won't require depth testing
  150.         glDisable(GL_DEPTH_TEST);
  151.  
  152.         // Enable blending
  153.         glEnable(GL_BLEND);
  154.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  155.  
  156.         // Set clear to transparent black
  157.         glClearColor(0f, 0f, 0f, 0f);
  158.  
  159.         // ... initialize resources here ...
  160.     }
  161.  
  162.     // Called to render our game
  163.     protected void render() {
  164.         // Clear the screen
  165.         glClear(GL_COLOR_BUFFER_BIT);
  166.         glBegin(GL_POINTS);
  167.  
  168.  
  169.         for ( int i = 0; i < getDisplayWidth() ; i ++ )
  170.             for ( int j = 0; j < getDisplayHeight() ; j ++ )
  171.                 Population [ i ] [ j ].drawState (  1 + 6 * i,
  172.                         1 + 6 * j);
  173.         // ... render our game here ...
  174.     }
  175.  
  176.     // Called to resize our game
  177.     protected void resize() {
  178.         glViewport(0, 0, Display.getWidth(), Display.getHeight());
  179.         // ... update our projection matrices here ...
  180.     }
  181.  
  182.     // Called to destroy our game upon exiting
  183.     protected void dispose() {
  184.         // ... dispose of any textures, etc ...
  185.     }
  186. }
  187.  
  188.  
  189. import static org.lwjgl.opengl.GL11.*;
  190. import org.lwjgl.LWJGLException;
  191. import org.lwjgl.opengl.Display;
  192. import org.lwjgl.opengl.DisplayMode;
  193.  
  194. class Cell {
  195.     boolean currentGen, nextGen;
  196.     int numNeighbors = 0;
  197.     Cell neighbors [ ] = new Cell [ 8 ];
  198.     int counter = 1;
  199.  
  200.  
  201.     public void initialize ( ) {
  202.         currentGen =  Math.random() > .5 ;
  203.     }
  204.  
  205.     public void setNeighbor ( Cell n ) {
  206.         neighbors [ numNeighbors ] = n;
  207.         numNeighbors ++;
  208.     }
  209.  
  210.     public void computeNextGen ( ) {
  211.         int i;
  212.         int aliveNeighbors = 0;
  213.  
  214.         for ( i = 0 ; i < numNeighbors ; i ++ )
  215.             if ( neighbors [ i ].isAlive ( ) )
  216.                 aliveNeighbors ++;
  217.  
  218.         if ( currentGen )
  219.             if ( ( aliveNeighbors == 2 ) || ( aliveNeighbors == 3 ) )
  220.                 nextGen = true;
  221.             else
  222.                 nextGen = false;
  223.         else
  224.         if ( aliveNeighbors == 3 )
  225.             nextGen = true;
  226.         else
  227.             nextGen = false;
  228.     }
  229.  
  230.     public void updateGen ( ) {
  231.         currentGen = nextGen;
  232.         counter++;
  233.     }
  234.  
  235.     public boolean isAlive ( ) {
  236.         return currentGen;
  237.     }
  238.  
  239.     public void drawState ( int x, int y ) {
  240.         if ( currentGen )
  241.          glColor3f(0.0f, 1.0f, 0.0f);
  242.         else
  243.         glColor3f(0.0f, 0.0f, 0.0f);
  244.  
  245.         glVertex2f(x - (1/2) * gameOfLife.getDisplayWidth(),y- (1/2) * gameOfLife.getDisplayHeight());
  246.     }
  247.     public int getCounter ( ){
  248.         return counter;
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment