Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2005:
[Freeciv-Dev] (PR#13011) move color system into client common
Home

[Freeciv-Dev] (PR#13011) move color system into client common

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13011) move color system into client common
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 9 May 2005 11:00:48 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch moves the color system (struct color *, init_colors, etc. -
used for compatibility with paletted displays and modeled on the Xlib
system) into the client common code.

The main advantage of this is it allows us to move away from an
enumerated color system.  First of all it's easier to add new colors to
the enumeration.  Secondly we can allocate direct RGB colors outside of
the enumeration using alloc_color, and let the GUI code worry about any
palette issues.

At some point in the future all the color RGB values that we use should
be determined by the tileset.

-jason

Index: client/Makefile.am
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/Makefile.am,v
retrieving revision 1.65
diff -u -r1.65 Makefile.am
--- client/Makefile.am  5 May 2005 21:09:15 -0000       1.65
+++ client/Makefile.am  9 May 2005 17:59:06 -0000
@@ -141,6 +141,8 @@
        climap.h        \
        clinet.c        \
        clinet.h        \
+       colors_common.c         \
+       colors_common.h         \
        control.c       \
        control.h       \
        goto.c          \
Index: client/colors_common.c
===================================================================
RCS file: client/colors_common.c
diff -N client/colors_common.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ client/colors_common.c      9 May 2005 17:59:06 -0000
@@ -0,0 +1,99 @@
+/********************************************************************** 
+ Freeciv - Copyright (C) 2005 - The Freeciv Project
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <assert.h>
+
+#include "shared.h"
+
+#include "colors_g.h"
+
+#include "colors_common.h"
+
+
+static struct color *colors[COLOR_STD_LAST];
+
+static struct rgbtriple {
+  int r, g, b;
+} colors_standard_rgb[COLOR_STD_LAST] = {
+  {  0,   0,   0},  /* Black */
+  {255, 255, 255},  /* White */
+  {255,   0,   0},  /* Red */
+  {255, 255,   0},  /* Yellow */
+  {  0, 255, 200},  /* Cyan */
+  {  0, 200,   0},  /* Ground (green) */
+  {  0,   0, 200},  /* Ocean (blue) */
+  { 86,  86,  86},  /* Background (gray) */
+  {128,   0,   0},  /* race0 */
+  {128, 255, 255},  /* race1 */
+  {255,   0,   0},  /* race2 */
+  {255,   0, 128},  /* race3 */
+  {  0,   0, 128},  /* race4 */
+  {255,   0, 255},  /* race5 */
+  {255, 128,   0},  /* race6 */
+  {255, 255, 128},  /* race7 */
+  {255, 128, 128},  /* race8 */
+  {  0,   0, 255},  /* race9 */
+  {  0, 255,   0},  /* race10 */
+  {  0, 128, 128},  /* race11 */
+  {  0,  64,  64},  /* race12 */
+  {198, 198, 198},  /* race13 */
+};
+
+static bool initted = FALSE;
+
+/****************************************************************************
+  Called when the client first starts to allocate the default colors.
+
+  Currently this must be called in ui_main, generally after UI
+  initialization.
+****************************************************************************/
+void init_color_system(void)
+{
+  int i;
+
+  assert(!initted);
+  initted = TRUE;
+
+  for (i = 0; i < COLOR_STD_LAST; i++) {
+    colors[i] = color_alloc(colors_standard_rgb[i].r,
+                           colors_standard_rgb[i].g,
+                           colors_standard_rgb[i].b);
+  }
+}
+
+/****************************************************************************
+  Called when the client first starts to free any allocated colors.
+****************************************************************************/
+void color_free_system(void)
+{
+  int i;
+
+  assert(initted);
+  initted = FALSE;
+
+  for (i = 0; i < COLOR_STD_LAST; i++) {
+    color_free(colors[i]);
+  }
+}
+
+/****************************************************************************
+  Return a pointer to the given "standard" color.
+****************************************************************************/
+struct color *get_color(enum color_std color)
+{
+  return colors[color];
+}
Index: client/colors_common.h
===================================================================
RCS file: client/colors_common.h
diff -N client/colors_common.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ client/colors_common.h      9 May 2005 17:59:06 -0000
@@ -0,0 +1,41 @@
+/********************************************************************** 
+ Freeciv - Copyright (C) 2005 - The Freeciv Project
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__COLORS_COMMON_H
+#define FC__COLORS_COMMON_H
+
+/* The color system is designed on the assumption that almost, but
+ * not quite, all displays will be truecolor. */
+
+struct color;
+
+enum color_std { 
+  COLOR_STD_BLACK, COLOR_STD_WHITE, COLOR_STD_RED,
+  COLOR_STD_YELLOW, COLOR_STD_CYAN,
+  COLOR_STD_GROUND, COLOR_STD_OCEAN,
+  COLOR_STD_BACKGROUND,
+  COLOR_STD_RACE0, COLOR_STD_RACE1, COLOR_STD_RACE2,
+  COLOR_STD_RACE3, COLOR_STD_RACE4, COLOR_STD_RACE5,
+  COLOR_STD_RACE6, COLOR_STD_RACE7, COLOR_STD_RACE8,
+  COLOR_STD_RACE9, COLOR_STD_RACE10, COLOR_STD_RACE11,
+  COLOR_STD_RACE12, COLOR_STD_RACE13,
+
+  COLOR_STD_LAST
+};
+
+void init_color_system(void);
+void color_free_system(void);
+
+struct color *get_color(enum color_std color);
+
+#endif /* FC__COLORS_COMMON_H */
Index: client/gui-gtk-2.0/canvas.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/canvas.c,v
retrieving revision 1.3
diff -u -r1.3 canvas.c
--- client/gui-gtk-2.0/canvas.c 15 Apr 2005 22:53:30 -0000      1.3
+++ client/gui-gtk-2.0/canvas.c 9 May 2005 17:59:06 -0000
@@ -171,7 +171,7 @@
                          enum color_std color,
                          int canvas_x, int canvas_y, int width, int height)
 {
-  GdkColor *col = colors_standard[color];
+  GdkColor *col = &get_color(color)->color;
 
   switch (pcanvas->type) {
     case CANVAS_PIXMAP:
@@ -202,7 +202,7 @@
   if (pcanvas->type == CANVAS_PIXMAP) {
     gdk_gc_set_clip_origin(fill_bg_gc, canvas_x, canvas_y);
     gdk_gc_set_clip_mask(fill_bg_gc, sprite_get_mask(psprite));
-    gdk_gc_set_foreground(fill_bg_gc, colors_standard[color]);
+    gdk_gc_set_foreground(fill_bg_gc, &get_color(color)->color);
 
     gdk_draw_rectangle(pcanvas->v.pixmap, fill_bg_gc, TRUE,
                       canvas_x, canvas_y, psprite->width, psprite->height);
@@ -220,7 +220,7 @@
   if (pcanvas->type == CANVAS_PIXMAP) {
     gdk_gc_set_clip_origin(fill_tile_gc, canvas_x, canvas_y);
     gdk_gc_set_clip_mask(fill_tile_gc, sprite_get_mask(psprite));
-    gdk_gc_set_foreground(fill_tile_gc, colors_standard[COLOR_STD_BLACK]);
+    gdk_gc_set_foreground(fill_tile_gc, &get_color(COLOR_STD_BLACK)->color);
     gdk_gc_set_stipple(fill_tile_gc, black50);
     gdk_gc_set_ts_origin(fill_tile_gc, canvas_x, canvas_y);
 
@@ -256,7 +256,7 @@
       break;
     }
 
-    gdk_gc_set_foreground(gc, colors_standard[color]);
+    gdk_gc_set_foreground(gc, &get_color(color)->color);
     gdk_draw_line(pcanvas->v.pixmap, gc,
                  start_x, start_y, start_x + dx, start_y + dy);
   }
@@ -316,7 +316,7 @@
     layout = pango_layout_new(gdk_pango_context_get());
   }
 
-  gdk_gc_set_foreground(civ_gc, colors_standard[color]);
+  gdk_gc_set_foreground(civ_gc, &get_color(color)->color);
   pango_layout_set_font_description(layout, *fonts[font].font);
   pango_layout_set_text(layout, text, -1);
 
Index: client/gui-gtk-2.0/colors.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/colors.c,v
retrieving revision 1.4
diff -u -r1.4 colors.c
--- client/gui-gtk-2.0/colors.c 14 Nov 2002 09:14:55 -0000      1.4
+++ client/gui-gtk-2.0/colors.c 9 May 2005 17:59:06 -0000
@@ -26,54 +26,6 @@
 
 #include "colors.h"
 
-static struct rgbtriple {
-  int r, g, b;
-} colors_standard_rgb[COLOR_STD_LAST] = {
-  {  0,   0,   0},  /* Black */
-  {255, 255, 255},  /* White */
-  {255,   0,   0},  /* Red */
-  {255, 255,   0},  /* Yellow */
-  {  0, 255, 200},  /* Cyan */
-  {  0, 200,   0},  /* Ground (green) */
-  {  0,   0, 200},  /* Ocean (blue) */
-  { 86,  86,  86},  /* Background (gray) */
-  {128,   0,   0},  /* race0 */
-  {128, 255, 255},  /* race1 */
-  {255,   0,   0},  /* race2 */
-  {255,   0, 128},  /* race3 */
-  {  0,   0, 128},  /* race4 */
-  {255,   0, 255},  /* race5 */
-  {255, 128,   0},  /* race6 */
-  {255, 255, 128},  /* race7 */
-  {255, 128, 128},  /* race8 */
-  {  0,   0, 255},  /* race9 */
-  {  0, 255,   0},  /* race10 */
-  {  0, 128, 128},  /* race11 */
-  {  0,  64,  64},  /* race12 */
-  {198, 198, 198},  /* race13 */
-};
-
-GdkColor *colors_standard [COLOR_STD_LAST];
-
-/*************************************************************
-...
-*************************************************************/
-static void alloc_standard_colors (void)
-{
-  GdkColormap *cmap;
-  int i;
-
-  for (i=0, cmap=gtk_widget_get_default_colormap (); i<COLOR_STD_LAST; i++) {
-    colors_standard[i]       = fc_malloc(sizeof(GdkColor));
-
-    colors_standard[i]->red  = colors_standard_rgb[i].r<<8;
-    colors_standard[i]->green= colors_standard_rgb[i].g<<8;
-    colors_standard[i]->blue = colors_standard_rgb[i].b<<8;
-  
-    gdk_rgb_find_color(cmap, colors_standard[i]);
-  }
-}
-
 /*************************************************************
 ...
 *************************************************************/
@@ -102,22 +54,27 @@
   return COLOR_DISPLAY;
 }
 
