changed most comments from C to C++ style for BD engine code
[rocksndiamonds.git] / src / game_bd / bd_colors.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "main_bd.h"
18
19
20 static char *c64_color_names[] =
21 {
22   "Black",
23   "White",
24   "Red",
25   "Cyan",
26   "Purple",
27   "Green",
28   "Blue",
29   "Yellow",
30   "Orange",
31   "Brown",
32   "LightRed",
33   "Gray1",
34   "Gray2",
35   "LightGreen",
36   "LightBlue",
37   "Gray3",
38 };
39
40 // return c64 color with index.
41 GdColor gd_c64_color(int index)
42 {
43   return (GD_COLOR_TYPE_C64 << 24) + index;
44 }
45
46 // return atari color with index.
47 GdColor gd_atari_color(int index)
48 {
49   return (GD_COLOR_TYPE_ATARI << 24) + index;
50 }
51
52 // return c64dtv color with index.
53 GdColor gd_c64dtv_color(int index)
54 {
55   return (GD_COLOR_TYPE_C64DTV << 24) + index;
56 }
57
58 // return "unknown color"
59 static GdColor unknown_color(void)
60 {
61   return (GD_COLOR_TYPE_UNKNOWN << 24);
62 }
63
64 // make up GdColor from r,g,b values.
65 GdColor gd_color_get_from_rgb(int r, int g, int b)
66 {
67   return (GD_COLOR_TYPE_RGB << 24) + (r << 16) + (g << 8) + b;
68 }
69
70 GdColor gd_color_get_from_string(const char *color)
71 {
72   int i, r, g, b;
73
74   for (i = 0; i < ARRAY_SIZE(c64_color_names); i++)
75     if (strEqualCase(color, c64_color_names[i]))
76       return gd_c64_color(i);
77
78   // we do not use sscanf(color, "atari..." as may be lowercase
79   if (strEqualCaseN(color, "Atari", strlen("Atari")))
80   {
81     const char *b = color + strlen("Atari");
82     int c;
83
84     if (sscanf(b, "%02x", &c) == 1)
85       return gd_atari_color(c);
86
87     Warn("Unknown Atari color: %s", color);
88
89     return unknown_color();
90   }
91
92   // we do not use sscanf(color, "c64dtv..." as may be lowercase
93   if (strEqualCaseN(color, "C64DTV", strlen("C64DTV")))
94   {
95     const char *b = color + strlen("C64DTV");
96     int c;
97
98     if (sscanf(b, "%02x", &c) == 1)
99       return gd_c64dtv_color(c);
100
101     Warn("Unknown C64DTV color: %s", color);
102
103     return unknown_color();
104   }
105
106   // may or may not have a #
107   if (color[0] == '#')
108     color++;
109
110   if (sscanf(color, "%02x%02x%02x", &r, &g, &b) != 3)
111   {
112     i = gd_random_int_range(0, 16);
113
114     Warn("Unkonwn color %s", color);
115
116     return unknown_color();
117   }
118
119   return gd_color_get_from_rgb(r, g, b);
120 }
121
122 const char *gd_color_get_string(GdColor color)
123 {
124   static char text[16];
125
126   if (gd_color_is_c64(color))
127   {
128     return c64_color_names[color & 0xff];
129   }
130
131   if (gd_color_is_atari(color))
132   {
133     sprintf(text, "Atari%02x", color & 0xff);
134     return text;
135   }
136
137   if (gd_color_is_dtv(color))
138   {
139     sprintf(text, "C64DTV%02x", color & 0xff);
140     return text;
141   }
142
143   sprintf(text, "#%02x%02x%02x", (color >> 16) & 255, (color >> 8) & 255, color & 255);
144   return text;
145 }
146
147 boolean gd_color_is_c64(GdColor color)
148 {
149   return (color >> 24) == GD_COLOR_TYPE_C64;
150 }
151
152 boolean gd_color_is_atari(GdColor color)
153 {
154     return (color >> 24) == GD_COLOR_TYPE_ATARI;
155 }
156
157 boolean gd_color_is_dtv(GdColor color)
158 {
159     return (color >> 24) == GD_COLOR_TYPE_C64DTV;
160 }
161
162 boolean gd_color_is_unknown(GdColor color)
163 {
164   return (color >> 24) == GD_COLOR_TYPE_UNKNOWN;
165 }
166
167 GdColor gd_gdash_color(int c)
168 {
169   // these values are taken from the title screen, drawn by cws.
170   // so menus and everything else will look nice!
171   // the 16 colors that can be used are the same as on c64.
172   // "Black", "White", "Red", "Cyan", "Purple", "Green", "Blue", "Yellow",
173   // "Orange", "Brown", "LightRed", "Gray1", "Gray2", "LightGreen", "LightBlue", "Gray3",
174   // not in the png: cyan, purple. gray3 is darker in the png.
175   // 17th color is the player's leg in the png. i not connected it to any c64
176   // color, but it is used for theme images for example.
177   const GdColor gdash_colors[] =
178   {
179     0x000000, 0xffffff, 0xe33939, 0x55aaaa, 0xaa55aa, 0x71aa55, 0x0039ff, 0xffff55,
180     0xe37139, 0xaa7139, 0xe09080, 0x555555, 0x717171, 0xc6e38e, 0xaaaaff, 0x8e8e8e,
181
182     0x5555aa,
183   };
184
185   return gdash_colors[c];
186 }