[AD] modesel patch

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hi,

gfx_mode_select_ex's behaviour annoys me: when I select another
graphics driver, resolution and color depth are reset, and when I
select another resolution color depth is reset.  I've made
patches for 4.0.3 and 4.1.9, which keep the current settings as
far as possible.  What do you think about it?

Regards,
Magnus Henoch
mange@xxxxxxxxxx
diff -ru allegro-4.0.3/src/modesel.c allegro-4.0.3-modesel/src/modesel.c
--- allegro-4.0.3/src/modesel.c	Wed Jul  3 22:18:05 2002
+++ allegro-4.0.3-modesel/src/modesel.c	Sat Feb  8 18:09:11 2003
@@ -145,23 +145,89 @@
 };
 
 
+/* bpp_index_to_bpp:
+ *  Returns the real bpp value of the nth depth of mode
+ *  what_mode of driver what_driver.
+ */
+static int bpp_index_to_bpp(int n, int what_driver, int what_mode)
+{
+   int i, j;
+   j = -1;
+   for (i=0; i < BPP_TOTAL; i++) {
+      if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
+         j++;
+         if (j == n) switch (i) {
+            case BPP_08: return 8;
+            case BPP_15: return 15;
+            case BPP_16: return 16;
+            case BPP_24: return 24;
+            case BPP_32: return 32;
+         }
+      }
+   }
+
+   /* this should never happen */
+   ASSERT(0);
+   return -1;
+}
+
+/* bpp_to_bpp_index:
+ *  Returns the index in the depth list of mode what_mode
+ *  of driver what_driver corresponding to the given
+ *  depth, or -1 if there is no such depth.
+ */
+static int bpp_to_bpp_index(int depth, int what_driver, int what_mode)
+{
+   int i, what_bpp = -1;
+   for (i=0; i < BPP_TOTAL; i++) {
+      if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
+         what_bpp++;
+         switch (depth) {
+            case  8: if (i == BPP_08) return what_bpp;
+            case 15: if (i == BPP_15) return what_bpp;
+            case 16: if (i == BPP_16) return what_bpp;
+            case 24: if (i == BPP_24) return what_bpp;
+            case 32: if (i == BPP_32) return what_bpp;
+         }
+      }
+   }
+
+   return -1;
+}
 
 /* d_listchange_proc:
  *  Stores the current driver in d1 and graphics mode in d2;
  *  if a new driver is selected in the listbox, it changes the
- *  w/h and cdepth listboxes so that they redraw and they
- *  lose their selections. likewise if a new w/h is selected the
- *  cdepth listbox is updated.
+ *  w/h and cdepth listboxes so that they redraw. selections are
+ *  kept if supported by the new driver. likewise if a new w/h
+ *  is selected the cdepth listbox is updated.
  */
 int d_listchange_proc(int msg, DIALOG* d, int c)
 {
+   int width, height, depth = 0;
+
    if (msg != MSG_IDLE)
       return D_O_K;
 
+   width = driver_list[d->d1].mode_list[d->d2].w;
+   height = driver_list[d->d1].mode_list[d->d2].h;
+   if (what_dialog == gfx_mode_ex_dialog) {
+     depth = bpp_index_to_bpp(what_dialog[GFX_DEPTHLIST].d1, d->d1, d->d2);
+   }
+
    if (what_dialog[GFX_DRIVERLIST].d1 != d->d1) {
+      int i;
+
       d->d1 = what_dialog[GFX_DRIVERLIST].d1;
-      d->d2 = what_dialog[GFX_MODELIST].d1;
       what_dialog[GFX_MODELIST].d1 = 0;
+      for (i = 0; i < driver_list[d->d1].mode_count; i++) {
+         if (driver_list[d->d1].mode_list[i].w == width &&
+	     driver_list[d->d1].mode_list[i].h == height) {
+	   what_dialog[GFX_MODELIST].d1 = i;
+	   break;
+	 }
+      }
+      d->d2 = what_dialog[GFX_MODELIST].d1;
       what_dialog[GFX_MODELIST].d2 = 0;
 
       scare_mouse();
@@ -169,7 +235,9 @@
       unscare_mouse();
 
       if (what_dialog == gfx_mode_ex_dialog) {
-         what_dialog[GFX_DEPTHLIST].d1 = 0;
+         what_dialog[GFX_DEPTHLIST].d1 = bpp_to_bpp_index(depth, d->d1, d->d2);
+         if (what_dialog[GFX_DEPTHLIST].d1 == -1)
+            what_dialog[GFX_DEPTHLIST].d1 = 0;
 
          scare_mouse();
          object_message(&what_dialog[GFX_DEPTHLIST], MSG_DRAW, 0);
@@ -181,7 +249,9 @@
       d->d2 = what_dialog[GFX_MODELIST].d1;
 
       if (what_dialog == gfx_mode_ex_dialog) {
-         what_dialog[GFX_DEPTHLIST].d1 = 0;
+         what_dialog[GFX_DEPTHLIST].d1 = bpp_to_bpp_index(depth, d->d1, d->d2);
+         if (what_dialog[GFX_DEPTHLIST].d1 == -1)
+            what_dialog[GFX_DEPTHLIST].d1 = 0;
 
          scare_mouse();
          object_message(&what_dialog[GFX_DEPTHLIST], MSG_DRAW, 0);
@@ -533,7 +603,7 @@
  */
 int gfx_mode_select_ex(int *card, int *w, int *h, int *color_depth)
 {
-   int i, j, ret, what_driver, what_mode, what_bpp;
+   int i, ret, what_driver, what_mode, what_bpp;
 
    clear_keybuf();
 
@@ -563,20 +633,9 @@
    }
 
    what_mode = i;
-   what_bpp = -1;
+   what_bpp = bpp_to_bpp_index(*color_depth, what_driver, what_mode);
 
-   for (i=0; i < BPP_TOTAL; i++) {
-      if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
-         what_bpp++;
-         switch (*color_depth) {
-            case  8: if (i == BPP_08) gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-            case 15: if (i == BPP_15) gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-            case 16: if (i == BPP_16) gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-            case 24: if (i == BPP_24) gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-            case 32: if (i == BPP_32) gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-         }
-      }
-   }
+   gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp;
 
    gfx_mode_ex_dialog[GFX_TITLE].dp = (void*)get_config_text("Graphics Mode");
    gfx_mode_ex_dialog[GFX_OK].dp = (void*)get_config_text("OK");
@@ -596,19 +655,7 @@
    *w = driver_list[what_driver].mode_list[what_mode].w;
    *h = driver_list[what_driver].mode_list[what_mode].h;
 
-   j = -1;
-   for (i=0; i < BPP_TOTAL; i++) {
-      if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
-         j++;
-         if (j == what_bpp) switch (i) {
-            case BPP_08: *color_depth = 8;  break;
-            case BPP_15: *color_depth = 15; break;
-            case BPP_16: *color_depth = 16; break;
-            case BPP_24: *color_depth = 24; break;
-            case BPP_32: *color_depth = 32; break;
-         }
-      }
-   }
+   *color_depth = bpp_index_to_bpp(what_bpp, what_driver, what_mode);
 
    destroy_driver_list();
 
--- allegro-4.1.9/src/modesel.c	Sun Jan 26 18:22:55 2003
+++ allegro-4.1.9-modesel/src/modesel.c	Sun Feb  9 00:28:35 2003
@@ -145,31 +145,100 @@
 };
 
 
+/* bpp_index_to_bpp:
+ *  Returns the real bpp value of the nth depth of mode
+ *  what_mode of driver what_driver.
+ */
+static int bpp_index_to_bpp(int n, int what_driver, int what_mode)
+{
+   int i, j;
+   j = -1;
+   for (i=0; i < BPP_TOTAL; i++) {
+      if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
+         j++;
+         if (j == n) switch (i) {
+            case BPP_08: return 8;
+            case BPP_15: return 15;
+            case BPP_16: return 16;
+            case BPP_24: return 24;
+            case BPP_32: return 32;
+         }
+      }
+   }
+
+   /* this should never happen */
+   ASSERT(0);
+   return -1;
+}
+
+/* bpp_to_bpp_index:
+ *  Returns the index in the depth list of mode what_mode
+ *  of driver what_driver corresponding to the given
+ *  depth, or -1 if there is no such depth.
+ */
+static int bpp_to_bpp_index(int depth, int what_driver, int what_mode)
+{
+   int i, what_bpp = -1;
+   for (i=0; i < BPP_TOTAL; i++) {
+      if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
+         what_bpp++;
+         switch (depth) {
+            case  8: if (i == BPP_08) return what_bpp;
+            case 15: if (i == BPP_15) return what_bpp;
+            case 16: if (i == BPP_16) return what_bpp;
+            case 24: if (i == BPP_24) return what_bpp;
+            case 32: if (i == BPP_32) return what_bpp;
+         }
+      }
+   }
+
+   return -1;
+}
 
 /* d_listchange_proc:
  *  Stores the current driver in d1 and graphics mode in d2;
  *  if a new driver is selected in the listbox, it changes the
- *  w/h and cdepth listboxes so that they redraw and they
- *  lose their selections. likewise if a new w/h is selected the
- *  cdepth listbox is updated.
+ *  w/h and cdepth listboxes so that they redraw. selections are
+ *  kept if supported by the new driver. likewise if a new w/h
+ *  is selected the cdepth listbox is updated.
  */
 int d_listchange_proc(int msg, DIALOG* d, int c)
 {
+   int width, height, depth = 0;
+
    ASSERT(d);
    
    if (msg != MSG_IDLE)
       return D_O_K;
 
+   width = driver_list[d->d1].mode_list[d->d2].w;
+   height = driver_list[d->d1].mode_list[d->d2].h;
+   if (what_dialog == gfx_mode_ex_dialog) {
+     depth = bpp_index_to_bpp(what_dialog[GFX_DEPTHLIST].d1, d->d1, d->d2);
+   }
+ 
    if (what_dialog[GFX_DRIVERLIST].d1 != d->d1) {
+      int i;
+
       d->d1 = what_dialog[GFX_DRIVERLIST].d1;
-      d->d2 = what_dialog[GFX_MODELIST].d1;
       what_dialog[GFX_MODELIST].d1 = 0;
+      for (i = 0; i < driver_list[d->d1].mode_count; i++) {
+         if (driver_list[d->d1].mode_list[i].w == width &&
+             driver_list[d->d1].mode_list[i].h == height) {
+           what_dialog[GFX_MODELIST].d1 = i;
+           break;
+         }
+      }
+      d->d2 = what_dialog[GFX_MODELIST].d1;
       what_dialog[GFX_MODELIST].d2 = 0;
 
       object_message(&what_dialog[GFX_MODELIST], MSG_DRAW, 0);
 
       if (what_dialog == gfx_mode_ex_dialog) {
-         what_dialog[GFX_DEPTHLIST].d1 = 0;
+         what_dialog[GFX_DEPTHLIST].d1 = bpp_to_bpp_index(depth, d->d1, d->d2);
+         if (what_dialog[GFX_DEPTHLIST].d1 == -1)
+            what_dialog[GFX_DEPTHLIST].d1 = 0;
+
          object_message(&what_dialog[GFX_DEPTHLIST], MSG_DRAW, 0);
       }
    }
@@ -178,7 +247,10 @@
       d->d2 = what_dialog[GFX_MODELIST].d1;
 
       if (what_dialog == gfx_mode_ex_dialog) {
-         what_dialog[GFX_DEPTHLIST].d1 = 0;
+         what_dialog[GFX_DEPTHLIST].d1 = bpp_to_bpp_index(depth, d->d1, d->d2);
+         if (what_dialog[GFX_DEPTHLIST].d1 == -1)
+            what_dialog[GFX_DEPTHLIST].d1 = 0;
+
          object_message(&what_dialog[GFX_DEPTHLIST], MSG_DRAW, 0);
       }
    }
