Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2005:
[Freeciv-Dev] (PR#12060) generating the "correct" tile mask
Home

[Freeciv-Dev] (PR#12060) generating the "correct" tile mask

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12060) generating the "correct" tile mask
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 7 Feb 2005 15:32:25 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12060 >

Here is an updated and improved patch.  Also attached are the masks
generated.

-jason

? fog
? fog.c
? fog.png
? foo
? client/mask-128x64.png
? client/mask-12x6.png
? client/mask-16x8.png
? client/mask-192x96.png
? client/mask-24x12.png
? client/mask-256x128.png
? client/mask-2x1.png
? client/mask-32x16.png
? client/mask-48x24.png
? client/mask-4x2.png
? client/mask-64x32.png
? client/mask-8x4.png
? client/mask-96x48.png
? client/masks.png
? data/isotrident/grid.png
? data/isotrident/grid.xcf
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.178
diff -u -r1.178 mapview_common.c
--- client/mapview_common.c     7 Feb 2005 22:26:21 -0000       1.178
+++ client/mapview_common.c     7 Feb 2005 23:31:36 -0000
@@ -2737,3 +2737,70 @@
   /* Create a dummy map to make sure mapview_canvas.store is never NULL. */
   map_canvas_resized(1, 1);
 }
+
+/**************************************************************************
+  Generate a canvas mask of the given size.
+
+  Canvases have no transparency so the tile is just black and white.
+**************************************************************************/
+struct canvas *generate_tile_mask(int W, int H,
+                                 int *canvas_width, int *canvas_height)
+{
+  const int old_W = NORMAL_TILE_WIDTH, old_H = NORMAL_TILE_HEIGHT;
+  int x, y, i;
+  struct canvas *canvas;
+  int offsets[4][2];
+
+  NORMAL_TILE_WIDTH = W;
+  NORMAL_TILE_HEIGHT = H;
+
+  offsets[0][0] = offsets[0][1] = 0;
+  map_to_gui_vector(&offsets[1][0], &offsets[1][1], 0, 1);
+  map_to_gui_vector(&offsets[2][0], &offsets[2][1], 1, 0);
+  map_to_gui_vector(&offsets[3][0], &offsets[3][1], 1, 1);  
+  for (i = 0; i < 4; i++) {
+    offsets[i][0] /= 2;
+    offsets[i][1] /= 2;
+  }
+
+  *canvas_width = 4 * (W + 1) + 1;
+  *canvas_height = H + 2;
+
+  /* White is used for the foreground, because the caller may want to use the
+   * returned canvas as its own mask. */
+  canvas = canvas_create(*canvas_width, *canvas_height);
+  canvas_put_rectangle(canvas, COLOR_STD_GROUND, 0, 0, *canvas_width,
+                      *canvas_height);
+  for (i = 0; i < 4; i++) {
+    for (x = 0; x < W; x++) {
+      for (y = 0; y < H; y++) {
+       int map_x, map_y;
+       int idx;
+       enum color_std colors[] = {
+         COLOR_STD_RED,
+         COLOR_STD_WHITE,
+         COLOR_STD_OCEAN,
+         COLOR_STD_YELLOW
+       };
+       const int gui_x = x + offsets[i][0], gui_y = y + offsets[i][1];
+       const int canvas_x = x + i * (W + 1) + 1, canvas_y = y + 1;
+
+       gui_to_map_pos(&map_x, &map_y, gui_x, gui_y);
+       idx = (map_x & 1) + 2 *(map_y & 1);
+       assert(idx >= 0 && idx < ARRAY_SIZE(colors));
+       canvas_put_rectangle(canvas, colors[idx], canvas_x, canvas_y, 1, 1);
+      }
+    }
+    if (H % 4 != 0) {
+      /* If the dimensions aren't divisible by 4 we can't generate the offset
+       * masks. (Technically the corner mask only requires divisibility by
+       * 2.) */
+      break;
+    }
+  }
+
+  NORMAL_TILE_WIDTH = old_W;
+  NORMAL_TILE_HEIGHT = old_H;
+
+  return canvas;
+}
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.88
diff -u -r1.88 mapview_common.h
--- client/mapview_common.h     7 Feb 2005 22:26:21 -0000       1.88
+++ client/mapview_common.h     7 Feb 2005 23:31:36 -0000
@@ -306,4 +306,7 @@
 bool map_canvas_resized(int width, int height);
 void init_mapcanvas_and_overview(void);
 
+struct canvas *generate_tile_mask(int W, int H,
+                                 int *canvas_width, int *canvas_height);
+
 #endif /* FC__MAPVIEW_COMMON_H */
Index: client/gui-gtk-2.0/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gui_main.c,v
retrieving revision 1.107
diff -u -r1.107 gui_main.c
--- client/gui-gtk-2.0/gui_main.c       5 Feb 2005 19:46:32 -0000       1.107
+++ client/gui-gtk-2.0/gui_main.c       7 Feb 2005 23:31:37 -0000
@@ -1042,6 +1042,49 @@
   log_set_callback(log_callback_utf8);
 }
 
+static void make_mask(void)
+{
+  struct canvas *canvas;
+  struct Sprite *sprite;
+  int dimensions[][2] = {{256, 128}, {192, 96}, {128, 64}, {96, 48}, 
+                        {64, 32}, {48, 24}, {32, 16}, {24, 12},
+                        {16, 8}, {12, 6}, {8, 4}, {4, 2}, {2, 1}};
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(dimensions); i++) {
+    const int w = dimensions[i][0], h = dimensions[i][1];
+    int cw, ch;
+    GdkPixbuf *pix, *cpix;
+    char buf[1024];
+    GError *error = NULL;
+
+    canvas = generate_tile_mask(w, h, &cw, &ch);
+    assert(canvas->type == CANVAS_PIXMAP);
+
+    /* Add two new references since ctor_sprite_mask doesn't. */
+    cpix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, cw, ch);
+    gdk_pixbuf_get_from_drawable(cpix, canvas->v.pixmap, NULL             ,
+                                0, 0, 0, 0, cw, ch);
+    sprite = ctor_sprite(cpix);
+
+    my_snprintf(buf, sizeof(buf), "mask-%dx%d.png", w, h);
+
+    freelog(LOG_NORMAL, "Generating %d x %d mask as '%s'.", w, h, buf);
+
+    pix = sprite_get_pixbuf(sprite);
+    if (!gdk_pixbuf_save(pix, buf, "png", &error, NULL)) {
+      if (error) {
+       freelog(LOG_NORMAL, "Save error: %s", error->message);
+      } else {
+       freelog(LOG_NORMAL, "Unknown save error.");
+      }
+    }
+
+    free_sprite(sprite);
+    canvas_free(canvas);
+  }
+}
+
 /**************************************************************************
  called from main(), is what it's named.
 **************************************************************************/
@@ -1190,6 +1233,8 @@
 
   set_client_state(CLIENT_PRE_GAME_STATE);
 
+  make_mask();
+
   gtk_main();
 
   spaceship_dialog_done();

PNG image


[Prev in Thread] Current Thread [Next in Thread]