Added linux native src files

This commit is contained in:
Elias Naur 2002-11-15 10:40:55 +00:00
parent 8e31225b1c
commit a3624b0216
10 changed files with 8421 additions and 0 deletions

131
src/native/linux/Game.java Normal file
View File

@ -0,0 +1,131 @@
import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.input.*;
public final class Game {
static {
try {
DisplayMode[] modes = Display.getAvailableDisplayModes();
System.out.println("Available display modes:");
for (int i = 0; i < modes.length; i ++)
System.out.println(modes[i]);
// For now let's just pick a mode we're certain to have
Display.create(new DisplayMode(640, 480, 16, 60), true);
System.out.println("Created display.");
} catch (Exception e) {
System.err.println("Failed to create display due to "+e);
System.exit(1);
}
}
public static final GL gl = new GL(16, 0, 16, 8);
public static final GLU glu = new GLU(gl);
static {
try {
gl.create();
System.out.println("Created OpenGL.");
} catch (Exception e) {
System.err.println("Failed to create OpenGL due to "+e);
System.exit(1);
}
}
/** Is the game finished? */
private static boolean finished;
/** A rotating square! */
private static float angle;
/**
* No construction allowed
*/
private Game() {
}
public static void main(String[] arguments) {
try {
init();
while (!finished) {
// Keyboard.poll();
mainLoop();
render();
gl.swapBuffers();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
cleanup();
}
}
/**
* All calculations are done in here
*/
private static void mainLoop() {
angle += 1f;
if (angle > 360.0f)
angle = 0.0f;
Mouse.poll();
if (Mouse.dx != 0 || Mouse.dy != 0)
System.out.println("Mouse moved " + Mouse.dx + " " + Mouse.dy);
for (int i = 0; i < 8; i++)
if (Mouse.isButtonDown(i))
System.out.println("Button " + i + " down");
/* Keyboard.poll();
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
finished = true;*/
Keyboard.read();
if (Keyboard.getNumKeyboardEvents() > 0) {
Keyboard.next();
if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state)
finished = true;
}
}
/**
* All rendering is done in here
*/
private static void render() {
gl.clear(GL.COLOR_BUFFER_BIT);
gl.pushMatrix();
gl.translatef(Display.getWidth() / 2, Display.getHeight() / 2, 0.0f);
gl.rotatef(angle, 0, 0, 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();
}
/**
* Initialize
*/
private static void init() throws Exception {
Keyboard.create();
Keyboard.enableBuffer();
Mouse.create();
// Go into orthographic projection mode.
gl.matrixMode(GL.PROJECTION);
gl.loadIdentity();
glu.ortho2D(0, Display.getWidth(), 0, Display.getHeight());
gl.matrixMode(GL.MODELVIEW);
gl.loadIdentity();
gl.viewport(0, 0, Display.getWidth(), Display.getHeight());
// Fix the refresh rate to the display frequency.
gl.wglSwapIntervalEXT(1);
}
/**
* Cleanup
*/
private static void cleanup() {
Keyboard.destroy();
Mouse.destroy();
gl.destroy();
Display.destroy();
}
}

29
src/native/linux/Makefile Normal file
View File

@ -0,0 +1,29 @@
GCC = gcc
LINK = gcc
JAVAHOME=/usr/java/j2sdk1.4.1_01
JAVAC=$(JAVAHOME)/bin/javac
JAVAH=$(JAVAHOME)/bin/javah
CP=../../java
JAVAFILES=$(shell find ../../java -name \*.java -print|grep -v CVS)
CLASSFILES=$(JAVAFILES:.java=.class)
all: liblwjgl.so
liblwjgl.so: org_lwjgl_Display.o org_lwjgl_Sys.o org_lwjgl_opengl_BaseGL.o org_lwjgl_opengl_GL.o org_lwjgl_opengl_CoreGL.o org_lwjgl_input_Keyboard.o org_lwjgl_opengl_GLU.o org_lwjgl_input_Mouse.o
$(LINK) -shared -o $@ $^ -L/usr/X11R6/lib -lX11 -lXext -lXxf86vm -lGL -lGLU
cp $@ ../../../bin/
.SUFFIXES: .cpp .so .o .java .h .class
%.class: %.java
$(JAVAC) -deprecation -O -source 1.4 -classpath $(CP) $<
%.o : %.c
$(GCC) -Wall -I$(JAVAHOME)/include -I../common -I$(JAVAHOME)/include/linux -c -o $@ $<
testprog : main.o
$(LINK) -o $@ $< -L/usr/X11R6/lib -lX11 -lXext -lXxf86vm -lGL -lGLU
.PHONY clean:
rm -rf *.o *.so
rm -rf `find ../../ -name \*.class -print`

View File

@ -0,0 +1,174 @@
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/extensions/xf86vmode.h>
#include <GL/glx.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <jni.h>
#include "org_lwjgl_Display.h"
Display * disp;
int screen;
Window win;
XF86VidModeModeInfo **avail_modes;
XVisualInfo * vis_info;
void waitMapped(Display *disp, Window win) {
XEvent event;
do {
XMaskEvent(disp, StructureNotifyMask, &event);
} while ((event.type != MapNotify) || (event.xmap.event != win));
}
int getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeInfo ***avail_modes) {
unsigned int event_base, error_base, xvid_ver, xvid_rev;
if (!XF86VidModeQueryExtension(disp, &event_base, &error_base)) {
#ifdef _DEBUG
printf("XF86VidMode extention not available\n");
#endif
return 0;
}
XF86VidModeQueryVersion(disp, &xvid_ver, &xvid_rev);
#ifdef _DEBUG
printf("XF86VidMode extention version %i.%i\n", xvid_ver, xvid_rev);
#endif
XF86VidModeGetAllModeLines(disp, screen, num_modes, avail_modes);
return 1;
}
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jboolean fullscreen) {
Window root_win;
XSetWindowAttributes attribs;
Colormap cmap;
int attribmask;
int bpe = bpp/4;
int attriblist[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, GLX_RED_SIZE, bpe, GLX_GREEN_SIZE, bpe, GLX_BLUE_SIZE, bpe, GLX_ALPHA_SIZE, bpe, None};
int num_modes, i;
disp = XOpenDisplay(NULL);
if (disp == NULL) {
#ifdef _DEBUG
printf("Could not open X connection\n");
#endif
return JNI_FALSE;
}
screen = DefaultScreen(disp);
root_win = RootWindow(disp, screen);
vis_info = glXChooseVisual(disp, screen, attriblist);
if (vis_info == NULL) {
#ifdef _DEBUG
printf("Could not choose glx visual\n");
#endif
return JNI_FALSE;
}
cmap = XCreateColormap(disp, root_win, vis_info->visual, AllocNone);
attribs.colormap = cmap;
attribs.event_mask = ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
attribs.background_pixel = 0xFF000000;
attribs.event_mask = StructureNotifyMask;
attribmask = CWColormap | CWBackPixel | CWEventMask;
if (fullscreen) {
attribmask |= CWOverrideRedirect;
attribs.override_redirect = True;
}
win = XCreateWindow(disp, root_win, 0, 0, width, height, 0, vis_info->depth, InputOutput, vis_info->visual, attribmask, &attribs);
#ifdef _DEBUG
printf("Created window\n");
#endif
if (fullscreen) {
XMapRaised(disp, win);
waitMapped(disp, win);
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
#ifdef _DEBUG
printf("Could not get display modes\n");
#endif
return JNI_FALSE;
}
for ( i = 0; i < num_modes; ++i ) {
#ifdef _DEBUG
printf("Mode %d: %dx%d\n", i, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay);
#endif
if (avail_modes[i]->hdisplay == width && avail_modes[i]->vdisplay == height) {
if (!XF86VidModeSwitchToMode(disp, screen, avail_modes[i])) {
#ifdef _DEBUG
printf("Could not switch mode\n");
#endif
return JNI_FALSE;
}
}
}
XF86VidModeSetViewPort(disp, screen, 0, 0);
} else {
XMapWindow(disp, win);
waitMapped(disp, win);
}
XClearWindow(disp, win);
XSync(disp, True);
return JNI_TRUE;
}
JNIEXPORT void JNICALL Java_org_lwjgl_Display_nDestroy(JNIEnv * env, jclass clazz) {
XDestroyWindow(disp, win);
if (!XF86VidModeSwitchToMode(disp, screen, avail_modes[0])) {
#ifdef _DEBUG
printf("Could not switch mode\n");
#endif
}
XFree(vis_info);
XFree(avail_modes);
XCloseDisplay(disp);
#ifdef _DEBUG
printf("Closed X connection\n");
#endif
}
/*
* Class: org_lwjgl_Display
* Method: getAvailableDisplayModes
* Signature: ()[Lorg/lwjgl/DisplayMode;
*/
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes
(JNIEnv * env, jclass clazz)
{
int num_modes, i;
Display *disp = XOpenDisplay(NULL);
int screen = DefaultScreen(disp);
XF86VidModeModeInfo **avail_modes;
int depth = DefaultDepth(disp, screen);
if (disp == NULL) {
#ifdef _DEBUG
printf("Could not open X connection\n");
#endif
return NULL;
}
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
#ifdef _DEBUG
printf("Could not get display modes\n");
#endif
return NULL;
}
// Allocate an array of DisplayModes big enough
jclass displayModeClass = (*env)->FindClass(env, "org/lwjgl/DisplayMode");
jobjectArray ret = (*env)->NewObjectArray(env, num_modes, displayModeClass, NULL);
jmethodID displayModeConstructor = (*env)->GetMethodID(env, displayModeClass, "<init>", "(IIII)V");
for (i = 0; i < num_modes; i++) {
jobject displayMode = (*env)->NewObject(env, displayModeClass, displayModeConstructor, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay, depth, 0);
(*env)->SetObjectArrayElement(env, ret, i, displayMode);
}
XFree(avail_modes);
XCloseDisplay(disp);
return ret;
}