-/*************************************************************
-...
-*************************************************************/
-void init_color_system(void)
+/****************************************************************************
+  Allocate a color (adjusting it for our colormap if necessary on paletted
+  systems) and return a pointer to it.
+****************************************************************************/
+struct color *color_alloc(int r, int g, int b)
 {
-  alloc_standard_colors();
+  struct color *color = fc_malloc(sizeof(*color));
+  GdkColormap *cmap = gtk_widget_get_default_colormap();
+
+  color->color.red = r << 8;
+  color->color.green = g << 8;
+  color->color.blue = b << 8;
+  gdk_rgb_find_color(cmap, &color->color);
+
+  return color;
 }
 
-/*************************************************************
-...
-*************************************************************/
-void free_color_system(void)
+/****************************************************************************
+  Free a previously allocated color.  See color_alloc.
+****************************************************************************/
+void color_free(struct color *color)
 {
-  int i;
-
-  for (i = 0; i < COLOR_STD_LAST; i++) {
-    free(colors_standard[i]);
-  }
+  free(color);
 }
Index: client/gui-gtk-2.0/colors.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/colors.h,v
retrieving revision 1.1
diff -u -r1.1 colors.h
--- client/gui-gtk-2.0/colors.h 11 Mar 2002 23:09:43 -0000      1.1
+++ client/gui-gtk-2.0/colors.h 9 May 2005 17:59:06 -0000
@@ -17,6 +17,14 @@
 
 #include "colors_g.h"
 
-extern GdkColor *colors_standard[COLOR_STD_LAST];
+struct color {
+  GdkColor color;
+};
+
+enum Display_color_type {
+  BW_DISPLAY, GRAYSCALE_DISPLAY, COLOR_DISPLAY
+};
+
+enum Display_color_type get_visual(void);
 
 #endif  /* FC__COLORS_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.122
diff -u -r1.122 gui_main.c
--- client/gui-gtk-2.0/gui_main.c       5 May 2005 19:22:24 -0000       1.122
+++ client/gui-gtk-2.0/gui_main.c       9 May 2005 17:59:06 -0000
@@ -943,9 +943,9 @@
 
   for (i = 0; i < 5; i++) {
     gtk_widget_modify_bg(GTK_WIDGET(overview_canvas), i,
-                        colors_standard[COLOR_STD_BLACK]);
+                        &get_color(COLOR_STD_BLACK)->color);
     gtk_widget_modify_bg(GTK_WIDGET(map_canvas), i,
-                        colors_standard[COLOR_STD_BLACK]);
+                        &get_color(COLOR_STD_BLACK)->color);
   }
 
   gtk_widget_add_events(map_canvas, GDK_EXPOSURE_MASK
@@ -1215,7 +1215,7 @@
   happiness_dialog_done();
   diplomacy_dialog_done();
   cma_fe_done();
-  free_color_system();
+  color_free_system();
   tileset_free_tiles(tileset);
 }
 
Index: client/gui-gtk-2.0/gui_stuff.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gui_stuff.c,v
retrieving revision 1.21
diff -u -r1.21 gui_stuff.c
--- client/gui-gtk-2.0/gui_stuff.c      10 Apr 2005 03:31:23 -0000      1.21
+++ client/gui-gtk-2.0/gui_stuff.c      9 May 2005 17:59:07 -0000
@@ -683,7 +683,7 @@
        GtkWidget *label = dlg->v.tab.label;
 
        gtk_widget_modify_fg(label, GTK_STATE_ACTIVE,
-           colors_standard[COLOR_STD_RED]);
+                            &get_color(COLOR_STD_RED)->color);
       }
     }
     break;
@@ -731,7 +731,7 @@
        GtkWidget *label = dlg->v.tab.label;
 
        gtk_widget_modify_fg(label, GTK_STATE_ACTIVE,
-           colors_standard[COLOR_STD_RACE9]);
+                            &get_color(COLOR_STD_RACE9)->color);
       }
     }
     break;
Index: client/gui-gtk-2.0/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/helpdlg.c,v
retrieving revision 1.47
diff -u -r1.47 helpdlg.c
--- client/gui-gtk-2.0/helpdlg.c        5 May 2005 18:32:47 -0000       1.47
+++ client/gui-gtk-2.0/helpdlg.c        9 May 2005 17:59:07 -0000
@@ -200,7 +200,7 @@
     gtk_tree_store_set(tstore, &l,
                       1, -1,
                       2, tech,
-                      3, colors_standard[bg],
+                      3, &get_color(bg)->color
                       -1);
     return;
   }
@@ -227,7 +227,7 @@
   gtk_tree_store_set(tstore, &l,
                     1, turns_to_tech,
                     2, tech,
-                    3, colors_standard[bg],
+                    3, &get_color(bg)->color,
                     -1);
 
   if (--levels <= 0)
