Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Failed to Set Display Mode  (Read 3088 times)
0 Members and 1 Guest are viewing this topic.
Offline nickdotjava

Junior Member




I have fallen to the dark side.  I'm using DX9


« Posted 2003-09-18 19:23:19 »

I'm using LWJGL .6.  Any reason on why I would get the "failed to set display mode error"?  It worked fine a week ago.  The only change I've made to my computer is installed the latest nvidia drivers.  I have the Gainward GForceFX5600 w/ 256 MB.

Thanks!

-Nick

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline nickdotjava

Junior Member




I have fallen to the dark side.  I'm using DX9


« Reply #1 - Posted 2003-09-18 19:34:35 »

I actually think I've found the problem.  The only option my monitor has for refresh rate is 60hz.  The display mode it tries to set is 72hz.  Is there a way to tell it that the max refresh should be 60?

Thanks!

-Nick

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2003-09-18 20:05:09 »

I find it hard to believe that your monitor can't do 72Hz suddenly... something's broken. I suggest you upgrade to LWJGL 0.7 now too before you get in too deep with 0.6 Cheesy

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #3 - Posted 2003-09-18 22:24:06 »

Quote
Is there a way to tell it that the max refresh should be 60?


Well, regardless of the cause of this new behaviour, yes there is a way of asking for 60Hz: just check that mode.freq == 60 in your DisplayMode selection code.

Hellomynameis Charlie Dobbie.
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2003-09-19 00:00:58 »

Actually what you should be doing is trying several modes out one after the other. It says in our not-very-clear specification that we cannot guarantee that every mode in the list of returned modes is valid which is why we can throw an Exception on setDisplayMode().

First you should filter out any unnacceptable modes. Then you should sort your display modes in order of preference and then try them all, one at a time, until one works. Only when everything has failed should you call Sys.alert() and inform the user that something is awry.

Here's a nifty bit of code in the SPGL which does this for you:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
package com.shavenpuppy.jglib;

import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;

import org.lwjgl.DisplayMode;
import org.lwjgl.Sys;
/**
 * Display initialization.
 */

public final class Display {
     
      private static final boolean DEBUG = false;
     
      /**
       * Determine the available display modes that match the specified minimum and maximum criteria.
       * If any given criterium is specified as -1 then it is ignored.
       *
       * @param minWidth the minimum display resolution in pixels
       * @param minHeight the minimum display resolution in pixels
       * @param maxWidth the maximum display resolution in pixels
       * @param maxHeight the maximum display resolution in pixels
       * @param minBPP the minimum bit depth per pixel
       * @param maxBPP the maximum bit depth per pixel
       * @param minFreq the minimum display frequency in Hz
       * @param maxFreq the maximum display frequency in Hz
       * @return an array of matching display modes
       */

      public static DisplayMode[] getAvailableDisplayModes(int minWidth, int minHeight, int maxWidth, int maxHeight, int minBPP, int maxBPP,
            int minFreq, int maxFreq)
      {
            // First get the available display modes
           DisplayMode[] modes = org.lwjgl.Display.getAvailableDisplayModes();
           
            if (Sys.DEBUG || DEBUG) {
                  System.out.println("Available screen modes:");
                  for (int i = 0; i < modes.length; i ++) {
                        System.out.println(modes[i]);
                  }
            }
           
            ArrayList matches = new ArrayList(modes.length);

            for (int i = 0; i < modes.length; i ++) {
                  assert modes[i] != null : ""+i+" "+modes.length;
                  if (minWidth != -1 && modes[i].width < minWidth)
                        continue;
                  if (maxWidth != -1 && modes[i].width > maxWidth)
                        continue;
                  if (minHeight != -1 && modes[i].height < minHeight)
                        continue;
                  if (maxHeight != -1 && modes[i].height > maxHeight)
                        continue;
                  if (minBPP != -1 && modes[i].bpp < minBPP)
                        continue;
                  if (maxBPP != -1 && modes[i].bpp > maxBPP)
                        continue;
                  //if (modes[i].bpp == 24)
                 //      continue;
                 if (modes[i].freq != 0) {
                        if (minFreq != -1 && modes[i].freq < minFreq)
                              continue;
                        if (maxFreq != -1 && modes[i].freq > maxFreq)
                              continue;
                  }
                  matches.add(modes[i]);
            }
           
            DisplayMode[] ret = new DisplayMode[matches.size()];
            matches.toArray(ret);
            if (Sys.DEBUG && DEBUG) {
                  System.out.println("Filtered screen modes:");
                  for (int i = 0; i < ret.length; i ++) {
                        System.out.println(ret[i]);
                  }
            }
           
            return ret;
      }
     
