Complete.Org: Mailing Lists: Archives: freeciv-dev: January 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, 31 Jan 2005 12:08:14 -0800
Reply-to: bugs@xxxxxxxxxxx

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

This patch causes the gtk2 client to generate the correct tile mask when 
run.

It uses linear algebra to fill out the actual pixels that are a part of 
the tile.  This is more accurate than using the gridlines or 
hand-drawing because it is guaranteed to correspond to mouse clicks.

Note that it won't work for hex tiles.  This is a known bug.

-jason

? mask-64x32.png
? vgcore.pid7932
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.171
diff -u -r1.171 mapview_common.c
--- client/mapview_common.c     5 Jan 2005 23:24:24 -0000       1.171
+++ client/mapview_common.c     31 Jan 2005 20:07:59 -0000
@@ -2599,3 +2599,38 @@
   /* 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)
+{
+  const int old_W = NORMAL_TILE_WIDTH, old_H = NORMAL_TILE_HEIGHT;
+  int x, y;
+  struct canvas *canvas;
+
+  NORMAL_TILE_WIDTH = W;
+  NORMAL_TILE_HEIGHT = H;
+
+  /* White is used for the foreground, because the caller may want to use the
+   * returned canvas as its own mask. */
+  canvas = canvas_create(W, H);
+  canvas_put_rectangle(canvas, COLOR_STD_BLACK, 0, 0, W, H);
+  for (x = 0; x < W; x++) {
+    for (y = 0; y < H; y++) {
+      int mx, my;
+
+      gui_to_map_pos(&mx, &my, x, y);
+      if (mx == 0 && my == 0) {
+       canvas_put_rectangle(canvas, COLOR_STD_WHITE, x, y, 1, 1);
+      }
+    }
+  }
+
+  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.85
diff -u -r1.85 mapview_common.h
--- client/mapview_common.h     25 Dec 2004 20:38:14 -0000      1.85
+++ client/mapview_common.h     31 Jan 2005 20:08:00 -0000
@@ -230,4 +230,6 @@
 bool map_canvas_resized(int width, int height);
 void init_mapcanvas_and_overview(void);
 
+struct canvas *generate_tile_mask(int W, int H);
+
 #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.103
diff -u -r1.103 gui_main.c
--- client/gui-gtk-2.0/gui_main.c       28 Jan 2005 19:57:09 -0000      1.103
+++ client/gui-gtk-2.0/gui_main.c       31 Jan 2005 20:08:00 -0000
@@ -1058,6 +1058,46 @@
   log_set_callback(log_callback_utf8);
 }
 
+static void make_mask(void)
+{
+  struct canvas *canvas;
+  struct Sprite *sprite;
+  int dimensions[][2] = {{64, 32}};
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(dimensions); i++) {
+    int w = dimensions[i][0];
+    int h = dimensions[i][1];
+    GdkPixbuf *pix;
+    char buf[1024];
+    GError *error = NULL;
+
+    canvas = generate_tile_mask(w, h);
+    assert(canvas->type == CANVAS_PIXMAP);
+
+    /* Add two new references since ctor_sprite_mask doesn't. */
+    g_object_ref(canvas->v.pixmap);
+    g_object_ref(canvas->v.pixmap);
+    sprite = ctor_sprite_mask(canvas->v.pixmap, canvas->v.pixmap, w, h);
+
+    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.
 **************************************************************************/
@@ -1206,6 +1246,8 @@
 
   set_client_state(CLIENT_PRE_GAME_STATE);
 
+  make_mask();
+
   gtk_main();
 
   spaceship_dialog_done();

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12060) generating the "correct" tile mask, Jason Short <=