-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurface.h
140 lines (129 loc) · 4.18 KB
/
surface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Template, BUAS version https://www.buas.nl/games
// IGAD/BUAS(NHTV)/UU - Jacco Bikker - 2006-2020
#pragma once
namespace Tmpl8 {
constexpr int RedMask = 0xff0000;
constexpr int GreenMask = 0x00ff00;
constexpr int BlueMask = 0x0000ff;
typedef unsigned int Pixel; // unsigned int is assumed to be 32-bit, which seems a safe assumption.
inline Pixel AddBlend( Pixel a_Color1, Pixel a_Color2 )
{
const unsigned int r = (a_Color1 & RedMask) + (a_Color2 & RedMask);
const unsigned int g = (a_Color1 & GreenMask) + (a_Color2 & GreenMask);
const unsigned int b = (a_Color1 & BlueMask) + (a_Color2 & BlueMask);
const unsigned r1 = (r & RedMask) | (RedMask * (r >> 24));
const unsigned g1 = (g & GreenMask) | (GreenMask * (g >> 16));
const unsigned b1 = (b & BlueMask) | (BlueMask * (b >> 8));
return (r1 + g1 + b1);
}
// subtractive blending
inline Pixel SubBlend( Pixel a_Color1, Pixel a_Color2 )
{
int red = (a_Color1 & RedMask) - (a_Color2 & RedMask);
int green = (a_Color1 & GreenMask) - (a_Color2 & GreenMask);
int blue = (a_Color1 & BlueMask) - (a_Color2 & BlueMask);
if (red < 0) red = 0;
if (green < 0) green = 0;
if (blue < 0) blue = 0;
return static_cast<Pixel>(red + green + blue);
}
class Surface
{
enum { OWNER = 1 };
public:
// constructor / destructor
Surface( int a_Width, int a_Height, Pixel* a_Buffer, int a_Pitch );
Surface( int a_Width, int a_Height );
Surface( char* a_File );
~Surface();
// member data access
Pixel* GetBuffer() { return m_Buffer; }
void SetBuffer( Pixel* a_Buffer ) { m_Buffer = a_Buffer; }
int GetWidth() { return m_Width; }
int GetHeight() { return m_Height; }
int GetPitch() { return m_Pitch; }
void SetPitch( int a_Pitch ) { m_Pitch = a_Pitch; }
// Special operations
void InitCharset();
void SetChar( int c, char* c1, char* c2, char* c3, char* c4, char* c5 );
void Centre( char* a_String, int y1, Pixel color );
void Print( char* a_String, int x1, int y1, Pixel color );
void Clear( Pixel a_Color );
void Line( float x1, float y1, float x2, float y2, Pixel color );
void Plot( int x, int y, Pixel c );
void LoadImage( char* a_File );
void CopyTo( Surface* a_Dst, int a_X, int a_Y );
void BlendCopyTo( Surface* a_Dst, int a_X, int a_Y );
void ScaleColor( unsigned int a_Scale );
void Box( int x1, int y1, int x2, int y2, Pixel color );
void Bar( int x1, int y1, int x2, int y2, Pixel color );
void Resize( Surface* a_Orig );
private:
// Attributes
Pixel* m_Buffer{nullptr};
int m_Width{0}, m_Height{0};
int m_Pitch{0};
int m_Flags{0};
// Static attributes for the buildin font
static char s_Font[51][5][6];
static bool fontInitialized;
int s_Transl[256]{};
};
class Sprite
{
public:
// Sprite flags
enum
{
FLARE = (1<< 0),
OPFLARE = (1<< 1),
FLASH = (1<< 4),
DISABLED = (1<< 6),
GMUL = (1<< 7),
BLACKFLARE = (1<< 8),
BRIGHTEST = (1<< 9),
RFLARE = (1<<12),
GFLARE = (1<<13),
NOCLIP = (1<<14)
};
// Structors
Sprite( Surface* a_Surface, unsigned int a_NumFrames );
~Sprite();
// Methods
void Draw( Surface* a_Target, int a_X, int a_Y );
void DrawScaled( int a_X, int a_Y, int a_Width, int a_Height, Surface* a_Target );
void SetFlags( unsigned int a_Flags ) { m_Flags = a_Flags; }
void SetFrame( unsigned int a_Index ) { m_CurrentFrame = a_Index; }
unsigned int GetFlags() const { return m_Flags; }
int GetWidth() { return m_Width; }
int GetHeight() { return m_Height; }
Pixel* GetBuffer() { return m_Surface->GetBuffer(); }
unsigned int Frames() { return m_NumFrames; }
Surface* GetSurface() { return m_Surface; }
private:
// Methods
void InitializeStartData();
// Attributes
int m_Width, m_Height, m_Pitch;
unsigned int m_NumFrames;
unsigned int m_CurrentFrame;
unsigned int m_Flags;
unsigned int** m_Start;
Surface* m_Surface;
};
class Font
{
public:
Font();
Font( char* a_File, char* a_Chars );
~Font();
void Print( Surface* a_Target, char* a_Text, int a_X, int a_Y, bool clip = false );
void Centre( Surface* a_Target, char* a_Text, int a_Y );
int Width( char* a_Text );
int Height() { return m_Surface->GetHeight(); }
void YClip( int y1, int y2 ) { m_CY1 = y1; m_CY2 = y2; }
private:
Surface* m_Surface;
int* m_Offset, *m_Width, *m_Trans, m_Height, m_CY1, m_CY2;
};
}; // namespace Tmpl8