mirror of
https://github.com/shadowfacts/lwjgl2-arm64.git
synced 2026-04-05 14:35:58 +00:00
Implemented Pbuffers on win32 and linux (render to texture will be done later)
Implemented native cursor caps (one bit transparancy, alpha translucency and animations)
This commit is contained in:
parent
586c0f4647
commit
9bcaa18c3d
22 changed files with 1262 additions and 201 deletions
|
|
@ -71,15 +71,15 @@ public abstract class Window {
|
|||
*
|
||||
* Only one Window can be created() at a time; to create another Window you must
|
||||
* first destroy() the first window.
|
||||
*
|
||||
* The dimensions may be ignored if the window cannot be made non-
|
||||
* fullscreen. The position may be ignored in either case.
|
||||
*
|
||||
* The dimensions may be ignored if the window cannot be made non-
|
||||
* fullscreen. The position may be ignored in either case.
|
||||
*
|
||||
* @param title The window's title
|
||||
* @param x Position on x axis of top left corner of window.
|
||||
* @param y Position on y axis of top left corner of window.
|
||||
* @param width Width of window
|
||||
* @param height Height of window
|
||||
* @param y Position on y axis of top left corner of window.
|
||||
* @param width Width of window
|
||||
* @param height Height of window
|
||||
* @throws RuntimeException if you attempt to create more than one window at the same time
|
||||
*/
|
||||
protected Window(String title, int x, int y, int width, int height) {
|
||||
|
|
|
|||
|
|
@ -64,12 +64,14 @@ public class Cursor {
|
|||
* @param height cursor image height
|
||||
* @param xHotspot the x coordinate of the cursor hotspot
|
||||
* @param yHotspot the y coordinate of the cursor hotspot
|
||||
* @param numImages number of cursor images specified. Must be 1 if animations are not supported.
|
||||
* @param cursorAddress the address of an int array containing the cursor image
|
||||
* @param delayAddresses the address of animation frame delays, if numImages is greater than 1, else Sys.NULL
|
||||
* @throws Exception if the cursor could not be created for any reason
|
||||
*/
|
||||
public Cursor(int width, int height, int xHotspot, int yHotspot, int imageAddress) throws Exception {
|
||||
public Cursor(int width, int height, int xHotspot, int yHotspot, int numImages, int imageAddress, int delayAddresses) throws Exception {
|
||||
assert Mouse.isCreated();
|
||||
nativeHandle = nCreateCursor(width, height, xHotspot, yHotspot, imageAddress);
|
||||
nativeHandle = nCreateCursor(width, height, xHotspot, yHotspot, numImages, imageAddress, delayAddresses);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -89,7 +91,7 @@ public class Cursor {
|
|||
/**
|
||||
* Native method to create a native cursor
|
||||
*/
|
||||
private static native int nCreateCursor(int width, int height, int xHotspot, int yHotspot, int imageAddresses);
|
||||
private static native int nCreateCursor(int width, int height, int xHotspot, int yHotspot, int numImages, int imageAddresses, int delayAddresses);
|
||||
|
||||
/**
|
||||
* Native method to destroy a native cursor
|
||||
|
|
|
|||
|
|
@ -48,6 +48,9 @@ import org.lwjgl.*;
|
|||
* @version $Revision$
|
||||
*/
|
||||
public class Mouse {
|
||||
public final static int CURSOR_ONE_BIT_TRANSPARANCY = 1;
|
||||
public final static int CURSOR_8_BIT_ALPHA = 2;
|
||||
public final static int CURSOR_ANIMATION = 4;
|
||||
|
||||
/** Has the mouse been created? */
|
||||
private static boolean created;
|
||||
|
|
@ -92,17 +95,21 @@ public class Mouse {
|
|||
}
|
||||
|
||||
/**
|
||||
* Check native cursor support
|
||||
* @return true if native cursors are supported
|
||||
* Get the capabilities of the native cursor. Return a bit mask of the native cursor capabilities.
|
||||
* The CURSOR_ONE_BIT_TRANSPARANCY indicates support for cursors with one bit transparancy,
|
||||
* the CURSOR_8_BIT_ALPHA indicates support for 8 bit alpha and CURSOR_ANIMATION indicates
|
||||
* support for cursor animations.
|
||||
*
|
||||
* @return A bit mask with native cursor capabilities.
|
||||
*/
|
||||
public static boolean isNativeCursorSupported() {
|
||||
return nIsNativeCursorSupported();
|
||||
public static int getNativeCursorCaps() {
|
||||
return nGetNativeCursorCaps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Native function to determine native cursor support
|
||||
*/
|
||||
private static native boolean nIsNativeCursorSupported();
|
||||
private static native int nGetNativeCursorCaps();
|
||||
|
||||
/**
|
||||
* Binds a native cursor. If the cursor argument is null, the
|
||||
|
|
@ -120,7 +127,7 @@ public class Mouse {
|
|||
* @throws Exception if the cursor could not be set for any reason
|
||||
*/
|
||||
public static Cursor setNativeCursor(Cursor cursor) throws Exception {
|
||||
assert created && isNativeCursorSupported();
|
||||
assert created && ((getNativeCursorCaps() | CURSOR_ONE_BIT_TRANSPARANCY) != 0);
|
||||
Cursor oldCursor = currentCursor;
|
||||
currentCursor = cursor;
|
||||
if (currentCursor != null) {
|
||||
|
|
@ -136,7 +143,8 @@ public class Mouse {
|
|||
|
||||
/**
|
||||
* Gets the minimum size of a native cursor. Can only be called if
|
||||
* The Mouse is created and isNativeCursorSupported() returns true
|
||||
* The Mouse is created and cursor caps includes at least
|
||||
* CURSOR_ONE_BIT_TRANSPARANCY.
|
||||
*
|
||||
* @return the maximum size of a native cursor
|
||||
*/
|
||||
|
|
@ -149,7 +157,8 @@ public class Mouse {
|
|||
|
||||
/**
|
||||
* Gets the maximum size of a native cursor. Can only be called if
|
||||
* The Mouse is created and isNativeCursorSupported() returns true
|
||||
* The Mouse is created and cursor caps includes at least
|
||||
* CURSOR_ONE_BIT_TRANSPARANCY.
|
||||
*
|
||||
* @return the maximum size of a native cursor
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -88,12 +88,12 @@ public class BaseGL extends Window {
|
|||
* support windowed mode, then the width and height must match the current
|
||||
* display resolution, or an Exception will be thrown. Otherwise a fullscreen
|
||||
* window will be created.
|
||||
*
|
||||
*
|
||||
* @param title The title of the window
|
||||
* @param x The position of the window on the x axis. May be ignored.
|
||||
* @param y The position of the window on the y axis. May be ignored.
|
||||
* @param y The position of the window on the y axis. May be ignored.
|
||||
* @param width The width of the window's client area
|
||||
* @param height The height of the window's client area
|
||||
* @param height The height of the window's client area
|
||||
* @param bpp Require colour bits
|
||||
* @param alpha Required alpha bits
|
||||
* @param depth Required depth bits
|
||||
|
|
|
|||
162
src/java/org/lwjgl/opengl/Pbuffer.java
Normal file
162
src/java/org/lwjgl/opengl/Pbuffer.java
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* Copyright (c) 2002 Lightweight Java Game Library Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.lwjgl.opengl;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.Window;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
*
|
||||
* Pbuffer encapsulates an OpenGL pbuffer.
|
||||
*
|
||||
* Each instance of GL is only valid in the thread that creates it.
|
||||
* In addition, only one instance of an OpenGL window or Pbuffer may be
|
||||
* the current GL context in any one thread. To make a GL instance the
|
||||
* current context, use makeCurrent().
|
||||
*
|
||||
* @author elias_naur <elias_naur@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class Pbuffer {
|
||||
public final static int PBUFFER_SUPPORTED = 1;
|
||||
|
||||
static {
|
||||
System.loadLibrary(Sys.getLibraryName());
|
||||
}
|
||||
|
||||
/** Handle to the native GL rendering context */
|
||||
protected final int handle;
|
||||
|
||||
/** Current Pbuffer */
|
||||
private static Pbuffer currentBuffer = null;
|
||||
|
||||
/**
|
||||
* Construct an instance of a Pbuffer. If this fails then an Exception will be thrown.
|
||||
* The buffer is single-buffered.
|
||||
*
|
||||
* NOTE: An OpenGL window must be created before a Pbuffer can be created. The Pbuffer will
|
||||
* have its own context that shares display lists and textures with the OpenGL window context,
|
||||
* but it will have its own OpenGL state. Therefore, state changes to a pbuffer will not be seen
|
||||
* in the window context and vice versa.
|
||||
*
|
||||
* NOTE: Some OpenGL implementations requires the shared contexts to use the same pixel format.
|
||||
* So if possible, use the same bpp, alpha, depth and stencil values used to create the main window.
|
||||
*
|
||||
* @param width Pbuffer width
|
||||
* @param height Pbuffer height
|
||||
* @param bpp Minimum bits per pixel
|
||||
* @param alpha Minimum bits per pixel in alpha buffer
|
||||
* @param depth Minimum bits per pixel in depth buffer
|
||||
* @param stencil Minimum bits per pixel in stencil buffer
|
||||
*/
|
||||
public Pbuffer(int width, int height, int bpp, int alpha, int depth, int stencil) throws Exception {
|
||||
handle = nCreate(width, height, bpp, alpha, depth, stencil);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to release the current Pbuffer context and make the OpenGL window current.
|
||||
*/
|
||||
public static void releaseContext() {
|
||||
currentBuffer = null;
|
||||
nReleaseContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to test for validity of the buffer. If this function returns true,
|
||||
* the buffer contents is lost. The buffer can still be used, but the results are undefined.
|
||||
* The application is expected to release the buffer if needed, destroy it and recreate a new
|
||||
* buffer.
|
||||
*
|
||||
* @return true if the buffer is lost and destroyed, false if the buffer is valid.
|
||||
*/
|
||||
public boolean isBufferLost() {
|
||||
return nIsBufferLost(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Native method to test for buffer integrity
|
||||
*/
|
||||
private native static boolean nIsBufferLost(int handle);
|
||||
|
||||
/**
|
||||
* Native method to release the context.
|
||||
*/
|
||||
private native static void nReleaseContext();
|
||||
|
||||
/**
|
||||
* Method to make the Pbuffer context current. All subsequent OpenGL
|
||||
* calls will go to this buffer.
|
||||
*/
|
||||
public void makeCurrent() {
|
||||
currentBuffer = this;
|
||||
nMakeCurrent(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Native method to make a pbuffer current.
|
||||
*/
|
||||
private native static void nMakeCurrent(int handle);
|
||||
|
||||
/**
|
||||
* Gets the Pbuffer capabilities. Only the flag PBUFFER_SUPPORTED is
|
||||
* available, and indicates that Pbuffers can be created.
|
||||
*
|
||||
* @return a bitmask of Pbuffer capabilities.
|
||||
*/
|
||||
public static native int getPbufferCaps();
|
||||
|
||||
/**
|
||||
* Native method to create a Pbuffer
|
||||
*/
|
||||
private native static int nCreate(
|
||||
int width,
|
||||
int height,
|
||||
int bpp,
|
||||
int alpha,
|
||||
int depth,
|
||||
int stencil) throws Exception;
|
||||
|
||||
/**
|
||||
* Destroys the Pbuffer. The buffer must not be current.
|
||||
*/
|
||||
public void destroy() {
|
||||
assert currentBuffer != this : "Pbuffers must not be current when releasing it";
|
||||
nDestroy(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Natively destroy any GL-related stuff
|
||||
*/
|
||||
private native static void nDestroy(int handle);
|
||||
}
|
||||
|
|
@ -104,19 +104,19 @@ public class HWCursorTest {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!Mouse.isNativeCursorSupported()) {
|
||||
if ((Mouse.getNativeCursorCaps() & Mouse.CURSOR_ONE_BIT_TRANSPARANCY) == 0) {
|
||||
System.out.println("No HW cursor support!");
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println("Maximum native cursor size: " + Mouse.getMaxCursorSize() + ", min size: " + Mouse.getMinCursorSize());
|
||||
mouse_x = mouse_y = 0;
|
||||
// int num_images = 3;
|
||||
int num_images = 3;
|
||||
int image_size = Mouse.getMaxCursorSize()*Mouse.getMaxCursorSize();
|
||||
IntBuffer cursor_images = ByteBuffer.allocateDirect(/*num_images**/image_size*4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
/* IntBuffer delays = ByteBuffer.allocateDirect(num_images*4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
IntBuffer cursor_images = ByteBuffer.allocateDirect(num_images*image_size*4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
IntBuffer delays = ByteBuffer.allocateDirect(num_images*4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
delays.put(0, 500);
|
||||
delays.put(1, 500);
|
||||
delays.put(2, 500);*/
|
||||
delays.put(2, 500);
|
||||
int color_scale = 255/Mouse.getMaxCursorSize();
|
||||
int bit_mask = 0x81000000;
|
||||
for (int j = 0; j < image_size; j++) {
|
||||
|
|
@ -125,16 +125,22 @@ public class HWCursorTest {
|
|||
int color = (j*color_scale/Mouse.getMaxCursorSize()) << 16;
|
||||
cursor_images.put(0*image_size + j, 0x00000020 | color | bit_mask);
|
||||
}
|
||||
/* for (int j = 0; j < image_size; j++) {
|
||||
for (int j = 0; j < image_size; j++) {
|
||||
if (j % 4 == 0)
|
||||
bit_mask = (~bit_mask) & 0x81000000;
|
||||
int color = (j*color_scale/Mouse.getMaxCursorSize()) << 8;
|
||||
cursor_images.put(1*image_size + j, 0x80000000 | color);
|
||||
cursor_images.put(1*image_size + j, 0x00000020 | color | bit_mask);
|
||||
}
|
||||
for (int j = 0; j < image_size; j++) {
|
||||
int color = j*color_scale/Mouse.getMaxCursorSize();
|
||||
cursor_images.put(2*image_size + j, 0x80000000 | color);
|
||||
}*/
|
||||
if (j % 4 == 0)
|
||||
bit_mask = (~bit_mask) & 0x81000000;
|
||||
int color = (j*color_scale/Mouse.getMaxCursorSize());
|
||||
cursor_images.put(2*image_size + j, 0x00000020 | color | bit_mask);
|
||||
}
|
||||
try {
|
||||
cursor = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2/*, num_images*/, Sys.getDirectBufferAddress(cursor_images)/*, Sys.getDirectBufferAddress(delays)*/);
|
||||
if ((Mouse.getNativeCursorCaps() | Mouse.CURSOR_ANIMATION) == 0)
|
||||
num_images = 1;
|
||||
cursor = new Cursor(Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize(), Mouse.getMaxCursorSize()/2, Mouse.getMaxCursorSize()/2, num_images, Sys.getDirectBufferAddress(cursor_images), Sys.getDirectBufferAddress(delays));
|
||||
Mouse.setNativeCursor(cursor);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
|||
427
src/java/org/lwjgl/test/opengl/PbufferTest.java
Normal file
427
src/java/org/lwjgl/test/opengl/PbufferTest.java
Normal file
|
|
@ -0,0 +1,427 @@
|
|||
/*
|
||||
* Copyright (c) 2003 Lightweight Java Game Library Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'Lightweight Java Game Library' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package org.lwjgl.test.opengl;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.input.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.vector.Vector2f;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
/**
|
||||
* $Id$
|
||||
*
|
||||
* Tests Pbuffers
|
||||
*
|
||||
* @author elias_naur <elias_naur@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class PbufferTest {
|
||||
|
||||
/** Intended deiplay mode */
|
||||
private DisplayMode mode;
|
||||
|
||||
/** GL instance */
|
||||
private GL gl;
|
||||
|
||||
/** GLU instance */
|
||||
private GLU glu;
|
||||
|
||||
/** our quad moving around */
|
||||
private Vector2f quadPosition;
|
||||
|
||||
/** our quadVelocity */
|
||||
private Vector2f quadVelocity;
|
||||
|
||||
/** angle of quad */
|
||||
private float angle;
|
||||
|
||||
/** degrees to rotate per frame */
|
||||
private float angleRotation = 1.0f;
|
||||
|
||||
/** Max speed of all changable attributes */
|
||||
private static final float MAX_SPEED = 20.0f;
|
||||
|
||||
/** Pbuffer instance */
|
||||
private static Pbuffer pbuffer;
|
||||
|
||||
/** The shared texture */
|
||||
private static int tex_handle;
|
||||
|
||||
/**
|
||||
* Executes the test
|
||||
*/
|
||||
public void execute() {
|
||||
initialize();
|
||||
|
||||
mainLoop();
|
||||
|
||||
cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the test
|
||||
*/
|
||||
private void initialize() {
|
||||
try {
|
||||
//find displaymode
|
||||
mode = findDisplayMode(800, 600, 16);
|
||||
|
||||
// start of in windowed mode
|
||||
gl = new GL("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0);
|
||||
// gl = new GL("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0);
|
||||
gl.create();
|
||||
glu = new GLU(gl);
|
||||
if ((Pbuffer.getPbufferCaps() & Pbuffer.PBUFFER_SUPPORTED) == 0) {
|
||||
System.out.println("No Pbuffer support!");
|
||||
System.exit(1);
|
||||
}
|
||||
System.out.println("Pbuffer support detected");
|
||||
|
||||
glInit();
|
||||
initPbuffer();
|
||||
|
||||
Keyboard.create();
|
||||
|
||||
quadPosition = new Vector2f(100f, 100f);
|
||||
quadVelocity = new Vector2f(1.0f, 1.0f);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the main loop of the "test"
|
||||
*/
|
||||
private void mainLoop() {
|
||||
while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)
|
||||
&& !gl.isCloseRequested()) {
|
||||
// allow subsystem to get a chance to run too
|
||||
gl.tick();
|
||||
|
||||
if (!gl.isMinimized()) {
|
||||
// check keyboard input
|
||||
processKeyboard();
|
||||
|
||||
// do "game" logic, and render it
|
||||
logic();
|
||||
render();
|
||||
|
||||
// paint window
|
||||
gl.paint();
|
||||
} else {
|
||||
|
||||
// no need to render/paint if nothing has changed (ie. window dragged over)
|
||||
if (gl.isDirty()) {
|
||||
render();
|
||||
gl.paint();
|
||||
}
|
||||
|
||||
// don't waste cpu time, sleep more
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException inte) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the logic
|
||||
*/
|
||||
private void logic() {
|
||||
angle += angleRotation;
|
||||
if (angle > 90.0f) {
|
||||
angle = 0.0f;
|
||||
}
|
||||
|
||||
quadPosition.x += quadVelocity.x;
|
||||
quadPosition.y += quadVelocity.y;
|
||||
|
||||
//check colision with vertical border border
|
||||
if (quadPosition.x + 50 >= mode.width || quadPosition.x - 50 <= 0) {
|
||||
quadVelocity.x *= -1;
|
||||
}
|
||||
|
||||
//check collision with horizontal border
|
||||
if (quadPosition.y + 50 >= mode.height || quadPosition.y - 50 <= 0) {
|
||||
quadVelocity.y *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void render() {
|
||||
if (pbuffer.isBufferLost()) {
|
||||
System.out.println("Buffer contents lost - will recreate the buffer");
|
||||
Pbuffer.releaseContext();
|
||||
pbuffer.destroy();
|
||||
initPbuffer();
|
||||
}
|
||||
pbuffer.makeCurrent();
|
||||
// Pbuffer rendering
|
||||
//clear background
|
||||
gl.clear(GL.COLOR_BUFFER_BIT);
|
||||
|
||||
// draw white quad
|
||||
gl.pushMatrix();
|
||||
{
|
||||
gl.translatef(quadPosition.x, quadPosition.y, 0);
|
||||
gl.rotated(angle, 0.0f, 0.0f, 1.0f);
|
||||
gl.color3f(1.0f, 1.0f, 1.0f);
|
||||
gl.begin(GL.QUADS);
|
||||
{
|
||||
gl.vertex2i(-50, -50);
|
||||
gl.vertex2i(50, -50);
|
||||
gl.vertex2i(50, 50);
|
||||
gl.vertex2i(-50, 50);
|
||||
}
|
||||
gl.end();
|
||||
}
|
||||
gl.popMatrix();
|
||||
gl.copyTexImage2D(GL.TEXTURE_2D, 0, GL.RGB, 0, 0, 256, 256, 0);
|
||||
Pbuffer.releaseContext();
|
||||
|
||||
// OpenGL window rendering
|
||||
gl.clear(GL.COLOR_BUFFER_BIT);
|
||||
// draw white quad
|
||||
gl.pushMatrix();
|
||||
{
|
||||
gl.translatef(quadPosition.x, quadPosition.y, 0);
|
||||
gl.rotated(angle, 0.0f, 0.0f, 1.0f);
|
||||
gl.color3f(1.0f, 1.0f, 0.0f);
|
||||
gl.begin(GL.QUADS);
|
||||
{
|
||||
gl.texCoord2f(0f, 0f);
|
||||
gl.vertex2i(-50, -50);
|
||||
gl.texCoord2f(1f, 0f);
|
||||
gl.vertex2i(50, -50);
|
||||
gl.texCoord2f(1f, 1f);
|
||||
gl.vertex2i(50, 50);
|
||||
gl.texCoord2f(0f, 1f);
|
||||
gl.vertex2i(-50, 50);
|
||||
}
|
||||
gl.end();
|
||||
}
|
||||
gl.popMatrix();
|
||||
}
|
||||
|
||||
private void initPbuffer() {
|
||||
try {
|
||||
pbuffer = new Pbuffer(256, 256, mode.bpp, 0, 0, 0);
|
||||
pbuffer.makeCurrent();
|
||||
initGLState(256, 256, 0.5f);
|
||||
gl.bindTexture(GL.TEXTURE_2D, tex_handle);
|
||||
Pbuffer.releaseContext();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes keyboard input
|
||||
*/
|
||||
private void processKeyboard() {
|
||||
Keyboard.poll();
|
||||
|
||||
//check for fullscreen key
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
|
||||
|
||||
try {
|
||||
destroyTexture();
|
||||
Keyboard.destroy();
|
||||
Pbuffer.releaseContext();
|
||||
pbuffer.destroy();
|
||||
gl.destroy();
|
||||
|
||||
Display.setDisplayMode(mode);
|
||||
gl = new GL("Test", mode.bpp, 0, 0, 0);
|
||||
gl.create();
|
||||
glInit();
|
||||
initPbuffer();
|
||||
glu = new GLU(gl);
|
||||
|
||||
Keyboard.create();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//check for window key
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
|
||||
try {
|
||||
destroyTexture();
|
||||
Keyboard.destroy();
|
||||
Pbuffer.releaseContext();
|
||||
pbuffer.destroy();
|
||||
gl.destroy();
|
||||
|
||||
Display.resetDisplayMode();
|
||||
gl = new GL("Test", 50, 50, mode.width, mode.height, mode.bpp, 0, 0, 0);
|
||||
gl.create();
|
||||
glInit();
|
||||
initPbuffer();
|
||||
glu = new GLU(gl);
|
||||
|
||||
|
||||
Keyboard.create();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//check for speed changes
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
|
||||
quadVelocity.y += 0.1f;
|
||||
}
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
|
||||
quadVelocity.y -= 0.1f;
|
||||
}
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
|
||||
quadVelocity.x += 0.1f;
|
||||
}
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
|
||||
quadVelocity.x -= 0.1f;
|
||||
}
|
||||
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) {
|
||||
angleRotation += 0.1f;
|
||||
}
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) {
|
||||
angleRotation -= 0.1f;
|
||||
}
|
||||
|
||||
//throttle
|
||||
if (quadVelocity.x < -MAX_SPEED) {
|
||||
quadVelocity.x = -MAX_SPEED;
|
||||
}
|
||||
if (quadVelocity.x > MAX_SPEED) {
|
||||
quadVelocity.x = MAX_SPEED;
|
||||
}
|
||||
if (quadVelocity.y < -MAX_SPEED) {
|
||||
quadVelocity.y = -MAX_SPEED;
|
||||
}
|
||||
if (quadVelocity.y > MAX_SPEED) {
|
||||
quadVelocity.y = MAX_SPEED;
|
||||
}
|
||||
|
||||
if (angleRotation < 0.0f) {
|
||||
angleRotation = 0.0f;
|
||||
}
|
||||
if (angleRotation > MAX_SPEED) {
|
||||
angleRotation = MAX_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
private void destroyTexture() {
|
||||
IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
buffer.put(0, tex_handle);
|
||||
gl.deleteTextures(1, Sys.getDirectBufferAddress(buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up the test
|
||||
*/
|
||||
private void cleanup() {
|
||||
destroyTexture();
|
||||
Keyboard.destroy();
|
||||
Pbuffer.releaseContext();
|
||||
pbuffer.destroy();
|
||||
gl.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a displaymode, if one such is available
|
||||
*
|
||||
* @param width Required width
|
||||
* @param height Required height
|
||||
* @param bpp Minimum required bits per pixel
|
||||
* @return
|
||||
*/
|
||||
private DisplayMode findDisplayMode(int width, int height, int bpp) {
|
||||
DisplayMode[] modes = Display.getAvailableDisplayModes();
|
||||
for (int i = 0; i < modes.length; i++) {
|
||||
if (modes[i].width == width
|
||||
&& modes[i].height == height
|
||||
&& modes[i].bpp >= bpp) {
|
||||
return modes[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void initGLState(int width, int height, float color) {
|
||||
gl.matrixMode(GL.PROJECTION);
|
||||
gl.loadIdentity();
|
||||
glu.ortho2D(0, mode.width, 0, mode.height);
|
||||
gl.matrixMode(GL.MODELVIEW);
|
||||
gl.loadIdentity();
|
||||
gl.viewport(0, 0, width, height);
|
||||
|
||||
//set clear color to black
|
||||
gl.clearColor(color, color, color, 0.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes OGL
|
||||
*/
|
||||
private void glInit() {
|
||||
// Go into orthographic projection mode.
|
||||
gl.determineAvailableExtensions();
|
||||
//sync frame (only works on windows)
|
||||
if (GL.WGL_EXT_swap_control) {
|
||||
GL.wglSwapIntervalEXT(1);
|
||||
}
|
||||
gl.texEnvf(GL.TEXTURE_ENV, GL.TEXTURE_ENV_MODE, GL.REPLACE);
|
||||
gl.enable(GL.TEXTURE_2D);
|
||||
// Create shared texture
|
||||
IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
||||
gl.genTextures(1, Sys.getDirectBufferAddress(buffer));
|
||||
tex_handle = buffer.get(0);
|
||||
gl.bindTexture(GL.TEXTURE_2D, tex_handle);
|
||||
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP);
|
||||
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP);
|
||||
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR);
|
||||
gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR);
|
||||
initGLState(mode.width, mode.height, 0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test entry point
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.println(
|
||||
"Change between fullscreen and windowed mode, by pressing F and W respectively");
|
||||
System.out.println("Move quad using arrowkeys, and change rotation using +/-");
|
||||
PbufferTest fswTest = new PbufferTest();
|
||||
fswTest.execute();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue