Replace AWT code for getAvailableDisplayMode and other DisplayModes

stuff to use pure native Cococa code.
This commit is contained in:
kappaOne 2013-02-08 23:29:15 +00:00
parent 409b3fea6d
commit db4eb9c5c8
3 changed files with 88 additions and 41 deletions

View File

@ -39,8 +39,6 @@ package org.lwjgl.opengl;
*/
import java.awt.Canvas;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
@ -57,10 +55,6 @@ import org.lwjgl.MemoryUtil;
import org.lwjgl.LWJGLException;
import org.lwjgl.LWJGLUtil;
import com.apple.eawt.Application;
import com.apple.eawt.ApplicationAdapter;
import com.apple.eawt.ApplicationEvent;
import static org.lwjgl.opengl.GL11.*;
final class MacOSXDisplay implements DisplayImplementation {
@ -72,7 +66,7 @@ final class MacOSXDisplay implements DisplayImplementation {
private Robot robot;
private MacOSXMouseEventQueue mouse_queue;
private KeyboardEventQueue keyboard_queue;
private java.awt.DisplayMode requested_mode;
private DisplayMode requested_mode;
/* Members for native window use */
private MacOSXNativeMouse mouse;
@ -99,6 +93,10 @@ final class MacOSXDisplay implements DisplayImplementation {
private native ByteBuffer nCreateWindow(int x, int y, int width, int height, boolean fullscreen, boolean undecorated, boolean resizable, boolean parented, ByteBuffer peer_info_handle, ByteBuffer window_handle) throws LWJGLException;
private native Object nGetCurrentDisplayMode();
private native void nGetDisplayModes(Object modesList);
private native boolean nIsMiniaturized(ByteBuffer window_handle);
private native boolean nIsFocused(ByteBuffer window_handle);
@ -219,29 +217,25 @@ final class MacOSXDisplay implements DisplayImplementation {
public String getVersion() {
return null;
}
private static boolean equals(java.awt.DisplayMode awt_mode, DisplayMode mode) {
return awt_mode.getWidth() == mode.getWidth() && awt_mode.getHeight() == mode.getHeight()
&& awt_mode.getBitDepth() == mode.getBitsPerPixel() && awt_mode.getRefreshRate() == mode.getFrequency();
private static boolean equals(DisplayMode mode1, DisplayMode mode2) {
return mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight()
&& mode1.getBitsPerPixel() == mode2.getBitsPerPixel() && mode1.getFrequency() == mode2.getFrequency();
}
public void switchDisplayMode(DisplayMode mode) throws LWJGLException {
java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes();
for ( java.awt.DisplayMode awt_mode : awt_modes ) {
if (equals(awt_mode, mode)) {
requested_mode = awt_mode;
DisplayMode[] modes = getAvailableDisplayModes();
for (DisplayMode available_mode : modes) {
if (equals(available_mode, mode)) {
requested_mode = available_mode;
return;
}
}
throw new LWJGLException(mode + " is not supported");
}
private static GraphicsDevice getDevice() {
GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = g_env.getDefaultScreenDevice();
return device;
}
public void resetDisplayMode() {
requested_mode = null;
restoreGamma();
@ -249,32 +243,24 @@ final class MacOSXDisplay implements DisplayImplementation {
private native void restoreGamma();
private static DisplayMode createLWJGLDisplayMode(java.awt.DisplayMode awt_mode) {
int bit_depth;
int refresh_rate;
int awt_bit_depth = awt_mode.getBitDepth();
int awt_refresh_rate = awt_mode.getRefreshRate();
if (awt_bit_depth != java.awt.DisplayMode.BIT_DEPTH_MULTI)
bit_depth = awt_bit_depth;
else
bit_depth = 32; // Assume the best bit depth
if (awt_refresh_rate != java.awt.DisplayMode.REFRESH_RATE_UNKNOWN)
refresh_rate = awt_refresh_rate;
else
refresh_rate = 0;
return new DisplayMode(awt_mode.getWidth(), awt_mode.getHeight(), bit_depth, refresh_rate);
public Object createDisplayMode(int width, int height, int bitsPerPixel, int refreshRate) {
return new DisplayMode(width, height, bitsPerPixel, refreshRate);
}
public DisplayMode init() throws LWJGLException {
return createLWJGLDisplayMode(getDevice().getDisplayMode());
java.awt.Toolkit.getDefaultToolkit(); // force start AWT Application loop
return (DisplayMode) nGetCurrentDisplayMode();
}
public void addDisplayMode(Object modesList, int width, int height, int bitsPerPixel, int refreshRate) {
List<DisplayMode> modes = (List<DisplayMode>) modesList;
DisplayMode displayMode = new DisplayMode(width, height, bitsPerPixel, refreshRate);
modes.add(displayMode);
}
public DisplayMode[] getAvailableDisplayModes() throws LWJGLException {
java.awt.DisplayMode[] awt_modes = getDevice().getDisplayModes();
List<DisplayMode> modes = new ArrayList<DisplayMode>();
for ( java.awt.DisplayMode awt_mode : awt_modes )
if ( awt_mode.getBitDepth() >= 16 )
modes.add(createLWJGLDisplayMode(awt_mode));
nGetDisplayModes(modes); // will populate the above list
return modes.toArray(new DisplayMode[modes.size()]);
}

View File

@ -83,6 +83,8 @@ typedef struct {
}
+ (NSOpenGLPixelFormat*)defaultPixelFormat;
- (BOOL)windowShouldClose:(id)sender;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format;
- (void)setOpenGLContext:(NSOpenGLContext*)context;
- (NSOpenGLContext*)openGLContext;

View File

@ -618,6 +618,65 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nIsNativeMode(JNI
}
}
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetCurrentDisplayMode(JNIEnv *env, jobject this) {
jclass displayClass = (*env)->GetObjectClass(env, this);
jmethodID createDisplayModeMethod = (*env)->GetMethodID(env, displayClass, "createDisplayMode", "(IIII)Ljava/lang/Object;");
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(kCGDirectMainDisplay);
int width = (int) CGDisplayModeGetWidth(mode);
int height = (int) CGDisplayModeGetHeight(mode);
int refreshRate = (int)CGDisplayModeGetRefreshRate(mode);
int bitsPerPixel;
// get bitsPerPixel
CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(mode);
if(CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
bitsPerPixel = 16;
}
else {
bitsPerPixel = 32;
}
jobject displayMode = (*env)->CallObjectMethod(env, this, createDisplayModeMethod, width, height, bitsPerPixel, refreshRate);
return displayMode;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_MacOSXDisplay_nGetDisplayModes(JNIEnv *env, jobject this, jobject modesList) {
CFArrayRef modes = CGDisplayCopyAllDisplayModes(kCGDirectMainDisplay, NULL);
jclass displayClass = (*env)->GetObjectClass(env, this);
jmethodID addDisplayModeMethod = (*env)->GetMethodID(env, displayClass, "addDisplayMode", "(Ljava/lang/Object;IIII)V");
int i = 0;
for (i = 0; i < CFArrayGetCount(modes); i++) {
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, i);
int width = (int) CGDisplayModeGetWidth(mode);
int height = (int) CGDisplayModeGetHeight(mode);
int refreshRate = (int)CGDisplayModeGetRefreshRate(mode);
int bitsPerPixel;
// get bitsPerPixel
CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding(mode);
if(CFStringCompare(pixelEncoding, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
bitsPerPixel = 32;
}
else if(CFStringCompare(pixelEncoding, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
bitsPerPixel = 16;
}
else {
continue; // ignore DisplayMode of other bitsPerPixel rates
}
(*env)->CallVoidMethod(env, this, addDisplayModeMethod, modesList, width, height, bitsPerPixel, refreshRate);
}
}
JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getJNIVersion(JNIEnv *env, jobject ignored) {
return org_lwjgl_MacOSXSysImplementation_JNI_VERSION;
}