dos3d

view src/palman.c @ 8:569d62294ae3

minor
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 12:45:18 +0200
parents 7f12c7d084d5
children
line source
1 /*
2 256-color 3D graphics hack for real-mode DOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <string.h>
20 #include "palman.h"
22 #define MAX_COLORS 256
23 static struct palm_color colors[MAX_COLORS];
24 static int base_index[MAX_COLORS];
26 static struct palm_color pal[MAX_COLORS];
27 static unsigned int ncol, pcol;
28 static int range;
30 void palm_clear(void)
31 {
32 ncol = 0;
33 }
35 int palm_add_color(unsigned char r, unsigned char g, unsigned char b)
36 {
37 if(ncol >= MAX_COLORS) {
38 return -1;
39 }
40 colors[ncol].r = r;
41 colors[ncol].g = g;
42 colors[ncol].b = b;
43 ncol++;
44 return 0;
45 }
47 int palm_color_index(unsigned char r, unsigned char g, unsigned char b)
48 {
49 int i;
51 for(i=0; i<ncol; i++) {
52 if(colors[i].r == r && colors[i].g == g && colors[i].b == b) {
53 return i;
54 }
55 }
56 return -1;
57 }
59 /* TODO: build an octree */
60 int palm_build(void)
61 {
62 int i, j;
64 if(!ncol) {
65 return -1;
66 }
68 /* gradient range for each color */
69 range = MAX_COLORS / ncol;
71 if(range <= 1) {
72 memcpy(pal, colors, ncol * sizeof *pal);
73 return 0;
74 }
75 pcol = 0;
77 for(i=0; i<ncol; i++) {
78 unsigned short r, g, b, dr, dg, db;
80 base_index[i] = pcol;
82 dr = ((unsigned short)colors[i].r << 8) / (range - 1);
83 dg = ((unsigned short)colors[i].g << 8) / (range - 1);
84 db = ((unsigned short)colors[i].b << 8) / (range - 1);
86 r = g = b = 0;
87 for(j=0; j<range; j++) {
88 pal[pcol].r = (unsigned char)(r >> 8);
89 pal[pcol].g = (unsigned char)(g >> 8);
90 pal[pcol].b = (unsigned char)(b >> 8);
91 pcol++;
92 r += dr;
93 g += dg;
94 b += db;
95 }
96 }
97 return pcol;
98 }
100 struct palm_color *palm_palette(void)
101 {
102 return pal;
103 }
105 int palm_palette_size(void)
106 {
107 return pcol;
108 }
111 int palm_color_base(unsigned char r, unsigned char g, unsigned char b)
112 {
113 int c;
115 if((c = palm_color_index(r, g, b)) == -1) {
116 return -1;
117 }
118 return base_index[c];
119 }
121 int palm_color_range(void)
122 {
123 return range;
124 }