Skip to content

Commit 9b120dc

Browse files
committed
Example LWJGL project.
1 parent 24d47c4 commit 9b120dc

9 files changed

Lines changed: 227 additions & 117 deletions

File tree

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<project name="SampleJavaProject" default="jar"
2+
<project name="SampleLwjglProject" default="jar"
33
xmlns:ivy="antlib:org.apache.ivy.ant">
44

55
<!-- Project-specific configuration -->

ivy.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
<?xml version="1.0"?>
2-
<ivy-module version="2.0">
2+
<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
33
<info organisation="com.nullprogram" module="sample-java-project"/>
44
<configurations>
55
<conf name="default"/>
66
<conf name="build" extends="default" visibility="private"/>
77
<conf name="test" extends="build" visibility="private"/>
88
</configurations>
99
<dependencies>
10+
<dependency org="org.lwjgl.lwjgl" name="lwjgl" rev="2.8.2"
11+
conf="default"/>
12+
<dependency org="org.lwjgl.lwjgl" name="lwjgl-platform" rev="2.8.2"
13+
conf="default">
14+
<artifact name="lwjgl-platform" e:classifier="natives-linux"/>
15+
<artifact name="lwjgl-platform" e:classifier="natives-windows"/>
16+
<artifact name="lwjgl-platform" e:classifier="natives-osx"/>
17+
</dependency>
18+
<dependency org="net.java.jinput" name="jinput-platform" rev="2.0.5"
19+
conf="default">
20+
<artifact name="jinput-platform" e:classifier="natives-linux"/>
21+
<artifact name="jinput-platform" e:classifier="natives-windows"/>
22+
<artifact name="jinput-platform" e:classifier="natives-osx"/>
23+
</dependency>
24+
<dependency org="com.nullprogram" name="native-guide" rev="0.2"
25+
conf="default"/>
1026
<dependency org="commons-cli" name="commons-cli" rev="1.2"
1127
conf="default"/>
1228
<dependency org="junit" name="junit" rev="4.10"

