1 /*
2  *  ppui/Graphics.h
3  *
4  *  Copyright 2009 Peter Barth
5  *
6  *  This file is part of Milkytracker.
7  *
8  *  Milkytracker is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  Milkytracker is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with Milkytracker.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 /////////////////////////////////////////////////////////////////
24 //
25 //	Graphics class
26 //
27 /////////////////////////////////////////////////////////////////
28 #ifndef __GRAPHICS_H__
29 #define __GRAPHICS_H__
30 
31 #include "GraphicsAbstract.h"
32 
33 class PPGraphicsFrameBuffer : public PPGraphicsAbstract
34 {
35 protected:
36 	pp_int32 pitch;
37 	pp_uint8* buffer;
38 
39 public:
PPGraphicsFrameBuffer(pp_int32 w,pp_int32 h,pp_int32 p,void * buff)40 	PPGraphicsFrameBuffer(pp_int32 w, pp_int32 h, pp_int32 p, void* buff) :
41 		PPGraphicsAbstract(w, h),
42 		pitch(p), buffer((pp_uint8*)buff)
43 	{
44 	}
45 
setBufferProperties(pp_int32 p,void * buff)46 	void setBufferProperties(pp_int32 p, void* buff)
47 	{
48 		pitch = p;
49 		buffer = (pp_uint8*)buff;
50 	}
51 };
52 
53 #define __EMPTY__
54 
55 #define SUBCLASS_GRAPHICS(baseclass, prologue, name, epilogue) \
56 class name : public baseclass \
57 { \
58 private: \
59 	prologue \
60 public: \
61 	name(pp_int32 w, pp_int32 h, pp_int32 p, void* buff); \
62 	virtual void setPixel(pp_int32 x, pp_int32 y); \
63 	virtual void setPixel(pp_int32 x, pp_int32 y, const PPColor& color); \
64 	virtual void fill(PPRect r); \
65 	virtual void fill(); \
66 	virtual void drawHLine(pp_int32 x1, pp_int32 x2, pp_int32 y); \
67 	virtual void drawVLine(pp_int32 y1, pp_int32 y2, pp_int32 x); \
68 	virtual void drawLine(pp_int32 x1, pp_int32 y1, pp_int32 x2, pp_int32 y2); \
69 	virtual void drawAntialiasedLine(pp_int32 x1, pp_int32 y1, pp_int32 x2, pp_int32 y2); \
70 	virtual void blit(const pp_uint8* src, const PPPoint& p, const PPSize& size, pp_uint32 pitch, pp_uint32 bpp, pp_int32 intensity = 256); \
71 	virtual void drawChar(pp_uint8 chr, pp_int32 x, pp_int32 y, bool underlined = false); \
72 	virtual void drawString(const char* str, pp_int32 x, pp_int32 y, bool underlined = false); \
73 	virtual void drawStringVertical(const char* str, pp_int32 x, pp_int32 y, bool underlined = false); \
74 	epilogue \
75 }; \
76 
77 // trans is level of transparency - 0 = opaque, 255 = transparent
set_pixel_transp(PPGraphicsAbstract * g,pp_int32 x,pp_int32 y,PPColor * col,unsigned char trans)78 static inline void set_pixel_transp(PPGraphicsAbstract* g, pp_int32 x, pp_int32 y, PPColor* col, unsigned char trans)
79 {
80 	PPColor newColor;
81 	newColor.r = (col->r * (unsigned int)(255-trans))>>8;
82 	newColor.g = (col->g * (unsigned int)(255-trans))>>8;
83 	newColor.b = (col->b * (unsigned int)(255-trans))>>8;
84 	g->setPixel(x, y, newColor);
85 }
86 
87 // used for win32
88 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, __EMPTY__, PPGraphics_BGR24, __EMPTY__)
89 // OSX (carbon, 32 bits with alpha channel)
90 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, __EMPTY__, PPGraphics_ARGB32, __EMPTY__)
91 // used for wince (GAPI)
92 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, __EMPTY__, PPGraphics_16BIT, __EMPTY__)
93 // OSX (carbon, 16 bit color, one unused bit)
94 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, __EMPTY__, PPGraphics_15BIT, __EMPTY__)
95 // currently unused, big endian compatible version of PPGraphics_BGR24
96 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, __EMPTY__, PPGraphics_BGR24_SLOW, __EMPTY__)
97 
98 #define PROLOGUE \
99 	pp_uint32 bitPosR, bitPosG, bitPosB;
100 
101 #define EPILOGUE \
102 	void setComponentBitpositions(pp_uint32 bitPosR, pp_uint32 bitPosG, pp_uint32 bitPosB) \
103 	{ \
104 		this->bitPosR = bitPosR; this->bitPosG = bitPosG; this->bitPosB = bitPosB; \
105 	}
106 
107 // used in the SDL port, arbitrary bit positions but a little bit slower than the rest
108 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, PROLOGUE, PPGraphics_24bpp_generic, EPILOGUE)
109 SUBCLASS_GRAPHICS(PPGraphicsFrameBuffer, PROLOGUE, PPGraphics_32bpp_generic, EPILOGUE)
110 
111 #undef EPILOGUE
112 #undef PROLOGUE
113 
114 #undef SUBCLASS_GRAPHICSABSTRACT
115 
116 #undef __EMPTY__
117 
118 #endif
119