OSDN Git Service

バックバッファを作ってみた。
[shooting3/shootinggame.git] / ShootingGame / BasicSprites.h
1 //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
2 //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
3 //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
4 //// PARTICULAR PURPOSE.
5 ////
6 //// Copyright (c) Microsoft Corporation. All rights reserved
7
8 #pragma once
9
10 #include "BasicMath.h"
11 #include <vector>
12 #include <map>
13 #include <memory>
14
15 namespace BasicSprites
16 {
17     namespace Parameters
18     {
19         // The index buffer format for feature-level 9.1 devices may only be 16 bits.
20         // With 4 vertices per sprite, this allows a maximum of (1 << 16) / 4 sprites.
21         static unsigned int MaximumCapacityCompatible = (1 << 16) / 4;
22     }
23
24     enum class PositionUnits
25     {
26         DIPs,         // Interpret position as device-independent pixel values.
27         Pixels,       // Interpret position as pixel values.
28         Normalized,   // Interpret position as a fraction of the render target dimensions.
29         UniformWidth, // Interpret position as a fraction of the render target width.
30         UniformHeight // Interpret position as a fraction of the render target height.
31     };
32
33     enum class SizeUnits
34     {
35         DIPs,      // Interpret size as device-independent pixel values.
36         Pixels,    // Interpret size as pixel values.
37         Normalized // Interpret size as a multiplier of the pixel size of the sprite.
38     };
39
40     enum class BlendMode
41     {
42         Alpha,   // Use alpha blending (out = old * (1 - new.a) + new * new.a).
43         Additive // Use additive blending (out = old + new * new.a).
44     };
45
46     enum class RenderTechnique
47     {
48         Replication,
49         Instancing,
50         GeometryShader
51     };
52
53     struct ReplicationVertex
54     {
55         float2 pos;
56         float2 tex;
57         unsigned int color;
58     };
59
60     struct InstancingVertex
61     {
62         float2 pos;
63         float2 tex;
64     };
65
66     struct InstanceData
67     {
68         float2 origin;
69         float2 offset;
70         float rotation;
71         unsigned int color;
72     };
73
74     struct TextureMapElement
75     {
76         Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv;
77         float2 size;
78     };
79
80     struct SpriteRunInfo
81     {
82         ID3D11ShaderResourceView* textureView;
83         ID3D11BlendState1* blendState;
84         unsigned int numSprites;
85     };
86
87     ref class SpriteBatch
88     {
89     public:
90         SpriteBatch();
91         void Initialize(
92             _In_ ID3D11Device1* d3dDevice,
93             _In_ int capacity
94             );
95         void AddTexture(
96             _In_ ID3D11Texture2D* texture
97             );
98         void RemoveTexture(
99             _In_ ID3D11Texture2D* texture
100             );
101         void Begin();
102         void End();
103         void Draw(
104             _In_ ID3D11Texture2D* texture,
105             _In_ float2 position,
106             _In_ PositionUnits positionUnits
107             );
108         void Draw(
109             _In_ ID3D11Texture2D* texture,
110             _In_ float2 position,
111             _In_ PositionUnits positionUnits,
112             _In_ float2 size,
113             _In_ SizeUnits sizeUnits
114             );
115         void Draw(
116             _In_ ID3D11Texture2D* texture,
117             _In_ float2 position,
118             _In_ PositionUnits positionUnits,
119             _In_ float2 size,
120             _In_ SizeUnits sizeUnits,
121             _In_ float4 color
122             );
123         void Draw(
124             _In_ ID3D11Texture2D* texture,
125             _In_ float2 position,
126             _In_ PositionUnits positionUnits,
127             _In_ float2 size,
128             _In_ SizeUnits sizeUnits,
129             _In_ float4 color,
130             _In_ float rotation
131             );
132         void Draw(
133             _In_ ID3D11Texture2D* texture,
134             _In_ float2 position,
135             _In_ PositionUnits positionUnits,
136             _In_ float2 size,
137             _In_ SizeUnits sizeUnits,
138             _In_ float4 color,
139             _In_ float rotation,
140             _In_ BlendMode blendMode
141             );
142
143     private:
144         unsigned int MakeUnorm(float4 color);
145         float2 StandardOrigin(float2 position, PositionUnits positionUnits, float2 renderTargetSize, float dpi);
146         float2 StandardOffset(float2 size, SizeUnits sizeUnits, float2 spriteSize, float dpi);
147
148         Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice;
149         Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_d3dContext;
150         Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
151         Microsoft::WRL::ComPtr<ID3D11Buffer> m_instanceDataBuffer;
152         Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
153         Microsoft::WRL::ComPtr<ID3D11BlendState1> m_blendStateAlpha;
154         Microsoft::WRL::ComPtr<ID3D11BlendState1> m_blendStateAdditive;
155         Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
156         Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
157         Microsoft::WRL::ComPtr<ID3D11GeometryShader> m_geometryShader;
158         Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
159         Microsoft::WRL::ComPtr<ID3D11SamplerState> m_linearSampler;
160         Microsoft::WRL::ComPtr<ID3D11Buffer> m_renderTargetInfoCbuffer;
161
162         std::unique_ptr<ReplicationVertex[]> m_vertexData;
163         std::unique_ptr<InstanceData[]> m_instanceData;
164         std::map<ID3D11Texture2D*, TextureMapElement> m_textureMap;
165         std::vector<SpriteRunInfo> m_spriteRuns;
166
167         RenderTechnique m_technique;
168         ID3D11ShaderResourceView* m_currentTextureView;
169         ID3D11BlendState1* m_currentBlendState;
170         float2 m_renderTargetSize;
171         int m_capacity;
172         int m_spritesInRun;
173         int m_numSpritesDrawn;
174         float m_dpi;
175     };
176 }