Writing a game on LWJGL
3r3-31.
Creating a game on LWJGL # 1
3r33333.3r33333.
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. 3r33380. 3r33333.
To create your game you need the following:
3r33333.
3r3333. Ability to write Java code (minimum necessary: understanding of the work of conditions, cycles, strings, arrays, functions, classes) 3-33430.
3r3333. Development environment (I will use Eclipse)
3r3333. LWJGL library
3r3333. Video card that supports OpengGL version not lower than ???r33430.
3r3333. Leisure time
3r3333. The idea of
3r33432. 3r33333.
Who cares, I ask under the cat. 3r33333.
How to install Eclipse . 3r33333.
Download LWJGL ??? , 3D graphics library, add her in your project. 3r33333.
3r33333.
Download slick-util , a library that simplifies loading textures, add it to the project. 3r33333.
3r33333.
Step two, check 3r33280. 3r33333.
First of all, create the Main class in the main package, put the following code in it:
3r375. 3r33333.
3r33333.
If the line with input is not highlighted in red, then everything is OK (on the screen it is highlighted in yellow, since this library is not used in the code). 3r33333.
3r33333.
Step three, display 3—3–3280. 3r33333.
Fill the Main class with the following code:
The starting code is 3r33285.
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class Main {
static int width = 80? 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 ()) /?
Display.getDesktopDisplayMode (). GetHeight () - mainDisplayMode.
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);
}
}
3r33358.
3r33333.
3r3442. 3r3442. 3r33333.
3r33333.
So what happens here:
Display.
Display.setResizable (true);
Display.setVSyncEnabled (true);
Display.setDisplayMode (mainDisplayMode);
Display.create ();
3r33358.
3r33333.
3r33333.
In these lines:
1. sets the display position (in the center of the screen) 3r33333.
2. It is allowed to change its size
3. Vertical sync turns on
4. Set the DisplayMode in which the screen sizes and some other parameters
are stored.
5. in the last line the display is created and displayed on the screen. 3r33333.
3r33333.
static void update () {
if (Display.wasResized ()) {
width = Display.getWidth ();
height = Display.getHeight ();
}
Display.update ();
Display.sync (60);
if (Display.isCloseRequested ()) {
running = false;
}
}
3r33358.
3r33333.
3r33333.
In the update function, which is called in an infinite loop:
1. Check for resizing the
window.
2. Update display
3. In the Display.sync (60) line, the display synchronizes with 60 hertz (if you have a different screen refresh rate, for example, 5? 7? 12? 14? etc., set your frequency). 3r33333.
4. Verify that the window is closed
3r33333.
Notice how the program ends when the user clicks on the cross, the window’s close flag, Display.isCloseRequested () becomes true, and the running variable becomes false, and the next iteration will not execute the while (running) loop and go to the Display lines .destroy (), which will destroy the display and System.exit (0), the call of which will end the program. 3r33333.
3r33333.
Step Four, OpenGL
3r33333. import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class Main {
static int width = 80? 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 ()) /?
Display.getDesktopDisplayMode (). GetHeight () - mainDisplayMode.
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);
}
}
3r33358.
3r33333.
3r3442. 3r3442. 3r33333.
3r33333.
So what happens here:
Display.
Display.setResizable (true);
Display.setVSyncEnabled (true);
Display.setDisplayMode (mainDisplayMode);
Display.create ();
3r33358.
3r33333.
3r33333.
In these lines:
1. sets the display position (in the center of the screen) 3r33333.
2. It is allowed to change its size
3. Vertical sync turns on
4. Set the DisplayMode in which the screen sizes and some other parameters
are stored.
5. in the last line the display is created and displayed on the screen. 3r33333.
3r33333.
static void update () {
if (Display.wasResized ()) {
width = Display.getWidth ();
height = Display.getHeight ();
}
Display.update ();
Display.sync (60);
if (Display.isCloseRequested ()) {
running = false;
}
}
3r33358.
3r33333.
3r33333.
In the update function, which is called in an infinite loop:
1. Check for resizing the
window.
2. Update display
3. In the Display.sync (60) line, the display synchronizes with 60 hertz (if you have a different screen refresh rate, for example, 5? 7? 12? 14? etc., set your frequency). 3r33333.
4. Verify that the window is closed
3r33333.
Notice how the program ends when the user clicks on the cross, the window’s close flag, Display.isCloseRequested () becomes true, and the running variable becomes false, and the next iteration will not execute the while (running) loop and go to the Display lines .destroy (), which will destroy the display and System.exit (0), the call of which will end the program. 3r33333.
3r33333.
Step Four, OpenGL
To initialize OpenGL to the main function before the while (running) loop, add the following code:
if (! GLContext.getCapabilities (). OpenGL33) {
System.err.println ("Your video card does not support OpenGL 3.3.");
System.exit (0);
}
3r33358.
3r33333.
This code checks if the video card supports OpenGL 3.? if the video card does not support this version, try updating the drivers or switching to another video card (if you have more than one). 3r33333.
3r33333.
So, the check is done, if everything is good, the video card supports OpenGL 3.? then we move on. After the verification code, add the following code before the loop:
glShadeModel (GL_SMOOTH);
glEnable (GL_TEXTURE_2D);
glEnable (GL_DEPTH_TEST);
glClearColor (1f, 0f, 0f, 1f);
3r33358.
3r33333.
This code will configure OpenGL as follows:
1. Turn on soft lighting in shaders
2. Turn on textures
3. Turn on depth checking (so that nearby objects overlap distant ones)
4. Set the cleaning color to
3r33333.
In the loop while (running), before calling the function update (), add the line
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3r33358.
3r33333.
3r33333.
Which will clear the color buffer and depth buffer every time the screen is refreshed. 3r33333.
3r33333.
The resulting code is
3r33333.
Click to view [/b]
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 = 80? 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 ()) /? (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 ("Your video card does not support 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);
}
}
3r33358.
3r33333.
3r3442. 3r3442. 3r33333.
3r33333.
This concludes the first article on writing a game on LWJGL, if the survey contains more than 50% positive answers, I will continue to write a series of articles. 3r3442.
3r33333. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () ();
3r3442.
Only registered users can participate in the survey. Enter , you are welcome. 3r33380.
3r33383.
3r33385.
Continue writing a series of articles? 3r33434.
3r3442.
3r33393.
3r33395.
3r33399.
3r3402.
3r33418.
3r33420.
3r33424. Yes
3r33427.
3r33430.
3r33418.
3r33420.
3r33424. No 3r33434.
3r33427.
3r33430.
3r33432.
3r33434.
3r3442.
2 users have voted. Abstained 2 users. 3r3442.
3r3442.
It may be interesting
This publication has no comments.
weber
Author28-10-2018, 12:38
Publication DateGame development / JavaScript
Category- Comments: 0
- Views: 273
Comments
this is really nice to read..informative post is very good to read..thanks a lot! How is the cost of house cleaning calculated?
It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.
entegrasyon programları
entegrasyon programları
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
entegrasyon programları
entegrasyon programları
I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work...먹튀
Lose Weight Market provides the best fitness tips, workout guides, keto recipes and diet plans, yoga workout routine and plans, healthy recipes, and more! Check Out: Lose Weight Market
Corvus Health provides medical training services as well as recruiting high quality health workers for you or placing our own best team in your facility. Check Out: Health Workforce Recruitment
Corvus Health provides medical training services as well as recruiting high quality health workers for you or placing our own best team in your facility. Check Out: Health Workforce Recruitment