deepstone
changeset 1:0b7f840afe4a
palette management
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 21 Nov 2011 08:35:58 +0200 |
parents | f04884489bad |
children | 8c9a63a902d5 |
files | Makefile src/palman.c src/palman.h src/test.c |
diffstat | 4 files changed, 174 insertions(+), 25 deletions(-) [+] |
line diff
1.1 --- a/Makefile Mon Nov 21 06:14:01 2011 +0200 1.2 +++ b/Makefile Mon Nov 21 08:35:58 2011 +0200 1.3 @@ -1,5 +1,6 @@ 1.4 obj = src/test.o \ 1.5 src/mingl.o src/mglrast.o src/mglgen.o \ 1.6 + src/palman.o \ 1.7 dosemu/dosemu.o 1.8 dep = $(obj:.o=.d) 1.9 bin = test
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/palman.c Mon Nov 21 08:35:58 2011 +0200 2.3 @@ -0,0 +1,110 @@ 2.4 +/* 2.5 +256-color 3D graphics hack for real-mode DOS. 2.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 2.7 + 2.8 +This program is free software: you can redistribute it and/or modify 2.9 +it under the terms of the GNU General Public License as published by 2.10 +the Free Software Foundation, either version 3 of the License, or 2.11 +(at your option) any later version. 2.12 + 2.13 +This program is distributed in the hope that it will be useful, 2.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 2.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2.16 +GNU General Public License for more details. 2.17 + 2.18 +You should have received a copy of the GNU General Public License 2.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 2.20 +*/ 2.21 + 2.22 +#include <string.h> 2.23 +#include "palman.h" 2.24 + 2.25 +#define MAX_COLORS 256 2.26 +static struct palm_color colors[MAX_COLORS]; 2.27 +static int base_index[MAX_COLORS]; 2.28 + 2.29 +static struct palm_color pal[MAX_COLORS]; 2.30 +static unsigned int ncol, pcol; 2.31 +static int range; 2.32 + 2.33 +void palm_clear(void) 2.34 +{ 2.35 + ncol = 0; 2.36 +} 2.37 + 2.38 +int palm_add_color(unsigned char r, unsigned char g, unsigned char b) 2.39 +{ 2.40 + if(ncol >= MAX_COLORS) { 2.41 + return -1; 2.42 + } 2.43 + colors[ncol].r = r; 2.44 + colors[ncol].g = g; 2.45 + colors[ncol].b = b; 2.46 + ncol++; 2.47 + return 0; 2.48 +} 2.49 + 2.50 +/* TODO: build an octree */ 2.51 +int palm_build(void) 2.52 +{ 2.53 + int i, j; 2.54 + 2.55 + /* gradient range for each color */ 2.56 + range = MAX_COLORS / ncol; 2.57 + 2.58 + if(range == 1) { 2.59 + memcpy(pal, colors, ncol * sizeof *pal); 2.60 + return 0; 2.61 + } 2.62 + pcol = 0; 2.63 + 2.64 + for(i=0; i<ncol; i++) { 2.65 + unsigned int r, g, b, dr, dg, db; 2.66 + 2.67 + base_index[i] = pcol; 2.68 + 2.69 + dr = (colors[i].r << 8) / (range - 1); 2.70 + dg = (colors[i].g << 8) / (range - 1); 2.71 + db = (colors[i].b << 8) / (range - 1); 2.72 + 2.73 + r = g = b = 0; 2.74 + for(j=0; j<range; j++) { 2.75 + pal[pcol].r = (unsigned char)(r >> 8); 2.76 + pal[pcol].g = (unsigned char)(g >> 8); 2.77 + pal[pcol].b = (unsigned char)(b >> 8); 2.78 + pcol++; 2.79 + r += dr; 2.80 + g += dg; 2.81 + b += db; 2.82 + } 2.83 + } 2.84 + return pcol; 2.85 +} 2.86 + 2.87 +struct palm_color *palm_palette(void) 2.88 +{ 2.89 + return pal; 2.90 +} 2.91 + 2.92 +int palm_palette_size(void) 2.93 +{ 2.94 + return pcol; 2.95 +} 2.96 + 2.97 + 2.98 +int palm_color_base(unsigned char r, unsigned char g, unsigned char b) 2.99 +{ 2.100 + int i; 2.101 + 2.102 + for(i=0; i<pcol; i++) { 2.103 + if(colors[i].r == r && colors[i].g == g && colors[i].b == b) { 2.104 + return base_index[i]; 2.105 + } 2.106 + } 2.107 + return -1; 2.108 +} 2.109 + 2.110 +int palm_color_range(void) 2.111 +{ 2.112 + return range; 2.113 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/palman.h Mon Nov 21 08:35:58 2011 +0200 3.3 @@ -0,0 +1,38 @@ 3.4 +/* 3.5 +256-color 3D graphics hack for real-mode DOS. 3.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 3.7 + 3.8 +This program is free software: you can redistribute it and/or modify 3.9 +it under the terms of the GNU General Public License as published by 3.10 +the Free Software Foundation, either version 3 of the License, or 3.11 +(at your option) any later version. 3.12 + 3.13 +This program is distributed in the hope that it will be useful, 3.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 3.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3.16 +GNU General Public License for more details. 3.17 + 3.18 +You should have received a copy of the GNU General Public License 3.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 3.20 +*/ 3.21 + 3.22 +#ifndef PALMAN_H_ 3.23 +#define PALMAN_H_ 3.24 + 3.25 +struct palm_color { 3.26 + unsigned char r, g, b; 3.27 +}; 3.28 + 3.29 +void palm_clear(void); 3.30 +int palm_add_color(unsigned char r, unsigned char g, unsigned char b); 3.31 + 3.32 +int palm_build(void); 3.33 + 3.34 +struct palm_color *palm_palette(void); 3.35 +int palm_palette_size(void); 3.36 + 3.37 +int palm_color_base(unsigned char r, unsigned char g, unsigned char b); 3.38 +int palm_color_range(void); 3.39 + 3.40 + 3.41 +#endif /* PALMAN_H_ */
4.1 --- a/src/test.c Mon Nov 21 06:14:01 2011 +0200 4.2 +++ b/src/test.c Mon Nov 21 08:35:58 2011 +0200 4.3 @@ -24,10 +24,7 @@ 4.4 #include "mingl.h" 4.5 #include "timer.h" 4.6 #include "mouse.h" 4.7 - 4.8 -#define ROFFS 64 4.9 -#define GOFFS 128 4.10 -#define BOFFS 192 4.11 +#include "palman.h" 4.12 4.13 static int init(void); 4.14 static void shutdown(void); 4.15 @@ -42,6 +39,9 @@ 4.16 4.17 static unsigned char *fbuf; 4.18 4.19 +static int white_base, red_base, green_base, blue_base; 4.20 +static int grad_range; 4.21 + 4.22 static int use_vsync = 1; 4.23 static int under_windows = 0; 4.24 static unsigned long num_frm; 4.25 @@ -98,6 +98,7 @@ 4.26 static int init(void) 4.27 { 4.28 int i; 4.29 + struct palm_color *pal; 4.30 4.31 init_timer(under_windows ? 0 : 100); 4.32 4.33 @@ -109,12 +110,21 @@ 4.34 signal(SIGILL, sighandler); 4.35 signal(SIGABRT, sighandler); 4.36 4.37 - for(i=0; i<64; i++) { 4.38 - int x = i << 2; 4.39 - set_palette(i, x, x, x); 4.40 - set_palette(i + ROFFS, x, 0, 0); 4.41 - set_palette(i + GOFFS, 0, x, 0); 4.42 - set_palette(i + BOFFS, 0, 0, x); 4.43 + palm_add_color(255, 255, 255); 4.44 + palm_add_color(255, 0, 0); 4.45 + palm_add_color(0, 255, 0); 4.46 + palm_add_color(0, 0, 255); 4.47 + palm_build(); 4.48 + 4.49 + white_base = palm_color_base(255, 255, 255); 4.50 + red_base = palm_color_base(255, 0, 0); 4.51 + green_base = palm_color_base(0, 255, 0); 4.52 + blue_base = palm_color_base(0, 0, 255); 4.53 + grad_range = palm_color_range(); 4.54 + 4.55 + pal = palm_palette(); 4.56 + for(i=0; i<palm_palette_size(); i++) { 4.57 + set_palette(i, pal[i].r, pal[i].g, pal[i].b); 4.58 } 4.59 4.60 if(mgl_init(320, 200) == -1) { 4.61 @@ -125,7 +135,7 @@ 4.62 4.63 mgl_enable(MGL_CULL_FACE); 4.64 mgl_enable(MGL_SMOOTH); 4.65 - mgl_color_range(63); /* gradient range */ 4.66 + mgl_color_range(grad_range - 1); /* gradient range */ 4.67 4.68 mgl_enable(MGL_LIGHTING); 4.69 mgl_light_intensity(0, 1.0); 4.70 @@ -162,30 +172,20 @@ 4.71 4.72 switch(prim) { 4.73 case TORUS: 4.74 - mgl_index(GOFFS); 4.75 + mgl_index(green_base); 4.76 mgl_torus(1.0, 0.25, 16, 8); 4.77 break; 4.78 case SPHERE: 4.79 - mgl_index(BOFFS); 4.80 + mgl_index(blue_base); 4.81 mgl_sphere(1.0, 16, 8); 4.82 break; 4.83 case CUBE: 4.84 - mgl_index(ROFFS); 4.85 + mgl_index(red_base); 4.86 mgl_cube(1.0); 4.87 } 4.88 4.89 - /*mgl_begin(MGL_QUADS); 4.90 - mgl_index(ROFFS); 4.91 - mgl_color1f(1.0); 4.92 - mgl_vertex2f(-1, -1); 4.93 - mgl_vertex2f(1, -1); 4.94 - mgl_color1f(0.1); 4.95 - mgl_vertex2f(1, 1); 4.96 - mgl_vertex2f(-1, 1); 4.97 - mgl_end();*/ 4.98 - 4.99 if(!auto_rotate) { 4.100 - draw_cursor(fbuf, 320, 200, mx, my, 63); 4.101 + draw_cursor(fbuf, 320, 200, mx, my, white_base + grad_range - 1); 4.102 } 4.103 4.104 copy_frame(fbuf);