Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created with IntelliJ IDEA.
- * User: Bobby
- * Date: 11/9/13
- * Time: 8:57 AM
- * To change this template use File | Settings | File Templates.
- */
- import static org.lwjgl.opengl.GL11.*;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- /**
- * A bare-bones implementation of a LWJGL application.
- * @author davedes
- */
- public class gameOfLife {
- // Whether to enable VSync in hardware.
- public static final boolean VSYNC = true;
- // Width and height of our window
- public static final int WIDTH = 800;
- public static final int HEIGHT = 600;
- public int generation;
- // Whether to use fullscreen mode
- public static final boolean FULLSCREEN = false;
- // Whether our game loop is running
- protected boolean running = false;
- public static void main(String[] args) throws LWJGLException {
- new gameOfLife().start();
- }
- public static int getDisplayHeight(){
- return HEIGHT;
- }
- public static int getDisplayWidth(){
- return WIDTH;
- }
- Cell Population [ ] [ ] = new Cell [ getDisplayWidth() ] [ getDisplayHeight() ];
- public void initialize(){
- int i, j;
- for ( i = 0 ; i < 20 ; i ++ )
- for ( j = 0 ; j < 20 ; j ++ )
- Population [ i ] [ j ].initialize ( );
- }
- void next_generation ( ) {
- int i, j;
- for ( i = 0; i < getDisplayWidth() ; i ++ )
- for ( j = 0; j < getDisplayHeight() ; j++ )
- Population [ i ] [ j ].computeNextGen ( );
- for ( i = 0; i < getDisplayWidth() ; i ++ )
- for ( j = 0; j < getDisplayHeight() ; j++ )
- Population [ i ] [ j ].updateGen ( );
- }
- public void init ( ) {
- int i, j, k, l;
- int rand;
- for ( i = 0 ; i < getDisplayWidth() ; i ++ )
- for ( j = 0 ; j < getDisplayHeight() ; j ++ )
- Population [ i ] [ j ] = new Cell ( );
- for ( i = 0 ; i < getDisplayWidth() ; i ++ )
- for ( j = 0 ; j < getDisplayHeight() ; j ++ ){
- for ( k = -1 ; k <= 1 ; k ++ )
- for ( l = -1 ; l <= 1 ; l ++ )
- if ( ( i + k >= 0 ) &&
- ( i + k < getDisplayWidth() ) &&
- ( j + l >= 0 ) &&
- ( j + l < getDisplayHeight() ) &&
- ( ( k != 0 ) || ( l != 0 ) ) ){
- Population [ i ] [ j ].setNeighbor (
- Population [ i + k] [ j + l ] );
- }
- }
- initialize ( );
- }
- // Start our game
- public void start() throws LWJGLException {
- // Set up our display
- Display.setTitle("Display example"); //title of our window
- Display.setResizable(true); //whether our window is resizable
- Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); //resolution of our display
- Display.setVSyncEnabled(VSYNC); //whether hardware VSync is enabled
- Display.setFullscreen(FULLSCREEN); //whether fullscreen is enabled
- //create and show our display
- Display.create();
- glPointSize(20);
- // Create our OpenGL context and initialize any resources
- create();
- // Call this before running to set up our initial size
- resize();
- init();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity(); // Resets any previous projection matrices
- glOrtho(0, HEIGHT, WIDTH, 0, 1, -1);
- glMatrixMode(GL_MODELVIEW);
- glClear(GL_COLOR_BUFFER_BIT);
- running = true;
- // While we're still running and the user hasn't closed the window...
- while (running && !Display.isCloseRequested()) {
- // If the game was resized, we need to update our projection
- if (Display.wasResized())
- resize();
- // Render the game
- render();
- // Flip the buffers and sync to 60 FPS
- Display.update();
- Display.sync(60);
- generation++;
- System.out.println("Generation:" + generation);
- next_generation();
- }
- // Dispose any resources and destroy our window
- dispose();
- Display.destroy();
- }
- // Exit our game loop and close the window
- public void exit() {
- running = false;
- }
- // Called to setup our game and context
- protected void create() {
- // 2D games generally won't require depth testing
- glDisable(GL_DEPTH_TEST);
- // Enable blending
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- // Set clear to transparent black
- glClearColor(0f, 0f, 0f, 0f);
- // ... initialize resources here ...
- }
- // Called to render our game
- protected void render() {
- // Clear the screen
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_POINTS);
- for ( int i = 0; i < getDisplayWidth() ; i ++ )
- for ( int j = 0; j < getDisplayHeight() ; j ++ )
- Population [ i ] [ j ].drawState ( 1 + 6 * i,
- 1 + 6 * j);
- // ... render our game here ...
- }
- // Called to resize our game
- protected void resize() {
- glViewport(0, 0, Display.getWidth(), Display.getHeight());
- // ... update our projection matrices here ...
- }
- // Called to destroy our game upon exiting
- protected void dispose() {
- // ... dispose of any textures, etc ...
- }
- }
- import static org.lwjgl.opengl.GL11.*;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.opengl.Display;
- import org.lwjgl.opengl.DisplayMode;
- class Cell {
- boolean currentGen, nextGen;
- int numNeighbors = 0;
- Cell neighbors [ ] = new Cell [ 8 ];
- int counter = 1;
- public void initialize ( ) {
- currentGen = Math.random() > .5 ;
- }
- public void setNeighbor ( Cell n ) {
- neighbors [ numNeighbors ] = n;
- numNeighbors ++;
- }
- public void computeNextGen ( ) {
- int i;
- int aliveNeighbors = 0;
- for ( i = 0 ; i < numNeighbors ; i ++ )
- if ( neighbors [ i ].isAlive ( ) )
- aliveNeighbors ++;
- if ( currentGen )
- if ( ( aliveNeighbors == 2 ) || ( aliveNeighbors == 3 ) )
- nextGen = true;
- else
- nextGen = false;
- else
- if ( aliveNeighbors == 3 )
- nextGen = true;
- else
- nextGen = false;
- }
- public void updateGen ( ) {
- currentGen = nextGen;
- counter++;
- }
- public boolean isAlive ( ) {
- return currentGen;
- }
- public void drawState ( int x, int y ) {
- if ( currentGen )
- glColor3f(0.0f, 1.0f, 0.0f);
- else
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex2f(x - (1/2) * gameOfLife.getDisplayWidth(),y- (1/2) * gameOfLife.getDisplayHeight());
- }
- public int getCounter ( ){
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment