Rework civ2civ3 ruleset help and documentation.
[freeciv.git] / client / colors_common.c
blob2de78bada529ca02326eaeafd7542af3a52b2356
1 /**********************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "log.h"
20 #include "shared.h"
22 /* common */
23 #include "player.h"
24 #include "rgbcolor.h"
26 /* client/include */
27 #include "colors_g.h"
29 /* client */
30 #include "tilespec.h"
32 #include "colors_common.h"
34 struct color_system {
35 struct rgbcolor **stdcolors;
38 /****************************************************************************
39 Called when the client first starts to allocate the default colors.
41 Currently this must be called in ui_main, generally after UI
42 initialization.
43 ****************************************************************************/
44 struct color_system *color_system_read(struct section_file *file)
46 struct color_system *colors = fc_malloc(sizeof(*colors));
47 enum color_std stdcolor;
49 colors->stdcolors = fc_calloc(COLOR_LAST, sizeof(*colors->stdcolors));
51 for (stdcolor= color_std_begin(); stdcolor!= color_std_end();
52 stdcolor= color_std_next(stdcolor)) {
53 struct rgbcolor *prgbcolor = NULL;
54 if (rgbcolor_load(file, &prgbcolor, "colors.%s0",
55 color_std_name(stdcolor))) {
56 *(colors->stdcolors + stdcolor) = prgbcolor;
57 } else {
58 log_error("Color %s: %s", color_std_name(stdcolor), secfile_error());
59 *(colors->stdcolors + stdcolor) = rgbcolor_new(0, 0, 0);
63 return colors;
66 /****************************************************************************
67 Called when the client first starts to free any allocated colors.
68 ****************************************************************************/
69 void color_system_free(struct color_system *colors)
71 enum color_std stdcolor;
73 for (stdcolor= color_std_begin(); stdcolor!= color_std_end();
74 stdcolor= color_std_next(stdcolor)) {
75 rgbcolor_destroy(*(colors->stdcolors + stdcolor));
78 free(colors->stdcolors);
80 free(colors);
83 /****************************************************************************
84 Return the RGB color, allocating it if necessary.
85 ****************************************************************************/
86 struct color *ensure_color(struct rgbcolor *rgb)
88 fc_assert_ret_val(rgb != NULL, NULL);
90 if (!rgb->color) {
91 rgb->color = color_alloc(rgb->r, rgb->g, rgb->b);
93 return rgb->color;
96 /****************************************************************************
97 Return a pointer to the given "standard" color.
98 ****************************************************************************/
99 struct color *get_color(const struct tileset *t, enum color_std stdcolor)
101 struct color_system *colors = get_color_system(t);
103 fc_assert_ret_val(colors != NULL, NULL);
105 return ensure_color(*(colors->stdcolors + stdcolor));
108 /**********************************************************************
109 Return the color of the player.
110 ***********************************************************************/
111 struct color *get_player_color(const struct tileset *t,
112 const struct player *pplayer)
114 fc_assert_ret_val(pplayer != NULL, NULL);
115 fc_assert_ret_val(pplayer->rgb != NULL, NULL);
117 return ensure_color(pplayer->rgb);
120 /****************************************************************************
121 Return a pointer to the given "terrain" color.
122 ****************************************************************************/
123 struct color *get_terrain_color(const struct tileset *t,
124 const struct terrain *pterrain)
126 fc_assert_ret_val(pterrain != NULL, NULL);
127 fc_assert_ret_val(pterrain->rgb != NULL, NULL);
129 return ensure_color(pterrain->rgb);