LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Orangy Tang on October 10, 2004, 13:13:11

Title: Colour depth for display
Post by: Orangy Tang on October 10, 2004, 13:13:11
oNyx recently pointed out that Quix doesn't work fullscreen (http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=Announcements;action=display;num=1092604670;start=30#30) any more. :shock:  With a previous build it worked fine (after I remembered to check for 0Hz display modes as well).

Now I notice that the space invaders demo *does* work for him both windowed and fullscreen, which happens to use the bpp of the current display mode (the desktop I assume?). Other than that my code appears pretty much identical - I allow any bpp, but if two modes have identical size & refresh I favour ones with higher bpp.

In theory this should be ok, as if it fails on the 32 it'll keep trying others and eventually get to a 16bpp one. However it seems to create the display then fail to display anything. Is there anything I can do to detect such a situation?
Title: Colour depth for display
Post by: oNyx on October 10, 2004, 23:55:42
Hm. Well, I'm running 800x600x32 therefore I tried changing the resolution before running your game, but it doesn't help :/

If you paste your init code I can toy around with it until I get it running. So far I'm pretty clueless why it doesn't work.
Title: Colour depth for display
Post by: Orangy Tang on October 11, 2004, 18:39:30
Cheers, that'd be a good way of figuring this out. :)

This is a display test thingy I was going to have you try:

package quix;

import java.util.ArrayList;
import java.util.Collections;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;


/**
* @author John Campbell
*/
public class DisplayTest
{


public static void main(String[] args)
{
int method = 1;
if (args.length == 1)
{
String arg1 = args[0];

if (arg1.equals("1"))
method = 1;

if (arg1.equals("2"))
method = 2;

if (arg1.equals("3"))
method = 3;
}

switch (method)
{
case 1:
createMethodOne();
break;
case 2:
createMethodTwo();
break;
case 3:
createMethodThree();
break;
default:
System.exit(-1);
}

run();
}


/** Identical to QuixGame. */
private static void createMethodOne()
{
System.out.println("Create method ONE.");

try
{
createFullscreenDisplay(true, false);
}
catch (LWJGLException e)
{
System.out.println("No suitable display modes found.");
System.exit(-1);
}
}
/** Like QuixGame, but limited to desktop colour depth */
private static void createMethodTwo()
{
System.out.println("Create method TWO.");
try
{
createFullscreenDisplay(true, true);
}
catch (LWJGLException e)
{
System.out.println("No suitable display modes found.");
System.exit(-1);
}
}

/** Copy-paste stright from QuixGame. */
private static void createFullscreenDisplay(boolean fullscreen, boolean useDesktopColour) throws LWJGLException
{
System.out.println("Limiting selection to desktop colours:"+useDesktopColour);

System.out.println("Attempting to create screen display...");

if (fullscreen)
System.out.println("Running in fullscreen mode.");
else
System.out.println("Running in windowed mode.");

// Choose a display mode (monitor setting, dimensions&freq)
DisplayMode[] modes = Display.getAvailableDisplayModes();

// Find suitable
ArrayList suitable = new ArrayList();
for (int i=0; i<modes.length; i++)
{
DisplayMode dm = modes[i];
if (dm.getWidth() == 800 && dm.getHeight() == 600)
{
if (dm.getFrequency() == 60 || dm.getFrequency() == 0)
{
if (useDesktopColour)
{
// Only add if a lower bpp.
if (dm.getBitsPerPixel() <= Display.getDisplayMode().getBitsPerPixel())
suitable.add(dm);
}
else
{
// Just add it.
suitable.add(dm);
}
}

}
}

if (suitable.size() == 0)
{
// No suitable display modes!
System.out.println("No suitable display modes detected!");
throw new LWJGLException("No suitable display modes found.");
}

Collections.sort(suitable, new DisplayModeComparator() );

System.out.println("Suitable display modes by preference:");
for (int i=0; i<suitable.size(); i++)
{
DisplayMode mode = (DisplayMode)suitable.get(i);
System.out.println("\t"+mode);
}

DisplayMode usedMode = null;
for (int i=0; i<suitable.size(); i++)
{
try
{
usedMode = (DisplayMode)suitable.get(i);
System.out.println("Trying mode "+usedMode);
Display.setDisplayMode( usedMode );
break;
}
catch (Exception e)
{
// Failed to set display mode
e.printStackTrace();
}
}


try
{
System.out.println("Creating display");

Display.setTitle("Quix!");
Display.setFullscreen(fullscreen);
Display.create();

if (usedMode.getFrequency() == 60)
Display.setVSyncEnabled(true);
}
catch (LWJGLException e1)
{
System.out.println("Could not create display.");
e1.printStackTrace();
throw e1;
}
}

/** Snagged pretty much direct from the LWJGL space invaders example. */
private static void createMethodThree()
{
System.out.println("Create method THREE.");

// get modes
DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(
800, 600, 800, 600, // Min & max res.
-1, -1, // Ignore bpp
60, 60); // Always 60Hz

try
{
org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
"width=" + 800,
"height=" + 600,
"freq=" + 60,
"bpp="
+ org.lwjgl.opengl.Display.getDisplayMode()
.getBitsPerPixel() });

Display.setFullscreen(true);
Display.create();
}
catch (Exception e)
{
System.out.println("Could not set display mode.");
e.printStackTrace();
System.exit(-1);
}
}



