123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /* $Id$
- * MegaZeux
- *
- * Copyright (C) 1996 Greg Janson
- * Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
- *
- * 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 2 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, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- //Palette code. Works on both the EGA and the VGA, although fading and
- //intensity percentage resolution is much less on the EGA, and rgb values
- //are rounded to 6 bit when output to the graphics card. (VGA rgb values
- //are 18 bit)
- #include "palette.h"
- #include "retrace.h"
- #include "comp_chk.h"
- //Current palette w/o intensity adjustments (18 bit)
- char current_pal[16][3];
- //Current intensity level (percentage) per color
- char current_intensity[16];
- //Intensity modified palette (updated at all times)
- char intensity_pal[16][3];
- //Whether we are currently "faded out" from a quick fade function
- char faded_out=0;
- //Saved intensities while faded out
- char saved_intensity[16];
- //Default EGA hardware palette. Also used for VGA palette refrencing.
- char default_EGA_hardware_pal[16]={
- 0,1,2,3,4,5,20,7,56,57,58,59,60,61,62,63 };
- //Default palette rgbs
- char default_pal[16][3]={
- { 00,00,00 },
- { 00,00,42 },
- { 00,42,00 },
- { 00,42,42 },
- { 42,00,00 },
- { 42,00,42 },
- { 42,21,00 },
- { 42,42,42 },
- { 21,21,21 },
- { 21,21,63 },
- { 21,63,21 },
- { 21,63,63 },
- { 63,21,21 },
- { 63,21,63 },
- { 63,63,21 },
- { 63,63,63 } };
- //Set one EGA palette register. Blanks screen. Sets attr, 0-15, to
- //ccode, 0-63.
- void set_ega_register(char attr,char ccode) {
- asm {
- mov dx,03C0h
- mov al,attr
- out dx,al
- mov al,ccode
- out dx,al
- }
- }
- //Unblank the screen after setting EGA palette registers.
- void unblank_screen(void) {
- asm {
- mov dx,03C0h
- mov al,20h
- out dx,al
- out dx,al
- }
- }
- //Set one VGA DAC palette register.
- void set_vga_register(char color,char r,char g,char b) {
- //Change color number to DAC register
- if(color<0) color=-color;
- else color=default_EGA_hardware_pal[color];
- asm {
- mov dx,03C8h
- mov al,color
- out dx,al
- inc dx
- mov al,r
- out dx,al
- mov al,g
- out dx,al
- mov al,b
- out dx,al
- }
- }
- //Initialize palette- On both EGA and VGA, sets the proper palette
- //codes. (IE sets up the default EGA palette) Call before using any
- //other code. Also call on exit or anytime to fully reset the palette
- //to its defaults.
- void init_palette(void) {
- int t1;
- //On both EGA and VGA, we must set all 16 registers to the defaults.
- //Also copy it over to the current palette.
- wait_retrace();
- for(t1=0;t1<16;t1++) {
- set_ega_register(t1,default_EGA_hardware_pal[t1]);
- current_intensity[t1]=saved_intensity[t1]=100;
- if(vga_avail)
- set_vga_register(t1,default_pal[t1][0],default_pal[t1][1],
- default_pal[t1][2]);
- intensity_pal[t1][0]=current_pal[t1][0]=default_pal[t1][0];
- intensity_pal[t1][1]=current_pal[t1][1]=default_pal[t1][1];
- intensity_pal[t1][2]=current_pal[t1][2]=default_pal[t1][2];
- }
- unblank_screen();
- faded_out=0;
- //Palette is now fully at default, and all data is initialized.
- }
- //Set current palette intensity, internally only.
- void set_palette_intensity(char percent) {
- int t1,t2;
- if(percent<0) percent=0;
- if(percent>100) percent=100;
- if(faded_out) {
- //If faded out, save in saved_intensity and exit
- for(t1=0;t1<16;t1++)
- saved_intensity[t1]=percent;
- return;
- }
- //Copy palette to intensity palette, adjusting intensity
- for(t1=0;t1<16;t1++) {
- current_intensity[t1]=percent;
- for(t2=0;t2<3;t2++) {
- intensity_pal[t1][t2]=current_pal[t1][t2]*percent/100;
- }
- }
- //Done
- return;
- }
- //Set current palette intensity, internally only, but for only one color.
- void set_color_intensity(char color,char percent) {
- int t2;
- if(percent<0) percent=0;
- if(percent>100) percent=100;
- if(faded_out) {
- //Put in saved if faded out
- saved_intensity[color]=percent;
- return;
- }
- //Copy palette to intensity palette, adjusting intensity
- current_intensity[color]=percent;
- for(t2=0;t2<3;t2++)
- intensity_pal[color][t2]=current_pal[color][t2]*percent/100;
- //Done
- return;
- }
- //Set rgb for one color, internally only. Sets current_pal. Also sets
- //intensity_pal according to current intensity. R G B can range from
- //0 to 63.
- void set_rgb(char color,char r,char g,char b) {
- int t2;
- if(r<0) r=0; if(g<0) g=0; if(b<0) b=0;
- if(r>63) r=63; if(g>63) g=63; if(b>63) b=63;
- //Set current pal
- current_pal[color][0]=r;
- current_pal[color][1]=g;
- current_pal[color][2]=b;
- //Copy palette to intensity palette, adjusting intensity
- for(t2=0;t2<3;t2++)
- intensity_pal[color][t2]=current_pal[color][t2]*
- current_intensity[color]/100;
- //Done
- return;
- }
- //Update palette onscreen. Waits for retrace if specified. (default)
- void update_palette(char wait_for_retrace) {
- int t1,t2,r,g,b;
- if(faded_out) return;
- //Wait for retrace if applicable
- if(wait_for_retrace) wait_retrace();
- //VGA
- if(vga_avail) {
- for(t1=0;t1<16;t1++)
- set_vga_register(t1,intensity_pal[t1][0],intensity_pal[t1][1],
- intensity_pal[t1][2]);
- //Special code for #0
- set_vga_register(-6,intensity_pal[0][0],intensity_pal[0][1],
- intensity_pal[0][2]);
- for(t1=8;t1<20;t1++)
- set_vga_register(-t1,intensity_pal[0][0],intensity_pal[0][1],
- intensity_pal[0][2]);
- for(t1=21;t1<56;t1++)
- set_vga_register(-t1,intensity_pal[0][0],intensity_pal[0][1],
- intensity_pal[0][2]);
- //Done
- return;
- }
- //EGA- turn 18 bit to 6 bit
- for(t1=0;t1<16;t1++) {
- t2=0;
- r=intensity_pal[t1][0];
- g=intensity_pal[t1][1];
- b=intensity_pal[t1][2];
- if(r<16) ;
- else if(r<32) t2|=32;
- else if(r<48) t2|=4;
- else t2|=36;
- if(g<16) ;
- else if(g<32) t2|=16;
- else if(g<48) t2|=2;
- else t2|=18;
- if(b<16) ;
- else if(b<32) t2|=8;
- else if(b<48) t2|=1;
- else t2|=9;
- set_ega_register(t1,t2);
- }
- unblank_screen();
- //Done
- }
- //Very quick fade out. Saves intensity table for fade in. Be sure
- //to use in conjuction with the next function.
- void vquick_fadeout(void) {
- int t1,t2;
- if(faded_out) return;
- //Save intensity table
- for(t1=0;t1<16;t1++)
- saved_intensity[t1]=current_intensity[t1];
- //Quick fadeout
- for(t1=0;t1<10;t1++) {
- for(t2=0;t2<16;t2++)
- set_color_intensity(t2,current_intensity[t2]-10);
- update_palette();
- }
- //Done
- faded_out=1;
- }
- //Very quick fade in. Uses intensity table saved from fade out. For
- //use in conjuction with the previous function.
- void vquick_fadein(void) {
- int t1,t2;
- if(!faded_out) return;
- //Clear now so update function works
- faded_out=0;
- //Quick fadein
- for(t1=0;t1<10;t1++) {
- for(t2=0;t2<16;t2++) {
- current_intensity[t2]+=10;
- if(current_intensity[t2]>saved_intensity[t2])
- current_intensity[t2]=saved_intensity[t2];
- set_color_intensity(t2,current_intensity[t2]);
- }
- update_palette();
- }
- //Done
- }
- //Instant fade out
- void insta_fadeout(void) {
- int t1;
- if(faded_out) return;
- //Save intensity table
- for(t1=0;t1<16;t1++)
- saved_intensity[t1]=current_intensity[t1];
- for(t1=0;t1<16;t1++)
- set_color_intensity(t1,0);
- update_palette();
- //Done
- faded_out=1;
- }
- //Insta fade in
- void insta_fadein(void) {
- int t1;
- if(!faded_out) return;
- //Clear now so update function works
- faded_out=0;
- //Quick fadein
- for(t1=0;t1<16;t1++)
- set_color_intensity(t1,current_intensity[t1]=saved_intensity[t1]);
- update_palette();
- //Done
- }
- //Sets RGB's to default EGA palette
- void default_palette(void) {
- for(int t1=0;t1<16;t1++)
- set_rgb(t1,default_pal[t1][0],default_pal[t1][1],
- default_pal[t1][2]);
- update_palette();
- }
- //Get the current intensity of a color
- char get_color_intensity(char color) {
- if(faded_out)
- return saved_intensity[color];
- return current_intensity[color];
- }
- //Gets the current RGB of a color
- void get_rgb(char color,char &r,char &g,char &b) {
- r=current_pal[color][0];
- g=current_pal[color][1];
- b=current_pal[color][2];
- }
- //Returns non-zero if faded out
- char is_faded(void) {
- return faded_out;
- }
|