View File

@ -0,0 +1,140 @@
/*
* Copyright (c) 2002 Light Weight 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.
*/
/**
* $Id$
*
* Win32 system library.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
#include "org_lwjgl_Sys.h"
long int hires_timer_freq; // Hires timer frequency
long int hires_timer_start; // Hires timer start
long int hires_timer; // Hires timer current time
/*
* Class: org_lwjgl_Sys
* Method: getDirectBufferAddress
* Signature: (Ljava/nio/Buffer;)I
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_Sys_getDirectBufferAddress
(JNIEnv * env, jclass clazz, jobject buf)
{
return (jint) (*env)->GetDirectBufferAddress(env, buf);
}
/*
* Class: org_lwjgl_Sys
* Method: createDirectBuffer
* Signature: (II)Ljava/nio/ByteBuffer;
*/
JNIEXPORT jobject JNICALL Java_org_lwjgl_Sys_createDirectBuffer
(JNIEnv * env, jclass clazz, jint address, jint length)
{
return (*env)->NewDirectByteBuffer(env, (void *)address, length);
}
/*
* Class: org_lwjgl_Sys
* Method: getTimerResolution
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTimerResolution
(JNIEnv * env, jclass clazz)
{
return hires_timer_freq;
}
/*
* Class: org_lwjgl_Sys
* Method: getTime
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTime
(JNIEnv * env, jclass clazz)
{
// QueryPerformanceCounter((LARGE_INTEGER*) &hires_timer);
hires_timer -= hires_timer_start;
return hires_timer;
}
/*
* Class: org_lwjgl_Sys
* Method: setTime
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setTime
(JNIEnv * env, jclass clazz, jlong startTime)
{
/* QueryPerformanceFrequency((LARGE_INTEGER*) &hires_timer_freq);
QueryPerformanceCounter((LARGE_INTEGER*) &hires_timer_start);*/
hires_timer_start -= startTime;
}
/*
* Class: org_lwjgl_Sys
* Method: setProcessPriority
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setProcessPriority
(JNIEnv * env, jclass clazz, jint priority)
{
/* HANDLE me = GetCurrentProcess();
int win32priority;
switch (priority) {
case org_lwjgl_Sys_REALTIME_PRIORITY:
win32priority = REALTIME_PRIORITY_CLASS;
break;
case org_lwjgl_Sys_HIGH_PRIORITY:
win32priority = HIGH_PRIORITY_CLASS;
break;
case org_lwjgl_Sys_NORMAL_PRIORITY:
win32priority = NORMAL_PRIORITY_CLASS;
break;
case org_lwjgl_Sys_LOW_PRIORITY:
win32priority = IDLE_PRIORITY_CLASS;
break;
default:
return;
}
if (!SetPriorityClass(me, win32priority)) {
#ifdef _DEBUG
printf("Failed to set priority class.\n");
#endif
}*/
}

