Tweaks
This commit is contained in:
parent
f6456e9a4c
commit
6a52c16dd5
|
@ -144,134 +144,129 @@ thing finished on time and under budget!</i></p>
|
||||||
write a fullscreen Java game using LWJGL. Because we're games programmers, we
|
write a fullscreen Java game using LWJGL. Because we're games programmers, we
|
||||||
don't want to do a Hello World as we'd probably rather shoot it. But before we
|
don't want to do a Hello World as we'd probably rather shoot it. But before we
|
||||||
can make bullets we must pay homage to the <i>rotating square!</i></p>
|
can make bullets we must pay homage to the <i>rotating square!</i></p>
|
||||||
<p style="background-color: #FFFFCC; border-style: solid; border-width: 1; padding: 3"><code>public
|
<p style="background-color: #FFFFCC; border-style: solid; border-width: 1; padding: 3"><code>import
|
||||||
|
org.lwjgl.*;<br>
|
||||||
|
import org.lwjgl.opengl.*;<br>
|
||||||
|
import org.lwjgl.input.*;<br>
|
||||||
|
<br>
|
||||||
|
<br>
|
||||||
|
public
|
||||||
final class Game {<br>
|
final class Game {<br>
|
||||||
static {<br>
|
static {<br>
|
||||||
try {<br>
|
try {<br>
|
||||||
DisplayMode[] modes = Display.getAvailableDisplayModes();<br>
|
DisplayMode[] modes = Display.getAvailableDisplayModes();<br>
|
||||||
System.out.println("Available display modes:");<br>
|
System.out.println("Available display modes:");<br>
|
||||||
for (int i = 0; i < modes.length; i ++)<br>
|
for (int i = 0; i < modes.length; i ++)<br>
|
||||||
|
|
||||||
System.out.println(modes[i]);<br>
|
System.out.println(modes[i]);<br>
|
||||||
// For now let's just pick a mode we're certain to have<br>
|
// For now let's just pick a mode we're certain to have<br>
|
||||||
Display.create(new DisplayMode(640, 480, 16, 60), true);<br>
|
Display.create(new DisplayMode(640, 480, 16, 60), true);<br>
|
||||||
System.out.println("Created display.");<br>
|
System.out.println("Created display.");<br>
|
||||||
} catch (Exception e) {<br>
|
} catch (Exception e) {<br>
|
||||||
System.err.println("Failed to create display due to "+e);<br>
|
System.err.println("Failed to create display due to "+e);<br>
|
||||||
System.exit(1);<br>
|
System.exit(1);<br>
|
||||||
}<br>
|
}<br>
|
||||||
}<br>
|
}<br>
|
||||||
public static final GL gl = new GL(16, 0, 16, 8);<br>
|
<br>
|
||||||
static {<br>
|
public static final GL gl = new GL(16, 0, 16, 8);<br>
|
||||||
try {<br>
|
static {<br>
|
||||||
gl.create();<br>
|
try {<br>
|
||||||
System.out.println("Created OpenGL.");<br>
|
gl.create();<br>
|
||||||
} catch (Exception e) {<br>
|
System.out.println("Created OpenGL.");<br>
|
||||||
System.err.println("Failed to create OpenGL due to "+e);<br>
|
} catch (Exception e) {<br>
|
||||||
System.exit(1);<br>
|
System.err.println("Failed to create OpenGL due to "+e);<br>
|
||||||
}<br>
|
System.exit(1);<br>
|
||||||
}<br>
|
}<br>
|
||||||
/** A handy number formatter for use displaying FPS and the like */<br>
|
}<br>
|
||||||
public static NumberFormat fmt = new DecimalFormat();<br>
|
<br>
|
||||||
static {<br>
|
/** Is the game finished? */<br>
|
||||||
fmt.setMaximumFractionDigits(1);<br>
|
private static boolean finished;<br>
|
||||||
fmt.setMinimumFractionDigits(1);<br>
|
<br>
|
||||||
fmt.setGroupingUsed(false);<br>
|
/** A rotating square! */<br>
|
||||||
}<br>
|
private static float angle;<br>
|
||||||
/** Is the game finished? */<br>
|
<br>
|
||||||
private static boolean finished;<br>
|
/**<br>
|
||||||
/** A rotating square! */<br>
|
* No construction allowed<br>
|
||||||
private static float angle;<br>
|
*/<br>
|
||||||
/**<br>
|
private Game() {<br>
|
||||||
* No construction allowed<br>
|
}<br>
|
||||||
*/<br>
|
<br>
|
||||||
private Game() {<br>
|
public static void main(String[] arguments) {<br>
|
||||||
}<br>
|
try {<br>
|
||||||
public static void main(String[] arguments) {<br>
|
init();<br>
|
||||||
try {<br>
|
while (!finished) {<br>
|
||||||
init();<br>
|
|
||||||
int frames = 0;<br>
|
|
||||||
float fps = 0.0f;<br>
|
|
||||||
Timer timer = new Timer();<br>
|
|
||||||
timer.reset();<br>
|
|
||||||
timer.resume();<br>
|
|
||||||
while (!finished) {<br>
|
|
||||||
AbsMouse.poll();<br>
|
|
||||||
Keyboard.poll();<br>
|
Keyboard.poll();<br>
|
||||||
Keyboard.read();<br>
|
|
||||||
gl.clear(GL.COLOR_BUFFER_BIT);<br>
|
|
||||||
mainLoop();<br>
|
mainLoop();<br>
|
||||||
|
|
||||||
render();<br>
|
render();<br>
|
||||||
|
|
||||||
gl.swapBuffers();<br>
|
gl.swapBuffers();<br>
|
||||||
// Count the frames per second. Do with this what you will..<br>
|
}</code> <code><br>
|
||||||
frames++;<br>
|
} catch (Throwable t) {<br>
|
||||||
float time = timer.getTime();<br>
|
|
||||||
if (time >= 1.0f) {<br>
|
|
||||||
fps = (int) (frames / time);<br>
|
|
||||||
timer.reset();<br>
|
|
||||||
frames = 0;<br>
|
|
||||||
}<br>
|
|
||||||
}<br>
|
|
||||||
} catch (Throwable t) {<br>
|
|
||||||
t.printStackTrace();<br>
|
t.printStackTrace();<br>
|
||||||
} finally {<br>
|
} finally {<br>
|
||||||
cleanup();<br>
|
cleanup();<br>
|
||||||
}<br>
|
}<br>
|
||||||
}<br>
|
}<br>
|
||||||
<br>
|
<br>
|
||||||
/**<br>
|
/**<br>
|
||||||
* All calculations are done in here<br>
|
* All calculations are done in here<br>
|
||||||
*/<br>
|
*/<br>
|
||||||
private static void mainLoop() {<br>
|
private static void mainLoop() {<br>
|
||||||
angle += 1f;<br>
|
angle += 1f;<br>
|
||||||
if (angle > 360.0f)<br>
|
if (angle > 360.0f)<br>
|
||||||
angle = 0.0f;<br>
|
angle = 0.0f;<br>
|
||||||
<br>
|
<br>
|
||||||
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))<br>
|
Keyboard.poll();
|
||||||
finished = true;<br>
|
|
||||||
}<br>
|
|
||||||
<br>
|
<br>
|
||||||
/**<br>
|
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))<br>
|
||||||
* All rendering is done in here<br>
|
finished = true;<br>
|
||||||
*/<br>
|
}<br>
|
||||||
private static void render() {<br>
|
|
||||||
<br>
|
<br>
|
||||||
gl.pushMatrix();<br>
|
/**<br>
|
||||||
gl.translatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);<br>
|
* All rendering is done in here<br>
|
||||||
gl.rotatef(angle, 0, 0, 1.0f);<br>
|
*/<br>
|
||||||
gl.begin(GL.QUADS);<br>
|
private static void render() {<br>
|
||||||
gl.vertex2i(-50, -50);<br>
|
gl.clear(GL.COLOR_BUFFER_BIT);<br>
|
||||||
gl.vertex2i(50, -50);<br>
|
gl.pushMatrix();<br>
|
||||||
gl.vertex2i(50, 50);<br>
|
gl.translatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);<br>
|
||||||
gl.vertex2i(-50, 50);<br>
|
gl.rotatef(angle, 0, 0, 1.0f);<br>
|
||||||
gl.end();<br>
|
gl.begin(GL.QUADS);<br>
|
||||||
gl.popMatrix();<br>
|
gl.vertex2i(-50, -50);<br>
|
||||||
}<br>
|
gl.vertex2i(50, -50);<br>
|
||||||
/**<br>
|
gl.vertex2i(50, 50);<br>
|
||||||
* Initialize<br>
|
gl.vertex2i(-50, 50);<br>
|
||||||
*/<br>
|
gl.end();<br>
|
||||||
private static void init() throws Exception {<br>
|
gl.popMatrix();<br>
|
||||||
Keyboard.create();<br>
|
}<br>
|
||||||
Keyboard.enableBuffer();<br>
|
<br>
|
||||||
Mouse.create();<br>
|
/**<br>
|
||||||
// Go into orthographic projection mode.<br>
|
* Initialize<br>
|
||||||
gl.matrixMode(GL.PROJECTION);<br>
|
*/<br>
|
||||||
gl.loadIdentity();<br>
|
private static void init() throws Exception {<br>
|
||||||
gl.glu.ortho2D(0, Display.getWidth(), 0, Display.getHeight());<br>
|
Keyboard.create();<br>
|
||||||
gl.matrixMode(GL.MODELVIEW);<br>
|
// Go into orthographic projection mode.<br>
|
||||||
gl.loadIdentity();<br>
|
gl.matrixMode(GL.PROJECTION);<br>
|
||||||
gl.viewport(0, 0, Display.getWidth(), Display.getHeight());<br>
|
gl.loadIdentity();<br>
|
||||||
// Fix the refresh rate to the display frequency.<br>
|
glu.ortho2D(0, Display.getWidth(), 0, Display.getHeight());<br>
|
||||||
gl.wglSwapIntervalEXT(1);<br>
|
gl.matrixMode(GL.MODELVIEW);<br>
|
||||||
}<br>
|
gl.loadIdentity();<br>
|
||||||
/**<br>
|
gl.viewport(0, 0, Display.getWidth(), Display.getHeight());<br>
|
||||||
* Cleanup<br>
|
// Fix the refresh rate to the display frequency.<br>
|
||||||
*/<br>
|
gl.wglSwapIntervalEXT(1);<br>
|
||||||
private static void cleanup() {<br>
|
}<br>
|
||||||
Keyboard.destroy();<br>
|
<br>
|
||||||
Mouse.destroy();<br>
|
/**<br>
|
||||||
gl.destroy();<br>
|
* Cleanup<br>
|
||||||
Display.destroy();<br>
|
*/<br>
|
||||||
}<br>
|
private static void cleanup() {<br>
|
||||||
|
Keyboard.destroy();<br>
|
||||||
|
</code> <code>gl.destroy();<br>
|
||||||
|
Display.destroy();<br>
|
||||||
|
}<br>
|
||||||
}</code></p>
|
}</code></p>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in New Issue