1 /*
2  *
3  *  Iter Vehemens ad Necem (IVAN)
4  *  Copyright (C) Timo Kiviluoto
5  *  Released under the GNU General
6  *  Public License
7  *
8  *  See LICENSING which should be included
9  *  along with this file for more details
10  *
11  */
12 
13 #ifndef __BITMAP_H__
14 #define __BITMAP_H__
15 
16 #include "v2.h"
17 #include "SDL.h"
18 
19 class bitmap;
20 class rawbitmap;
21 class outputfile;
22 class inputfile;
23 class festring;
24 
25 typedef void (*bitmapeditor)(bitmap*, truth);
26 
27 struct blitdata
28 {
29   bitmap* Bitmap;
30   v2 Src;
31   v2 Dest;
32   v2 Border;
33   union
34   {
35     col24 Luminance;
36     int Flags, Stretch;
37   };
38   col16 MaskColor;
39   ulong CustomData;
40 };
41 #define DEFAULT_BLITDATA {NULL,{0,0},{0,0},{0,0}, {0}, TRANSPARENT_COLOR,0} //easy initializer TODO should be TRANSPARENT_COLOR? TODO update everywere with this default and just apply there differences to shrink the code?
42 
43 class bitmap
44 {
45  public:
46   friend class cachedfont;
47   bitmap(cfestring&);
48   bitmap(cbitmap*, int = 0, truth = true);
49   bitmap(v2);
50   bitmap(v2, col16);
51   ~bitmap();
52   void Save(outputfile&) const;
53   void Load(inputfile&);
54   void Save(cfestring&) const;
PutPixel(int X,int Y,col16 Color)55   void PutPixel(int X, int Y, col16 Color) { Image[Y][X] = Color; }
PutPixel(v2 Pos,col16 Color)56   void PutPixel(v2 Pos, col16 Color) { Image[Pos.Y][Pos.X] = Color; }
57   void PowerPutPixel(int, int, col16, alpha, priority);
GetPixel(int X,int Y)58   col16 GetPixel(int X, int Y) const { return Image[Y][X]; }
GetPixel(v2 Pos)59   col16 GetPixel(v2 Pos) const { return Image[Pos.Y][Pos.X]; }
60 
61   void Fill(int, int, int, int, col16);
62   void Fill(v2, int, int, col16);
63   void Fill(int, int, v2, col16);
64   void Fill(v2, v2, col16);
65 
66   void ClearToColor(col16);
67   void NormalBlit(cblitdata&) const;
68   void NormalBlit(bitmap*, int = 0) const;
69   void FastBlit(bitmap*) const;
70   void FastBlit(bitmap*, v2) const;
71 
72   void LuminanceBlit(cblitdata&) const;
73   void NormalMaskedBlit(cblitdata&) const;
74   void LuminanceMaskedBlit(cblitdata&) const;
75   void SimpleAlphaBlit(bitmap*, alpha, col16 = TRANSPARENT_COLOR) const;
76   void AlphaMaskedBlit(cblitdata&) const;
77   void AlphaLuminanceBlit(cblitdata&) const;
78 
79   void DrawLine(int, int, int, int, col16, truth = false);
80   void DrawLine(v2, int, int, col16, truth = false);
81   void DrawLine(int, int, v2, col16, truth = false);
82   void DrawLine(v2, v2, col16, truth = false);
83 
84   void DrawVerticalLine(int, int, int, col16, truth = false);
85   void DrawHorizontalLine(int, int, int, col16, truth = false);
86 
87   void StretchBlit(cblitdata&) const;
88   void StretchBlitXbrz(cblitdata&,bool) const;
89   SDL_Surface* CopyToSurface(v2 v2TopLeft, v2 v2Size, col16 MaskColor = TRANSPARENT_COLOR, SDL_Surface* srf = NULL) const;
90 
91   static void ResetBlitdataRotation(blitdata& B);
92   static void ConfigureBlitdataRotation(blitdata& B,int i);
93 
94   void DrawRectangle(int, int, int, int, col16, truth = false);
95   void DrawRectangle(v2, int, int, col16, truth = false);
96   void DrawRectangle(int, int, v2, col16, truth = false);
97   void DrawRectangle(v2, v2, col16, truth = false);
98 
99   void BlitAndCopyAlpha(bitmap*, int = 0) const;
100   void MaskedPriorityBlit(cblitdata&) const;
101   void AlphaPriorityBlit(cblitdata&) const;
102   void FastBlitAndCopyAlpha(bitmap*) const;
GetSize()103   v2 GetSize() const { return Size; }
104   void DrawPolygon(int, int, int, int, col16, truth = true, truth = false, double = 0);
105   void CreateAlphaMap(alpha);
106   truth Fade(long&, packalpha&, int);
SetAlpha(int X,int Y,alpha Alpha)107   void SetAlpha(int X, int Y, alpha Alpha) { AlphaMap[Y][X] = Alpha; }
SetAlpha(v2 Pos,alpha Alpha)108   void SetAlpha(v2 Pos, alpha Alpha) { AlphaMap[Pos.Y][Pos.X] = Alpha; }
GetAlpha(int X,int Y)109   alpha GetAlpha(int X, int Y) const { return AlphaMap[Y][X]; }
GetAlpha(v2 Pos)110   alpha GetAlpha(v2 Pos) const { return AlphaMap[Pos.Y][Pos.X]; }
111   void Outline(col16, alpha, priority);
112   void FadeToScreen(bitmapeditor = 0);
113   void CreateFlames(rawbitmap*, v2, ulong, int);
IsValidPos(v2 What)114   truth IsValidPos(v2 What) const { return What.X >= 0 && What.Y >= 0 && What.X < Size.X && What.Y < Size.Y; }
IsValidPos(int X,int Y)115   truth IsValidPos(int X, int Y) const { return X >= 0 && Y >= 0 && X < Size.X && Y < Size.Y; }
116   void CreateSparkle(v2, int);
117   void CreateFlies(ulong, int, int);
118   void CreateLightning(ulong, col16);
119   truth CreateLightning(v2, v2, int, col16);
GetImage()120   packcol16** GetImage() const { return Image; }
GetAlphaMap()121   packalpha** GetAlphaMap() const { return AlphaMap; }
122   static truth PixelVectorHandler(long, long);
123   void FillAlpha(alpha);
124   void InitPriorityMap(priority);
125   void FillPriority(priority);
126   void SafeSetPriority(int, int, priority);
SafeSetPriority(v2 Pos,priority What)127   void SafeSetPriority(v2 Pos, priority What) { SafeSetPriority(Pos.X, Pos.Y, What); }
128   void SafeUpdateRandMap(v2, truth);
129   void UpdateRandMap(long, truth);
130   void InitRandMap();
131   v2 RandomizePixel() const;
132   void AlphaPutPixel(int, int, col16, col24, alpha);
AlphaPutPixel(v2 Pos,col16 Color,col24 Luminance,alpha Alpha)133   void AlphaPutPixel(v2 Pos, col16 Color, col24 Luminance, alpha Alpha) { AlphaPutPixel(Pos.X, Pos.Y, Color, Luminance, Alpha); }
134   void CalculateRandMap();
135   alpha CalculateAlphaAverage() const;
ActivateFastFlag()136   void ActivateFastFlag() { FastFlag = 1; }
DeactivateFastFlag()137   void DeactivateFastFlag() { FastFlag = 0; }
138   void Wobble(int, int, truth);
139   void MoveLineVertically(int, int);
140   void MoveLineHorizontally(int, int);
141   void InterLace();
142 
143   truth HasColor(col16 findColor);
144   void ReplaceColor(col16 findColor,col16 replaceWith);
145   void CopyLineFrom(int iYDest, bitmap* bmpFrom, int iYFrom, int iSize, bool bFailSafe=false);
146  protected:
147   v2 Size;
148   ulong XSizeTimesYSize : 31;
149   ulong FastFlag : 1;
150   packcol16** Image;
151   packalpha** AlphaMap;
152   packpriority** PriorityMap;
153   truth* RandMap;
154   SDL_Surface* img;
155   SDL_Surface* imgStretched;
156 };
157 
SafeUpdateRandMap(v2 Pos,truth What)158 inline void bitmap::SafeUpdateRandMap(v2 Pos, truth What)
159 {
160   if(RandMap)
161     UpdateRandMap(Pos.Y * Size.X + Pos.X, What);
162 }
163 
SafeSetPriority(int x,int y,priority What)164 inline void bitmap::SafeSetPriority(int x, int y, priority What)
165 {
166   if(PriorityMap)
167     PriorityMap[y][x] = What;
168 }
169 
FastBlit(bitmap * Bitmap)170 inline void bitmap::FastBlit(bitmap* Bitmap) const
171 {
172   memcpy(Bitmap->Image[0], Image[0], XSizeTimesYSize * sizeof(packcol16));
173 }
174 
FastBlit(bitmap * Bitmap,v2 Pos)175 inline void bitmap::FastBlit(bitmap* Bitmap, v2 Pos) const
176 {
177   packcol16** SrcImage = Image;
178   packcol16** DestImage = Bitmap->Image;
179   cint Bytes = Size.X * sizeof(packcol16);
180   cint Height = Size.Y;
181 
182   for(int y = 0; y < Height; ++y)
183     memcpy(&DestImage[Pos.Y + y][Pos.X], SrcImage[y], Bytes);
184 }
185 
NormalBlit(bitmap * Bitmap,int Flags)186 inline void bitmap::NormalBlit(bitmap* Bitmap, int Flags) const
187 {
188   blitdata B = { Bitmap,
189                  { 0, 0 },
190                  { 0, 0 },
191                  { Size.X, Size.Y },
192                  { static_cast<col24>(Flags) }, // stupid union initialization rules...
193                  0,
194                  0 };
195   NormalBlit(B);
196 }
197 
198 outputfile& operator<<(outputfile&, cbitmap*);
199 inputfile& operator>>(inputfile&, bitmap*&);
200 
201 class cachedfont : public bitmap
202 {
203  public:
204   cachedfont(v2);
205   cachedfont(v2, col16);
~cachedfont()206   ~cachedfont() { delete [] MaskMap; }
207   void PrintCharacter(cblitdata) const;
208   void CreateMaskMap();
209  private:
210   packcol16** MaskMap;
211 };
212 
213 #endif
214