1 /* -*- Mode: C++; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*-
2  *
3  * Quadra, an action puzzle game
4  * Copyright (C) 1998-2000  Ludus Design
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #include "palette.h"
22 
23 #include "video.h"
24 
25 Palette noir;
26 
load(const Image & raw)27 void Palette::load(const Image& raw) {
28   size=raw.palettesize();
29   int j=0;
30   for(int i(0); i<size; i++) {
31     pal[i].r = raw.pal()[j++];
32     pal[i].g = raw.pal()[j++];
33     pal[i].b = raw.pal()[j++];
34   }
35 }
36 
Remap(const Palette & d,Palette * src)37 Remap::Remap(const Palette& d, Palette* src): dst(d) {
38   if(src) {
39     for(int i=0; i<src->size; i++)
40       findrgb(i, src->r(i), src->g(i), src->b(i));
41   }
42 }
43 
findrgb(const Byte m,const Byte r,const Byte g,const Byte b)44 void Remap::findrgb(const Byte m, const Byte r, const Byte g, const Byte b) {
45   int best_diff=9999999, best_i=0, diff;
46   for(int i=1; i<dst.size; i++) {
47     diff=(int) ((dst.pal[i].r-r)*(dst.pal[i].r-r)*2 + (dst.pal[i].g-g)*(dst.pal[i].g-g)*3 + (dst.pal[i].b-b)*(dst.pal[i].b-b));
48     if(diff == 0) {
49       map[m] = i;
50       return;
51     }
52     if(diff < best_diff) {
53       best_i = i;
54       best_diff = diff;
55     }
56   }
57   map[m] = best_i;
58 }
59 
Fade(const Palette & dst,const Palette & src,int frame)60 Fade::Fade(const Palette& dst, const Palette& src, int frame) {
61   int j=0;
62   for(int i(0); i<256; i++) {
63     current[j++]=src.pal[i].r<<7;
64     current[j++]=src.pal[i].g<<7;
65     current[j++]=src.pal[i].b<<7;
66   }
67   newdest(dst, frame);
68 }
69 
setdest(const Palette & dst)70 void Fade::setdest(const Palette& dst) {
71   dest=dst;
72   int j=0;
73   for(int i(0); i<256; i++) {
74     current[j++]=dest.pal[i].r<<7;
75     current[j++]=dest.pal[i].g<<7;
76     current[j++]=dest.pal[i].b<<7;
77   }
78   video->setpal(dest);
79   currentframe=destframe;
80 }
81 
newdest(const Palette & dst,int frame)82 void Fade::newdest(const Palette& dst, int frame) {
83   dest=dst;
84   int j=0;
85 
86   for(int i(0); i<256; i++) {
87     delta[j]=((dest.pal[i].r<<7)-current[j])/frame;
88 	j++;
89     delta[j]=((dest.pal[i].g<<7)-current[j])/frame;
90 	j++;
91     delta[j]=((dest.pal[i].b<<7)-current[j])/frame;
92 	j++;
93   }
94   currentframe=0;
95   destframe=frame;
96 }
97 
step()98 int Fade::step() {
99   if(currentframe==destframe)
100     return 1;
101   else {
102     for(int i(0); i<768; i++)
103       current[i]+=delta[i];
104     currentframe++;
105     return 0;
106   }
107 }
108 
set()109 void Fade::set() {
110   if(currentframe==destframe)
111     return;
112   if(currentframe==destframe-1) {
113     video->setpal(dest);
114   } else {
115     Palette mypal;
116     for(int i(0); i<256; i++)
117       mypal.setcolor(i, current[i*3]>>7, current[i*3+1]>>7, current[i*3+2]>>7);
118     video->setpal(mypal);
119   }
120 }
121 
122