@@ -483,7 +555,7 @@
  */
 int gfx_mode_select_ex(int *card, int *w, int *h, int *color_depth)
 {
-   int i, j, ret, what_driver, what_mode, what_bpp, extd;
+   int i, ret, what_driver, what_mode, what_bpp, extd;
    ASSERT(card);
    ASSERT(w);
    ASSERT(h);
@@ -523,20 +595,9 @@
       }
 
       what_mode = i;
-      what_bpp = -1;
-
-      for (i=0; i < BPP_TOTAL; i++) {
-         if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
-            what_bpp++;
-            switch (*color_depth) {
-               case  8: if (i == BPP_08) what_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-               case 15: if (i == BPP_15) what_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-               case 16: if (i == BPP_16) what_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-               case 24: if (i == BPP_24) what_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-               case 32: if (i == BPP_32) what_dialog[GFX_DEPTHLIST].d1 = what_bpp; break;
-            }
-         }
-      }
+      what_bpp = bpp_to_bpp_index(*color_depth, what_driver, what_mode);
+  
+      gfx_mode_ex_dialog[GFX_DEPTHLIST].d1 = what_bpp;
    }
 
    centre_dialog(what_dialog);
@@ -557,19 +618,7 @@
    *h = driver_list[what_driver].mode_list[what_mode].h;
 
    if (extd) {
-      j = -1;
-      for (i=0; i < BPP_TOTAL; i++) {
-         if (driver_list[what_driver].mode_list[what_mode].bpp[i]) {
-            j++;
-            if (j == what_bpp) switch (i) {
-               case BPP_08: *color_depth = 8;  break;
-               case BPP_15: *color_depth = 15; break;
-               case BPP_16: *color_depth = 16; break;
-               case BPP_24: *color_depth = 24; break;
-               case BPP_32: *color_depth = 32; break;
-            }
-         }
-      }
+      *color_depth = bpp_index_to_bpp(what_bpp, what_driver, what_mode);
    }
 
    destroy_driver_list();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/