1 /*****************************************************************************/
2 /* */
3 /* PALETTE.CC */
4 /* */
5 /* (C) 1993-95 Ullrich von Bassewitz */
6 /* Zwehrenbuehlstrasse 33 */
7 /* D-72070 Tuebingen */
8 /* EMail: uz@ibb.schwaben.com */
9 /* */
10 /*****************************************************************************/
11
12
13
14 // $Id$
15 //
16 // $Log$
17 //
18 //
19
20
21
22 #include <string.h>
23
24 #include "palette.h"
25 #include "screen.h"
26 #include "streamid.h"
27
28
29
30 // Register the classes
31 LINK (Palette, ID_Palette);
32
33
34
35 // Instance of class Palette
36 Palette* Pal;
37
38
39
40 /*****************************************************************************/
41 /* Explicit template instantiation */
42 /*****************************************************************************/
43
44
45
46 #ifdef EXPLICIT_TEMPLATES
47 template class Collection<unsigned char>;
48 #endif
49
50
51
52 /*****************************************************************************/
53 /* class Palette */
54 /*****************************************************************************/
55
56
57
FreeItem(void * Item)58 void Palette::FreeItem (void* Item)
59 {
60 delete [] (unsigned char*) Item;
61 }
62
63
64
GetItem(Stream & S)65 void* Palette::GetItem (Stream& S)
66 {
67 return (void*) S.ReadStr ();
68 }
69
70
71
PutItem(Stream & S,void * Item) const72 void Palette::PutItem (Stream& S, void* Item) const
73 {
74 S.WriteStr ((char*) Item);
75 }
76
77
78
Palette()79 Palette::Palette () :
80 Collection<unsigned char> (10, 5, 1),
81 Debug (0)
82 {
83 }
84
85
86
Load(Stream & S)87 void Palette::Load (Stream& S)
88 {
89 Collection<unsigned char>::Load (S);
90 S >> Debug;
91 }
92
93
94
Store(Stream & S) const95 void Palette::Store (Stream& S) const
96 {
97 Collection<unsigned char>::Store (S);
98 S << Debug;
99 }
100
101
102
StreamableID() const103 u16 Palette::StreamableID () const
104 {
105 return ID_Palette;
106 }
107
108
109
Build()110 Streamable *Palette::Build ()
111 {
112 return new Palette (Empty);
113 }
114
115
116
Add(const unsigned char * Mono,const unsigned char * Color)117 unsigned Palette::Add (const unsigned char* Mono, const unsigned char* Color)
118 // Function to add a palette entry, returns the palette number
119 {
120 unsigned PalNum = Count / 2;
121 Insert ((unsigned char*) strcpy (new char [strlen ((char*) Mono)+1], (char*) Mono));
122 Insert ((unsigned char*) strcpy (new char [strlen ((char*) Color)+1], (char*) Color));
123 return PalNum;
124 }
125
126
127
BuildAttr(unsigned PalNum,unsigned AttrIndex,unsigned char C)128 u16 Palette::BuildAttr (unsigned PalNum, unsigned AttrIndex, unsigned char C)
129 // Builds an attribute char from the given palette and index and the given char.
130 {
131 unsigned char *P;
132 if (TheScreen->IsColor () == 0) {
133 // Mono
134 P = At (PalNum * 2);
135 } else {
136 // Color
137 P = At (PalNum * 2 + 1);
138 }
139
140 if (Debug) {
141 // Slows things down, so check only if Debug set
142 PRECONDITION (AttrIndex < strlen ((char*) P));
143 }
144 return (u16) ((u16 (P [AttrIndex]) << 8) | u16 (C));
145 }
146
147
148
149