project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Project information
2-
artifactId = sample-java-project
2+
artifactId = sample-lwjgl-project
33
description = An Ant-based sample Java project.
44
version = 1.0-SNAPSHOT
55
package.main = sample.java.project
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.nullprogram.lwjgl;
2+
3+
import com.nullprogram.guide.Arch;
4+
import static com.nullprogram.guide.NativeGuide.prepare;
5+
6+
/**
7+
* Prepares all of the LWJGL native binaries for loading.
8+
*/
9+
public final class Lwjgl {
10+
11+
/** Hidden constructor. */
12+
private Lwjgl() {
13+
}
14+
15+
/**
16+
* Prepares all of the LWJGL native libraries for loading.
17+
* @throws java.io.IOException when the libraries could not be loaded
18+
*/
19+
public static void setup() throws java.io.IOException {
20+
prepare(Arch.LINUX_64, "/libjinput-linux64.so");
21+
prepare(Arch.LINUX_32, "/libjinput-linux.so");
22+
prepare(Arch.LINUX_64, "/liblwjgl64.so");
23+
prepare(Arch.LINUX_32, "/liblwjgl.so");
24+
prepare(Arch.LINUX_64, "/libopenal64.so");
25+
prepare(Arch.LINUX_32, "/libopenal.so");
26+
prepare(Arch.MAC_64, "/libjinput-osx.jnilib");
27+
prepare(Arch.MAC_64, "/liblwjgl.jnilib");
28+
prepare(Arch.MAC_64, "/openal.dylib");
29+
prepare(Arch.WINDOWS_64, "/jinput-dx8_64.dll");
30+
prepare(Arch.WINDOWS_32, "/jinput-dx8.dll");
31+
prepare(Arch.WINDOWS_64, "/jinput-raw_64.dll");
32+
prepare(Arch.WINDOWS_32, "/jinput-raw.dll");
33+
prepare(Arch.WINDOWS_64, "/lwjgl64.dll");
34+
prepare(Arch.WINDOWS_32, "/lwjgl.dll");
35+
prepare(Arch.WINDOWS_32, "/OpenAL32.dll");
36+
prepare(Arch.WINDOWS_64, "/OpenAL64.dll");
37+
}
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Just a LWJGL native library loader, using NativeGuide.
3+
*/
4+
package com.nullprogram.lwjgl;

src/sample/java/project/SampleJavaProject.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package sample.java.project;
2+
3+
import lombok.extern.java.Log;
4+
import org.lwjgl.LWJGLException;
5+
import org.lwjgl.opengl.Display;
6+
import org.lwjgl.opengl.DisplayMode;
7+
import org.lwjgl.opengl.GL11;
8+
9+
/**
10+
* The main class.
11+
*
12+
* This is the main class of the application. It contains the main()
13+
* method, the first method called.
14+
*/
15+
@Log
16+
public class SampleLwjglProject implements Runnable {
17+
18+
/** The delay between frames. */
19+
private static final int FPS = 30;
20+
21+
/** Width of the display. */
22+
private static final int WIDTH = 800;
23+
24+
/** Height of the display. */
25+
private static final int HEIGHT = 600;
26+
27+
/** The length of one second in milliseconds. */
28+
private static final double SECOND = 1000d;
29+
30+
/** The size of the quad. */
31+
private static final float QUAD_SIZE = 200;
32+
33+
/** Rate of red oscillation. */
34+
private static final float RED_RATE = 1f;
35+
36+
/** Rate of green oscillation. */
37+
private static final float GREEN_RATE = 2.2f;
38+
39+
/** Rate of blue oscillation. */
40+
private static final float BLUE_RATE = 0.125f;
41+
42+
/** Rate of rotation oscillation. */
43+
private static final float ROT_RATE = 4f;
44+
45+
/** Range of rotation oscillation. */
46+
private static final float ROT_RANGE = 12;
47+
48+
/** Height division for quad. */
49+
private static final float HDIV = 4;
50+
51+
/** Width division for quad. */
52+
private static final float WDIV = 2;
53+
54+
/**
55+
* The main class.
56+
*
57+
* Print the "Hello, world!" string.
58+
*
59+
* @param args application input arguments
60+
*/
61+
public static void main(final String[] args) {
62+
try {
63+
com.nullprogram.lwjgl.Lwjgl.setup();
64+
} catch (java.io.IOException e) {
65+
log.severe("could not prepare libraries: " + e);
66+
System.exit(0);
67+
}
68+
new SampleLwjglProject().run();
69+
}
70+
71+
@Override
72+
public final void run() {
73+
try {
74+
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
75+
Display.create();
76+
init();
77+
} catch (LWJGLException e) {
78+
log.severe("could not prepare display: " + e);
79+
return;
80+
}
81+
82+
while (!Display.isCloseRequested()) {
83+
repaint();
84+
Display.update();
85+
Display.sync(FPS);
86+
}
87+
Display.destroy();
88+
}
89+
90+
/**
91+
* Initial display configuration.
92+
*/
93+
private void init() {
94+
GL11.glMatrixMode(GL11.GL_PROJECTION);
95+
GL11.glLoadIdentity();
96+
GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
97+
GL11.glMatrixMode(GL11.GL_MODELVIEW);
98+
}
99+
100+
/**
101+
* Draw the OpenGL display.
102+
*/
103+
private void repaint() {
104+
double time = System.currentTimeMillis() / SECOND;
105+
106+
/* Clear the screen and depth buffer */
107+
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
108+
109+
/* set the color of the quad (R,G,B,A) */
110+
float red = (float) Math.abs(Math.sin(time * RED_RATE));
111+
float green = (float) Math.abs(Math.cos(time * GREEN_RATE));
112+
float blue = (float) Math.abs(Math.tan(time * BLUE_RATE));
113+
GL11.glColor3f(red, green, blue);
114+
115+
/* draw quad */
116+
GL11.glPushMatrix();
117+
float r = (float) (Math.sin(time * ROT_RATE) * ROT_RANGE + ROT_RANGE);
118+
GL11.glRotatef(r, 0f, 0f, 1f);
119+
120+
GL11.glBegin(GL11.GL_QUADS);
121+
GL11.glVertex2f(WIDTH / WDIV, HEIGHT / HDIV);
122+
GL11.glVertex2f(WIDTH / WDIV + QUAD_SIZE, HEIGHT / HDIV);
123+
GL11.glVertex2f(WIDTH / WDIV + QUAD_SIZE, HEIGHT / HDIV + QUAD_SIZE);
124+
GL11.glVertex2f(WIDTH / WDIV, HEIGHT / HDIV + QUAD_SIZE);
125+
GL11.glEnd();
126+
127+
GL11.glPopMatrix();
128+
}
129+
}

test/sample/java/project/SampleJavaProjectTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sample.java.project;
2+
3+
import junit.framework.TestCase;
4+
import org.lwjgl.LWJGLException;
5+
import org.lwjgl.opengl.Display;
6+
7+
/**
8+
* A sample JUnit test.
9+
*
10+
* This test exists as a placeholder for the test unit framework.
11+
*/
12+
public class SampleLWJGLTest extends TestCase {
13+
14+
/**
15+
* JUnit set up method.
16+
*/
17+
public final void setUp() {
18+
try {
19+
com.nullprogram.lwjgl.Lwjgl.setup();
20+
} catch (java.io.IOException e) {
21+
fail("Failed to load native libraries.");
22+
}
23+
}
24+
25+
/**
26+
* Tests the add() method in the main class.
27+
*/
28+
public final void testDisplay() {
29+
try {
30+
Display.create();
31+
Display.update();
32+
Display.destroy();
33+
} catch (LWJGLException e) {
34+
fail("" + e);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)