D3DVIEW2.CPP

Go to the documentation of this file.
00001 /* --
00002 OpenFX version 1.0 - Modelling, Animation and Rendering Package
00003 Copyright (C) 2000 OpenFX Development Team
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 You may contact the OpenFX development team via elecronic mail
00020 at core@openfx.org, or visit our website at http://openfx.org for
00021 further information and support details.
00022 -- */
00023 
00024 // d3dview2.cpp
00025 
00026 static DWORD bppToddbd(int bpp){
00027     switch(bpp) {
00028     case 1:
00029         return DDBD_1;
00030     case 2:
00031         return DDBD_2;
00032     case 4:
00033         return DDBD_4;
00034     case 8:
00035         return DDBD_8;
00036     case 16:
00037         return DDBD_16;
00038     case 24:
00039         return DDBD_24;
00040     case 32:
00041         return DDBD_32;
00042     }
00043     return 0;
00044 }
00045 
00046 
00047 LPGUID FindDevice(D3DCOLORMODEL cm){
00048     LPDIRECTDRAW lpDD;
00049     LPDIRECT3D lpD3D;
00050     D3DFINDDEVICESEARCH search;
00051     static D3DFINDDEVICERESULT result;
00052     HRESULT error;
00053     HDC hdc;
00054     int bpp;
00055 
00056     hdc = GetDC(NULL);
00057     bpp = GetDeviceCaps(hdc, BITSPIXEL);
00058     ReleaseDC(NULL, hdc);
00059 
00060     if (DirectDrawCreate(NULL, &lpDD, NULL))
00061         return NULL;
00062 
00063     if (lpDD->QueryInterface(IID_IDirect3D, (void**) &lpD3D)) {
00064         lpDD->Release();
00065         return NULL;
00066     }
00067     
00068     memset(&search, 0, sizeof search);
00069     search.dwSize = sizeof search;
00070     search.dwFlags = D3DFDS_COLORMODEL;
00071     search.dcmColorModel = (cm == D3DCOLOR_MONO) ? D3DCOLOR_MONO : D3DCOLOR_RGB;
00072 
00073     memset(&result, 0, sizeof result);
00074     result.dwSize = sizeof result;
00075 
00076     error = lpD3D->FindDevice(&search, &result);
00077 
00078     if (error == DD_OK) {
00079         /*
00080          * If the device found is hardware but cannot support the current
00081          * bit depth, then fall back to software rendering.
00082          */
00083         if (result.ddHwDesc.dwFlags
00084             && !(result.ddHwDesc.dwDeviceRenderBitDepth & bppToddbd(bpp))) {
00085             
00086             search.dwFlags |= D3DFDS_HARDWARE;
00087             search.bHardware = FALSE;
00088             memset(&result, 0, sizeof result);
00089             result.dwSize = sizeof result;
00090             error = lpD3D->FindDevice(&search, &result);
00091         }
00092     }
00093 
00094     lpD3D->Release();
00095     lpDD->Release();
00096 
00097     if (error)
00098         return NULL;
00099     else
00100         return &result.guid;
00101 }
00102 
00103 static BOOL CreateDevice(HWND win, AppInfo* info){
00104     RECT r;
00105     int bpp;
00106     HDC hdc;
00107 
00108     GetClientRect(win, &r);
00109     if (FAILED(lpD3DRM->CreateDeviceFromClipper(lpDDClipper, FindDevice(info->model),
00110                                 r.right, r.bottom, &info->dev)))
00111                                 goto generic_error;
00112     hdc = GetDC(win);
00113     bpp = GetDeviceCaps(hdc, BITSPIXEL);
00114     ReleaseDC(win, hdc);
00115     switch (bpp)
00116     {
00117     case 1:
00118         if (FAILED(info->dev->SetShades(4)))
00119             goto generic_error;
00120         if (FAILED(lpD3DRM->SetDefaultTextureShades(4)))
00121             goto generic_error;
00122         break;
00123     case 16:
00124         if (FAILED(info->dev->SetShades(32)))
00125             goto generic_error;
00126         if (FAILED(lpD3DRM->SetDefaultTextureColors(64)))
00127             goto generic_error;
00128         if (FAILED(lpD3DRM->SetDefaultTextureShades(32)))
00129             goto generic_error;
00130         if (FAILED(info->dev->SetDither(FALSE)))
00131             goto generic_error;
00132         break;
00133     case 24:
00134     case 32:
00135         if (FAILED(info->dev->SetShades(256)))
00136             goto generic_error;
00137         if (FAILED(lpD3DRM->SetDefaultTextureColors(64)))
00138             goto generic_error;
00139         if (FAILED(lpD3DRM->SetDefaultTextureShades(256)))
00140             goto generic_error;
00141         if (FAILED(info->dev->SetDither(FALSE)))
00142             goto generic_error;
00143         break;
00144     default:
00145         if (FAILED(info->dev->SetDither(FALSE)))
00146             goto generic_error;
00147     }
00148     D3DRMRENDERQUALITY quality;
00149     quality = info->dev->GetQuality();
00150 //    quality = (quality & ~D3DRMFILL_MASK) | D3DRMFILL_POINTS;
00151 //    quality = (quality & ~D3DRMFILL_MASK) | D3DRMFILL_WIREFRAME;
00152     quality = (quality & ~D3DRMFILL_MASK) | D3DRMFILL_SOLID;
00153     quality = (quality & ~D3DRMSHADE_MASK) | D3DRMSHADE_GOURAUD;
00154     info->dev->SetQuality(quality);
00155     if (!CreateScene(info))
00156         goto ret_with_error;
00157     if (FAILED(lpD3DRM->CreateViewport(info->dev, info->camera, 0, 0,
00158         info->dev->GetWidth(),
00159         info->dev->GetHeight(), &info->view)))
00160         goto generic_error;
00161     if (FAILED(info->view->SetBack(D3DVAL(5000.0))))
00162         goto generic_error;
00163 
00164     return TRUE;
00165 generic_error:
00166     Msg("An error occurred while creating the device.\n");
00167 ret_with_error:
00168     return FALSE;
00169 }
00170 
00171 void __cdecl
00172 Msg( LPSTR fmt, ... )
00173 {
00174     char buff[256];
00175     va_list va;
00176 
00177     va_start(va, fmt);
00178     wvsprintf(buff, fmt, va);
00179     va_end(va);
00180 
00181     lstrcat(buff, "\r\n");
00182     MessageBox( NULL, buff, "Viewer Message", MB_OK );
00183 }
00184 
00185 char* LSTRRCHR( const char* lpString, int bChar )
00186 {
00187     if( lpString != NULL )
00188     {
00189         const char*     lpBegin;
00190 
00191         lpBegin = lpString;
00192 
00193         while( *lpString != 0 )
00194         {
00195             lpString++;
00196         }
00197         while( 1 )
00198         {
00199             if( *lpString == bChar )
00200             {
00201                 return (char*)lpString;
00202             }
00203 
00204             if( lpString == lpBegin )
00205             {
00206                 break;
00207             }
00208 
00209             lpString--;
00210         }
00211     }
00212 
00213     return NULL;
00214 }
00215 
00216 char*
00217 MyErrorToString(HRESULT error)
00218 {
00219     switch(error) {
00220         case DD_OK:
00221             /* Also includes D3D_OK and D3DRM_OK */
00222             return "No error.\0";
00223         case DDERR_ALREADYINITIALIZED:
00224             return "This object is already initialized.\0";
00225         case DDERR_BLTFASTCANTCLIP:
00226             return "Return if a clipper object is attached to the source surface passed into a BltFast call.\0";
00227         case DDERR_CANNOTATTACHSURFACE:
00228             return "This surface can not be attached to the requested surface.\0";
00229         case DDERR_CANNOTDETACHSURFACE:
00230             return "This surface can not be detached from the requested surface.\0";
00231         case DDERR_CANTCREATEDC:
00232             return "Windows can not create any more DCs.\0";
00233         case DDERR_CANTDUPLICATE:
00234             return "Can't duplicate primary & 3D surfaces, or surfaces that are implicitly created.\0";
00235         case DDERR_CLIPPERISUSINGHWND:
00236             return "An attempt was made to set a cliplist for a clipper object that is already monitoring an hwnd.\0";
00237         case DDERR_COLORKEYNOTSET:
00238             return "No src color key specified for this operation.\0";
00239         case DDERR_CURRENTLYNOTAVAIL:
00240             return "Support is currently not available.\0";
00241         case DDERR_DIRECTDRAWALREADYCREATED:
00242             return "A DirectDraw object representing this driver has already been created for this process.\0";
00243         case DDERR_EXCEPTION:
00244             return "An exception was encountered while performing the requested operation.\0";
00245         case DDERR_EXCLUSIVEMODEALREADYSET:
00246             return "An attempt was made to set the cooperative level when it was already set to exclusive.\0";
00247         case DDERR_GENERIC:
00248             return "Generic failure.\0";
00249         case DDERR_HEIGHTALIGN:
00250             return "Height of rectangle provided is not a multiple of reqd alignment.\0";
00251         case DDERR_HWNDALREADYSET:
00252             return "The CooperativeLevel HWND has already been set. It can not be reset while the process has surfaces or palettes created.\0";
00253         case DDERR_HWNDSUBCLASSED:
00254             return "HWND used by DirectDraw CooperativeLevel has been subclassed, this prevents DirectDraw from restoring state.\0";
00255         case DDERR_IMPLICITLYCREATED:
00256             return "This surface can not be restored because it is an implicitly created surface.\0";
00257         case DDERR_INCOMPATIBLEPRIMARY:
00258             return "Unable to match primary surface creation request with existing primary surface.\0";
00259         case DDERR_INVALIDCAPS:
00260             return "One or more of the caps bits passed to the callback are incorrect.\0";
00261         case DDERR_INVALIDCLIPLIST:
00262             return "DirectDraw does not support the provided cliplist.\0";
00263         case DDERR_INVALIDDIRECTDRAWGUID:
00264             return "The GUID passed to DirectDrawCreate is not a valid DirectDraw driver identifier.\0";
00265         case DDERR_INVALIDMODE:
00266             return "DirectDraw does not support the requested mode.\0";
00267         case DDERR_INVALIDOBJECT:
00268             return "DirectDraw received a pointer that was an invalid DIRECTDRAW object.\0";
00269         case DDERR_INVALIDPARAMS:
00270             return "One or more of the parameters passed to the function are incorrect.\0";
00271         case DDERR_INVALIDPIXELFORMAT:
00272             return "The pixel format was invalid as specified.\0";
00273         case DDERR_INVALIDPOSITION:
00274             return "Returned when the position of the overlay on the destination is no longer legal for that destination.\0";
00275         case DDERR_INVALIDRECT:
00276             return "Rectangle provided was invalid.\0";
00277         case DDERR_LOCKEDSURFACES:
00278             return "Operation could not be carried out because one or more surfaces are locked.\0";
00279         case DDERR_NO3D:
00280             return "There is no 3D present.\0";
00281         case DDERR_NOALPHAHW:
00282             return "Operation could not be carried out because there is no alpha accleration hardware present or available.\0";
00283         case DDERR_NOBLTHW:
00284             return "No blitter hardware present.\0";
00285         case DDERR_NOCLIPLIST:
00286             return "No cliplist available.\0";
00287         case DDERR_NOCLIPPERATTACHED:
00288             return "No clipper object attached to surface object.\0";
00289         case DDERR_NOCOLORCONVHW:
00290             return "Operation could not be carried out because there is no color conversion hardware present or available.\0";
00291         case DDERR_NOCOLORKEY:
00292             return "Surface doesn't currently have a color key\0";
00293         case DDERR_NOCOLORKEYHW:
00294             return "Operation could not be carried out because there is no hardware support of the destination color key.\0";
00295         case DDERR_NOCOOPERATIVELEVELSET:
00296             return "Create function called without DirectDraw object method SetCooperativeLevel being called.\0";
00297         case DDERR_NODC:
00298             return "No DC was ever created for this surface.\0";
00299         case DDERR_NODDROPSHW:
00300             return "No DirectDraw ROP hardware.\0";
00301         case DDERR_NODIRECTDRAWHW:
00302             return "A hardware-only DirectDraw object creation was attempted but the driver did not support any hardware.\0";
00303         case DDERR_NOEMULATION:
00304             return "Software emulation not available.\0";
00305         case DDERR_NOEXCLUSIVEMODE:
00306             return "Operation requires the application to have exclusive mode but the application does not have exclusive mode.\0";
00307         case DDERR_NOFLIPHW:
00308             return "Flipping visible surfaces is not supported.\0";
00309         case DDERR_NOGDI:
00310             return "There is no GDI present.\0";
00311         case DDERR_NOHWND:
00312             return "Clipper notification requires an HWND or no HWND has previously been set as the CooperativeLevel HWND.\0";
00313         case DDERR_NOMIRRORHW:
00314             return "Operation could not be carried out because there is no hardware present or available.\0";
00315         case DDERR_NOOVERLAYDEST:
00316             return "Returned when GetOverlayPosition is called on an overlay that UpdateOverlay has never been called on to establish a destination.\0";
00317         case DDERR_NOOVERLAYHW:
00318             return "Operation could not be carried out because there is no overlay hardware present or available.\0";
00319         case DDERR_NOPALETTEATTACHED:
00320             return "No palette object attached to this surface.\0";
00321         case DDERR_NOPALETTEHW:
00322             return "No hardware support for 16 or 256 color palettes.\0";
00323         case DDERR_NORASTEROPHW:
00324             return "Operation could not be carried out because there is no appropriate raster op hardware present or available.\0";
00325         case DDERR_NOROTATIONHW:
00326             return "Operation could not be carried out because there is no rotation hardware present or available.\0";
00327         case DDERR_NOSTRETCHHW:
00328             return "Operation could not be carried out because there is no hardware support for stretching.\0";
00329         case DDERR_NOT4BITCOLOR:
00330             return "DirectDrawSurface is not in 4 bit color palette and the requested operation requires 4 bit color palette.\0";
00331         case DDERR_NOT4BITCOLORINDEX:
00332             return "DirectDrawSurface is not in 4 bit color index palette and the requested operation requires 4 bit color index palette.\0";
00333         case DDERR_NOT8BITCOLOR:
00334             return "DirectDrawSurface is not in 8 bit color mode and the requested operation requires 8 bit color.\0";
00335         case DDERR_NOTAOVERLAYSURFACE:
00336             return "Returned when an overlay member is called for a non-overlay surface.\0";
00337         case DDERR_NOTEXTUREHW:
00338             return "Operation could not be carried out because there is no texture mapping hardware present or available.\0";
00339         case DDERR_NOTFLIPPABLE:
00340             return "An attempt has been made to flip a surface that is not flippable.\0";
00341         case DDERR_NOTFOUND:
00342             return "Requested item was not found.\0";
00343         case DDERR_NOTLOCKED:
00344             return "Surface was not locked.  An attempt to unlock a surface that was not locked at all, or by this process, has been attempted.\0";
00345         case DDERR_NOTPALETTIZED:
00346             return "The surface being used is not a palette-based surface.\0";
00347         case DDERR_NOVSYNCHW:
00348             return "Operation could not be carried out because there is no hardware support for vertical blank synchronized operations.\0";
00349         case DDERR_NOZBUFFERHW:
00350             return "Operation could not be carried out because there is no hardware support for zbuffer blitting.\0";
00351         case DDERR_NOZOVERLAYHW:
00352             return "Overlay surfaces could not be z layered based on their BltOrder because the hardware does not support z layering of overlays.\0";
00353         case DDERR_OUTOFCAPS:
00354             return "The hardware needed for the requested operation has already been allocated.\0";
00355         case DDERR_OUTOFMEMORY:
00356             return "DirectDraw does not have enough memory to perform the operation.\0";
00357         case DDERR_OUTOFVIDEOMEMORY:
00358             return "DirectDraw does not have enough memory to perform the operation.\0";
00359         case DDERR_OVERLAYCANTCLIP:
00360             return "The hardware does not support clipped overlays.\0";
00361         case DDERR_OVERLAYCOLORKEYONLYONEACTIVE:
00362             return "Can only have ony color key active at one time for overlays.\0";
00363         case DDERR_OVERLAYNOTVISIBLE:
00364             return "Returned when GetOverlayPosition is called on a hidden overlay.\0";
00365         case DDERR_PALETTEBUSY:
00366             return "Access to this palette is being refused because the palette is already locked by another thread.\0";
00367         case DDERR_PRIMARYSURFACEALREADYEXISTS:
00368             return "This process already has created a primary surface.\0";
00369         case DDERR_REGIONTOOSMALL:
00370             return "Region passed to Clipper::GetClipList is too small.\0";
00371         case DDERR_SURFACEALREADYATTACHED:
00372             return "This surface is already attached to the surface it is being attached to.\0";
00373         case DDERR_SURFACEALREADYDEPENDENT:
00374             return "This surface is already a dependency of the surface it is being made a dependency of.\0";
00375         case DDERR_SURFACEBUSY:
00376             return "Access to this surface is being refused because the surface is already locked by another thread.\0";
00377         case DDERR_SURFACEISOBSCURED:
00378             return "Access to surface refused because the surface is obscured.\0";
00379         case DDERR_SURFACELOST:
00380             return "Access to this surface is being refused because the surface memory is gone. The DirectDrawSurface object representing this surface should have Restore called on it.\0";
00381         case DDERR_SURFACENOTATTACHED:
00382             return "The requested surface is not attached.\0";
00383         case DDERR_TOOBIGHEIGHT:
00384             return "Height requested by DirectDraw is too large.\0";
00385         case DDERR_TOOBIGSIZE:
00386             return "Size requested by DirectDraw is too large, but the individual height and width are OK.\0";
00387         case DDERR_TOOBIGWIDTH:
00388             return "Width requested by DirectDraw is too large.\0";
00389         case DDERR_UNSUPPORTED:
00390             return "Action not supported.\0";
00391         case DDERR_UNSUPPORTEDFORMAT:
00392             return "FOURCC format requested is unsupported by DirectDraw.\0";
00393         case DDERR_UNSUPPORTEDMASK:
00394             return "Bitmask in the pixel format requested is unsupported by DirectDraw.\0";
00395         case DDERR_VERTICALBLANKINPROGRESS:
00396             return "Vertical blank is in progress.\0";
00397         case DDERR_WASSTILLDRAWING:
00398             return "Informs DirectDraw that the previous Blt which is transfering information to or from this Surface is incomplete.\0";
00399         case DDERR_WRONGMODE:
00400             return "This surface can not be restored because it was created in a different mode.\0";
00401         case DDERR_XALIGN:
00402             return "Rectangle provided was not horizontally aligned on required boundary.\0";
00403         case D3DERR_BADMAJORVERSION:
00404             return "D3DERR_BADMAJORVERSION\0";
00405         case D3DERR_BADMINORVERSION:
00406             return "D3DERR_BADMINORVERSION\0";
00407         case D3DERR_EXECUTE_LOCKED:
00408             return "D3DERR_EXECUTE_LOCKED\0";
00409         case D3DERR_EXECUTE_NOT_LOCKED:
00410             return "D3DERR_EXECUTE_NOT_LOCKED\0";
00411         case D3DERR_EXECUTE_CREATE_FAILED:
00412             return "D3DERR_EXECUTE_CREATE_FAILED\0";
00413         case D3DERR_EXECUTE_DESTROY_FAILED:
00414             return "D3DERR_EXECUTE_DESTROY_FAILED\0";
00415         case D3DERR_EXECUTE_LOCK_FAILED:
00416             return "D3DERR_EXECUTE_LOCK_FAILED\0";
00417         case D3DERR_EXECUTE_UNLOCK_FAILED:
00418             return "D3DERR_EXECUTE_UNLOCK_FAILED\0";
00419         case D3DERR_EXECUTE_FAILED:
00420             return "D3DERR_EXECUTE_FAILED\0";
00421         case D3DERR_EXECUTE_CLIPPED_FAILED:
00422             return "D3DERR_EXECUTE_CLIPPED_FAILED\0";
00423         case D3DERR_TEXTURE_NO_SUPPORT:
00424             return "D3DERR_TEXTURE_NO_SUPPORT\0";
00425         case D3DERR_TEXTURE_NOT_LOCKED:
00426             return "D3DERR_TEXTURE_NOT_LOCKED\0";
00427         case D3DERR_TEXTURE_LOCKED:
00428             return "D3DERR_TEXTURELOCKED\0";
00429         case D3DERR_TEXTURE_CREATE_FAILED:
00430             return "D3DERR_TEXTURE_CREATE_FAILED\0";
00431         case D3DERR_TEXTURE_DESTROY_FAILED:
00432             return "D3DERR_TEXTURE_DESTROY_FAILED\0";
00433         case D3DERR_TEXTURE_LOCK_FAILED:
00434             return "D3DERR_TEXTURE_LOCK_FAILED\0";
00435         case D3DERR_TEXTURE_UNLOCK_FAILED:
00436             return "D3DERR_TEXTURE_UNLOCK_FAILED\0";
00437         case D3DERR_TEXTURE_LOAD_FAILED:
00438             return "D3DERR_TEXTURE_LOAD_FAILED\0";
00439         case D3DERR_MATRIX_CREATE_FAILED:
00440             return "D3DERR_MATRIX_CREATE_FAILED\0";
00441         case D3DERR_MATRIX_DESTROY_FAILED:
00442             return "D3DERR_MATRIX_DESTROY_FAILED\0";
00443         case D3DERR_MATRIX_SETDATA_FAILED:
00444             return "D3DERR_MATRIX_SETDATA_FAILED\0";
00445         case D3DERR_SETVIEWPORTDATA_FAILED:
00446             return "D3DERR_SETVIEWPORTDATA_FAILED\0";
00447         case D3DERR_MATERIAL_CREATE_FAILED:
00448             return "D3DERR_MATERIAL_CREATE_FAILED\0";
00449         case D3DERR_MATERIAL_DESTROY_FAILED:
00450             return "D3DERR_MATERIAL_DESTROY_FAILED\0";
00451         case D3DERR_MATERIAL_SETDATA_FAILED:
00452             return "D3DERR_MATERIAL_SETDATA_FAILED\0";
00453         case D3DERR_LIGHT_SET_FAILED:
00454             return "D3DERR_LIGHT_SET_FAILED\0";
00455         case D3DRMERR_BADOBJECT:
00456             return "D3DRMERR_BADOBJECT\0";
00457         case D3DRMERR_BADTYPE:
00458             return "D3DRMERR_BADTYPE\0";
00459         case D3DRMERR_BADALLOC:
00460             return "D3DRMERR_BADALLOC\0";
00461         case D3DRMERR_FACEUSED:
00462             return "D3DRMERR_FACEUSED\0";
00463         case D3DRMERR_NOTFOUND:
00464             return "D3DRMERR_NOTFOUND\0";
00465         case D3DRMERR_NOTDONEYET:
00466             return "D3DRMERR_NOTDONEYET\0";
00467         case D3DRMERR_FILENOTFOUND:
00468             return "The file was not found.\0";
00469         case D3DRMERR_BADFILE:
00470             return "D3DRMERR_BADFILE\0";
00471         case D3DRMERR_BADDEVICE:
00472             return "D3DRMERR_BADDEVICE\0";
00473         case D3DRMERR_BADVALUE:
00474             return "D3DRMERR_BADVALUE\0";
00475         case D3DRMERR_BADMAJORVERSION:
00476             return "D3DRMERR_BADMAJORVERSION\0";
00477         case D3DRMERR_BADMINORVERSION:
00478             return "D3DRMERR_BADMINORVERSION\0";
00479         case D3DRMERR_UNABLETOEXECUTE:
00480             return "D3DRMERR_UNABLETOEXECUTE\0";
00481         default:
00482             return "Unrecognized error value.\0";
00483     }
00484 }
00485 

Generated on Sun Apr 27 14:20:10 2014 for OpenFX by  doxygen 1.5.6