Re: [AD] I've made a modification to the set_gfx_mode function

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


>> My own patch effecively duplicates the already existing code for parsing
>> the gfx_mode variable, so I think it might be sensible to move it over to a
>> seperate function (I'd be willing to do that, btw).
>
>Any patches which removes redundant code are welcome.
>

Ok, attached to this mail is a .diff that will move the code for parsing
the gfx_mode/gfx_modew config variables from set_gfx_mode() to a helper
function.

I made the .diff from the latest cvs graphics.c I could get. Note that this
diff presumes the patch I submitted earlier (the one that added the
gfx_modew variable), which I noticed was already applied to the graphics.c
I downloaded from cvs.

Evert
diff -U3 allegro_old/src/graphics.c allegro_modified/src/graphics.c
--- allegro_old/src/graphics.c	Fri Jul 27 23:21:12 2001
+++ allegro_modified/src/graphics.c	Fri Jul 27 23:28:14 2001
@@ -366,6 +366,77 @@
 
 
 
+/* get_config_gfx_driver:
+ *  Helper function for set_gfx_mode: it reads the gfx_card* config variables
+ *  and sets the gfx_driver variable if the config variable was found
+ */
+static int get_config_gfx_driver(char *gfx_card, int check_mode, int require_window, _DRIVER_INFO *driver_list, int card, int w, int h, int v_w, int v_h)
+{
+   int tried = FALSE;
+   char buf[512], tmp[64];
+   int c, n;
+
+   /* try the drivers that are listed in the config file */
+   for (n=-2; n<255; n++) {
+      switch (n) {
+
+	 case -2:
+	    /* example: gfx_cardw_640x480x16 = */
+	    uszprintf(buf, sizeof(buf), uconvert_ascii("%s_%dx%dx%d", tmp), uconvert_ascii(gfx_card, NULL), w, h, _color_depth);
+	    break;
+
+	 case -1:
+	    /* example: gfx_cardw_24bpp = */
+	    uszprintf(buf, sizeof(buf), uconvert_ascii("%s_%dbpp", tmp), uconvert_ascii(gfx_card, NULL), _color_depth);
+	    break;
+
+	 case 0:
+	    /* example: gfx_cardw = */
+	    uszprintf(buf, sizeof(buf), uconvert_ascii("%s", tmp), uconvert_ascii(gfx_card, NULL));
+	    break;
+
+	 default:
+	    /* example: gfx_cardw1 = */
+	    uszprintf(buf, sizeof(buf), uconvert_ascii("%s%d", tmp), uconvert_ascii(gfx_card, NULL), n);
+	    break;
+      }
+      card = get_config_id(uconvert_ascii("graphics", tmp), buf, GFX_AUTODETECT);
+
+      if (card != GFX_AUTODETECT) {
+	 for (c=0; driver_list[c].driver; c++) {
+	    if (driver_list[c].id == card) {
+	       gfx_driver = driver_list[c].driver;
+	       if (check_mode) {
+		  if (((require_window) && (!gfx_driver->windowed)) ||
+		      ((!require_window) && (gfx_driver->windowed))) {
+		     gfx_driver = NULL;
+		     continue;
+		  }
+	       }
+	       break;
+	    }
+	 }
+	 if (gfx_driver) {
+	    tried = TRUE;
+	    gfx_driver->name = gfx_driver->desc = get_config_text(gfx_driver->ascii_name);
+	    screen = gfx_driver->init(w, h, v_w, v_h, _color_depth);
+	    if (screen)
+	       break;
+	    else
+	       gfx_driver = NULL;
+	 }
+      }
+      else {
+	 if (n > 1)
+	    break;
+      }
+   }
+
+   return tried;
+}
+
+
+
 /* set_gfx_mode:
  *  Sets the graphics mode. The card should be one of the GFX_* constants
  *  from allegro.h, or GFX_AUTODETECT to accept any graphics driver. Pass
@@ -511,122 +582,12 @@
    usetc(allegro_error, 0);
 
    /* Try windowed mode drivers first if GFX_AUTODETECT_WINDOWED was selected */
-   if ((card == GFX_AUTODETECT) && (allow_config) && (require_window)) {
-      /* try the drivers that are listed in the config file */
-      for (n=-2; n<255; n++) {
-	 switch (n) {
-
-	    case -2:
-	       /* example: gfx_cardw_640x480x16 = */
-	       uszprintf(buf, sizeof(buf), uconvert_ascii("gfx_cardw_%dx%dx%d", tmp), w, h, _color_depth);
-	       break;
-
-	    case -1:
-	       /* example: gfx_cardw_24bpp = */
-	       uszprintf(buf, sizeof(buf), uconvert_ascii("gfx_cardw_%dbpp", tmp), _color_depth);
-	       break;
-
-	    case 0:
-	       /* example: gfx_cardw = */
-	       ustrzcpy(buf, sizeof(buf), uconvert_ascii("gfx_cardw", tmp));
-	       break;
-
-	    default:
-	       /* example: gfx_cardw1 = */
-	  uszprintf(buf, sizeof(buf), uconvert_ascii("gfx_cardw%d", tmp), n);
-	       break;
-	 }
-	 card = get_config_id(uconvert_ascii("graphics", tmp), buf, GFX_AUTODETECT);
-
-	 if (card != GFX_AUTODETECT) {
-	    for (c=0; driver_list[c].driver; c++) {
-	       if (driver_list[c].id == card) {
-		  gfx_driver = driver_list[c].driver;
-		  if (check_mode) {
-		     if (((require_window) && (!gfx_driver->windowed)) ||
-			 ((!require_window) && (gfx_driver->windowed))) {
-			gfx_driver = NULL;
-			continue;
-		     }
-		  }
-		  break;
-	       }
-	    }
-	    if (gfx_driver) {
-	       tried = TRUE;
-	       gfx_driver->name = gfx_driver->desc = get_config_text(gfx_driver->ascii_name);
-	       screen = gfx_driver->init(w, h, v_w, v_h, _color_depth);
-	       if (screen)
-		  break;
-	       else
-		  gfx_driver = NULL;
-	    }
-	 }
-	 else {
-	    if (n > 1)
-	       break;
-	 }
-      }
-   }
+   if ((card == GFX_AUTODETECT) && (allow_config) && (require_window))
+      tried = get_config_gfx_driver("gfx_cardw", check_mode, require_window, driver_list, card, w, h, v_w, v_h);
 
    /* check the gfx_card config variable if gfx_cardw wasn't used */
-   if ((card == GFX_AUTODETECT) && (allow_config) && (!gfx_driver)) {
-      /* try the drivers that are listed in the config file */
-      for (n=-2; n<255; n++) {
-	 switch (n) {
-
-	    case -2:
-	       /* example: gfx_card_640x480x16 = */
-	       uszprintf(buf, sizeof(buf), uconvert_ascii("gfx_card_%dx%dx%d", tmp), w, h, _color_depth);
-	       break;
-
-	    case -1:
-	       /* example: gfx_card_24bpp = */
-	       uszprintf(buf, sizeof(buf), uconvert_ascii("gfx_card_%dbpp", tmp), _color_depth);
-	       break;
-
-	    case 0:
-	       /* example: gfx_card = */
-	       ustrzcpy(buf, sizeof(buf), uconvert_ascii("gfx_card", tmp));
-	       break;
-
-	    default:
-	       /* example: gfx_card1 = */
-	       uszprintf(buf, sizeof(buf), uconvert_ascii("gfx_card%d", tmp), n);
-	       break;
-	 }
-	 card = get_config_id(uconvert_ascii("graphics", tmp), buf, GFX_AUTODETECT);
-
-	 if (card != GFX_AUTODETECT) {
-	    for (c=0; driver_list[c].driver; c++) {
-	       if (driver_list[c].id == card) {
-		  gfx_driver = driver_list[c].driver;
-		  if (check_mode) {
-		     if (((require_window) && (!gfx_driver->windowed)) ||
-			 ((!require_window) && (gfx_driver->windowed))) {
-			gfx_driver = NULL;
-			continue;
-		     }
-		  }
-		  break;
-	       }
-	    }
-	    if (gfx_driver) {
-	       tried = TRUE;
-	       gfx_driver->name = gfx_driver->desc = get_config_text(gfx_driver->ascii_name);
-	       screen = gfx_driver->init(w, h, v_w, v_h, _color_depth);
-	       if (screen)
-		  break;
-	       else
-		  gfx_driver = NULL;
-	    }
-	 }
-	 else {
-	    if (n > 1)
-	       break;
-	 }
-      }
-   }
+   if ((card == GFX_AUTODETECT) && (allow_config) && (!gfx_driver))
+      tried = get_config_gfx_driver("gfx_card", check_mode, require_window, driver_list, card, w, h, v_w, v_h);
 
    if (!tried) {
       /* search table for the requested driver */


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