Guest User

Untitled

a guest
Jul 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. package game;
  2. //
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6.  
  7. import org.lwjgl.input.Keyboard;
  8. import org.lwjgl.opengl.Display;
  9. import org.lwjgl.opengl.DisplayMode;
  10. import org.lwjgl.opengl.GL11;
  11. import org.lwjgl.util.Timer;
  12. import org.lwjgl.util.glu.GLU;
  13. import org.newdawn.slick.opengl.TextureLoader;
  14.  
  15. public class WarZone {
  16.  
  17. private boolean done = false;
  18. private String windowTitle = "War Zone";
  19. private DisplayMode displayMode;
  20. private Timer timer;
  21. private float dt;
  22.  
  23. public static void main(String[] args) {
  24. new WarZone().run(false);
  25. }
  26.  
  27. public void run(boolean fullscreen) {
  28. try {
  29. init();
  30. switchToOrtho();
  31. while (!done) {
  32. timer.tick();
  33. update();
  34. render();
  35. Display.update();
  36. }
  37. cleanup();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. System.exit(0);
  41. }
  42. }
  43.  
  44. private void update() {
  45. // Exit if Escape is pressed or window is closed
  46. if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) || Display.isCloseRequested()) {
  47. done = true;
  48. return;
  49. }
  50. }
  51.  
  52. private boolean render() {
  53. GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth buffer
  54. GL11.glLoadIdentity(); // Reset the current modelview matrix
  55.  
  56. int w = displayMode.getWidth();
  57. int h = displayMode.getHeight();
  58.  
  59. GL11.glColor3f(1, 0, 0);
  60. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  61. GL11.glBegin(GL11.GL_QUADS);
  62. GL11.glVertex2i(0, 0);
  63. GL11.glVertex2i(w, 0);
  64. GL11.glVertex2i(w, h);
  65. GL11.glVertex2i(0, h);
  66. GL11.glEnd();
  67. //if(true)return false;
  68. GL11.glColor3f(0, 1, 1);
  69. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
  70. GL11.glBegin(GL11.GL_QUADS);
  71. GL11.glTexCoord2f(0, 0);
  72. GL11.glVertex2i(1, 1);
  73. GL11.glTexCoord2f(1, 0);
  74. GL11.glVertex2i(w - 1, 1);
  75. GL11.glTexCoord2f(1, 1);
  76. GL11.glVertex2i(w - 1, h - 1);
  77. GL11.glTexCoord2f(0, 1);
  78. GL11.glVertex2i(1, h - 1);
  79. GL11.glEnd();
  80.  
  81. return true; // Rendered correctly
  82. }
  83.  
  84. public static void switchToOrtho() {
  85. GL11.glDisable(GL11.GL_DEPTH_TEST);
  86. GL11.glDisable(GL11.GL_LIGHTING);
  87. GL11.glMatrixMode(GL11.GL_PROJECTION);
  88. GL11.glPushMatrix();
  89. GL11.glLoadIdentity();
  90. GL11.glOrtho(0, Display.getDisplayMode().getWidth(), 0, Display.getDisplayMode().getHeight(), -1, 1);
  91. GL11.glMatrixMode(GL11.GL_MODELVIEW);
  92. GL11.glLoadIdentity();
  93. }
  94.  
  95. public static void switchToFrustum() {
  96. GL11.glEnable(GL11.GL_DEPTH_TEST);
  97. GL11.glEnable(GL11.GL_LIGHTING);
  98. GL11.glMatrixMode(GL11.GL_PROJECTION);
  99. GL11.glPopMatrix();
  100. GL11.glMatrixMode(GL11.GL_MODELVIEW);
  101. }
  102.  
  103. private void init() throws Exception {
  104. createWindow();
  105. initGL();
  106. load();
  107. }
  108.  
  109. private void load() throws FileNotFoundException, IOException {
  110. TextureLoader.getTexture("BMP", new FileInputStream("res/temp/Main_Menu_Play_Button.bmp"), true).getTextureID();
  111. }
  112.  
  113. private void createWindow() throws Exception {
  114. DisplayMode availibleDisplayModes[] = Display.getAvailableDisplayModes();
  115. for (DisplayMode d:availibleDisplayModes) {
  116. if (d.getWidth() == 640 && d.getHeight() == 480 && d.getBitsPerPixel() == 32) {
  117. displayMode = d;
  118. break;
  119. }
  120. }
  121. Display.setDisplayMode(displayMode);
  122. Display.setTitle(windowTitle);
  123. Display.create();
  124. }
  125.  
  126. private void initGL() {
  127. GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable texture mapping
  128. GL11.glShadeModel(GL11.GL_SMOOTH); // Enable smooth shading
  129. GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background
  130. GL11.glClearDepth(1.0f); // Depth buffer setup
  131. GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables depth testing
  132. GL11.glDepthFunc(GL11.GL_LEQUAL); // Type of depth testing
  133.  
  134. GL11.glMatrixMode(GL11.GL_PROJECTION); // Select projection matrix
  135. GL11.glLoadIdentity(); // Reset the projection matrix
  136.  
  137. // Calculate the aspect ratio of the window
  138. GLU.gluPerspective(45.0f, (float)displayMode.getWidth() / (float)displayMode.getHeight(), 0.1f, 100.0f);
  139. GL11.glMatrixMode(GL11.GL_MODELVIEW);// Select the modelview matrix
  140. GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);// Most precise perspective calculations
  141. }
  142.  
  143. public void requestFinish() {
  144. done = true;
  145. }
  146.  
  147. private void cleanup() {
  148. Display.destroy();
  149. }
  150.  
  151. }
  152.  
  153. GL11.glColor3f(0, 1, 1);
  154. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
  155. GL11.glBegin(GL11.GL_QUADS);
  156. GL11.glTexCoord2f(0, 0);
  157. GL11.glVertex2i(1, 1);
  158. GL11.glTexCoord2f(1, 0);
  159. GL11.glVertex2i(w - 1, 1);
  160. GL11.glTexCoord2f(1, 1);
  161. GL11.glVertex2i(w - 1, h - 1);
  162. GL11.glTexCoord2f(0, 1);
  163. GL11.glVertex2i(1, h - 1);
  164. GL11.glEnd();
Add Comment
Please, Sign In to add comment