rev |
line source |
nuclear@1
|
1 /*
|
nuclear@1
|
2 256-color 3D graphics hack for real-mode DOS.
|
nuclear@1
|
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@1
|
4
|
nuclear@1
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@1
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@1
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@1
|
8 (at your option) any later version.
|
nuclear@1
|
9
|
nuclear@1
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@1
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@1
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@1
|
13 GNU General Public License for more details.
|
nuclear@1
|
14
|
nuclear@1
|
15 You should have received a copy of the GNU General Public License
|
nuclear@1
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@1
|
17 */
|
nuclear@1
|
18
|
nuclear@1
|
19 #include <string.h>
|
nuclear@1
|
20 #include "palman.h"
|
nuclear@1
|
21
|
nuclear@1
|
22 #define MAX_COLORS 256
|
nuclear@1
|
23 static struct palm_color colors[MAX_COLORS];
|
nuclear@1
|
24 static int base_index[MAX_COLORS];
|
nuclear@1
|
25
|
nuclear@1
|
26 static struct palm_color pal[MAX_COLORS];
|
nuclear@1
|
27 static unsigned int ncol, pcol;
|
nuclear@1
|
28 static int range;
|
nuclear@1
|
29
|
nuclear@1
|
30 void palm_clear(void)
|
nuclear@1
|
31 {
|
nuclear@1
|
32 ncol = 0;
|
nuclear@1
|
33 }
|
nuclear@1
|
34
|
nuclear@1
|
35 int palm_add_color(unsigned char r, unsigned char g, unsigned char b)
|
nuclear@1
|
36 {
|
nuclear@1
|
37 if(ncol >= MAX_COLORS) {
|
nuclear@1
|
38 return -1;
|
nuclear@1
|
39 }
|
nuclear@1
|
40 colors[ncol].r = r;
|
nuclear@1
|
41 colors[ncol].g = g;
|
nuclear@1
|
42 colors[ncol].b = b;
|
nuclear@1
|
43 ncol++;
|
nuclear@1
|
44 return 0;
|
nuclear@1
|
45 }
|
nuclear@1
|
46
|
nuclear@4
|
47 int palm_color_index(unsigned char r, unsigned char g, unsigned char b)
|
nuclear@4
|
48 {
|
nuclear@4
|
49 int i;
|
nuclear@4
|
50
|
nuclear@4
|
51 for(i=0; i<ncol; i++) {
|
nuclear@4
|
52 if(colors[i].r == r && colors[i].g == g && colors[i].b == b) {
|
nuclear@4
|
53 return i;
|
nuclear@4
|
54 }
|
nuclear@4
|
55 }
|
nuclear@4
|
56 return -1;
|
nuclear@4
|
57 }
|
nuclear@4
|
58
|
nuclear@1
|
59 /* TODO: build an octree */
|
nuclear@1
|
60 int palm_build(void)
|
nuclear@1
|
61 {
|
nuclear@1
|
62 int i, j;
|
nuclear@1
|
63
|
nuclear@8
|
64 if(!ncol) {
|
nuclear@8
|
65 return -1;
|
nuclear@8
|
66 }
|
nuclear@8
|
67
|
nuclear@1
|
68 /* gradient range for each color */
|
nuclear@1
|
69 range = MAX_COLORS / ncol;
|
nuclear@1
|
70
|
nuclear@8
|
71 if(range <= 1) {
|
nuclear@1
|
72 memcpy(pal, colors, ncol * sizeof *pal);
|
nuclear@1
|
73 return 0;
|
nuclear@1
|
74 }
|
nuclear@1
|
75 pcol = 0;
|
nuclear@1
|
76
|
nuclear@1
|
77 for(i=0; i<ncol; i++) {
|
nuclear@6
|
78 unsigned short r, g, b, dr, dg, db;
|
nuclear@1
|
79
|
nuclear@1
|
80 base_index[i] = pcol;
|
nuclear@1
|
81
|
nuclear@6
|
82 dr = ((unsigned short)colors[i].r << 8) / (range - 1);
|
nuclear@6
|
83 dg = ((unsigned short)colors[i].g << 8) / (range - 1);
|
nuclear@6
|
84 db = ((unsigned short)colors[i].b << 8) / (range - 1);
|
nuclear@1
|
85
|
nuclear@1
|
86 r = g = b = 0;
|
nuclear@1
|
87 for(j=0; j<range; j++) {
|
nuclear@1
|
88 pal[pcol].r = (unsigned char)(r >> 8);
|
nuclear@1
|
89 pal[pcol].g = (unsigned char)(g >> 8);
|
nuclear@1
|
90 pal[pcol].b = (unsigned char)(b >> 8);
|
nuclear@1
|
91 pcol++;
|
nuclear@1
|
92 r += dr;
|
nuclear@1
|
93 g += dg;
|
nuclear@1
|
94 b += db;
|
nuclear@1
|
95 }
|
nuclear@1
|
96 }
|
nuclear@1
|
97 return pcol;
|
nuclear@1
|
98 }
|
nuclear@1
|
99
|
nuclear@1
|
100 struct palm_color *palm_palette(void)
|
nuclear@1
|
101 {
|
nuclear@1
|
102 return pal;
|
nuclear@1
|
103 }
|
nuclear@1
|
104
|
nuclear@1
|
105 int palm_palette_size(void)
|
nuclear@1
|
106 {
|
nuclear@1
|
107 return pcol;
|
nuclear@1
|
108 }
|
nuclear@1
|
109
|
nuclear@1
|
110
|
nuclear@1
|
111 int palm_color_base(unsigned char r, unsigned char g, unsigned char b)
|
nuclear@1
|
112 {
|
nuclear@4
|
113 int c;
|
nuclear@1
|
114
|
nuclear@4
|
115 if((c = palm_color_index(r, g, b)) == -1) {
|
nuclear@4
|
116 return -1;
|
nuclear@1
|
117 }
|
nuclear@4
|
118 return base_index[c];
|
nuclear@1
|
119 }
|
nuclear@1
|
120
|
nuclear@1
|
121 int palm_color_range(void)
|
nuclear@1
|
122 {
|
nuclear@1
|
123 return range;
|
nuclear@1
|
124 }
|