SHOW:
|
|
- or go back to the newest paste.
| 1 | - | Concurrency::details::_ReentrantPPLLock lock; |
| 1 | + | #include "StdAfx.h" |
| 2 | #include "oohh.hpp" | |
| 3 | - | const char *my_call(lua_State *L, int arguments, int results) |
| 3 | + | |
| 4 | #include "ID3DSystem.h" | |
| 5 | - | Concurrency::details::_ReentrantPPLLock::_Scoped_lock slock(lock); |
| 5 | + | |
| 6 | #include "d3d9.h" | |
| 7 | - | if (!lua_isfunction(L, -(arguments+1))) |
| 7 | + | #include "D3dx9core.h" |
| 8 | ||
| 9 | - | auto err = lua_tostring(L, -1); |
| 9 | + | class CMyD3D : private ID3DEventListener |
| 10 | - | lua_remove(L, -1); |
| 10 | + | |
| 11 | - | return err; |
| 11 | + | protected: |
| 12 | bool bDX9; | |
| 13 | ||
| 14 | - | if (lua_pcall(L, arguments, results, 0) != 0) |
| 14 | + | union device // Declare union type |
| 15 | - | {
|
| 15 | + | {
|
| 16 | - | auto err = lua_tostring(L, -1); |
| 16 | + | void* ptr; |
| 17 | - | lua_remove(L, -1); |
| 17 | + | IDirect3DDevice9* dx9; |
| 18 | - | return err; |
| 18 | + | //ID3D11Device* dx11; |
| 19 | } m_pDevice; | |
| 20 | ||
| 21 | IDirect3DStateBlock9* m_pStateBlock; | |
| 22 | ||
| 23 | void OnPrePresent() | |
| 24 | {
| |
| 25 | if(bDX9) | |
| 26 | {
| |
| 27 | if(!m_pStateBlock) | |
| 28 | m_pDevice.dx9->CreateStateBlock(D3DSBT_ALL, &m_pStateBlock); | |
| 29 | ||
| 30 | m_pStateBlock->Capture(); | |
| 31 | my->CallHook("PostDirectDraw");
| |
| 32 | m_pStateBlock->Apply(); | |
| 33 | } | |
| 34 | }; | |
| 35 | ||
| 36 | void OnPostPresent() {};
| |
| 37 | void OnPreReset() {
| |
| 38 | SAFE_RELEASE(m_pStateBlock); | |
| 39 | }; | |
| 40 | void OnPostReset() {};
| |
| 41 | void OnPostBeginScene() {};
| |
| 42 | ||
| 43 | public: | |
| 44 | CMyD3D() | |
| 45 | {
| |
| 46 | m_pDevice.ptr = NULL; | |
| 47 | m_pStateBlock = NULL; | |
| 48 | ||
| 49 | if(gD3DSystem) | |
| 50 | {
| |
| 51 | // Initialize the device | |
| 52 | m_pDevice.ptr = gD3DSystem->GetDevice(); | |
| 53 | bDX9 = true; | |
| 54 | ||
| 55 | ||
| 56 | // the listeners will be called renderer thread. | |
| 57 | gD3DSystem->RegisterListener(this); | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | ~CMyD3D() | |
| 62 | {
| |
| 63 | if(gD3DSystem) | |
| 64 | gD3DSystem->UnregisterListener(this); | |
| 65 | ||
| 66 | SAFE_RELEASE(m_pStateBlock); | |
| 67 | } | |
| 68 | }; | |
| 69 | ||
| 70 | #define D3DFVF_VERTEXFORMAT2D (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1) | |
| 71 | ||
| 72 | struct VertexFormat | |
| 73 | {
| |
| 74 | FLOAT x, y, z, rhw; | |
| 75 | DWORD color; | |
| 76 | float u, v; | |
| 77 | }; | |
| 78 | ||
| 79 | static CMyD3D *m_listener; | |
| 80 | static IDirect3DDevice9 *m_device; | |
| 81 | static int m_vertnum = 0; | |
| 82 | static const int max_verts = 1024; | |
| 83 | VertexFormat m_verts[max_verts]; | |
| 84 | static D3DCOLOR m_color; | |
| 85 | ||
| 86 | //DirectX9::DirectX9(IDirect3DDevice9* pDevice) | |
| 87 | LUALIB_FUNCTION(directx, Open) | |
| 88 | {
| |
| 89 | m_device = (IDirect3DDevice9 *)gD3DSystem->GetDevice(); | |
| 90 | //m_device = static_cast<IDirect3DDevice9 *>(gEnv->pRenderer->EF_Query(EFQ_D3DDevice)); | |
| 91 | ||
| 92 | m_vertnum = 0; | |
| 93 | ||
| 94 | for (int i = 0; i < max_verts; i++) | |
| 95 | {
| |
| 96 | m_verts[i].z = 0.5f; | |
| 97 | m_verts[i].rhw = 1.0f; | |
| 98 | } | |
| 99 | ||
| 100 | if (m_listener) | |
| 101 | {
| |
| 102 | delete m_listener; | |
| 103 | m_listener = nullptr; | |
| 104 | } | |
| 105 | ||
| 106 | m_listener = new CMyD3D(); | |
| 107 | ||
| 108 | return 0; | |
| 109 | } | |
| 110 | ||
| 111 | ||
| 112 | LUALIB_FUNCTION(directx, SetTransform) | |
| 113 | {
| |
| 114 | m_device->SetTransform(my->ToEnum<D3DTRANSFORMSTATETYPE>(1), (D3DMATRIX *)&my->ToMatrix44(2)); | |
| 115 | ||
| 116 | return 0; | |
| 117 | } | |
| 118 | ||
| 119 | LUALIB_FUNCTION(directx, GetTransform) | |
| 120 | {
| |
| 121 | auto out = Matrix44(); | |
| 122 | ||
| 123 | m_device->GetTransform(my->ToEnum<D3DTRANSFORMSTATETYPE>(1), (D3DMATRIX *)&out); | |
| 124 | ||
| 125 | my->Push(out); | |
| 126 | ||
| 127 | return 1; | |
| 128 | } | |
| 129 | ||
| 130 | void Flush() | |
| 131 | {
| |
| 132 | if (m_vertnum > 0) | |
| 133 | {
| |
| 134 | //DWORD old = 0; | |
| 135 | //m_device->GetFVF(&old); | |
| 136 | m_device->SetFVF(D3DFVF_VERTEXFORMAT2D); | |
| 137 | m_device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, m_vertnum/3, &m_verts[0], sizeof(VertexFormat)); | |
| 138 | //m_device->SetFVF(old); | |
| 139 | ||
| 140 | m_vertnum = 0; | |
| 141 | } | |
| 142 | } | |
| 143 | ||
| 144 | LUALIB_FUNCTION(directx, Begin) | |
| 145 | {
| |
| 146 | m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); | |
| 147 | m_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); | |
| 148 | m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); | |
| 149 | ||
| 150 | m_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); | |
| 151 | m_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); | |
| 152 | m_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); | |
| 153 | ||
| 154 | m_device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); | |
| 155 | m_device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); | |
| 156 | ||
| 157 | m_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); | |
| 158 | m_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); | |
| 159 | m_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT); | |
| 160 | ||
| 161 | m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); | |
| 162 | m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); | |
| 163 | m_device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT); | |
| 164 | ||
| 165 | m_device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); | |
| 166 | m_device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); | |
| 167 | ||
| 168 | return 0; | |
| 169 | } | |
| 170 | ||
| 171 | LUALIB_FUNCTION(directx, End) | |
| 172 | {
| |
| 173 | Flush(); | |
| 174 | ||
| 175 | return 0; | |
| 176 | } | |
| 177 | ||
| 178 | void AddVert(int x, int y) | |
| 179 | {
| |
| 180 | if (m_vertnum >= max_verts - 1) | |
| 181 | {
| |
| 182 | Flush(); | |
| 183 | } | |
| 184 | ||
| 185 | m_verts[m_vertnum].x = (float)x; | |
| 186 | m_verts[m_vertnum].y = (float)y; | |
| 187 | m_verts[m_vertnum].color = m_color; | |
| 188 | ||
| 189 | m_vertnum++; | |
| 190 | } | |
| 191 | ||
| 192 | ||
| 193 | ||
| 194 | void AddVert(int x, int y, float u, float v) | |
| 195 | {
| |
| 196 | if ( m_vertnum >= max_verts - 1 ) | |
| 197 | {
| |
| 198 | Flush(); | |
| 199 | } | |
| 200 | ||
| 201 | m_verts[m_vertnum].x = -0.5f + (float)x; | |
| 202 | m_verts[m_vertnum].y = -0.5f + (float)y; | |
| 203 | m_verts[m_vertnum].u = u; | |
| 204 | m_verts[m_vertnum].v = v; | |
| 205 | ||
| 206 | m_verts[m_vertnum].color = m_color; | |
| 207 | ||
| 208 | m_vertnum++; | |
| 209 | } | |
| 210 | ||
| 211 | LUALIB_FUNCTION(directx, AddVert) | |
| 212 | {
| |
| 213 | auto x = my->ToNumber(1); | |
| 214 | auto y = my->ToNumber(2); | |
| 215 | ||
| 216 | AddVert(x, y); | |
| 217 | ||
| 218 | return 0; | |
| 219 | } | |
| 220 | ||
| 221 | LUALIB_FUNCTION(directx, AddVertUV) | |
| 222 | {
| |
| 223 | auto x = my->ToNumber(1); | |
| 224 | auto y = my->ToNumber(2); | |
| 225 | ||
| 226 | auto u = my->ToNumber(3); | |
| 227 | auto v = my->ToNumber(4); | |
| 228 | ||
| 229 | AddVert(x,y,u,v); | |
| 230 | ||
| 231 | return 0; | |
| 232 | } | |
| 233 | ||
| 234 | LUALIB_FUNCTION(directx, DrawFilledRect) | |
| 235 | {
| |
| 236 | auto x = my->ToNumber(1); | |
| 237 | auto y = my->ToNumber(2); | |
| 238 | auto w = my->ToNumber(3); | |
| 239 | auto h = my->ToNumber(4); | |
| 240 | ||
| 241 | m_device->SetTexture(0, NULL); | |
| 242 | ||
| 243 | AddVert(x, y); | |
| 244 | AddVert(x + w, y); | |
| 245 | AddVert(x, y + h); | |
| 246 | ||
| 247 | AddVert(x + w, y); | |
| 248 | AddVert(x + w, y + h); | |
| 249 | AddVert(x, y + h); | |
| 250 | ||
| 251 | return 0; | |
| 252 | } | |
| 253 | ||
| 254 | LUALIB_FUNCTION(directx, SetDrawColor) | |
| 255 | {
| |
| 256 | auto r = my->ToNumber(1); | |
| 257 | auto g = my->ToNumber(2); | |
| 258 | auto b = my->ToNumber(3); | |
| 259 | auto a = my->ToNumber(4); | |
| 260 | ||
| 261 | m_color = D3DCOLOR_ARGB((char)(a*255), (char)(r*255), (char)(g*255), (char)(b*255)); | |
| 262 | ||
| 263 | return 0; | |
| 264 | } | |
| 265 | ||
| 266 | LUAMTA_FUNCTION(dxtexture, Remove) | |
| 267 | {
| |
| 268 | auto tex = my->ToPointer<IDirect3DTexture9>(1, "dxtexture"); | |
| 269 | ||
| 270 | tex->Release(); | |
| 271 | ||
| 272 | my->MakeNull(tex); | |
| 273 | ||
| 274 | return 0; | |
| 275 | } | |
| 276 | ||
| 277 | LUAMTA_FUNCTION(dxfont, Remove) | |
| 278 | {
| |
| 279 | auto font = (LPD3DXFONT)my->ToPointer<void *>(1, "dxfont"); | |
| 280 | ||
| 281 | font->Release(); | |
| 282 | ||
| 283 | my->MakeNull(font); | |
| 284 | ||
| 285 | return 0; | |
| 286 | } | |
| 287 | ||
| 288 | LUALIB_FUNCTION(directx, LoadFont) | |
| 289 | {
| |
| 290 | auto face_name = my->ToString(1); | |
| 291 | auto bold = my->ToBoolean(2); | |
| 292 | auto size = my->ToNumber(3); | |
| 293 | ||
| 294 | D3DXFONT_DESC fd; | |
| 295 | ||
| 296 | strcpy_s( fd.FaceName, LF_FACESIZE, face_name); | |
| 297 | ||
| 298 | fd.Width = 0; | |
| 299 | fd.MipLevels = 1; | |
| 300 | fd.CharSet = DEFAULT_CHARSET; | |
| 301 | fd.Height = size; | |
| 302 | fd.OutputPrecision = OUT_DEFAULT_PRECIS; | |
| 303 | fd.Italic = 0; | |
| 304 | fd.Weight = bold ? FW_BOLD : FW_NORMAL; | |
| 305 | fd.Quality = PROOF_QUALITY; | |
| 306 | fd.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; | |
| 307 | ||
| 308 | LPD3DXFONT font; | |
| 309 | D3DXCreateFontIndirect(m_device, &fd, &font); | |
| 310 | ||
| 311 | RECT rctA = {0,0,0,0};
| |
| 312 | font->DrawText(NULL, "A", -1, &rctA, DT_CALCRECT | DT_LEFT | DT_TOP | DT_SINGLELINE, 0); | |
| 313 | ||
| 314 | RECT rctSpc = {0,0,0,0};
| |
| 315 | font->DrawText(NULL, "A A", -1, &rctSpc, DT_CALCRECT | DT_LEFT | DT_TOP | DT_SINGLELINE, 0); | |
| 316 | ||
| 317 | my->Push(font, "dxfont"); | |
| 318 | my->Push(rctSpc.right - rctA.right * 2); | |
| 319 | ||
| 320 | return 2; | |
| 321 | } | |
| 322 | ||
| 323 | LUALIB_FUNCTION(directx, RenderText) | |
| 324 | {
| |
| 325 | auto font = (LPD3DXFONT)my->ToPointer<void *>(1, "dxfont"); | |
| 326 | auto x = my->ToNumber(2); | |
| 327 | auto y = my->ToNumber(3); | |
| 328 | auto str = my->ToString(4); | |
| 329 | ||
| 330 | Flush(); | |
| 331 | ||
| 332 | RECT ClipRect = { x, y, 0, 0 };
| |
| 333 | font->DrawText(NULL, str, -1, &ClipRect, DT_LEFT | DT_TOP | DT_NOCLIP | DT_SINGLELINE, m_color); | |
| 334 | ||
| 335 | return 0; | |
| 336 | } | |
| 337 | ||
| 338 | LUALIB_FUNCTION(directx, MeasureText) | |
| 339 | {
| |
| 340 | auto font = (LPD3DXFONT)my->ToPointer<void *>(1, "dxfont"); | |
| 341 | auto str = my->ToString(2); | |
| 342 | auto space_width = my->ToNumber(3, 0); | |
| 343 | ||
| 344 | if (strcmp(str, "") == 0) | |
| 345 | {
| |
| 346 | RECT rct = {0,0,0,0};
| |
| 347 | font->DrawText(NULL, "W", -1, &rct, DT_CALCRECT, 0); | |
| 348 | ||
| 349 | my->Push(0); | |
| 350 | my->Push(rct.bottom); | |
| 351 | ||
| 352 | return 2; | |
| 353 | } | |
| 354 | ||
| 355 | RECT rct = {0,0,0,0};
| |
| 356 | font->DrawText(NULL, str, -1, &rct, DT_CALCRECT | DT_LEFT | DT_TOP | DT_SINGLELINE, 0); | |
| 357 | ||
| 358 | // 32 == space | |
| 359 | for (int i = strlen(str); i >= 0 && str[i] == 32; i--) | |
| 360 | {
| |
| 361 | rct.right += space_width; | |
| 362 | } | |
| 363 | ||
| 364 | my->Push(rct.right); | |
| 365 | my->Push(rct.bottom); | |
| 366 | ||
| 367 | return 2; | |
| 368 | } | |
| 369 | ||
| 370 | LUALIB_FUNCTION(directx, StartClip) | |
| 371 | {
| |
| 372 | auto x = my->ToNumber(1); | |
| 373 | auto y = my->ToNumber(2); | |
| 374 | auto w = my->ToNumber(3); | |
| 375 | auto h = my->ToNumber(4); | |
| 376 | ||
| 377 | Flush(); | |
| 378 | ||
| 379 | m_device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); | |
| 380 | ||
| 381 | RECT r; | |
| 382 | ||
| 383 | r.left = ceil((float)x); | |
| 384 | r.right = ceil((float)x + w); | |
| 385 | r.top = ceil((float)y); | |
| 386 | r.bottom = ceil((float)y + h); | |
| 387 | ||
| 388 | m_device->SetScissorRect(&r); | |
| 389 | ||
| 390 | return 0; | |
| 391 | } | |
| 392 | ||
| 393 | LUALIB_FUNCTION(directx, EndClip) | |
| 394 | {
| |
| 395 | Flush(); | |
| 396 | m_device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); | |
| 397 | ||
| 398 | return 0; | |
| 399 | } | |
| 400 | ||
| 401 | LUALIB_FUNCTION(directx, LoadTexture) | |
| 402 | {
| |
| 403 | IDirect3DTexture9 *ptr = NULL; | |
| 404 | D3DXIMAGE_INFO params; | |
| 405 | ||
| 406 | HRESULT hr = D3DXCreateTextureFromFileEx( | |
| 407 | m_device, | |
| 408 | my->ToString(1), | |
| 409 | 0, | |
| 410 | 0, | |
| 411 | D3DX_DEFAULT, | |
| 412 | 0, | |
| 413 | D3DFMT_UNKNOWN, | |
| 414 | D3DPOOL_MANAGED, | |
| 415 | D3DX_DEFAULT, | |
| 416 | D3DX_DEFAULT, | |
| 417 | 0, ¶ms, | |
| 418 | NULL, | |
| 419 | &ptr | |
| 420 | ); | |
| 421 | ||
| 422 | if ( hr != S_OK ) | |
| 423 | {
| |
| 424 | return 0; | |
| 425 | } | |
| 426 | ||
| 427 | my->Push(ptr, "dxtexture"); | |
| 428 | ||
| 429 | return 1; | |
| 430 | } | |
| 431 | ||
| 432 | LUALIB_FUNCTION(directx, DrawTexturedRect) | |
| 433 | {
| |
| 434 | auto tex = my->ToPointer<IDirect3DTexture9>(1, "dxtexture"); | |
| 435 | ||
| 436 | auto x = my->ToNumber(2); | |
| 437 | auto y = my->ToNumber(3); | |
| 438 | auto w = my->ToNumber(4); | |
| 439 | auto h = my->ToNumber(5); | |
| 440 | ||
| 441 | auto u1 = my->ToNumber(6); | |
| 442 | auto v1 = my->ToNumber(7); | |
| 443 | auto u2 = my->ToNumber(8); | |
| 444 | auto v2 = my->ToNumber(9); | |
| 445 | ||
| 446 | m_device->SetTexture(0, tex); | |
| 447 | ||
| 448 | AddVert(x, y, u1, v1); | |
| 449 | AddVert(x + w, y, u2, v1); | |
| 450 | AddVert(x, y + h, u1, v2); | |
| 451 | ||
| 452 | AddVert(x + w, y, u2, v1); | |
| 453 | AddVert(x + w, y + h, u2, v2); | |
| 454 | AddVert(x, y + h, u1, v2); | |
| 455 | ||
| 456 | return 0; | |
| 457 | } | |
| 458 | ||
| 459 | LUALIB_FUNCTION(directx, BeginContext) | |
| 460 | {
| |
| 461 | m_device->BeginScene(); | |
| 462 | m_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(128, 128, 128), 1, 0); | |
| 463 | ||
| 464 | return 0; | |
| 465 | } | |
| 466 | ||
| 467 | LUALIB_FUNCTION(directx, EndContext) | |
| 468 | {
| |
| 469 | m_device->EndScene(); | |
| 470 | ||
| 471 | return 0; | |
| 472 | } | |
| 473 | ||
| 474 | LUALIB_FUNCTION(directx, Clear) | |
| 475 | {
| |
| 476 | m_device->Clear(0, NULL, my->ToNumber(4, 0), D3DCOLOR_XRGB((char)my->ToNumber(1, 255), (char)my->ToNumber(2, 255), (char)my->ToNumber(3, 255)), my->ToNumber(5, 0), 0); | |
| 477 | ||
| 478 | return 0; | |
| 479 | } |