View File

@ -0,0 +1,163 @@
/*
* Copyright (c) 2002 Light Weight 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.
*/
/**
* $Id$
*
* Win32 keyboard handling.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
#include <X11/X.h>
#include <X11/Xlib.h>
#include <string.h>
#include <assert.h>
#include "org_lwjgl_input_Keyboard.h"
#define KEYBOARD_BUFFER_SIZE 50
#define KEYBOARD_SIZE 256
short readBuffer[KEYBOARD_BUFFER_SIZE];
jfieldID fid_readBuffer;
jfieldID fid_readBufferAddress;
unsigned char key_buf[KEYBOARD_SIZE];
extern Display *disp;
extern Window win;
/*
* Class: org_lwjgl_input_Keyboard
* Method: initIDs
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_initIDs
(JNIEnv * env, jclass clazz)
{
// Get a global class instance, just to be sure
static jobject globalClassLock = NULL;
if (globalClassLock == NULL) {
globalClassLock = (*env)->NewGlobalRef(env, clazz);
}
fid_readBuffer = (*env)->GetStaticFieldID(env, clazz, "readBuffer", "Ljava/nio/ByteBuffer;");
fid_readBufferAddress = (*env)->GetStaticFieldID(env, clazz, "readBufferAddress", "I");
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nCreate
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Keyboard_nCreate
(JNIEnv * env, jclass clazz)
{
int result = XGrabKeyboard(disp, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
if (result != GrabSuccess) {
#ifdef _DEBUG
printf("Could not grab keyboard\n");
#endif
return JNI_FALSE;
}
memset(key_buf, 0, KEYBOARD_SIZE*sizeof(unsigned char));
return JNI_TRUE;
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nDestroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nDestroy
(JNIEnv * env, jclass clazz)
{
XUngrabKeyboard(disp, CurrentTime);
}
int checkKeyEvents(unsigned char *result_buf) {
XEvent event;
int count = 0;
int buf_count = 0;
while (XCheckMaskEvent(disp, KeyPressMask | KeyReleaseMask, &event)) {
unsigned char keycode = (unsigned char)((event.xkey.keycode - 8) & 0xff);
if (result_buf != NULL) {
result_buf[buf_count++] = keycode;
result_buf[buf_count++] = 1;
}
count++;
if (event.type == KeyPress) {
key_buf[keycode] = 1;
} else if (event.type == KeyRelease) {
key_buf[keycode] = 0;
} else
assert(0);
}
return count;
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nPoll
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nPoll
(JNIEnv * env, jclass clazz, jint buf)
{
checkKeyEvents(NULL);
memcpy((unsigned char*)buf, key_buf, KEYBOARD_SIZE*sizeof(unsigned char));
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nRead
* Signature: (I)V
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
(JNIEnv * env, jclass clazz, jint keys)
{
return checkKeyEvents((unsigned char *)keys);
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nEnableBuffer
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nEnableBuffer
(JNIEnv * env, jclass clazz)
{
jobject newBuffer = (*env)->NewDirectByteBuffer(env, &readBuffer, KEYBOARD_BUFFER_SIZE);
(*env)->SetStaticObjectField(env, clazz, fid_readBuffer, newBuffer);
(*env)->SetStaticIntField(env, clazz, fid_readBufferAddress, (jint) (&readBuffer));
return KEYBOARD_BUFFER_SIZE;
}

View File

@ -0,0 +1,191 @@
/*
* Copyright (c) 2002 Light Weight 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.
*/
/**
* $Id$
*
* Win32 mouse handling.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
#include <X11/X.h>
#include <X11/Xlib.h>
#include <assert.h>
#include <string.h>
#include "org_lwjgl_input_Mouse.h"
#define NUM_BUTTONS 8
extern Display *disp;
extern Window win;
jfieldID fid_button;
jfieldID fid_dx;
jfieldID fid_dy;
jfieldID fid_dz;
int last_x;
int last_y;
int current_x;
int current_y;
unsigned char buttons[NUM_BUTTONS];
/*
* Class: org_lwjgl_input_Mouse
* Method: initIDs
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_initIDs
(JNIEnv * env, jclass clazz)
{
// Get a global class instance, just to be sure
static jobject globalClassLock = NULL;
if (globalClassLock == NULL) {
globalClassLock = (*env)->NewGlobalRef(env, clazz);
}
// Now cache the field IDs:
if (fid_button == NULL) {
fid_button = (*env)->GetStaticFieldID(env, clazz, "button", "[Z");
}
if (fid_dx == NULL) {
fid_dx = (*env)->GetStaticFieldID(env, clazz, "dx", "I");
}
if (fid_dy == NULL) {
fid_dy = (*env)->GetStaticFieldID(env, clazz, "dy", "I");
}
if (fid_dz == NULL) {
fid_dz = (*env)->GetStaticFieldID(env, clazz, "dz", "I");
}
}
/*
* Class: org_lwjgl_input_Mouse
* Method: nCreate
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate
(JNIEnv * env, jclass clazz)
{
int i;
current_x = current_y = last_x = last_y = 0;
for (i = 0; i < NUM_BUTTONS; i++)
buttons[i] = 0;
int result = XGrabPointer(disp, win, False, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
if (result != GrabSuccess) {
#ifdef _DEBUG
printf("Could not grab mouse\n");
#endif
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* Class: org_lwjgl_input_Mouse
* Method: nDestroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy
(JNIEnv * env, jclass clazz)
{
XUngrabPointer(disp, CurrentTime);
}
void setButtonState(XButtonEvent event, unsigned char val) {
switch (event.button) {
case Button1:
buttons[0] = val;
break;
case Button2:
buttons[1] = val;
break;
case Button3:
buttons[2] = val;
break;
case Button4:
buttons[3] = val;
break;
case Button5:
buttons[4] = val;
break;
default: assert(0);
}
}
int checkPointer() {
XEvent event;
int count = 0;
while (XCheckMaskEvent(disp, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &event)) {
count++;
switch (event.type) {
case ButtonPress:
setButtonState(event.xbutton, 1);
break;
case ButtonRelease:
setButtonState(event.xbutton, 0);
break;
case MotionNotify:
current_x = event.xbutton.x;
current_y = event.xbutton.y;
break;
default: assert(0);
}
}
return count;
}
/*
* Class: org_lwjgl_input_Mouse
* Method: nPoll
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll
(JNIEnv * env, jclass clazz)
{
checkPointer();
int moved_x = current_x - last_x;
int moved_y = current_y - last_y;
(*env)->SetStaticIntField(env, clazz, fid_dx, (jint)moved_x);
(*env)->SetStaticIntField(env, clazz, fid_dy, (jint)moved_y);
(*env)->SetStaticIntField(env, clazz, fid_dz, (jint)0);
last_x = current_x;
last_y = current_y;
jbooleanArray buttonsArray = (jbooleanArray) (*env)->GetStaticObjectField(env, clazz, fid_button);
unsigned char * class_buttons = (unsigned char *) (*env)->GetPrimitiveArrayCritical(env, buttonsArray, NULL);
memcpy(class_buttons, buttons, NUM_BUTTONS*sizeof(unsigned char));
(*env)->ReleasePrimitiveArrayCritical(env, buttonsArray, class_buttons, 0);
}

View File

@ -0,0 +1,111 @@
/*
* Copyright (c) 2002 Light Weight 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.
*/
/**
* $Id$
*
* Base Win32 functionality for GL.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
#include <GL/glx.h>
#include "org_lwjgl_opengl_BaseGL.h"
GLXContext context = NULL; // OpenGL rendering context
extern XVisualInfo * vis_info;
extern Window win;
extern Display * disp;
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nCreate
* Signature: (IIII)Z
*/
JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_BaseGL_nCreate
(JNIEnv * env, jobject obj, jint colorBits, jint alphaBits, jint depthBits, jint stencilBits)
{
if (!vis_info) {
#ifdef _DEBUG
printf("No visual info\n");
#endif
return JNI_FALSE;
}
context = glXCreateContext(disp, vis_info, NULL, True);
if (!context) {
#ifdef _DEBUG
printf("Could not create context\n");
#endif
return JNI_FALSE;
}
return JNI_TRUE;
}
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nDestroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroy
(JNIEnv * env, jobject obj)
{
glXMakeCurrent(disp, None, NULL);
// Delete the rendering context
if (context != NULL)
glXDestroyContext(disp, context);
}
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: swapBuffers
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_swapBuffers(JNIEnv * env, jobject obj)
{
glXSwapBuffers(disp, win);
}
/*
* Class: org_lwjgl_opengl_BaseGL
* Method: nMakeCurrent
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nMakeCurrent
(JNIEnv * env, jobject obj)
{
glXMakeCurrent(disp, win, context);
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,162 @@
/*
* Copyright (c) 2002 Light Weight 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.
*/
/**
* $Id$
*
* GLU library.
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
*/
#include "org_lwjgl_opengl_GLU.h"
#include "checkGLerror.h"
#include <GL/glu.h>
/*
* Class: org_lwjgl_opengl_GLU
* Method: getString
*/
JNIEXPORT jstring JNICALL Java_org_lwjgl_opengl_GLU_getString(JNIEnv * env, jobject obj, jint p0)
{
const char * msg = (const char *) gluGetString((GLint) p0);
jstring ret = (*env)->NewStringUTF(env, msg);
return ret;
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: errorString
*/
JNIEXPORT jstring JNICALL Java_org_lwjgl_opengl_GLU_errorString(JNIEnv * env, jobject obj, jint p0)
{
const GLubyte * msg = gluErrorString((GLint) p0);
jstring ret = (*env)->NewStringUTF(env, (const char *) msg);
return ret;
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: ortho2D
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLU_ortho2D(JNIEnv * env, jobject obj, jdouble p0, jdouble p1, jdouble p2, jdouble p3)
{
gluOrtho2D((GLdouble) p0, (GLdouble) p1, (GLdouble) p2, (GLdouble) p3);
CHECK_GL_ERROR
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: perspective
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLU_perspective(JNIEnv * env, jobject obj, jdouble p0, jdouble p1, jdouble p2, jdouble p3)
{
gluPerspective((GLdouble) p0, (GLdouble) p1, (GLdouble) p2, (GLdouble) p3);
CHECK_GL_ERROR
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: pickMatrix
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLU_pickMatrix(JNIEnv * env, jobject obj, jdouble p0, jdouble p1, jdouble p2, jdouble p3, jint p4)
{
gluPickMatrix((GLdouble) p0, (GLdouble) p1, (GLdouble) p2, (GLdouble) p3, (GLint *) p4);
CHECK_GL_ERROR
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: lookAt
*/
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLU_lookAt(JNIEnv * env, jobject obj, jdouble p0, jdouble p1, jdouble p2, jdouble p3, jdouble p4, jdouble p5, jdouble p6, jdouble p7, jdouble p8)
{
gluLookAt((GLdouble) p0, (GLdouble) p1, (GLdouble) p2, (GLdouble) p3, (GLdouble) p4, (GLdouble) p5, (GLdouble) p6, (GLdouble) p7, (GLdouble) p8);
CHECK_GL_ERROR
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: project
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_GLU_project(JNIEnv * env, jobject obj, jdouble p0, jdouble p1, jdouble p2, jint p3, jint p4, jint p5, jint p6, jint p7, jint p8)
{
jint ret = (jint) gluProject((GLdouble) p0, (GLdouble) p1, (GLdouble) p2, (const GLdouble *) p3, (const GLdouble *) p4, (const GLint *) p5, (GLdouble *) p6, (GLdouble *) p7, (GLdouble *) p8);
CHECK_GL_ERROR
return ret;
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: unProject
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_GLU_unProject(JNIEnv * env, jobject obj, jdouble p0, jdouble p1, jdouble p2, jint p3, jint p4, jint p5, jint p6, jint p7, jint p8)
{
jint ret = (jint) gluUnProject((GLdouble) p0, (GLdouble) p1, (GLdouble) p2, (const GLdouble *) p3, (const GLdouble *) p4, (const GLint *) p5, (GLdouble *) p6, (GLdouble *) p7, (GLdouble *) p8);
CHECK_GL_ERROR
return ret;
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: scaleImage
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_GLU_scaleImage(JNIEnv * env, jobject obj, jint p0, jint p1, jint p2, jint p3, jint p4, jint p5, jint p6, jint p7, jint p8)
{
jint ret = (jint) gluScaleImage((GLint) p0, (GLint) p1, (GLint) p2, (GLint) p3, (const void *) p4, (GLint) p5, (GLint) p6, (GLint) p7, (void *) p8);
CHECK_GL_ERROR
return ret;
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: build1DMipmaps
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_GLU_build1DMipmaps(JNIEnv * env, jobject obj, jint p0, jint p1, jint p2, jint p3, jint p4, jint p5)
{
jint ret = (jint) gluBuild1DMipmaps((GLint) p0, (GLint) p1, (GLint) p2, (GLint) p3, (GLint) p4, (const void *) p5);
CHECK_GL_ERROR
return ret;
}
/*
* Class: org_lwjgl_opengl_GLU
* Method: build2DMipmaps
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_GLU_build2DMipmaps(JNIEnv * env, jobject obj, jint p0, jint p1, jint p2, jint p3, jint p4, jint p5, jint p6)
{
jint ret = (jint) gluBuild2DMipmaps((GLint) p0, (GLint) p1, (GLint) p2, (GLint) p3, (GLint) p4, (GLint) p5, (const void *) p6);
CHECK_GL_ERROR
return ret;
}