      /**
       * Create the display by choosing from a list of display modes based on an order of preference.
       * You must supply a list of allowable display modes, probably by calling getAvailableDisplayModes(),
       * and an array with the order in which you would like them sorted in descending order.
       * This method attempts to create the topmost display mode; if that fails, it will try the next one,
       * and so on, until there are no modes left. If no mode is set at the end, an exception is thrown.
       * @param dm[] a list of display modes to choose from
       * @param param[] the names of the DisplayMode fields in the order in which you would like them sorted.
       * @param fullscreen whether to go fullscreen
       * @param title The app title
       * @return the chosen display mode
       * @throws NoSuchFieldException if one of the params is not a field in DisplayMode
       * @throws Exception if no display mode could be set
       * @see org.lwjgj.DisplayMode
       */

      public static DisplayMode setDisplayMode(DisplayMode[] dm, final String[] param) throws Exception {
           
           
            class Sorter implements Comparator {
                 
                  final Field[] field;
                  final int[] order;
                  final boolean[] usePreferred;
                  final int[] preferred;
                 
                  Sorter() throws NoSuchFieldException {
                        field = new Field[param.length];
                        order = new int[param.length];
                        preferred = new int[param.length];
                        usePreferred = new boolean[param.length];
                        for (int i = 0; i < field.length; i ++) {
                              int idx = param[i].indexOf('=');
                              if (idx > 0) {
                                    preferred[i] = Integer.parseInt(param[i].substring(idx + 1, param[i].length()));
                                    usePreferred[i] = true;
                                    param[i] = param[i].substring(0, idx);
                                    field[i] = DisplayMode.class.getField(param[i]);
                              } else if (param[i].charAt(0) == '-') {
                                    field[i] = DisplayMode.class.getField(param[i].substring(1));
                                    order[i] = -1;
                              } else {
                                    field[i] = DisplayMode.class.getField(param[i]);
                                    order[i] = 1;
                              }
                        }
                  }
                 
                  /**
                   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
                   */

                  public int compare(Object o1, Object o2) {
                        DisplayMode dm1 = (DisplayMode) o1;
                        DisplayMode dm2 = (DisplayMode) o2;
                       
                        for (int i = 0; i < field.length; i ++) {
                              try {
                                    int f1 = field[i].getInt(dm1);
                                    int f2 = field[i].getInt(dm2);
                                   
                                    if (usePreferred[i] && f1 != f2) {
                                          if (f1 == preferred[i])
                                                return -1;
                                          else if (f2 == preferred[i])
                                                return 1;
                                          else {
                                                // Score according to the difference between the values
                                               int absf1 = Math.abs(f1 - preferred[i]);
                                                int absf2 = Math.abs(f2 - preferred[i]);
                                                if (absf1 < absf2)
                                                      return -1;
                                                else if (absf1 > absf2)
                                                      return 1;
                                                else
                                                      continue;
                                          }
                                    } else if (f1 < f2)
                                          return order[i];
                                    else if (f1 == f2)
                                          continue;
                                    else
                                          return -order[i];
                                   
                              } catch (Exception e) {
                                    assert false : e.getMessage();                                    
                              }
                        }
                       
                        return 0;
                  }
            }
           
            // Sort the display modes
           Arrays.sort(dm, new Sorter());
           
            // Try them out in the appropriate order
           if (Sys.DEBUG || DEBUG) {
                  System.out.println("Sorted display modes:");
                  for (int i = 0; i < dm.length; i ++) {
                        System.out.println(dm[i]);
                  }
            }
            for (int i = 0; i < dm.length; i ++) {
                  try {
                        if (Sys.DEBUG || DEBUG)
                              System.out.println("Attempting to set displaymode: "+dm[i]);
                        org.lwjgl.Display.setDisplayMode(dm[i]);
                        return dm[i];
                  } catch (Exception e) {
                        if (Sys.DEBUG || DEBUG) {
                              System.out.println("Failed to set display mode to "+dm[i]);
                              e.printStackTrace();
                        }
                  }
            }
           
            throw new Exception("Failed to set display mode.");
      }
     
}



Cas Smiley

Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2003-09-19 00:02:08 »

