Writing a game is a complex and interesting process that takes a lot of time. In this series of articles I will explain how to create a simple 3D Java game using the LWJGL library.
To create your game you need the following:
import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; public class Main { static int width = 800, height = 600; static DisplayMode mainDisplayMode = new DisplayMode(width, height); static boolean running = true; static void update() { if (Display.wasResized()) { width = Display.getWidth(); height = Display.getHeight(); } Display.update(); Display.sync(60); if (Display.isCloseRequested()) { running = false; } } public static void main(String[] args) { try { Display.setLocation( (Display.getDesktopDisplayMode().getWidth() - mainDisplayMode.getWidth()) / 2, Display.getDesktopDisplayMode().getHeight() - mainDisplayMode.getHeight()) / 2); Display.setResizable(true); Display.setVSyncEnabled(true); Display.setDisplayMode(mainDisplayMode); Display.create(); } catch (LWJGLException ex) { System.err.println(ex.getStackTrace().toString()); Display.destroy(); System.exit(1); } while (running) { update(); } Display.destroy(); System.exit(0); } }
Display.setLocation( (Display.getDesktopDisplayMode().getWidth() - mainDisplayMode.getWidth()) / 2, (Display.getDesktopDisplayMode().getHeight() - mainDisplayMode.getHeight()) / 2); Display.setResizable(true); Display.setVSyncEnabled(true); Display.setDisplayMode(mainDisplayMode); Display.create();
static void update() { if (Display.wasResized()) { width = Display.getWidth(); height = Display.getHeight(); } Display.update(); Display.sync(60); if (Display.isCloseRequested()) { running = false; } }
if (!GLContext.getCapabilities().OpenGL33) { System.err.println(" OpenGL 3.3."); System.exit(0); }
glShadeModel(GL_SMOOTH); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glClearColor(1f, 0f, 0f, 1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; import static org.lwjgl.opengl.GL11.GL_SMOOTH; import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; import static org.lwjgl.opengl.GL11.glClear; import static org.lwjgl.opengl.GL11.glClearColor; import static org.lwjgl.opengl.GL11.glEnable; import static org.lwjgl.opengl.GL11.glShadeModel; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GLContext; public class Main { static int width = 800, height = 600; static DisplayMode mainDisplayMode = new DisplayMode(width, height); static boolean running = true; static void update() { if (Display.wasResized()) { width = Display.getWidth(); height = Display.getHeight(); } Display.update(); Display.sync(60); if (Display.isCloseRequested()) { running = false; } } public static void main(String[] args) { try { Display.setLocation((Display.getDesktopDisplayMode().getWidth() - mainDisplayMode.getWidth()) / 2, (Display.getDesktopDisplayMode().getHeight() - mainDisplayMode.getHeight()) / 2); Display.setResizable(true); Display.setVSyncEnabled(true); Display.setDisplayMode(mainDisplayMode); Display.create(); } catch (LWJGLException ex) { System.err.println(ex.getStackTrace().toString()); Display.destroy(); System.exit(1); } if (!GLContext.getCapabilities().OpenGL33) { System.err.println(" OpenGL 3.3."); System.exit(0); } glShadeModel(GL_SMOOTH); glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glClearColor(1f, 0f, 0f, 1f); while (running) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); update(); } Display.destroy(); System.exit(0); } }
Source: https://habr.com/ru/post/427979/
All Articles