private static void run()
{
boolean gameRunning = true;

while (gameRunning)
{
Display.update();

// The window is in the foreground, so we should play the game
update();

// Stabilise the framerate if we haven't got vsync
Display.sync(60);


if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
gameRunning = false;
}

System.out.println("App.exit");
Display.destroy();
System.exit(0);
}

private static void update()
{
GL11.glBegin(GL11.GL_QUADS);
{
GL11.glColor3f(1f, 1f, 1f);

int w = Display.getDisplayMode().getWidth();
int h = Display.getDisplayMode().getHeight();

float x = (float)w * 0.2f;
float y = (float)h * 0.2f;
float x2 = (float)w * 0.8f;
float y2 = (float)h * 0.8f;

GL11.glVertex2f(x, y);
GL11.glVertex2f(x2, y);
GL11.glVertex2f(x2, y2);
GL11.glVertex2f(x, y2);

}
GL11.glEnd();
}
}

You probably need the mode sorter as well:

package quix;

import java.util.Comparator;

import org.lwjgl.opengl.DisplayMode;

/**
* @author John
*/
public class DisplayModeComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
if (obj1 instanceof DisplayMode && obj2 instanceof DisplayMode)
{
DisplayMode dm1 = (DisplayMode)obj1;
DisplayMode dm2 = (DisplayMode)obj2;

if (dm1.getFrequency() == dm2.getFrequency())
{
// Sort by bpp if freq identical
if (dm1.getBitsPerPixel() == dm2.getBitsPerPixel())
return 0;
else if (dm1.getBitsPerPixel() > dm2.getBitsPerPixel())
return -1;
else
return 1;

}
else if (dm1.getFrequency() > dm2.getFrequency())
{
return -1;
}
else
{
return 1;
}


// if (dm1.bpp > dm2.bpp)
// return 1;
// else
// return 2;
}
else
return 0;
}
}

Should be fairly self-explanitory. I wish I could give some hints as to what might be the problem but I'm at something of a loss. :oops:
Title: Colour depth for display
Post by: oNyx on October 12, 2004, 11:07:02
I tryed all 3 methods and none of em worked. :?

org.lwjgl.examples.Game doesn't work, too.

Ok. That's odd.

I have 3(!) different 0.92 builds here... and I tried em all...

filesize | fullscreen | windowed
880052 ok/ok
883274 ok/ok
835225 b0rked/ok

Nr3 is the one from Matzon (the same you got/used for Quix)... it seems to be slightly broken. With the other builds all 3 display setup methods work flawlessly.
Title: Colour depth for display
Post by: Orangy Tang on October 13, 2004, 17:44:07
I guess that points to a problem with the LWJGL build rather than my own code then. Great, now I can pass the blame. :wink: I guess Matzon or someone will know the differences between the builds.
Title: Colour depth for display
Post by: Matzon on October 14, 2004, 05:35:17
ehm. No. :)
The build I made was just from cvs - and *should* be no different from 0.92, except for the allocat method bug.

Apparently something else must also have changed...
This is a fresh dll, please try that:
http://matzon.dk/brian/lwjgl/builds/lwjgl.zip
Title: Colour depth for display
Post by: oNyx on October 14, 2004, 18:06:02
Using that dll with the two working builds results in:

UnsatisfiedLinkError: nCreateWindow

Using the dll with that broken lwjgl version works, but fullscreen is still broken there. Hm... :?