1 /* XQF - Quake server browser and launcher
2 * Copyright (C) 1998-2000 Roman Pozlevich <roma@botik.ru>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
17 */
18
19 #include <sys/types.h>
20 #include <string.h> /* strlen, strcpy, strncpy, strcmp, strncmp, memset */
21 #include <sys/stat.h> /* stat */
22 #include <unistd.h> /* stat */
23
24 #include <gtk/gtk.h>
25
26 #include "xqf-ui.h"
27 #include "pref.h"
28 #include "utils.h"
29 #include "pixmaps.h"
30 #include "dialogs.h"
31 #include "skin.h"
32 #include "skin_pcx.h"
33
34 #define NOSKIN_BCOLOR 0x80
35 #define NOSKIN_WCOLOR 0xC0
36
37
38 static guchar quake_pallete[768] = {
39 #include "quake_pal.h"
40 };
41
42
43 static guchar quake2_pallete[768] = {
44 #include "quake2_pal.h"
45 };
46
47
48 static GdkColor pcolors[14];
49 static int pcolors_allocated = FALSE;
50
51
dir_qw_skin_filter(const char * dir,const char * str)52 static char *dir_qw_skin_filter (const char *dir, const char *str) {
53 int len;
54 char *res;
55
56 if (!str)
57 return NULL;
58
59 len = strlen (str);
60
61 if (len <= 4 || strcmp (str + len - 4, ".pcx"))
62 return NULL;
63
64 len -= 4;
65
66 res = g_malloc (len + 1);
67 strncpy (res, str, len);
68 res[len] = '\0';
69
70 return res;
71 }
72
73
dir_q2_skin_filter(const char * dir,const char * str)74 static char *dir_q2_skin_filter (const char *dir, const char *str) {
75 int len;
76 char *res;
77
78 if (!str)
79 return NULL;
80
81 len = strlen (str);
82
83 if (len <= 6 || strcmp (str + len - 6, "_i.pcx"))
84 return NULL;
85
86 len -= 6;
87
88 res = g_malloc (len + 1);
89 strncpy (res, str, len);
90 res[len] = '\0';
91
92 return res;
93 }
94
95
get_qw_skin_list(const char * dir)96 GList *get_qw_skin_list (const char *dir) {
97 GList *list;
98 char *path;
99
100 path = file_in_dir (dir, "qw/skins");
101 list = dir_to_list (path, dir_qw_skin_filter);
102 g_free (path);
103
104 return list;
105 }
106
107
dir_alldirs_filter(const char * dir,const char * str)108 static char *dir_alldirs_filter (const char *dir, const char *str) {
109 struct stat st_buf;
110 int res;
111 char *file;
112
113 if (!str || str[0] == '.')
114 return NULL;
115
116 file = file_in_dir (dir, str);
117 res = stat (file, &st_buf);
118 g_free (file);
119
120 if (res == 0 && S_ISDIR (st_buf.st_mode))
121 return g_strdup (str);
122
123 return NULL;
124 }
125
126
get_q2_skin_list(const char * dir)127 GList *get_q2_skin_list (const char *dir) {
128 GList *list = NULL;
129 GList *dirs;
130 GList *tmp;
131 GList *skins;
132 GList *skin;
133 char *path;
134 char *skin_dir;
135 char *skin_str;
136
137 path = file_in_dir (dir, "baseq2/players");
138 dirs = dir_to_list (path, dir_alldirs_filter);
139
140 for (tmp = dirs; tmp; tmp = tmp->next) {
141 skin_dir = file_in_dir (path, (char *) tmp->data);
142 skins = dir_to_list (skin_dir, dir_q2_skin_filter);
143 g_free (skin_dir);
144
145 for (skin = skins; skin; skin = skin->next) {
146 skin_str = file_in_dir ((char *) tmp->data, (char *) skin->data);
147 list = g_list_prepend (list, skin_str);
148 }
149 }
150
151 g_free (path);
152 list = g_list_reverse (list);
153 return list;
154 }
155
156
draw_qw_skin(GtkWidget * preview,guchar * data,int top,int bottom)157 void draw_qw_skin (GtkWidget *preview, guchar *data, int top, int bottom) {
158 guchar buf[320*3];
159 guchar colormap[256];
160 guchar *ptr;
161 int i, j, k;
162 int fixed_top_color = fix_qw_player_color (top);
163 int fixed_bottom_color = fix_qw_player_color (bottom);
164
165 if (data) {
166 for (i = 0; i < 256; i++)
167 colormap[i] = i;
168
169 if (fixed_top_color != 1) {
170 for (i = 0; i<16; i++)
171 colormap[16*1 + i] = fixed_top_color*16 +
172 ((fixed_top_color <= 7)? i : 15 - i);
173 }
174
175 if (fixed_bottom_color != 6) {
176 for (i = 0; i<16; i++)
177 colormap[16*6 + i] = fixed_bottom_color*16 +
178 ((fixed_bottom_color <= 7)? i : 15 - i);
179 }
180
181 for (i = 0; i < 200; i++) {
182 ptr = buf;
183 for (j = 0; j < 320; j++) {
184 *ptr++ = quake_pallete[colormap[*data] * 3 + 0];
185 *ptr++ = quake_pallete[colormap[*data] * 3 + 1];
186 *ptr++ = quake_pallete[colormap[*data] * 3 + 2];
187 data++;
188 }
189 gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, i, 320);
190 }
191 }
192 else {
193 for (j = 0, k = NOSKIN_WCOLOR; j < 200; j += 40) {
194 i = k;
195 for (ptr = buf; ptr < buf+320*3; ptr += 40*3) {
196 memset (ptr, i, 40*3);
197 i = (NOSKIN_WCOLOR + NOSKIN_BCOLOR) - i;
198 }
199 for (i = 0; i < 40; i++) {
200 gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, j+i, 320);
201 }
202 k = (NOSKIN_WCOLOR + NOSKIN_BCOLOR) - k;
203 }
204 }
205
206 gtk_widget_draw (preview, NULL);
207 }
208
209
draw_q2_skin(GtkWidget * preview,guchar * data,int scale)210 void draw_q2_skin (GtkWidget *preview, guchar *data, int scale) {
211 guchar *buf;
212 guchar *ptr;
213 int i, j, k;
214
215 buf = g_malloc (32*scale*3);
216
217 if (data) {
218 for (i = 0; i < 32*scale;) {
219 ptr = buf;
220 for (j = 0; j < 32; j++) {
221 for (k = 0; k < scale; k++) {
222 *ptr++ = quake2_pallete[*data * 3 + 0];
223 *ptr++ = quake2_pallete[*data * 3 + 1];
224 *ptr++ = quake2_pallete[*data * 3 + 2];
225 }
226 data++;
227 }
228 for (k = 0; k < scale; k++) {
229 gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, i++, 32*scale);
230 }
231 }
232 }
233 else {
234 for (j = 0, k = NOSKIN_WCOLOR; j < 32*scale; j += 32) {
235 i = k;
236 for (ptr = buf; ptr < buf+32*scale*3; ptr += 32*3) {
237 memset (ptr, i, 32*3);
238 i = (NOSKIN_WCOLOR + NOSKIN_BCOLOR) - i;
239 }
240 for (i = 0; i < 32; i++) {
241 gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, j+i, 32*scale);
242 }
243 k = (NOSKIN_WCOLOR + NOSKIN_BCOLOR) - k;
244 }
245 }
246
247 g_free (buf);
248
249 gtk_widget_draw (preview, NULL);
250 }
251
252
skin_filename(char * skin,char * subdir,char * ext)253 static char *skin_filename (char *skin, char *subdir, char *ext) {
254 char *filename;
255 char *ptr;
256
257 filename = g_malloc (strlen (subdir) + strlen (skin) + strlen (ext) + 1);
258
259 ptr = filename;
260 strcpy (ptr, subdir); ptr += strlen (subdir);
261 strcpy (ptr, skin); ptr += strlen (skin);
262 strcpy (ptr, ext);
263
264 return filename;
265 }
266
267
get_qw_skin(char * skin,char * path)268 guchar *get_qw_skin (char *skin, char *path) {
269 char *filename;
270 char *skinsdir;
271 guchar *pcx;
272 int skinlen;
273
274 if (!skin || skin[0] == '\0')
275 skin = "base.pcx";
276
277 skinsdir = file_in_dir (path, "qw/skins/");
278 skinlen = strlen (skin);
279
280 if (skinlen > 4 && g_ascii_strcasecmp (skin + skinlen - 4, ".pcx") == 0)
281 filename = file_in_dir (skinsdir, skin);
282 else
283 filename = skin_filename (skin, skinsdir, ".pcx");
284
285 pcx = read_skin_pcx (filename, FALSE);
286
287 #ifdef DEBUG
288 if (!pcx)
289 fprintf (stderr, "cannot load QW skin %s\n", filename);
290 #endif
291
292 g_free (filename);
293 g_free (skinsdir);
294 return pcx;
295 }
296
get_q2_skin(char * skin,char * path)297 guchar *get_q2_skin (char *skin, char *path) {
298 char *skinsdir;
299 char *filename;
300 guchar *pcx;
301 // int skinlen;
302
303 if (!skin || skin[0] == '\0')
304 return NULL;
305
306 skinsdir = file_in_dir (path, "baseq2/players/");
307 // skinlen = strlen (skin);
308 filename = skin_filename (skin, skinsdir, "_i.pcx");
309
310 pcx = read_skin_pcx (filename, TRUE);
311
312 #ifdef DEBUG
313 if (!pcx)
314 fprintf (stderr, "cannot load Q2 skin %s\n", filename);
315 #endif
316
317 g_free (filename);
318 g_free (skinsdir);
319 return pcx;
320 }
321
322
convert_color(unsigned c)323 static gushort convert_color (unsigned c) {
324 c *= 257; /* scale from char to short */
325 c = c + c/3 + 0x2800; /* add brightness */
326 return (c > 0xffff)? 0xffff : c;
327 }
328
329
allocate_quake_player_colors(GdkWindow * window)330 void allocate_quake_player_colors (GdkWindow *window) {
331 GdkColormap *colormap;
332 int i, j;
333
334 if (!pcolors_allocated) {
335 colormap = gdk_window_get_colormap (window);
336
337 for (i = 0; i < 14; i++) {
338 j = (i<8)? 11 : 15 - 11;
339 pcolors[i].pixel = 0;
340 pcolors[i].red = convert_color (quake_pallete [(i*16 + j)*3 + 0]);
341 pcolors[i].green = convert_color (quake_pallete [(i*16 + j)*3 + 1]);
342 pcolors[i].blue = convert_color (quake_pallete [(i*16 + j)*3 + 2]);
343 if (!gdk_color_alloc (colormap, &pcolors[i])) {
344 g_warning ("unable to allocate color: ( %d %d %d )",
345 pcolors[i].red, pcolors[i].green, pcolors[i].blue);
346 }
347 }
348 pcolors_allocated = TRUE;
349 }
350 }
351
352
set_bg_color(GtkWidget * widget,int color)353 void set_bg_color (GtkWidget *widget, int color) {
354 GtkStyle *style;
355
356 style = gtk_style_copy (widget->style);
357 style->bg [GTK_STATE_NORMAL] = pcolors [color];
358 style->bg [GTK_STATE_ACTIVE] = pcolors [color];
359 style->bg [GTK_STATE_PRELIGHT] = pcolors [color];
360 style->bg [GTK_STATE_SELECTED] = pcolors [color];
361 style->bg [GTK_STATE_INSENSITIVE] = pcolors [color];
362
363 gtk_widget_set_style (widget, style);
364 }
365
366
create_color_menu(void (* callback)(GtkWidget *,int))367 GtkWidget *create_color_menu (void (*callback) (GtkWidget*, int)) {
368 GtkWidget *menu;
369 GtkWidget *menu_item;
370 GtkWidget *button;
371 int i;
372
373 menu = gtk_menu_new ();
374
375 for (i = 0; i < 14; i++) {
376
377 /* ugly, ugly, ugly... */
378
379 button = gtk_button_new_with_label (" ");
380 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_HALF);
381 gtk_widget_set_sensitive (button, FALSE);
382 gtk_widget_set_usize (button, 40, -1);
383 gtk_widget_show (button);
384
385 menu_item = gtk_menu_item_new ();
386 gtk_container_add (GTK_CONTAINER (menu_item), button);
387 gtk_menu_append (GTK_MENU (menu), menu_item);
388 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
389 GTK_SIGNAL_FUNC (callback), GINT_TO_POINTER(i));
390 gtk_widget_show (menu_item);
391
392 set_bg_color (menu_item, i);
393 set_bg_color (button, i);
394 }
395
396 return menu;
397 }
398
399
qw_colors_pixmap_create(GtkWidget * window,unsigned char top,unsigned char bottom,GSList ** cache)400 GdkPixmap *qw_colors_pixmap_create (GtkWidget *window, unsigned char top,
401 unsigned char bottom, GSList **cache) {
402 GdkPixmap *pixmap;
403 unsigned key;
404 int w, h;
405
406 key = (top << 8) + bottom;
407
408 if (cache) {
409 pixmap_cache_lookup (*cache, &pixmap, NULL, key);
410 if (pixmap)
411 return pixmap;
412 }
413
414 if (!GTK_WIDGET_REALIZED (window))
415 gtk_widget_realize (window);
416
417 h = player_clist->row_height - 2;
418 w = h * 3 / 2;
419
420 pixmap = two_colors_pixmap (window->window, w, h, &pcolors[top],
421 &pcolors[bottom]);
422 if (cache)
423 pixmap_cache_add (cache, pixmap, NULL, key);
424
425 return pixmap;
426 }
427
428