Platform_Win32_3D.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_3D.cpp *** 00002 00003 #include "Platform_Win32_3D.h" 00004 #include "Debug.h" 00005 #include "Platform_Win32_3D_Technology.h" 00006 #include "Platform_Win32_3D_D3D9.h" 00007 #include "Platform_Win32_3D_IndexBuffer.h" 00008 #include "Platform_Win32_3D_VertexBuffer.h" 00009 #include "Platform_Win32_3D_Texture.h" 00010 #include "DebugFont.h" 00011 #include "Platform_Win32_OS.h" 00012 #include "Platform_Time.h" 00013 #include "ArrayIterator.h" 00014 00015 #define WIN32_LEAN_AND_MEAN 00016 #define VC_EXTRALEAN 00017 #include <windows.h> 00018 #include <stdlib.h> 00019 00020 //*** Constructor *** 00021 00022 Platform_Win32_3D::Platform_Win32_3D(Platform_Win32_OS* os): 00023 windowHandle_(os->GetWindowHandle()), 00024 technology_(Technology_Undefined), 00025 technologyInstance_(0), 00026 fullscreen_(true), 00027 fullscreenWidth_(0), 00028 fullscreenHeight_(0), 00029 showfps_(false), 00030 disableOnWmSize_(0), 00031 ignoreNextOnWmSize_(false), 00032 lastBoundVertexBuffer_(0), 00033 lastBoundIndexBuffer_(0) 00034 { 00035 for (int i=0; i<8; i++) 00036 { 00037 lastBoundTextures_[i]=0; 00038 } 00039 00040 // Check commandline flags 00041 bool forceddraw=false; 00042 bool forcegdi=false; 00043 if (os->GetCommandLineString()) 00044 { 00045 char* cmdline=strdup(os->GetCommandLineString()); 00046 char* token=strtok(cmdline," "); 00047 while (token) 00048 { 00049 if (stricmp(token,"-window")==0) 00050 { 00051 fullscreen_=false; 00052 } 00053 if (stricmp(token,"-showfps")==0) 00054 { 00055 showfps_=true; 00056 } 00057 00058 token=strtok(0," "); 00059 } 00060 free(cmdline); 00061 } 00062 00063 00064 // Determine default screen size 00065 HWND desktopWindow=GetDesktopWindow(); 00066 RECT desktopRect; 00067 BOOL result=GetWindowRect(desktopWindow,&desktopRect); 00068 if (result) 00069 { 00070 fullscreenWidth_=desktopRect.right-desktopRect.left; 00071 fullscreenHeight_=desktopRect.bottom-desktopRect.top; 00072 00073 windowedWidth_=fullscreenWidth_-fullscreenWidth_/6; 00074 windowedHeight_=fullscreenHeight_-fullscreenHeight_/6; 00075 windowedX_=fullscreenWidth_-windowedWidth_; 00076 windowedY_=fullscreenHeight_-windowedHeight_; 00077 windowedX_/=2; 00078 windowedY_/=2; 00079 } 00080 00081 disableOnWmSize_++; 00082 00083 00084 // Initialize window size 00085 if (!GetFullscreen()) 00086 { 00087 SetWindowSize(); 00088 } 00089 00090 /* if (siEngine->GetCommandLineFlag("forceddraw") || IsRunningOnWine()) 00091 { 00092 SetTechnology(Technology_DDraw); 00093 } 00094 else if (siEngine->GetCommandLineFlag("forcegdi")) 00095 { 00096 SetTechnology(Technology_GDI); 00097 } 00098 else 00099 */ { 00100 SetTechnology(Technology_D3D9); 00101 } 00102 00103 // Show window 00104 ShowWindow(windowHandle_,SW_SHOW); 00105 00106 disableOnWmSize_--; 00107 } 00108 00109 00110 //*** Destructor *** 00111 00112 Platform_Win32_3D::~Platform_Win32_3D() 00113 { 00114 disableOnWmSize_++; 00115 00116 for (int i=0; i<vertexBuffers_.GetItemCount(); i++) 00117 { 00118 delete vertexBuffers_.Get(i); 00119 } 00120 00121 for (int i=0; i<indexBuffers_.GetItemCount(); i++) 00122 { 00123 delete indexBuffers_.Get(i); 00124 } 00125 00126 for (int i=0; i<textures_.GetItemCount(); i++) 00127 { 00128 delete textures_.Get(i); 00129 } 00130 00131 if (technologyInstance_) 00132 { 00133 delete technologyInstance_; 00134 } 00135 00136 disableOnWmSize_--; 00137 } 00138 00139 00140 //*** BeginScene *** 00141 00142 void Platform_Win32_3D::BeginScene(unsigned int color, float z, unsigned int stencil) 00143 { 00144 if (!technologyInstance_) 00145 { 00146 return; 00147 } 00148 00149 technologyInstance_->BeginScene(color,z,stencil); 00150 } 00151 00152 00153 //*** EndScene *** 00154 00155 void Platform_Win32_3D::EndScene() 00156 { 00157 if (!technologyInstance_) 00158 { 00159 return; 00160 } 00161 00162 technologyInstance_->EndScene(); 00163 } 00164 00165 00166 //*** CreateVertexBuffer *** 00167 00168 Platform_3D_VertexBuffer* Platform_Win32_3D::CreateVertexBuffer(int vertexFormat, int vertexCount, bool dynamic) 00169 { 00170 if (!technologyInstance_) 00171 { 00172 return 0; 00173 } 00174 00175 00176 Platform_Win32_3D_VertexBuffer* vertexBuffer=new Platform_Win32_3D_VertexBuffer(this, vertexFormat, vertexCount, dynamic); 00177 vertexBuffers_.Add(vertexBuffer); 00178 vertexBuffer->Reset(technologyInstance_); 00179 return vertexBuffer; 00180 } 00181 00182 00183 //*** CreateIndexBuffer *** 00184 00185 Platform_3D_IndexBuffer* Platform_Win32_3D::CreateIndexBuffer(int indexCount, bool dynamic) 00186 { 00187 if (!technologyInstance_) 00188 { 00189 return 0; 00190 } 00191 00192 Platform_Win32_3D_IndexBuffer* indexBuffer=new Platform_Win32_3D_IndexBuffer(this, indexCount, dynamic); 00193 indexBuffers_.Add(indexBuffer); 00194 indexBuffer->Reset(technologyInstance_); 00195 return indexBuffer; 00196 } 00197 00198 00199 //*** CreateTexture *** 00200 00201 Platform_3D_Texture* Platform_Win32_3D::CreateTexture(const Asset& asset) 00202 { 00203 if (!technologyInstance_) 00204 { 00205 return 0; 00206 } 00207 00208 Platform_Win32_3D_Texture* texture=new Platform_Win32_3D_Texture(this, asset); 00209 textures_.Add(texture); 00210 texture->Reset(technologyInstance_); 00211 return texture; 00212 } 00213 00214 00215 //*** CreateTexture *** 00216 00217 Platform_3D_Texture* Platform_Win32_3D::CreateTexture(const Image& image) 00218 { 00219 if (!technologyInstance_) 00220 { 00221 return 0; 00222 } 00223 00224 Platform_Win32_3D_Texture* texture=new Platform_Win32_3D_Texture(this, image); 00225 textures_.Add(texture); 00226 texture->Reset(technologyInstance_); 00227 return texture; 00228 } 00229 00230 00231 //*** Render *** 00232 00233 void Platform_Win32_3D::Render(int startVertex, int vertexCount, int startIndex, int indexCount) 00234 { 00235 if (!technologyInstance_) 00236 { 00237 return; 00238 } 00239 00240 technologyInstance_->Render(startVertex,vertexCount,startIndex,indexCount); 00241 } 00242 00243 00244 //*** Render *** 00245 00246 void Platform_Win32_3D::Render(int startVertex, int vertexCount) 00247 { 00248 if (!technologyInstance_) 00249 { 00250 return; 00251 } 00252 00253 technologyInstance_->Render(startVertex,vertexCount); 00254 } 00255 00256 00257 //*** SetWorldMatrix *** 00258 00259 void Platform_Win32_3D::SetWorldMatrix(const float worldMatrix[16]) 00260 { 00261 if (!technologyInstance_) 00262 { 00263 return; 00264 } 00265 00266 technologyInstance_->SetWorldMatrix(worldMatrix); 00267 } 00268 00269 00270 //*** SetViewMatrix *** 00271 00272 void Platform_Win32_3D::SetViewMatrix(const float viewMatrix[16]) 00273 { 00274 if (!technologyInstance_) 00275 { 00276 return; 00277 } 00278 00279 technologyInstance_->SetViewMatrix(viewMatrix); 00280 } 00281 00282 00283 //*** SetProjectionMatrix *** 00284 00285 void Platform_Win32_3D::SetProjectionMatrix(const float projectionMatrix[16]) 00286 { 00287 if (!technologyInstance_) 00288 { 00289 return; 00290 } 00291 00292 technologyInstance_->SetProjectionMatrix(projectionMatrix); 00293 } 00294 00295 00296 //*** GetLightCount *** 00297 00298 int Platform_Win32_3D::GetLightCount() 00299 { 00300 if (!technologyInstance_) 00301 { 00302 return 0; 00303 } 00304 00305 return technologyInstance_->GetLightCount(); 00306 } 00307 00308 00309 //*** EnableDirectionalLight *** 00310 00311 void Platform_Win32_3D::EnableDirectionalLight(int lightIndex, float colorR, float colorG, float colorB, float directionX, float directionY, float directionZ) 00312 { 00313 if (!technologyInstance_) 00314 { 00315 return; 00316 } 00317 00318 technologyInstance_->EnableDirectionalLight(lightIndex, colorR, colorG, colorB, directionX, directionY, directionZ); 00319 } 00320 00321 00322 //*** DisableLight *** 00323 00324 void Platform_Win32_3D::DisableLight(int lightIndex) 00325 { 00326 if (!technologyInstance_) 00327 { 00328 return; 00329 } 00330 00331 technologyInstance_->DisableLight(lightIndex); 00332 } 00333 00334 00335 //*** SetAmbientLight *** 00336 00337 void Platform_Win32_3D::SetAmbientLight(unsigned int color) 00338 { 00339 if (!technologyInstance_) 00340 { 00341 return; 00342 } 00343 00344 technologyInstance_->SetAmbientLight(color); 00345 } 00346 00347 00348 //*** EnableLighting *** 00349 00350 void Platform_Win32_3D::EnableLighting(bool enabled) 00351 { 00352 if (!technologyInstance_) 00353 { 00354 return; 00355 } 00356 00357 technologyInstance_->EnableLighting(enabled); 00358 } 00359 00360 00361 //*** EnableZRead *** 00362 00363 void Platform_Win32_3D::EnableZRead(bool enabled) 00364 { 00365 if (!technologyInstance_) 00366 { 00367 return; 00368 } 00369 00370 technologyInstance_->EnableZRead(enabled); 00371 } 00372 00373 00374 //*** EnableZWrite *** 00375 00376 void Platform_Win32_3D::EnableZWrite(bool enabled) 00377 { 00378 if (!technologyInstance_) 00379 { 00380 return; 00381 } 00382 00383 technologyInstance_->EnableZWrite(enabled); 00384 } 00385 00386 00387 //*** SetTechnology *** 00388 00389 void Platform_Win32_3D::SetTechnology(Platform_Win32_3D::Technology technology) 00390 { 00391 disableOnWmSize_++; 00392 00393 if (technologyInstance_) 00394 { 00395 delete technologyInstance_; 00396 technologyInstance_=0; 00397 } 00398 00399 technology_=technology; 00400 00401 if (technology_>=Technology_Undefined) 00402 { 00403 return; 00404 } 00405 00406 switch(technology_) 00407 { 00408 case Technology_D3D9: 00409 { 00410 technologyInstance_=new Platform_Win32_3D_D3D9(windowHandle_, GetFullscreen(), GetWidth(), GetHeight()); 00411 } break; 00412 00413 } 00414 00415 if (!technologyInstance_->Setup()) 00416 { 00417 DowngradeTechnology(); 00418 } 00419 else 00420 { 00421 for (int i=0; i<vertexBuffers_.GetItemCount(); i++) 00422 { 00423 vertexBuffers_.Get(i)->Reset(technologyInstance_); 00424 } 00425 00426 for (int i=0; i<indexBuffers_.GetItemCount(); i++) 00427 { 00428 indexBuffers_.Get(i)->Reset(technologyInstance_); 00429 } 00430 00431 for (int i=0; i<textures_.GetItemCount(); i++) 00432 { 00433 textures_.Get(i)->Reset(technologyInstance_); 00434 } 00435 00436 if (lastBoundVertexBuffer_) 00437 { 00438 lastBoundVertexBuffer_->Bind(); 00439 } 00440 00441 if (lastBoundIndexBuffer_) 00442 { 00443 lastBoundIndexBuffer_->Bind(); 00444 } 00445 00446 for (int stage=0; stage<8; stage++) 00447 { 00448 if (lastBoundTextures_[stage]) 00449 { 00450 lastBoundTextures_[stage]->Bind(stage); 00451 } 00452 } 00453 } 00454 00455 disableOnWmSize_--; 00456 } 00457 00458 00459 //*** DowngradeTechnology *** 00460 00461 void Platform_Win32_3D::DowngradeTechnology() 00462 { 00463 disableOnWmSize_++; 00464 00465 DebugPrint(("Method failed for technology %d, falling back on technology %d\n",technology_,technology_+1)); 00466 00467 if (technologyInstance_) 00468 { 00469 delete technologyInstance_; 00470 technologyInstance_=0; 00471 } 00472 00473 Technology newTechnology=(Technology)(technology_+1); 00474 00475 if (newTechnology<Technology_Undefined) 00476 { 00477 SetTechnology(newTechnology); 00478 disableOnWmSize_--; 00479 return; 00480 } 00481 00482 technology_=Technology_Undefined; 00483 technologyInstance_=0; 00484 00485 disableOnWmSize_--; 00486 } 00487 00488 00489 //*** SetFullscreen *** 00490 00491 void Platform_Win32_3D::SetFullscreen(bool fullscreen) 00492 { 00493 if (fullscreen_==fullscreen) 00494 { 00495 return; 00496 } 00497 00498 fullscreen_=fullscreen; 00499 00500 if (technologyInstance_) 00501 { 00502 disableOnWmSize_++; 00503 00504 delete technologyInstance_; 00505 technologyInstance_=0; 00506 00507 if (!GetFullscreen()) 00508 { 00509 SetWindowSize(); 00510 } 00511 00512 SetTechnology(technology_); 00513 00514 /* if (technology_==Technology_DDraw && !GetFullscreen()) 00515 { 00516 SetWindowSize(); 00517 } 00518 */ 00519 disableOnWmSize_--; 00520 } 00521 } 00522 00523 00524 //*** GetFullscreen *** 00525 00526 bool Platform_Win32_3D::GetFullscreen() 00527 { 00528 return fullscreen_; 00529 } 00530 00531 00532 //*** SetSize *** 00533 00534 void Platform_Win32_3D::SetSize(int width, int height) 00535 { 00536 windowedWidth_=width; 00537 windowedHeight_=height; 00538 00539 if (technologyInstance_) 00540 { 00541 disableOnWmSize_++; 00542 00543 delete technologyInstance_; 00544 technologyInstance_=0; 00545 00546 if (!GetFullscreen()) 00547 { 00548 SetWindowSize(); 00549 } 00550 00551 SetTechnology(technology_); 00552 00553 disableOnWmSize_--; 00554 00555 } 00556 00557 } 00558 00559 00560 //*** GetWidth *** 00561 00562 int Platform_Win32_3D::GetWidth() 00563 { 00564 if (fullscreen_) 00565 { 00566 return fullscreenWidth_; 00567 } 00568 00569 return windowedWidth_; 00570 } 00571 00572 00573 //*** GetHeight *** 00574 00575 int Platform_Win32_3D::GetHeight() 00576 { 00577 if (fullscreen_) 00578 { 00579 return fullscreenHeight_; 00580 } 00581 00582 return windowedHeight_; 00583 } 00584 00585 00586 //*** OnRestore *** 00587 00588 void Platform_Win32_3D::OnRestore() 00589 { 00590 ignoreNextOnWmSize_=true; 00591 } 00592 00593 00594 //*** OnWmSize *** 00595 00596 void Platform_Win32_3D::OnWmSize(int width, int height) 00597 { 00598 if (disableOnWmSize_ || fullscreen_ || ignoreNextOnWmSize_) 00599 { 00600 DebugPrint(("OnWmSize(%d,%d) - DISABLED\n",width,height)); 00601 ignoreNextOnWmSize_=false; 00602 return; 00603 } 00604 00605 disableOnWmSize_++; 00606 00607 DebugPrint(("OnWmSize(%d,%d) fullscreen_=%d\n",width,height,fullscreen_)); 00608 00609 /* if (technology_==Technology_DDraw && !technologyInstance_) 00610 { 00611 SetTechnology(technology_); 00612 } 00613 */ 00614 windowedWidth_=width; 00615 windowedHeight_=height; 00616 00617 if (!fullscreen_ && technologyInstance_) 00618 { 00619 delete technologyInstance_; 00620 technologyInstance_=0; 00621 00622 SetTechnology(technology_); 00623 } 00624 00625 disableOnWmSize_--; 00626 } 00627 00628 00629 00630 //*** OnMinimize *** 00631 00632 void Platform_Win32_3D::OnMinimize() 00633 { 00634 /* DebugPrint(("OnMinimize\n")); 00635 if (technologyInstance_) 00636 { 00637 delete technologyInstance_; 00638 technologyInstance_=0; 00639 } 00640 */ } 00641 00642 00643 //*** OnLoseFocus *** 00644 00645 void Platform_Win32_3D::OnLoseFocus() 00646 { 00647 disableOnWmSize_++; 00648 00649 if (fullscreen_ && technologyInstance_) 00650 { 00651 disableOnWmSize_++; 00652 DebugPrint(("OnLoseFocus\n")); 00653 delete technologyInstance_; 00654 technologyInstance_=0; 00655 disableOnWmSize_--; 00656 } 00657 } 00658 00659 00660 //*** OnGainFocus *** 00661 00662 void Platform_Win32_3D::OnGainFocus() 00663 { 00664 if (fullscreen_ && !technologyInstance_) 00665 { 00666 disableOnWmSize_++; 00667 DebugPrint(("OnGainFocus\n")); 00668 SetTechnology(technology_); 00669 disableOnWmSize_--; 00670 } 00671 00672 disableOnWmSize_--; 00673 } 00674 00675 00676 //*** SetWindowSize *** 00677 00678 void Platform_Win32_3D::SetWindowSize() 00679 { 00680 DebugPrint(("SetWindowSize\n")); 00681 00682 // Get the whole window area 00683 RECT rect1; 00684 BOOL ret=GetWindowRect(windowHandle_,&rect1); 00685 Assert(ret,"Couldn't get Window Rect of main window"); 00686 rect1.right-=rect1.left; 00687 rect1.bottom-=rect1.top; 00688 00689 // Get the client area 00690 RECT rect2; 00691 ret=GetClientRect(windowHandle_,&rect2); 00692 Assert(ret,"Couldn't get Client Rect of main window"); 00693 rect2.right-=rect2.left; 00694 rect2.bottom-=rect2.top; 00695 00696 // Calculate the size of the windows borders/title bar etc. 00697 int sx=rect1.right-rect2.right; 00698 int sy=rect1.bottom-rect2.bottom; 00699 00700 // Set the window to the required dimensions 00701 MoveWindow(windowHandle_,windowedX_,windowedY_,windowedWidth_+sx,windowedHeight_+sy,TRUE); 00702 } 00703 00704 00705 //*** IsRunningOnWine *** 00706 00707 bool Platform_Win32_3D::IsRunningOnWine() 00708 { 00709 HMODULE module=LoadLibrary("ntdll"); 00710 if (!module) 00711 { 00712 return false; 00713 } 00714 00715 FARPROC proc=GetProcAddress(module, "wine_get_unix_file_name"); 00716 FreeLibrary(module); 00717 00718 return proc!=0; 00719 } 00720 00721 00722 //*** VertexBufferBound *** 00723 00724 void Platform_Win32_3D::VertexBufferBound(Platform_Win32_3D_VertexBuffer* vertexBuffer) 00725 { 00726 lastBoundVertexBuffer_=vertexBuffer; 00727 } 00728 00729 00730 //*** IndexBufferBound *** 00731 00732 void Platform_Win32_3D::IndexBufferBound(Platform_Win32_3D_IndexBuffer* indexBuffer) 00733 { 00734 lastBoundIndexBuffer_=indexBuffer; 00735 } 00736 00737 00738 //*** TextureBound *** 00739 00740 void Platform_Win32_3D::TextureBound(int stage, Platform_Win32_3D_Texture* texture) 00741 { 00742 lastBoundTextures_[stage]=texture; 00743 } 00744 00745 00746 //*** VertexBufferDeleted *** 00747 00748 void Platform_Win32_3D::VertexBufferDeleted(Platform_Win32_3D_VertexBuffer* vertexBuffer) 00749 { 00750 if (lastBoundVertexBuffer_==vertexBuffer) 00751 { 00752 lastBoundVertexBuffer_=0; 00753 } 00754 ArrayIterator<Platform_Win32_3D_VertexBuffer*> it(vertexBuffers_); 00755 if (it.Find(vertexBuffer)) 00756 { 00757 vertexBuffers_.Remove(it); 00758 } 00759 } 00760 00761 00762 //*** IndexBufferDeleted *** 00763 00764 void Platform_Win32_3D::IndexBufferDeleted(Platform_Win32_3D_IndexBuffer* indexBuffer) 00765 { 00766 if (lastBoundIndexBuffer_==indexBuffer) 00767 { 00768 lastBoundIndexBuffer_=0; 00769 } 00770 00771 ArrayIterator<Platform_Win32_3D_IndexBuffer*> it(indexBuffers_); 00772 if (it.Find(indexBuffer)) 00773 { 00774 indexBuffers_.Remove(it); 00775 } 00776 } 00777 00778 00779 //*** TextureDeleted *** 00780 00781 void Platform_Win32_3D::TextureDeleted(Platform_Win32_3D_Texture* texture) 00782 { 00783 for (int stage=0; stage<8; stage++) 00784 { 00785 if (lastBoundTextures_[stage]==texture) 00786 { 00787 lastBoundTextures_[stage]=0; 00788 } 00789 } 00790 00791 ArrayIterator<Platform_Win32_3D_Texture*> it(textures_); 00792 if (it.Find(texture)) 00793 { 00794 textures_.Remove(it); 00795 } 00796 } 00797
Reproduction/republishing of any material on this site without permission is strictly prohibited.
