2004-11-25 23:31:38 +01:00
|
|
|
/*
|
2004-11-11 17:04:28 +01:00
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
|
|
|
|
* All rights reserved.
|
2004-11-25 23:31:38 +01:00
|
|
|
*
|
2004-11-11 17:04:28 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2004-11-25 23:31:38 +01:00
|
|
|
* modification, are permitted provided that the following conditions are
|
2004-11-11 17:04:28 +01:00
|
|
|
* met:
|
2004-11-25 23:31:38 +01:00
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2004-11-11 17:04:28 +01:00
|
|
|
* 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.
|
|
|
|
|
*
|
2004-11-25 23:31:38 +01:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
2004-11-11 17:04:28 +01:00
|
|
|
* from this software without specific prior written permission.
|
2004-11-25 23:31:38 +01:00
|
|
|
*
|
2004-11-11 17:04:28 +01:00
|
|
|
* 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
|
2004-11-25 23:31:38 +01:00
|
|
|
* 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
|
2004-11-11 17:04:28 +01:00
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
2004-11-25 23:31:38 +01:00
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
2004-11-11 17:04:28 +01:00
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A java implementation of a LWJGL compatible event queue.
|
|
|
|
|
* @author elias_naur
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.IntBuffer;
|
|
|
|
|
|
2006-06-26 15:50:48 +02:00
|
|
|
class EventQueue {
|
2004-11-25 23:31:38 +01:00
|
|
|
|
|
|
|
|
private static final int QUEUE_SIZE = 200;
|
|
|
|
|
|
2004-11-11 17:04:28 +01:00
|
|
|
private final int event_size;
|
2004-11-25 23:31:38 +01:00
|
|
|
|
2004-11-11 17:04:28 +01:00
|
|
|
private final IntBuffer queue;
|
|
|
|
|
|
2004-11-25 23:31:38 +01:00
|
|
|
protected EventQueue(int event_size) {
|
2004-11-11 17:04:28 +01:00
|
|
|
this.event_size = event_size;
|
|
|
|
|
this.queue = ByteBuffer.allocateDirect(QUEUE_SIZE*event_size).asIntBuffer();
|
|
|
|
|
}
|
2004-11-25 23:31:38 +01:00
|
|
|
|
2004-11-11 17:04:28 +01:00
|
|
|
protected synchronized void clearEvents() {
|
|
|
|
|
queue.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copy available events into the specified buffer.
|
|
|
|
|
* Note that the buffer position is maintained, to emulate
|
|
|
|
|
* the behaviour of the native event queues.
|
|
|
|
|
* @return the number of events copied
|
|
|
|
|
*/
|
|
|
|
|
public synchronized int copyEvents(IntBuffer dest) {
|
|
|
|
|
int old_position = dest.position();
|
|
|
|
|
queue.flip();
|
|
|
|
|
int old_limit = queue.limit();
|
|
|
|
|
if (dest.remaining() < queue.remaining())
|
|
|
|
|
queue.limit(dest.remaining() + queue.position());
|
|
|
|
|
dest.put(queue);
|
|
|
|
|
queue.limit(old_limit);
|
|
|
|
|
queue.compact();
|
|
|
|
|
int num_events = (dest.position() - old_position)/event_size;
|
|
|
|
|
dest.position(old_position);
|
|
|
|
|
return num_events;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Put an event into the queue.
|
|
|
|
|
* @return true if the event fitted into the queue, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
public synchronized boolean putEvent(int[] event) {
|
|
|
|
|
if (event.length != event_size)
|
|
|
|
|
throw new IllegalArgumentException("Internal error: event size " + event_size + " does not equals the given event size " + event.length);
|
|
|
|
|
if (queue.remaining() >= event.length) {
|
|
|
|
|
queue.put(event);
|
|
|
|
|
return true;
|
|
|
|
|
} else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|