1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14 
15 #define USE_CLIB
16 #include "util/wgt2allg.h"
17 #include "util/stream.h"
18 #include "core/assetmanager.h"
19 #include "gfx/bitmap.h"
20 
21 using namespace AGS::Common;
22 
23   Common::Bitmap *gl_VirtualScreenBitmap;
SetVirtualScreen(Bitmap * bitmap)24   Common::Bitmap *SetVirtualScreen(Bitmap *bitmap)
25   {
26     if (bitmap == NULL)
27     {
28       gl_VirtualScreenBitmap = BitmapHelper::GetScreenBitmap();
29     }
30     else
31     {
32       gl_VirtualScreenBitmap = bitmap;
33     }
34     return gl_VirtualScreenBitmap;
35   }
36 
37   // [IKM] A very, very dangerous stuff!
38   Bitmap gl_VirtualScreenWrapper;
SetVirtualScreenRaw(BITMAP * allegro_bitmap)39   Common::Bitmap *SetVirtualScreenRaw(BITMAP *allegro_bitmap)
40   {
41     gl_VirtualScreenWrapper.WrapAllegroBitmap(allegro_bitmap, true);
42     if (allegro_bitmap == NULL)
43     {
44       gl_VirtualScreenBitmap = BitmapHelper::GetScreenBitmap();
45 	}
46 	else
47     {
48       gl_VirtualScreenBitmap = &gl_VirtualScreenWrapper;
49 	}
50     return gl_VirtualScreenBitmap;
51   }
52 
GetVirtualScreen()53   Common::Bitmap *GetVirtualScreen()
54   {
55       return gl_VirtualScreenBitmap;
56   }
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
wsetrgb(int coll,int r,int g,int b,color * pall)63   void wsetrgb(int coll, int r, int g, int b, color * pall)
64   {
65     pall[coll].r = r;
66     pall[coll].g = g;
67     pall[coll].b = b;
68   }
69 
wcolrotate(unsigned char start,unsigned char finish,int dir,color * pall)70   void wcolrotate(unsigned char start, unsigned char finish, int dir, color * pall)
71   {
72     int jj;
73     if (dir == 0) {
74       // rotate left
75       color tempp = pall[start];
76 
77       for (jj = start; jj < finish; jj++)
78         pall[jj] = pall[jj + 1];
79 
80       pall[finish] = tempp;
81     }
82     else {
83       // rotate right
84       color tempp = pall[finish];
85 
86       for (jj = finish - 1; jj >= start; jj--)
87         pall[jj + 1] = pall[jj];
88 
89       pall[start] = tempp;
90     }
91   }
92 
wnewblock(Common::Bitmap * src,int x1,int y1,int x2,int y2)93   Bitmap *wnewblock(Common::Bitmap *src, int x1, int y1, int x2, int y2)
94   {
95     Bitmap *tempbitm;
96     int twid = (x2 - x1) + 1, thit = (y2 - y1) + 1;
97 
98     if (twid < 1)
99       twid = 1;
100 
101     if (thit < 1)
102       thit = 1;
103 
104     tempbitm = BitmapHelper::CreateBitmap(twid, thit);
105 
106     if (tempbitm == NULL)
107       return NULL;
108 
109     tempbitm->Blit(src, x1, y1, 0, 0, tempbitm->GetWidth(), tempbitm->GetHeight());
110     return tempbitm;
111   }
112 
wloadsprites(color * pall,char * filnam,Bitmap ** sarray,int strt,int eend)113   int wloadsprites(color * pall, char *filnam, Bitmap ** sarray, int strt, int eend)
114   {
115     int vers;
116     char buff[20];
117     int numspri = 0, vv, hh, wdd, htt;
118 
119     Stream *in = Common::AssetManager::OpenAsset(filnam);
120     if (in == NULL)
121       return -1;
122 
123     vers = in->ReadInt16();
124     in->ReadArray(&buff[0], 13, 1);
125     for (vv = 0; vv < 256; vv++)        // there's a filler byte
126       in->ReadArray(&pall[vv], 3, 1);
127 
128     if (vers > 4)
129       return -1;
130 
131     if (vers == 4)
132       numspri = in->ReadInt16();
133     else {
134       numspri = in->ReadInt16();
135       if ((numspri < 2) || (numspri > 200))
136         numspri = 200;
137     }
138 
139     for (vv = strt; vv <= eend; vv++)
140       sarray[vv] = NULL;
141 
142     for (vv = 0; vv <= numspri; vv++) {
143       int coldep = in->ReadInt16();
144 
145       if (coldep == 0) {
146         sarray[vv] = NULL;
147         if (in->EOS())
148           break;
149 
150         continue;
151       }
152 
153       if (in->EOS())
154         break;
155 
156       if (vv > eend)
157         break;
158 
159       wdd = in->ReadInt16();
160       htt = in->ReadInt16();
161       if (vv < strt) {
162           in->Seek(wdd * htt);
163         continue;
164       }
165       sarray[vv] = BitmapHelper::CreateBitmap(wdd, htt, coldep * 8);
166 
167       if (sarray[vv] == NULL) {
168         delete in;
169         return -1;
170       }
171 
172       for (hh = 0; hh < htt; hh++)
173         in->ReadArray(&sarray[vv]->GetScanLineForWriting(hh)[0], wdd * coldep, 1);
174     }
175     delete in;
176     return 0;
177   }
178 
wputblock(Common::Bitmap * ds,int xx,int yy,Bitmap * bll,int xray)179   void wputblock(Common::Bitmap *ds, int xx, int yy, Bitmap *bll, int xray)
180   {
181     if (xray)
182 	  ds->Blit(bll, xx, yy, Common::kBitmap_Transparency);
183     else
184       ds->Blit(bll, 0, 0, xx, yy, bll->GetWidth(), bll->GetHeight());
185   }
186 
187   Bitmap wputblock_wrapper; // [IKM] argh! :[
wputblock_raw(Common::Bitmap * ds,int xx,int yy,BITMAP * bll,int xray)188   void wputblock_raw(Common::Bitmap *ds, int xx, int yy, BITMAP *bll, int xray)
189   {
190 	wputblock_wrapper.WrapAllegroBitmap(bll, true);
191     if (xray)
192       ds->Blit(&wputblock_wrapper, xx, yy, Common::kBitmap_Transparency);
193     else
194       ds->Blit(&wputblock_wrapper, 0, 0, xx, yy, wputblock_wrapper.GetWidth(), wputblock_wrapper.GetHeight());
195   }
196 
197   const int col_lookups[32] = {
198     0x000000, 0x0000A0, 0x00A000, 0x00A0A0, 0xA00000,   // 4
199     0xA000A0, 0xA05000, 0xA0A0A0, 0x505050, 0x5050FF, 0x50FF50, 0x50FFFF,       // 11
200     0xFF5050, 0xFF50FF, 0xFFFF50, 0xFFFFFF, 0x000000, 0x101010, 0x202020,       // 18
201     0x303030, 0x404040, 0x505050, 0x606060, 0x707070, 0x808080, 0x909090,       // 25
202     0xA0A0A0, 0xB0B0B0, 0xC0C0C0, 0xD0D0D0, 0xE0E0E0, 0xF0F0F0
203   };
204 
205   /*
206   void wsetcolor(int nval)
207   {
208     __my_setcolor(&currentcolor, nval);
209   }
210   */
211 
212   int __wremap_keep_transparent = 1;
213 
wremap(color * pal1,Bitmap * picc,color * pal2)214   void wremap(color * pal1, Bitmap *picc, color * pal2)
215   {
216     int jj;
217     unsigned char color_mapped_table[256];
218 
219     for (jj = 0; jj < 256; jj++)
220     {
221       if ((pal1[jj].r == 0) && (pal1[jj].g == 0) && (pal1[jj].b == 0))
222       {
223         color_mapped_table[jj] = 0;
224       }
225       else
226       {
227         color_mapped_table[jj] = bestfit_color(pal2, pal1[jj].r, pal1[jj].g, pal1[jj].b);
228       }
229     }
230 
231     if (__wremap_keep_transparent > 0) {
232       // keep transparency
233       color_mapped_table[0] = 0;
234       // any other pixels which are being mapped to 0, map to 16 instead
235       for (jj = 1; jj < 256; jj++) {
236         if (color_mapped_table[jj] == 0)
237           color_mapped_table[jj] = 16;
238       }
239     }
240 
241     int pic_size = picc->GetWidth() * picc->GetHeight();
242     for (jj = 0; jj < pic_size; jj++) {
243       int xxl = jj % (picc->GetWidth()), yyl = jj / (picc->GetWidth());
244       int rr = picc->GetPixel(xxl, yyl);
245       picc->PutPixel(xxl, yyl, color_mapped_table[rr]);
246     }
247   }
248 
wremapall(color * pal1,Bitmap * picc,color * pal2)249   void wremapall(color * pal1, Bitmap *picc, color * pal2)
250   {
251     __wremap_keep_transparent--;
252     wremap(pal1, picc, pal2);
253     __wremap_keep_transparent++;
254   }
255 
256 
257 #ifdef __cplusplus
258 }
259 #endif