-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIrisViewport.cpp
More file actions
237 lines (189 loc) · 6.15 KB
/
IrisViewport.cpp
File metadata and controls
237 lines (189 loc) · 6.15 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "IrisViewport.h"
typedef union
{
ARGB Color;
struct
{
BYTE Blue;
BYTE Green;
BYTE Red;
BYTE Alpha;
};
}ARGBQuad, *PARGBQuad;
IrisViewport::IrisViewport(float x, float y, float width, float height, IDirect3DDevice9* tDevice){
this->rect = new IrisRect(x, y, width, height);
this->Device = tDevice;
ModuleIrisGraphics::addViewport(this);
ModuleIrisGraphics::sortViewports();
this->visible = true;
this->z = 0.0f;
this->ox = this->oy = 0.0f;
this->tone = NULL;
this->color = NULL;
MakeBuffer();
MakeTexture();
this->renderSurface = NULL;
}
IrisViewport::IrisViewport(IrisRect *rect, IDirect3DDevice9* tDevice){
this->rect = rect;
this->Device = tDevice;
ModuleIrisGraphics::addViewport(this);
ModuleIrisGraphics::sortViewports();
this->visible = true;
this->z = 0.0f;
this->ox = this->oy = 0.0f;
this->tone = NULL;
this->color = NULL;
MakeBuffer();
MakeTexture();
this->renderSurface = NULL;
}
list<IrisSprite*> IrisViewport::GetSprites(){
return this->sprites;
}
bool IrisViewport::compareSpriteWithZ(IrisSprite *s1, IrisSprite *s2){
if(s1->z < s2->z)
return true;
return false;
}
void IrisViewport::sortViewports(){
this->sprites.sort(IrisViewport::compareSpriteWithZ);
}
void IrisViewport::MakeBuffer(){
HRESULT r = this->Device->CreateVertexBuffer(6 * sizeof(Iris2DVertex), D3DUSAGE_WRITEONLY, Iris2DVertex::FVF, D3DPOOL_MANAGED, &(this->renderVertex), 0);
if (FAILED(r))
{
MessageBox(NULL, L"Create buffer failed!", L"Error", 0);
}
Iris2DVertex* v;
this->renderVertex->Lock(0, 0, (void**)&v, 0);
v[0] = Iris2DVertex(0.0f, (float)this->GetHeight(), 0.0f, 0.0f, 1.0f);
v[1] = Iris2DVertex(0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
v[2] = Iris2DVertex((float)this->GetWidth(), 0.0f, 0.0f, 1.0f, 0.0f);
v[3] = Iris2DVertex(0.0f, (float)this->GetHeight(), 0.0f, 0.0f, 1.0f);
v[4] = Iris2DVertex((float)this->GetWidth(), 0.0f, 0.0f, 1.0f, 0.0f);
v[5] = Iris2DVertex((float)this->GetWidth(),(float)this->GetHeight(), 0.0f, 1.0f, 1.0f);
this->renderVertex->Unlock();
}
void IrisViewport::MakeTexture(){
this->Device->CreateTexture(this->GetWidth(), this->GetHeight(), 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, &(this->renderTexture), NULL) ;
LPDIRECT3DSURFACE9 pRenderSurface;
LPDIRECT3DTEXTURE9 text;
LPDIRECT3DSURFACE9 surf;
D3DLOCKED_RECT lockbits;
this->renderTexture->GetSurfaceLevel(0, &pRenderSurface);
this->Device->CreateTexture(this->GetWidth(),this->GetHeight(), 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &text, NULL);
text->GetSurfaceLevel(0,&surf);
this->Device->GetRenderTargetData(pRenderSurface, surf);
surf->LockRect(&lockbits, NULL, D3DLOCK_READONLY);
pRenderSurface->UnlockRect();
PARGBQuad p = (PARGBQuad)(lockbits.pBits);
PARGBQuad c = 0;
for(unsigned int x = 0; x < this->GetWidth(); x++){
for(unsigned int y = 0; y < this->GetHeight(); y++){
c = p + x + y * lockbits.Pitch / sizeof(ARGBQuad);
c->Alpha = 0;
c->Blue = 0;// * opacity / 255 + c2->Blue * (1 - opacity / 255);
c->Red = 0;// * opacity / 255 + c2->Red * (1 - opacity / 255);
c->Green = 0; //* opacity / 255 + c2->Green * (1 - opacity / 255);
}
}
text->Release();
surf->Release();
}
void IrisViewport::Dispose(){
list<IrisSprite*>::iterator it;
for(it = this->sprites.begin(); it != this->sprites.end(); it++){
//(*it)->Dispose();
(*it)->SetViewport(ModuleIrisGraphics::getGViewport());
}
//this->sprites.clear();
ModuleIrisGraphics::deleteViewport(this);
d3d::Release<IDirect3DVertexBuffer9*>(this->renderVertex);
d3d::Release<IDirect3DTexture9*>(this->renderTexture);
}
void IrisViewport::AutoDispose(){
list<IrisSprite*>::iterator it;
for(it = this->sprites.begin(); it != this->sprites.end(); it++){
//(*it)->Dispose();
(*it)->AutoSetViewport(ModuleIrisGraphics::getGViewport());
}
//this->sprites.clear();
d3d::Release<IDirect3DVertexBuffer9*>(this->renderVertex);
d3d::Release<IDirect3DTexture9*>(this->renderTexture);
}
bool IrisViewport::Disposed(){
return this->sprites.empty();
}
void IrisViewport::Flash(IrisColor *color, int duration){
}
void IrisViewport::Update(){
list<IrisSprite*>::iterator it;
for(it = this->sprites.begin(); it != this->sprites.end(); it++){
(*it)->Update();
}
}
void IrisViewport::addSprite(IrisSprite* sprite){
this->sprites.push_back(sprite);
}
void IrisViewport::deleteSprite(IrisSprite* sprite){
this->sprites.remove(sprite);
}
void IrisViewport::Draw(){
if(!this->visible)
return;
RenderSurface();
RenderToBackbuffer();
}
void IrisViewport::RenderSurface(){
IDirect3DSurface9 *oldSurface;
//this->Device->SetViewport(&(this->d3dViewport));
//Make Link
this->renderTexture->GetSurfaceLevel(0, &(this->renderSurface));
//Save old surface
this->Device->GetRenderTarget(0, &oldSurface);
//Set new render target
this->Device->SetRenderTarget(0, this->renderSurface);
D3DXMATRIX proj;
D3DXMatrixOrthoOffCenterLH(&proj, 0.0f, (float)this->GetWidth(), (float)this->GetHeight(), 0.0f, 0, 9999.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
Device->BeginScene();
list<IrisSprite*>::iterator it;
for(it = this->sprites.begin(); it != this->sprites.end(); it++){
(*it)->Draw();
}
Device->EndScene();
this->Device->SetRenderTarget(0, oldSurface);
}
void IrisViewport::RenderToBackbuffer(){
D3DXMATRIX W;
D3DXMatrixIdentity(&W);
Device->SetTransform(D3DTS_WORLD, &W);
D3DXMATRIX translationMatrix;
D3DXMatrixIdentity(&translationMatrix);
D3DXMatrixTranslation(&translationMatrix, this->GetX(), this->GetY(), 0.0f);
Device->SetTransform(D3DTS_WORLD, &translationMatrix);
Device->SetTexture(0, this->renderTexture);
Device->SetFVF(Iris2DVertex::FVF);
Device->SetStreamSource(0, this->renderVertex, 0, sizeof(Iris2DVertex));
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
}
float IrisViewport::GetX(){
return this->rect->x;
}
float IrisViewport::GetY(){
return this->rect->y;
}
UINT IrisViewport::GetWidth(){
return (UINT)this->rect->width;
}
UINT IrisViewport::GetHeight(){
return (UINT)this->rect->height;
}
IrisViewport::~IrisViewport(void)
{
if(!this->Disposed())
this->Dispose();
}