GameOfLife.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package eu.tankernn.gameoflife;
  2. import org.lwjgl.LWJGLException;
  3. import org.lwjgl.Sys;
  4. import org.lwjgl.input.Keyboard;
  5. import org.lwjgl.input.Mouse;
  6. import org.lwjgl.opengl.Display;
  7. import org.lwjgl.opengl.DisplayMode;
  8. import org.lwjgl.opengl.GL11;
  9. import eu.tankernn.gameEngine.util.NativesExporter;
  10. public class GameOfLife {
  11. private static long lastFrameTime;
  12. private static float delta;
  13. private static boolean justPressed;
  14. private static boolean[][] cells;
  15. private static int gridWidth = 50, gridHeight = 50;
  16. private static int displayWidth = 800, displayHeight = 600;
  17. private static int cellWidth = displayWidth / gridWidth, cellHeight = displayHeight / gridHeight;
  18. private static boolean paused = false;
  19. private static boolean enableGrid = true;
  20. private static boolean debug = true;
  21. private static void initDisplay() {
  22. NativesExporter.exportNatives();
  23. try {
  24. Display.setDisplayMode(new DisplayMode(displayWidth, displayHeight));
  25. Display.create();
  26. } catch (LWJGLException e) {
  27. e.printStackTrace();
  28. System.exit(1);
  29. }
  30. // init OpenGL
  31. GL11.glMatrixMode(GL11.GL_PROJECTION);
  32. GL11.glLoadIdentity();
  33. GL11.glOrtho(0, displayWidth, 0, displayHeight, 1, -1);
  34. GL11.glMatrixMode(GL11.GL_MODELVIEW);
  35. }
  36. public static void main(String[] args) {
  37. initDisplay();
  38. cells = new boolean[gridWidth][gridHeight];
  39. while (!Display.isCloseRequested()) {
  40. long currentFrameTime = getCurrentTime();
  41. delta += (currentFrameTime - lastFrameTime) / 1000f;
  42. lastFrameTime = currentFrameTime;
  43. if (!paused && delta > 0.01f) {
  44. cells = update();
  45. delta = 0;
  46. }
  47. processInput();
  48. dumpCells();
  49. Display.update();
  50. }
  51. Display.destroy();
  52. }
  53. private static void processInput() {
  54. if (Mouse.isButtonDown(0) || Mouse.isButtonDown(1)) {
  55. int x, y;
  56. x = (Mouse.getX()) / cellWidth;
  57. y = (Mouse.getY()) / cellHeight;
  58. if (debug)
  59. System.out.println("X: " + x + ", Y: " + y);
  60. cells[x][y] = Mouse.isButtonDown(0);
  61. }
  62. if (Keyboard.isKeyDown(Keyboard.KEY_P)) {
  63. if (!justPressed)
  64. paused = !paused;
  65. justPressed = true;
  66. } else if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
  67. if (!justPressed)
  68. cells = new boolean[gridWidth][gridHeight];
  69. justPressed = true;
  70. } else if (Keyboard.isKeyDown(Keyboard.KEY_G)) {
  71. if (!justPressed)
  72. enableGrid = !enableGrid;
  73. justPressed = true;
  74. } else {
  75. justPressed = false;
  76. }
  77. }
  78. private static void dumpCells() {
  79. // Clear the screen and depth buffer
  80. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  81. // set the color of the quads (R,G,B,A)
  82. GL11.glColor3f(0.0f, 1.0f, 0.0f);
  83. for (int y = 0; y < cells[0].length; y++) {
  84. for (int x = 0; x < cells.length; x++) {
  85. if (debug)
  86. System.out.print(cells[x][y] ? "X" : "-");
  87. if (cells[x][y]) {
  88. // draw quad
  89. GL11.glBegin(GL11.GL_QUADS);
  90. GL11.glVertex2f(cellWidth * x, cellHeight * y);
  91. GL11.glVertex2f(cellWidth * x + cellWidth, cellHeight * y);
  92. GL11.glVertex2f(cellWidth * x + cellWidth, cellHeight * y + cellHeight);
  93. GL11.glVertex2f(cellWidth * x, cellHeight * y + cellHeight);
  94. GL11.glEnd();
  95. }
  96. }
  97. if (debug)
  98. System.out.println();
  99. }
  100. if (enableGrid) {
  101. // set the color of the lines (R,G,B,A)
  102. GL11.glColor3f(1.0f, 1.0f, 1.0f);
  103. for (int y = 0; y <= gridHeight; y++) {
  104. GL11.glBegin(GL11.GL_LINES);
  105. GL11.glVertex2f(0, cellHeight * y);
  106. GL11.glVertex2f(displayWidth, cellHeight * y);
  107. GL11.glEnd();
  108. }
  109. for (int x = 0; x <= gridWidth; x++) {
  110. GL11.glBegin(GL11.GL_LINES);
  111. GL11.glVertex2f(cellWidth * x, 0);
  112. GL11.glVertex2f(cellWidth * x, displayHeight);
  113. GL11.glEnd();
  114. }
  115. }
  116. if (debug)
  117. System.out.println();
  118. }
  119. private static boolean[][] update() {
  120. boolean[][] newcells = new boolean[gridWidth][gridHeight];
  121. for (int x = 0; x < cells.length; x++)
  122. for (int y = 0; y < cells[0].length; y++) {
  123. int neighbours = 0;
  124. try {
  125. if (cells[x][y + 1])
  126. neighbours++;
  127. if (cells[x][y - 1])
  128. neighbours++;
  129. if (cells[x + 1][y + 1])
  130. neighbours++;
  131. if (cells[x + 1][y - 1])
  132. neighbours++;
  133. if (cells[x - 1][y + 1])
  134. neighbours++;
  135. if (cells[x - 1][y - 1])
  136. neighbours++;
  137. if (cells[x + 1][y])
  138. neighbours++;
  139. if (cells[x - 1][y])
  140. neighbours++;
  141. } catch (ArrayIndexOutOfBoundsException ex) {
  142. continue;
  143. }
  144. if (cells[x][y]) {
  145. if (neighbours <= 1 || neighbours >= 4) {
  146. newcells[x][y] = false;
  147. } else {
  148. newcells[x][y] = true;
  149. }
  150. } else {
  151. if (neighbours == 3) {
  152. newcells[x][y] = true;
  153. } else {
  154. newcells[x][y] = false;
  155. }
  156. }
  157. }
  158. return newcells;
  159. }
  160. private static long getCurrentTime() {
  161. return Sys.getTime() * 1000 / Sys.getTimerResolution();
  162. }
  163. }