]> git.draconx.ca Git - rrace.git/blob - src/icon.c
4970694c1abca950ebdc4da91029e2a5e8585c4d
[rrace.git] / src / icon.c
1 /*
2  * Icon generation helpers for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <string.h>
21
22 #include "colour.h"
23 #include "icon.h"
24
25 static void format_row(unsigned char *row, unsigned long c,
26                        unsigned s, unsigned y, unsigned w, unsigned h)
27 {
28         unsigned x;
29
30         for (x = 0; x < w; x++) {
31                 row[x] = (c >> 8*COLOUR_PRIMARY) & 0xff;
32                 if (x < s || y < s)
33                         row[x] = (c >> 8*COLOUR_LIGHT) & 0xff;
34                 if ((x+s >= w && x+y >= w) || (y+s >= h && x+y >= h))
35                         row[x] = (c >> 8*COLOUR_DARK) & 0xff;
36         }
37 }
38
39 /*
40  * The 16x16 icon is drawn with 1px shadow, 6x6 tiles, with 1 pixel cropped
41  * off all the edge tiles.
42  */
43 void icon_tile16(unsigned char *icon, unsigned long c, int tile_x, int tile_y)
44 {
45         int out_x, out_y, out_w, y;
46         unsigned char row[6];
47
48         out_x = tile_x * 11 / 2;
49         out_y = tile_y * 11 / 2;
50         out_w = (5 + (tile_x == 1)) * sizeof row[0];
51
52         for (y = 0+(tile_y == 0); y < 6-(tile_y == 2); y++) {
53                 format_row(row, c, 1, y, 6, 6);
54                 memcpy(&icon[16 * out_y++ + out_x], &row[tile_x == 0], out_w);
55         }
56 }
57
58 /*
59  * The 24x24 icon is drawn with 1px shadow and 8x8 tiles.
60  */
61 void icon_tile24(unsigned char *icon, unsigned long c, int tile_x, int tile_y)
62 {
63         int out_x, out_y, y;
64
65         out_x = tile_x * 8;
66         out_y = tile_y * 8;
67
68         for (y = 0; y < 8; y++) {
69                 format_row(&icon[24 * out_y++ + out_x], c, 1, y, 8, 8);
70         }
71 }
72
73 /*
74  * The 32x32 icon is drawn with 1px shadow with slightly uneven tiles on
75  * an 11-10-11 pixel grid.
76  */
77 void icon_tile32(unsigned char *icon, unsigned long c, int tile_x, int tile_y)
78 {
79         int out_x, out_y, out_w, out_h, y;
80
81         out_x = 10*tile_x + (tile_x > 0);
82         out_y = 10*tile_y + (tile_y > 0);
83         out_w = 10 + (tile_x != 1);
84         out_h = 10 + (tile_y != 1);
85
86         for (y = 0; y < out_h; y++) {
87                 format_row(&icon[32 * out_y++ + out_x], c, 1, y, out_w, out_h);
88         }
89 }
90
91 /*
92  * The 48x48 icon is drawn with 2px shadow and 16x16 tiles.
93  */
94 void icon_tile48(unsigned char *icon, unsigned long c, int tile_x, int tile_y)
95 {
96         int out_x, out_y, y;
97
98         out_x = tile_x * 16;
99         out_y = tile_y * 16;
100
101         for (y = 0; y < 16; y++) {
102                 format_row(&icon[48 * out_y++ + out_x], c, 2, y, 16, 16);
103         }
104 }