# HG changeset patch # User John Tsiombikas # Date 1321857358 -7200 # Node ID 0b7f840afe4aefbb7afca2aeffdb522da0364b39 # Parent f04884489bad18f2e14b604d614296c99b188f87 palette management diff -r f04884489bad -r 0b7f840afe4a Makefile --- a/Makefile Mon Nov 21 06:14:01 2011 +0200 +++ b/Makefile Mon Nov 21 08:35:58 2011 +0200 @@ -1,5 +1,6 @@ obj = src/test.o \ src/mingl.o src/mglrast.o src/mglgen.o \ + src/palman.o \ dosemu/dosemu.o dep = $(obj:.o=.d) bin = test diff -r f04884489bad -r 0b7f840afe4a src/palman.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/palman.c Mon Nov 21 08:35:58 2011 +0200 @@ -0,0 +1,110 @@ +/* +256-color 3D graphics hack for real-mode DOS. +Copyright (C) 2011 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include "palman.h" + +#define MAX_COLORS 256 +static struct palm_color colors[MAX_COLORS]; +static int base_index[MAX_COLORS]; + +static struct palm_color pal[MAX_COLORS]; +static unsigned int ncol, pcol; +static int range; + +void palm_clear(void) +{ + ncol = 0; +} + +int palm_add_color(unsigned char r, unsigned char g, unsigned char b) +{ + if(ncol >= MAX_COLORS) { + return -1; + } + colors[ncol].r = r; + colors[ncol].g = g; + colors[ncol].b = b; + ncol++; + return 0; +} + +/* TODO: build an octree */ +int palm_build(void) +{ + int i, j; + + /* gradient range for each color */ + range = MAX_COLORS / ncol; + + if(range == 1) { + memcpy(pal, colors, ncol * sizeof *pal); + return 0; + } + pcol = 0; + + for(i=0; i> 8); + pal[pcol].g = (unsigned char)(g >> 8); + pal[pcol].b = (unsigned char)(b >> 8); + pcol++; + r += dr; + g += dg; + b += db; + } + } + return pcol; +} + +struct palm_color *palm_palette(void) +{ + return pal; +} + +int palm_palette_size(void) +{ + return pcol; +} + + +int palm_color_base(unsigned char r, unsigned char g, unsigned char b) +{ + int i; + + for(i=0; i + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef PALMAN_H_ +#define PALMAN_H_ + +struct palm_color { + unsigned char r, g, b; +}; + +void palm_clear(void); +int palm_add_color(unsigned char r, unsigned char g, unsigned char b); + +int palm_build(void); + +struct palm_color *palm_palette(void); +int palm_palette_size(void); + +int palm_color_base(unsigned char r, unsigned char g, unsigned char b); +int palm_color_range(void); + + +#endif /* PALMAN_H_ */ diff -r f04884489bad -r 0b7f840afe4a src/test.c --- a/src/test.c Mon Nov 21 06:14:01 2011 +0200 +++ b/src/test.c Mon Nov 21 08:35:58 2011 +0200 @@ -24,10 +24,7 @@ #include "mingl.h" #include "timer.h" #include "mouse.h" - -#define ROFFS 64 -#define GOFFS 128 -#define BOFFS 192 +#include "palman.h" static int init(void); static void shutdown(void); @@ -42,6 +39,9 @@ static unsigned char *fbuf; +static int white_base, red_base, green_base, blue_base; +static int grad_range; + static int use_vsync = 1; static int under_windows = 0; static unsigned long num_frm; @@ -98,6 +98,7 @@ static int init(void) { int i; + struct palm_color *pal; init_timer(under_windows ? 0 : 100); @@ -109,12 +110,21 @@ signal(SIGILL, sighandler); signal(SIGABRT, sighandler); - for(i=0; i<64; i++) { - int x = i << 2; - set_palette(i, x, x, x); - set_palette(i + ROFFS, x, 0, 0); - set_palette(i + GOFFS, 0, x, 0); - set_palette(i + BOFFS, 0, 0, x); + palm_add_color(255, 255, 255); + palm_add_color(255, 0, 0); + palm_add_color(0, 255, 0); + palm_add_color(0, 0, 255); + palm_build(); + + white_base = palm_color_base(255, 255, 255); + red_base = palm_color_base(255, 0, 0); + green_base = palm_color_base(0, 255, 0); + blue_base = palm_color_base(0, 0, 255); + grad_range = palm_color_range(); + + pal = palm_palette(); + for(i=0; i