Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 chris 1
#ifndef __D3DINTERFACE_H__
2
#define __D3DINTERFACE_H__
3
 
4
#include "Common.h"
5
#include "MemoryImage.h"
6
#include "SexyMatrix.h"
7
#include <d3d.h>
8
 
9
 
10
namespace Sexy
11
{
12
 
13
class DDInterface;
14
class SexyMatrix3;
15
class TriVertex;
16
 
17
///////////////////////////////////////////////////////////////////////////////
18
///////////////////////////////////////////////////////////////////////////////
19
// The following flags apply to to the mD3DFlags member of MemoryImage
20
enum D3DImageFlags
21
{
22
        D3DImageFlag_MinimizeNumSubdivisions    =                       0x0001,         // subdivide image into fewest possible textures (may use more memory)
23
        D3DImageFlag_Use64By64Subdivisions              =                       0x0002,         // good to use with image strips so the entire texture isn't pulled in when drawing just a piece
24
        D3DImageFlag_UseA4R4G4B4                                =                       0x0004,         // images with not too many color gradients work well in this format
25
        D3DImageFlag_UseA8R8G8B8                                =                       0x0008          // non-alpha images will be stored as R5G6B5 by default so use this option if you want a 32-bit non-alpha image
26
};
27
 
28
///////////////////////////////////////////////////////////////////////////////
29
///////////////////////////////////////////////////////////////////////////////
30
struct TextureDataPiece
31
{
32
        LPDIRECTDRAWSURFACE7 mTexture;
33
        int mWidth,mHeight;
34
};
35
 
36
///////////////////////////////////////////////////////////////////////////////
37
///////////////////////////////////////////////////////////////////////////////
38
enum PixelFormat
39
{
40
        PixelFormat_Unknown                             =                       0x0000,
41
        PixelFormat_A8R8G8B8                    =                       0x0001,
42
        PixelFormat_A4R4G4B4                    =                       0x0002,
43
        PixelFormat_R5G6B5                              =                       0x0004,
44
        PixelFormat_Palette8                    =                       0x0008
45
};
46
 
47
///////////////////////////////////////////////////////////////////////////////
48
///////////////////////////////////////////////////////////////////////////////
49
struct TextureData
50
{
51
public:
52
        typedef std::vector<TextureDataPiece> TextureVector;
53
 
54
        TextureVector mTextures;
55
        LPDIRECTDRAWPALETTE mPalette;
56
 
57
        int mWidth,mHeight;
58
        int mTexVecWidth, mTexVecHeight;
59
        int mTexPieceWidth, mTexPieceHeight;
60
        int mBitsChangedCount;
61
        int mTexMemSize;
62
        float mMaxTotalU, mMaxTotalV;
63
        PixelFormat mPixelFormat;
64
        DWORD mImageFlags;
65
 
66
        TextureData();
67
        ~TextureData();
68
 
69
        void ReleaseTextures();
70
 
71
        void CreateTextureDimensions(MemoryImage *theImage);
72
        void CreateTextures(MemoryImage *theImage, LPDIRECT3DDEVICE7 theDevice, LPDIRECTDRAW7 theDraw);
73
        void CheckCreateTextures(MemoryImage *theImage, LPDIRECT3DDEVICE7 theDevice, LPDIRECTDRAW7 theDraw);
74
        LPDIRECTDRAWSURFACE7 GetTexture(int x, int y, int &width, int &height, float &u1, float &v1, float &u2, float &v2);
75
        LPDIRECTDRAWSURFACE7 GetTextureF(float x, float y, float &width, float &height, float &u1, float &v1, float &u2, float &v2);
76
 
77
        void Blt(LPDIRECT3DDEVICE7 theDevice, float theX, float theY, const Rect& theSrcRect, const Color& theColor);
78
        void BltTransformed(LPDIRECT3DDEVICE7 theDevice, const SexyMatrix3 &theTrans, const Rect& theSrcRect, const Color& theColor, const Rect *theClipRect = NULL, float theX = 0, float theY = 0, bool center = false);     
79
        void BltTriangles(LPDIRECT3DDEVICE7 theDevice, const TriVertex theVertices[][3], int theNumTriangles, DWORD theColor, float tx = 0, float ty = 0);
80
};
81
 
82
///////////////////////////////////////////////////////////////////////////////
83
///////////////////////////////////////////////////////////////////////////////
84
class D3DInterface
85
{
86
public:
87
        HWND                                    mHWnd;
88
        int                                             mWidth;
89
        int                                             mHeight;
90
 
91
        LPDIRECTDRAW7                   mDD;
92
        LPDIRECTDRAWSURFACE7    mDDSDrawSurface;
93
        LPDIRECTDRAWSURFACE7    mZBuffer;
94
        LPDIRECT3D7                             mD3D;
95
        LPDIRECT3DDEVICE7               mD3DDevice;
96
        D3DVIEWPORT7                    mD3DViewport;
97
 
98
        bool                                    mSceneBegun;
99
        bool                                    mIsWindowed;
100
 
101
        typedef std::set<MemoryImage*> ImageSet;
102
        ImageSet mImageSet;
103
 
104
        typedef std::list<SexyMatrix3> TransformStack;
105
        TransformStack mTransformStack;
106
 
107
        static std::string mErrorString;
108
 
109
protected:
110
        void                                    UpdateViewport();
111
        bool                                    InitD3D();     
112
        void                                    SetupDrawMode(int theDrawMode, const Color &theColor, Image *theImage);
113
        static HRESULT CALLBACK PixelFormatsCallback(LPDDPIXELFORMAT theFormat, LPVOID lpContext);
114
 
115
public:
116
        D3DInterface();
117
        virtual ~D3DInterface();
118
 
119
        void                                    Cleanup();
120
        void                                    PushTransform(const SexyMatrix3 &theTransform, bool concatenate = true);
121
        void                                    PopTransform();
122
 
123
        bool                                    PreDraw();
124
        void                                    Flush();
125
        void                                    RemoveMemoryImage(MemoryImage *theImage);
126
 
127
        bool                                    CreateImageTexture(MemoryImage *theImage);
128
        bool                                    RecoverBits(MemoryImage* theImage);
129
        void                                    SetCurTexture(MemoryImage *theImage);
130
        void                                    Blt(Image* theImage, float theX, float theY, const Rect& theSrcRect, const Color& theColor, int theDrawMode, bool linearFilter = false);
131
        void                                    BltClipF(Image* theImage, float theX, float theY, const Rect& theSrcRect, const Rect *theClipRect, const Color& theColor, int theDrawMode);
132
        void                                    BltMirror(Image* theImage, float theX, float theY, const Rect& theSrcRect, const Color& theColor, int theDrawMode, bool linearFilter = false);
133
        void                                    StretchBlt(Image* theImage,  const Rect& theDestRect, const Rect& theSrcRect, const Rect* theClipRect, const Color &theColor, int theDrawMode, bool fastStretch, bool mirror = false);
134
        void                                    BltRotated(Image* theImage, float theX, float theY, const Rect* theClipRect, const Color& theColor, int theDrawMode, double theRot, float theRotCenterX, float theRotCenterY, const Rect& theSrcRect);
135
        void                                    BltTransformed(Image* theImage, const Rect* theClipRect, const Color& theColor, int theDrawMode, const Rect &theSrcRect, const SexyMatrix3 &theTransform, bool linearFilter, float theX = 0, float theY = 0, bool center = false);
136
        void                                    DrawLine(double theStartX, double theStartY, double theEndX, double theEndY, const Color& theColor, int theDrawMode);
137
        void                                    FillRect(const Rect& theRect, const Color& theColor, int theDrawMode);
138
        void                                    DrawTriangle(const TriVertex &p1, const TriVertex &p2, const TriVertex &p3, const Color &theColor, int theDrawMode);
139
        void                                    DrawTriangleTex(const TriVertex &p1, const TriVertex &p2, const TriVertex &p3, const Color &theColor, int theDrawMode, Image *theTexture, bool blend = true);
140
        void                                    DrawTrianglesTex(const TriVertex theVertices[][3], int theNumTriangles, const Color &theColor, int theDrawMode, Image *theTexture, float tx = 0, float ty = 0, bool blend = true);
141
        void                                    DrawTrianglesTexStrip(const TriVertex theVertices[], int theNumTriangles, const Color &theColor, int theDrawMode, Image *theTexture, float tx = 0, float ty = 0, bool blend = true);
142
        void                                    FillPoly(const Point theVertices[], int theNumVertices, const Rect *theClipRect, const Color &theColor, int theDrawMode, int tx, int ty);
143
 
144
        bool                                    InitFromDDInterface(DDInterface *theInterface);
145
 
146
        static void                             MakeDDPixelFormat(PixelFormat theFormatType, DDPIXELFORMAT* theFormat);
147
        static PixelFormat              GetDDPixelFormat(LPDDPIXELFORMAT theFormat);
148
        static bool                             CheckDXError(HRESULT theError, const char *theMsg="");
149
 
150
};
151
 
152
}
153
 
154
#endif //__D3DINTERFACE_H__