Here's an actual example of getting the right display mode, lifted straight from Alien Flux's init code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
            if (!AL.isCreated()) {
                  try {
                        AL.create();
                        soundPlayer = new SoundPlayer(16);
                  } catch (Exception e) {
                        e.printStackTrace();
                        Sys.alert(
                              GAME_TITLE,
                              "You need an OpenAL compatible sound card to to hear the sound effects and music in "
                                    + GAME_TITLE
                                    + ". You may have a suitable card but not have appropriate drivers. Please contact "
                                    + EMAIL_ADDRESS
                                    + " for assistance if you need help finding drivers for your sound card.");
                  }
            }

            if (!WINDOWED) {
                  try {
                        // Filter out modes smaller than 640x480, but anything bigger will do. We also pick frequencies between
                       // 60Hz and 85Hz as sometimes display drivers like to give us dodgy values which can blow up monitors.
                       // We don't care what colour depth it gives us.
                       DisplayMode[] dm = Display.getAvailableDisplayModes(640, 480, -1, -1, -1, -1, 60, 85);
                        Display.setDisplayMode(dm, new String[] {
                              "width="+options.getPreferredWidth(), // Will pick the preferred width, otherwise the nearest it can
                             "height="+options.getPreferredHeight(), // Will pick the preffered height, otherwise the nearest it can
                             "freq="+PREFERRED_FREQUENCY, // Will pick 60Hz over any other frequency, and otherwise pick the fastest it can
                             "bpp=" + (options.isTrueColour() ? "32" : "16"), // Pick 16 or 32 bit over any other depth, otherwise the nearest it can
                       });
                 
                  } catch (Exception e) {
                        e.printStackTrace();
                  }
            }
           
            try {
                  Window window;
                  if (WINDOWED) {
                        width = java.lang.Math.min(org.lwjgl.Display.getWidth(), WINDOW_WIDTH);
                        height = java.lang.Math.min(org.lwjgl.Display.getHeight(), WINDOW_HEIGHT);
                        Window.create(GAME_TITLE, 0, 0, width, height, 16, 0, 0, 0);
                  } else {
                        width = org.lwjgl.Display.getWidth();
                        height = org.lwjgl.Display.getHeight();
                        try {
                              Window.create(GAME_TITLE, 16, 0, 0, 0);
                        } catch (Exception e) {
                              width = java.lang.Math.min(org.lwjgl.Display.getWidth(), WINDOW_WIDTH);
                              height = java.lang.Math.min(org.lwjgl.Display.getHeight(), WINDOW_HEIGHT);
                              Window.create(GAME_TITLE, 0, 0, width, height, 16, 0, 0, 0);
                        }
                  }
                  GLCaps.determineAvailableExtensions();
            } catch (Exception e) {
                  e.printStackTrace();
                  cleanup();
                  Sys.alert(
                        GAME_TITLE,
                        "You need an OpenGL compatible graphics card to run "
                              + GAME_TITLE
                              + ". You may have a suitable graphics card but not have OpenGL drivers. Please contact "
                              + EMAIL_ADDRESS
                              + " for assistance if you need help finding OpenGL drivers for your graphics card.");
                  System.exit(0);
            }

           
            try {
                  Keyboard.create();
                  try {
                        Keyboard.enableBuffer();
                        try {
                              Keyboard.enableTranslation();
                        } catch (Exception e) {
                              System.out.println("Failed to enable translation:"+e);
                        }
                  } catch (Exception e) {
                        System.out.println("Failed to enable keyboard buffer:"+e);
                  }
            } catch (Exception e) {
                  System.out.println("Failed to create keyboard:"+e);
            }
            try {
                  Mouse.create();
            } catch (Exception e) {
                  System.out.println("Failed to create mouse:"+e);
            }
            try {
                  Controller.create();
            } catch (Exception e) {
                  System.out.println("Failed to create controller:"+e);
            }
           
            // Sync to vertical blank if we can to smooth things out a bit
           if (GLCaps.WGL_EXT_swap_control && org.lwjgl.Display.getFrequency() == PREFERRED_FREQUENCY && !Sys.DEBUG) {
                  GL.wglSwapIntervalEXT(1);
            } else {
                  // Turn off swap control if we can't get 60Hz display
                 GLCaps.WGL_EXT_swap_control = false;
            }


Cas Smiley

Offline nickdotjava

Junior Member




I have fallen to the dark side. &nbsp;I'm using DX9


« Reply #6 - Posted 2003-09-30 01:25:42 »

Thanks!  It works.

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2003-09-30 01:51:07 »

I should hope it does!

Cas Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (15 views)
2014-07-29 18:09:19

Riven (10 views)
2014-07-29 18:08:52

Dwinin (10 views)
2014-07-29 10:59:34

E.R. Fleming (28 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!