Index: client/gui-gtk-2.0/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/mapview.c,v
retrieving revision 1.173
diff -u -r1.173 mapview.c
--- client/gui-gtk-2.0/mapview.c        21 Apr 2005 02:43:23 -0000      1.173
+++ client/gui-gtk-2.0/mapview.c        9 May 2005 17:59:07 -0000
@@ -650,7 +650,7 @@
   if (fog) {
     gdk_gc_set_clip_origin(fill_tile_gc, canvas_x, canvas_y);
     gdk_gc_set_clip_mask(fill_tile_gc, sprite_get_mask(ssprite));
-    gdk_gc_set_foreground(fill_tile_gc, colors_standard[COLOR_STD_BLACK]);
+    gdk_gc_set_foreground(fill_tile_gc, &get_color(COLOR_STD_BLACK)->color);
     gdk_gc_set_ts_origin(fill_tile_gc, canvas_x, canvas_y);
     gdk_gc_set_stipple(fill_tile_gc, black50);
 
@@ -741,7 +741,7 @@
 {
   GdkPoint points[5];
 
-  gdk_gc_set_foreground(civ_gc, colors_standard[COLOR_STD_YELLOW]);
+  gdk_gc_set_foreground(civ_gc, &get_color(COLOR_STD_YELLOW)->color);
 
   /* gdk_draw_rectangle() must start top-left.. */
   points[0].x = canvas_x;
Index: client/gui-gtk-2.0/plrdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/plrdlg.c,v
retrieving revision 1.58
diff -u -r1.58 plrdlg.c
--- client/gui-gtk-2.0/plrdlg.c 5 May 2005 18:32:47 -0000       1.58
+++ client/gui-gtk-2.0/plrdlg.c 9 May 2005 17:59:07 -0000
@@ -110,10 +110,10 @@
 
   pixmap = gdk_pixmap_new(root_window, width, height, -1);
 
-  gdk_gc_set_foreground(civ_gc, colors_standard[COLOR_STD_BLACK]);
+  gdk_gc_set_foreground(civ_gc, &get_color(COLOR_STD_BLACK)->color);
   gdk_draw_rectangle(pixmap, civ_gc, TRUE, 0, 0, width, height);
 
-  gdk_gc_set_foreground(civ_gc, colors_standard[player_color(plr)]);
+  gdk_gc_set_foreground(civ_gc, &get_color(player_color(plr))->color);
   gdk_draw_rectangle(pixmap, civ_gc, TRUE, 1, 1, width - 2, height - 2);
 
   tmp = gdk_pixbuf_get_from_drawable(NULL, pixmap, NULL, 
Index: client/gui-stub/colors.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-stub/colors.c,v
retrieving revision 1.3
diff -u -r1.3 colors.c
--- client/gui-stub/colors.c    30 Nov 2002 19:27:37 -0000      1.3
+++ client/gui-stub/colors.c    9 May 2005 17:59:07 -0000
@@ -17,10 +17,27 @@
 
 #include "colors.h"
 
-/**************************************************************************
-  Initialize colors for the game.
-**************************************************************************/
-void init_color_system(void)
+/****************************************************************************
+  Allocate a color (adjusting it for our colormap if necessary on paletted
+  systems) and return a pointer to it.
+****************************************************************************/
+struct color *color_alloc(int r, int g, int b)
 {
+  struct color *color = fc_malloc(sizeof(*color));
+
   /* PORTME */
+  color->r = r;
+  color->g = g;
+  color->b = b;
+
+  return color;
+}
+
+/****************************************************************************
+  Free a previously allocated color.  See color_alloc.
+****************************************************************************/
+void color_free(struct color *color)
+{
+  /* PORTME */
+  free(color);
 }
Index: client/gui-stub/colors.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-stub/colors.h,v
retrieving revision 1.2
diff -u -r1.2 colors.h
--- client/gui-stub/colors.h    30 Nov 2002 19:27:37 -0000      1.2
+++ client/gui-stub/colors.h    9 May 2005 17:59:07 -0000
@@ -16,5 +16,9 @@
 
 #include "colors_g.h"
 
+struct color {
+  /* PORTME: color structure */
+  int r, g, b;
+};
 
 #endif                         /* FC__COLORS_H */
Index: client/include/canvas_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/canvas_g.h,v
retrieving revision 1.4
diff -u -r1.4 canvas_g.h
--- client/include/canvas_g.h   15 Apr 2005 22:53:30 -0000      1.4
+++ client/include/canvas_g.h   9 May 2005 17:59:07 -0000
@@ -20,6 +20,10 @@
 struct sprite;
 struct canvas;                 /* opaque type, real type is gui-dep */
 
+enum line_type {
+  LINE_NORMAL, LINE_BORDER, LINE_TILE_FRAME, LINE_GOTO
+};
+
 /* Creator and destructor */
 struct canvas *canvas_create(int width, int height);
 void canvas_free(struct canvas *store);
Index: client/include/colors_g.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/include/colors_g.h,v
retrieving revision 1.9
diff -u -r1.9 colors_g.h
--- client/include/colors_g.h   12 Mar 2004 04:57:52 -0000      1.9
+++ client/include/colors_g.h   9 May 2005 17:59:07 -0000
@@ -13,30 +13,11 @@
 #ifndef FC__COLORS_G_H
 #define FC__COLORS_G_H
 
-enum color_std { 
-  COLOR_STD_BLACK, COLOR_STD_WHITE, COLOR_STD_RED,
-  COLOR_STD_YELLOW, COLOR_STD_CYAN,
-  COLOR_STD_GROUND, COLOR_STD_OCEAN,
-  COLOR_STD_BACKGROUND,
-  COLOR_STD_RACE0, COLOR_STD_RACE1, COLOR_STD_RACE2,
-  COLOR_STD_RACE3, COLOR_STD_RACE4, COLOR_STD_RACE5,
-  COLOR_STD_RACE6, COLOR_STD_RACE7, COLOR_STD_RACE8,
-  COLOR_STD_RACE9, COLOR_STD_RACE10, COLOR_STD_RACE11,
-  COLOR_STD_RACE12, COLOR_STD_RACE13,
+#include "colors_common.h"
 
-  COLOR_STD_LAST
-};
+struct color;
 
-enum line_type {
-  LINE_NORMAL, LINE_BORDER, LINE_TILE_FRAME, LINE_GOTO
-};
-
-enum Display_color_type {
-  BW_DISPLAY, GRAYSCALE_DISPLAY, COLOR_DISPLAY
-};
-
-enum Display_color_type get_visual(void);
-void init_color_system(void);
-void free_color_system(void);
+struct color *color_alloc(int r, int g, int b);
+void color_free(struct color *color);
 
 #endif  /* FC__COLORS_G_H */

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13011) move color system into client common, Jason Short <=