home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / VIEWER / VIEWER.CPP < prev    next >
C/C++ Source or Header  |  1996-08-28  |  59KB  |  1,704 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995, 1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File: viewer.cpp
  6.  *
  7.  ***************************************************************************/
  8.  
  9. #define INITGUID
  10. #include <d3drmwin.h>
  11. #include "viewer.h"
  12.  
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <malloc.h>
  17. #include <math.h>
  18. #include <direct.h>
  19. #include "sel.h"
  20.  
  21. static char ViewerClass[32] = "ViewerClass";
  22. static BOOL Render(void);
  23. HRESULT loadTextures(char *name, void *arg, LPDIRECT3DRMTEXTURE *tex);
  24.  
  25. typedef struct _AppInfo
  26. {
  27.     LPDIRECT3DRMFRAME scene, camera;
  28.     LPDIRECT3DRMDEVICE dev;
  29.     LPDIRECT3DRMVIEWPORT view;
  30.     D3DRMCOLORMODEL model;
  31.     BOOL bMinimized;
  32. } AppInfo;
  33.  
  34. AppInfo *active_window = NULL;
  35.  
  36. LPDIRECT3DRM lpD3DRM = 0;
  37. LPDIRECTDRAWCLIPPER lpDDClipper = 0;
  38. BOOL bQuit = FALSE;
  39.  
  40. static BOOL FirstInstance(HINSTANCE);
  41. static HWND AnyInstance(HINSTANCE this_inst, int cmdshow);
  42. long FAR PASCAL WindowProc(HWND, UINT, WPARAM, LPARAM);
  43. static BOOL CreateDevice(HWND, AppInfo*);
  44. static void Idle();
  45. char* MyErrorToString(HRESULT error);
  46.  
  47. /* Msg
  48.  * Message output for error notification.
  49.  */
  50. void __cdecl
  51. Msg( LPSTR fmt, ... )
  52. {
  53.     char buff[256];
  54.  
  55.     wvsprintf(buff, fmt, (char *)(&fmt+1));
  56.     lstrcat(buff, "\r\n");
  57.     MessageBox( NULL, buff, "Viewer Message", MB_OK );
  58. }
  59.  
  60.  
  61. char* LSTRRCHR( const char* lpString, int bChar )
  62. {
  63.     if( lpString != NULL )
  64.     {
  65.         const char*     lpBegin;
  66.  
  67.         lpBegin = lpString;
  68.  
  69.         while( *lpString != 0 )
  70.         {
  71.             lpString++;
  72.         }
  73.  
  74.         while( 1 )
  75.         {
  76.             if( *lpString == bChar )
  77.             {
  78.                 return (char*)lpString;
  79.             }
  80.             
  81.             if( lpString == lpBegin )
  82.             {
  83.                 break;
  84.             }
  85.  
  86.             lpString--;
  87.         }
  88.     }
  89.  
  90.     return NULL;
  91. } /* LSTRRCHR */
  92.  
  93. /*
  94.  * Initialization, message loop
  95.  */
  96. int PASCAL WinMain
  97.     (HINSTANCE this_inst, HINSTANCE prev_inst, LPSTR cmdline, int cmdshow)
  98. {
  99.     MSG         msg;
  100.     int         idle;
  101.     int         done = FALSE;
  102.     HACCEL      accel;
  103.     HRESULT     rval;
  104.     HWND        hwnd;
  105.  
  106.     prev_inst = prev_inst;
  107.     cmdline = cmdline;
  108.  
  109.     rval = Direct3DRMCreate(&lpD3DRM);
  110.     if (rval != D3DRM_OK) {
  111.         Msg("Failed to create Direct3DRM.\n%s", MyErrorToString(rval));
  112.         return 1;
  113.     }
  114.     if (!prev_inst)
  115.         if (!FirstInstance(this_inst))
  116.             return 1;
  117.  
  118.     if (!(hwnd = AnyInstance(this_inst, cmdshow)))
  119.         return 1;
  120.     accel = LoadAccelerators(this_inst, "ViewerAccel");
  121.  
  122.     while (!done) {
  123.         idle = TRUE;
  124.         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  125.             idle = FALSE;
  126.             if (msg.message == WM_QUIT || bQuit) {
  127.                 done = TRUE;
  128.                 break;
  129.             }
  130.             if (!TranslateAccelerator(msg.hwnd, accel, &msg)) {
  131.                 TranslateMessage(&msg);
  132.                 DispatchMessage(&msg);
  133.             }
  134.         }
  135.         if (done) {
  136.             break;
  137.         } else if (!active_window->bMinimized && !bQuit) {
  138.             if (idle) Idle();
  139.             if (!Render()) {
  140.                 Msg("Rendering failed.\n");
  141.                 done = TRUE;
  142.                 break;
  143.             }
  144.         } else {
  145.             WaitMessage();
  146.         }
  147.     }
  148.     RELEASE(active_window->scene);
  149.     RELEASE(active_window->camera);
  150.     RELEASE(active_window->view);
  151.     RELEASE(active_window->dev);
  152.     ClearClipboard();
  153.     RELEASE(lpD3DRM);
  154.     RELEASE(lpDDClipper);
  155.     DestroyWindow(hwnd);
  156.     return msg.wParam;
  157. }
  158.  
  159. /*
  160.  * Register window class for the application, and do any other
  161.  * application initialization
  162.  */
  163. static BOOL FirstInstance(HINSTANCE this_inst)
  164. {
  165.     WNDCLASS    wc;
  166.     BOOL        rc;
  167.  
  168.     /*
  169.      * set up and register window class
  170.      */
  171.     wc.style = CS_HREDRAW | CS_VREDRAW;
  172.     wc.lpfnWndProc = WindowProc;
  173.     wc.cbClsExtra = 0;
  174.     wc.cbWndExtra = sizeof(DWORD);
  175.     wc.hInstance = this_inst;
  176.     wc.hIcon = LoadIcon(this_inst, "ViewerIcon");
  177.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  178.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  179.     wc.lpszMenuName = "ViewerMenu";
  180.     wc.lpszClassName = ViewerClass;
  181.     rc = RegisterClass(&wc);
  182.  
  183.     return rc;
  184. }
  185.  
  186. /*
  187.  * Do work required for every instance of the application:
  188.  * create the window, initialize data
  189.  */
  190. static HWND AnyInstance(HINSTANCE this_inst, int cmdshow)
  191. {
  192.     HWND win;
  193.     AppInfo *info;
  194.  
  195.     /*
  196.      * create main window
  197.      */
  198.     win =
  199.         CreateWindow
  200.         (   ViewerClass,                /* class */
  201.             "Direct3D Object Viewer",/* caption */
  202.             WS_OVERLAPPEDWINDOW,        /* style */
  203.             CW_USEDEFAULT,              /* init. x pos */
  204.             CW_USEDEFAULT,              /* init. y pos */
  205.             400,                        /* init. x size */
  206.             400,                        /* init. y size */
  207.             NULL,                       /* parent window */
  208.             NULL,                       /* menu handle */
  209.             this_inst,                  /* program handle */
  210.             NULL                        /* create parms */
  211.         );
  212.  
  213.  
  214.     if (!win) return FALSE;
  215.  
  216.     /*
  217.      * Create a clipper for this window
  218.      */
  219.     if (FAILED(DirectDrawCreateClipper(0, &lpDDClipper, NULL))) {
  220.         return FALSE;
  221.     }
  222.     if (FAILED(lpDDClipper->SetHWnd(0, win))) {
  223.         RELEASE(lpDDClipper);
  224.         return FALSE;
  225.     }
  226.     /*
  227.      * set up data associated with this window
  228.      */
  229.     info = (AppInfo*) malloc(sizeof(AppInfo));
  230.     SetWindowLong(win, 0, (long) info);
  231.     info->model = D3DCOLOR_MONO;
  232.     if (!CreateDevice(win, info)) {
  233.         return FALSE;
  234.     }
  235.  
  236.     /*
  237.      * display window
  238.      */
  239.     ShowWindow(win, cmdshow);
  240.     UpdateWindow(win);
  241.  
  242.     return win;
  243. }
  244.  
  245. /*
  246.  * Processes messages for the about dialog.
  247.  */
  248. BOOL FAR PASCAL AboutDlgProc
  249.     (HWND win, unsigned msg, WORD wparam, LONG lparam)
  250. {
  251.     lparam = lparam;
  252.  
  253.     switch (msg)
  254.     {
  255.     case WM_INITDIALOG:
  256.         return TRUE;
  257.  
  258.     case WM_COMMAND:
  259.         if (wparam == IDOK)
  260.         {   EndDialog(win, TRUE);
  261.             return TRUE;
  262.         }
  263.         break;
  264.     }
  265.     return FALSE;
  266. }
  267.  
  268. /*
  269.  * Create a simple scene.
  270.  */
  271. static BOOL CreateScene(AppInfo* info)
  272. {
  273.     LPDIRECT3DRMFRAME frame = NULL;
  274.     LPDIRECT3DRMFRAME light = NULL;
  275.     LPDIRECT3DRMMESHBUILDER builder = NULL;
  276.     LPDIRECT3DRMLIGHT light1 = NULL;
  277.     LPDIRECT3DRMLIGHT light2 = NULL;
  278.     LPDIRECT3DRMMATERIAL mat = NULL;
  279.     HRESULT rval;
  280.  
  281.     if (FAILED(lpD3DRM->CreateFrame(NULL, &info->scene)))
  282.         goto generic_error;
  283.     if (FAILED(lpD3DRM->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL, D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0), &light1)))
  284.         goto generic_error;
  285.     if (FAILED(lpD3DRM->CreateLightRGB(D3DRMLIGHT_AMBIENT, D3DVAL(0.1), D3DVAL(0.1), D3DVAL(0.1), &light2)))
  286.         goto generic_error;
  287.  
  288.     if (FAILED(lpD3DRM->CreateFrame(info->scene, &light)))
  289.         goto generic_error;
  290.     if (FAILED(light->SetPosition(info->scene, D3DVAL(2.0), D3DVAL(2.0), D3DVAL(5.0))))
  291.         goto generic_error;
  292.     if (FAILED(light->SetOrientation(info->scene, D3DVAL(-1.0), D3DVAL(-1.0), D3DVAL(1.0), D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0))))
  293.         goto generic_error;
  294.     if (FAILED(light->AddLight(light1)))
  295.         goto generic_error;
  296.     RELEASE(light1);
  297.     if (FAILED(info->scene->AddLight(light2)))
  298.         goto generic_error;
  299.     RELEASE(light2);
  300.     if (FAILED(lpD3DRM->CreateMeshBuilder(&builder)))
  301.         goto generic_error;
  302.     rval = builder->Load("camera.x", NULL, D3DRMLOAD_FROMFILE, NULL, NULL);
  303.     if (rval != D3DRM_OK) {
  304.         Msg("Failed to load camera.x.\n%s", MyErrorToString(rval));
  305.         goto ret_with_error;
  306.     }
  307.     if (FAILED(builder->SetQuality(D3DRMRENDER_UNLITFLAT)))
  308.         goto generic_error;
  309.     if (FAILED(light->AddVisual(builder)))
  310.         goto generic_error;
  311.     RELEASE(builder);
  312.     RELEASE(light);
  313.  
  314.     if (FAILED(lpD3DRM->CreateFrame(info->scene, &frame)))
  315.         goto generic_error;
  316.     if (FAILED(frame->SetRotation(info->scene, D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0), D3DVAL(-0.02))))
  317.         goto generic_error;
  318.     if (FAILED(frame->SetPosition(info->scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(10.0))))
  319.         goto generic_error;
  320.     if (FAILED(lpD3DRM->CreateMeshBuilder(&builder)))
  321.         goto generic_error;
  322.     rval = builder->Load("mslogo.x", NULL, D3DRMLOAD_FROMFILE,
  323.         loadTextures, NULL);
  324.     if (rval != D3DRM_OK) {
  325.         Msg("Failed to load mslogo.x.\n%s", MyErrorToString(rval));
  326.         goto ret_with_error;
  327.     }
  328.     if (FAILED(builder->SetColorRGB(D3DVAL(0.8), D3DVAL(0.8), D3DVAL(0.8))))
  329.         goto generic_error;
  330.     if (FAILED(lpD3DRM->CreateMaterial(D3DVAL(10.0), &mat)))
  331.         goto generic_error;
  332.     if (FAILED(builder->SetMaterial(mat)))
  333.         goto generic_error;
  334.  
  335.     RELEASE(mat);
  336.     if (FAILED(frame->AddVisual(builder)))
  337.         goto generic_error;
  338.     RELEASE(builder);
  339.     RELEASE(frame);
  340.  
  341.     if (FAILED(lpD3DRM->CreateFrame(info->scene, &info->camera)))
  342.         goto generic_error;
  343.     if (FAILED(info->camera->SetPosition(info->scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0))))
  344.         goto generic_error;
  345.     return TRUE;
  346. generic_error:
  347.     Msg("A failure occurred while creating the scene.\n");
  348. ret_with_error:
  349.     RELEASE(frame);
  350.     RELEASE(light);
  351.     RELEASE(builder);
  352.     RELEASE(light1);
  353.     RELEASE(light2);
  354.     RELEASE(mat);
  355.     return FALSE;
  356. }
  357.  
  358. static DWORD bppToddbd(int bpp)
  359. {
  360.     switch(bpp) {
  361.     case 1:
  362.         return DDBD_1;
  363.     case 2:
  364.         return DDBD_2;
  365.     case 4:
  366.         return DDBD_4;
  367.     case 8:
  368.         return DDBD_8;
  369.     case 16:
  370.         return DDBD_16;
  371.     case 24:
  372.         return DDBD_24;
  373.     case 32:
  374.         return DDBD_32;
  375.     }
  376.     return 0;
  377. }
  378.  
  379.  
  380. /*
  381.  * Find a device, preferably hardware, for a particular color model.
  382.  */
  383. LPGUID
  384. FindDevice(D3DCOLORMODEL cm)
  385. {
  386.     LPDIRECTDRAW lpDD;
  387.     LPDIRECT3D lpD3D;
  388.     D3DFINDDEVICESEARCH search;
  389.     static D3DFINDDEVICERESULT result;
  390.     HRESULT error;
  391.     HDC hdc;
  392.     int bpp;
  393.  
  394.     hdc = GetDC(NULL);
  395.     bpp = GetDeviceCaps(hdc, BITSPIXEL);
  396.     ReleaseDC(NULL, hdc);
  397.  
  398.     if (DirectDrawCreate(NULL, &lpDD, NULL))
  399.         return NULL;
  400.  
  401.     if (lpDD->QueryInterface(IID_IDirect3D, (void**) &lpD3D)) {
  402.         lpDD->Release();
  403.         return NULL;
  404.     }
  405.     
  406.     memset(&search, 0, sizeof search);
  407.     search.dwSize = sizeof search;
  408.     search.dwFlags = D3DFDS_COLORMODEL;
  409.     search.dcmColorModel = (cm == D3DCOLOR_MONO) ? D3DCOLOR_MONO : D3DCOLOR_RGB;
  410.  
  411.     memset(&result, 0, sizeof result);
  412.     result.dwSize = sizeof result;
  413.  
  414.     error = lpD3D->FindDevice(&search, &result);
  415.  
  416.     if (error == DD_OK) {
  417.         /*
  418.          * If the device found is hardware but cannot support the current
  419.          * bit depth, then fall back to software rendering.
  420.          */
  421.         if (result.ddHwDesc.dwFlags
  422.             && !(result.ddHwDesc.dwDeviceRenderBitDepth & bppToddbd(bpp))) {
  423.             
  424.             search.dwFlags |= D3DFDS_HARDWARE;
  425.             search.bHardware = FALSE;
  426.             memset(&result, 0, sizeof result);
  427.             result.dwSize = sizeof result;
  428.             error = lpD3D->FindDevice(&search, &result);
  429.         }
  430.     }
  431.  
  432.     lpD3D->Release();
  433.     lpDD->Release();
  434.  
  435.     if (error)
  436.         return NULL;
  437.     else
  438.         return &result.guid;
  439. }
  440.  
  441. /*
  442.  * Create the device and viewport.
  443.  */
  444. static BOOL CreateDevice(HWND win, AppInfo* info)
  445. {
  446.     RECT r;
  447.     int bpp;
  448.     HDC hdc;
  449.  
  450.     GetClientRect(win, &r);
  451.     if (FAILED(lpD3DRM->CreateDeviceFromClipper(lpDDClipper, FindDevice(info->model),
  452.                                 r.right, r.bottom, &info->dev)))
  453.                                 goto generic_error;
  454.     hdc = GetDC(win);
  455.     bpp = GetDeviceCaps(hdc, BITSPIXEL);
  456.     ReleaseDC(win, hdc);
  457.     switch (bpp)
  458.     {
  459.     case 1:
  460.         if (FAILED(info->dev->SetShades(4)))
  461.             goto generic_error;
  462.         if (FAILED(lpD3DRM->SetDefaultTextureShades(4)))
  463.             goto generic_error;
  464.         break;
  465.     case 16:
  466.         if (FAILED(info->dev->SetShades(32)))
  467.             goto generic_error;
  468.         if (FAILED(lpD3DRM->SetDefaultTextureColors(64)))
  469.             goto generic_error;
  470.         if (FAILED(lpD3DRM->SetDefaultTextureShades(32)))
  471.             goto generic_error;
  472.         if (FAILED(info->dev->SetDither(FALSE)))
  473.             goto generic_error;
  474.         break;
  475.     case 24:
  476.     case 32:
  477.         if (FAILED(info->dev->SetShades(256)))
  478.             goto generic_error;
  479.         if (FAILED(lpD3DRM->SetDefaultTextureColors(64)))
  480.             goto generic_error;
  481.         if (FAILED(lpD3DRM->SetDefaultTextureShades(256)))
  482.             goto generic_error;
  483.         if (FAILED(info->dev->SetDither(FALSE)))
  484.             goto generic_error;
  485.         break;
  486.     default:
  487.         if (FAILED(info->dev->SetDither(FALSE)))
  488.             goto generic_error;
  489.     }
  490.     if (!CreateScene(info))
  491.         goto ret_with_error;
  492.     if (FAILED(lpD3DRM->CreateViewport(info->dev, info->camera, 0, 0,
  493.         info->dev->GetWidth(),
  494.         info->dev->GetHeight(), &info->view)))
  495.         goto generic_error;
  496.     if (FAILED(info->view->SetBack(D3DVAL(5000.0))))
  497.         goto generic_error;
  498.  
  499.     return TRUE;
  500. generic_error:
  501.     Msg("An error occurred while creating the device.\n");
  502. ret_with_error:
  503.     return FALSE;
  504. }
  505.  
  506. /*
  507.  * Regenerate the device if the color model changes or the window size
  508.  * changes.
  509.  */
  510. static BOOL RebuildDevice(HWND win, AppInfo* info, int width, int height)
  511. {
  512.     HRESULT rval;
  513.     int old_dither = info->dev->GetDither();
  514.     D3DRMRENDERQUALITY old_quality = info->dev->GetQuality();
  515.     int old_shades = info->dev->GetShades();
  516.  
  517.     RELEASE(info->view);
  518.     RELEASE(info->dev);
  519.     rval = lpD3DRM->CreateDeviceFromClipper(lpDDClipper, FindDevice(info->model),
  520.                                 width, height, &info->dev);
  521.     if (rval != D3DRM_OK) {
  522.         Msg("Creating a device from HWND failed while rebuilding device.\n%s", MyErrorToString(rval));
  523.         return FALSE;
  524.     }
  525.  
  526.     if (FAILED(info->dev->SetDither(old_dither)))
  527.         goto generic_error;
  528.     if (FAILED(info->dev->SetQuality(old_quality)))
  529.         goto generic_error;
  530.     if (FAILED(info->dev->SetShades(old_shades)))
  531.         goto generic_error;
  532.     width = info->dev->GetWidth();
  533.     height = info->dev->GetHeight();
  534.     if (FAILED(lpD3DRM->CreateViewport(info->dev, info->camera,
  535.                           0, 0, width, height, &info->view)))
  536.                           goto generic_error;
  537.     if (FAILED(info->view->SetBack(D3DVAL(400.0))))
  538.         goto generic_error;
  539.     return TRUE;
  540. generic_error:
  541.     Msg("A failure occurred while rebuilding the device.\n");
  542.     return FALSE;
  543. }
  544.  
  545. /*
  546.  * Resize the viewport and device when the window size changes.
  547.  */
  548. static BOOL ResizeViewport(HWND win, AppInfo* info, int width, int height)
  549. {
  550.     int view_width = info->view->GetWidth();
  551.     int view_height = info->view->GetHeight();
  552.     int dev_width = info->dev->GetWidth();
  553.     int dev_height = info->dev->GetHeight();
  554.  
  555.     if (view_width == width && view_height == height)
  556.         return TRUE;
  557.     
  558.     if (width <= dev_width && height <= dev_height) {
  559.         RELEASE(info->view);
  560.         if (FAILED(lpD3DRM->CreateViewport(info->dev, info->camera, 0, 0, width, height, &info->view)))
  561.             goto generic_error;
  562.         if (FAILED(info->view->SetBack(D3DVAL(400.0))))
  563.             goto generic_error;
  564.     }
  565.  
  566.     if (!RebuildDevice(win, info, width, height))
  567.         return FALSE;
  568.     return TRUE;
  569. generic_error:
  570.     Msg("A failure occurred while resizing the viewport.\n");
  571.     return FALSE;
  572. }
  573.  
  574. /*
  575.  * Place an object in front of the camera.
  576.  */
  577. static BOOL PlaceMesh(LPDIRECT3DRMMESHBUILDER mesh, AppInfo *info)
  578. {
  579.     LPDIRECT3DRMFRAME frame;
  580.  
  581.     if (FAILED(lpD3DRM->CreateFrame(info->scene, &frame)))
  582.         return FALSE;
  583.     if (FAILED(frame->AddVisual(mesh)))
  584.         return FALSE;
  585.     if (FAILED(frame->SetPosition(info->camera, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(15.0))))
  586.         return FALSE;
  587.     frame->Release();
  588.     return TRUE;
  589. }
  590.  
  591. static BOOL ScaleScene(LPDIRECT3DRMFRAME frame, AppInfo *info)
  592. {
  593.   /* Some trickery form the RL2 viewer to scale a scene down to
  594.      managable proportions */
  595.   LPDIRECT3DRMMESHBUILDER mbuilder;
  596.   D3DRMBOX box;
  597.   D3DVALUE maxDim;
  598.  
  599.   lpD3DRM->CreateMeshBuilder(&mbuilder);
  600.   mbuilder->AddFrame(frame);
  601.   mbuilder->GetBox(&box);
  602.   mbuilder->Release();
  603.  
  604.   maxDim = box.max.x - box.min.x;
  605.   if (box.max.y - box.min.y > maxDim) 
  606.     maxDim = box.max.y - box.min.y;
  607.   if (box.max.z - box.min.z > maxDim)
  608.     maxDim = box.max.z - box.min.z;
  609.  
  610.   frame->AddScale(D3DRMCOMBINE_BEFORE, D3DDivide(D3DVAL(8.0), maxDim),
  611.                   D3DDivide(D3DVAL(8.0), maxDim),
  612.                   D3DDivide(D3DVAL(8.0), maxDim));
  613.  
  614.   frame->SetPosition(info->scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(15.0));
  615.  
  616.   return TRUE;
  617. }
  618.                                                 
  619.  
  620. typedef struct {
  621.     LPDIRECT3DRMANIMATIONSET animset;
  622.     D3DVALUE time;
  623. } animationCallbackArgs;
  624.  
  625. static void CDECL animationCallback(LPDIRECT3DRMFRAME obj, void* arg, D3DVALUE delta)
  626. {
  627.     animationCallbackArgs* cb = (animationCallbackArgs *) arg;
  628.  
  629.     obj = obj;
  630.     cb->animset->SetTime(cb->time);
  631.     cb->time += delta;
  632. }
  633.  
  634. static BOOL setAnimationCallback(LPDIRECT3DRMFRAME frame, 
  635.                                  LPDIRECT3DRMANIMATIONSET animset)
  636. {
  637.     animationCallbackArgs *cb;
  638.  
  639.     cb = (animationCallbackArgs*)malloc(sizeof(animationCallbackArgs));
  640.     if (!cb)
  641.         return FALSE;
  642.  
  643.     cb->animset = animset;
  644.     cb->time = D3DVAL(0);
  645.     if (FAILED(frame->AddMoveCallback(animationCallback, (void *) cb)))
  646.         return FALSE;
  647.     return TRUE;
  648. }
  649.  
  650. static BOOL LoadAnimationSet(const char *filename, AppInfo *info)
  651. {
  652.   LPDIRECT3DRMANIMATIONSET lpAnimSet;
  653.   LPDIRECT3DRMFRAME lpFrame;
  654.  
  655.   /* Create a new parent frame for the animation, load it, and set up the
  656.      callback */
  657.   
  658.   if (FAILED(lpD3DRM->CreateFrame(info->scene, &lpFrame))) 
  659.     return FALSE;
  660.  
  661.   if (FAILED(lpD3DRM->CreateAnimationSet(&lpAnimSet))) 
  662.     return FALSE;
  663.  
  664.   if (FAILED(lpAnimSet->Load((LPVOID)filename, NULL, 
  665.                              D3DRMLOAD_FROMFILE, loadTextures,
  666.                              NULL, lpFrame)))
  667.     return FALSE;
  668.  
  669.   ScaleScene(lpFrame, info);
  670.  
  671.   setAnimationCallback(lpFrame, lpAnimSet);
  672.  
  673.   return TRUE;
  674. }
  675.   
  676. static BOOL LoadFrameHierarchy(const char *filename, AppInfo *info)
  677. {
  678.   LPDIRECT3DRMFRAME lpFrame;
  679.   
  680.   if (FAILED(lpD3DRM->CreateFrame(info->scene, &lpFrame)))
  681.     return FALSE;
  682.  
  683.   if (FAILED(lpFrame->Load((LPVOID)filename, NULL, D3DRMLOAD_FROMFILE,
  684.                            loadTextures, NULL)))
  685.     return FALSE;
  686.  
  687.   ScaleScene(lpFrame, info);
  688.  
  689.   return TRUE;
  690. }
  691.  
  692.  
  693. static int
  694.     left_drag = FALSE, right_drag = FALSE,
  695.     last_x, last_y;
  696.  
  697. /*
  698.  * Render the scene into the viewport.
  699.  */
  700. static BOOL Render() {
  701.     if (FAILED(active_window->scene->Move(D3DVAL(1.0))))
  702.         return FALSE;
  703.     if (FAILED(active_window->view->Clear()))
  704.         return FALSE;
  705.     if (FAILED(active_window->view->Render(active_window->scene)))
  706.         return FALSE;
  707.     if (FAILED(active_window->dev->Update()))
  708.         return FALSE;
  709.     return TRUE;
  710. }
  711.  
  712. static void Idle()
  713. {
  714.     LPDIRECT3DRMFRAME selected = SelectedFrame();
  715.  
  716.     if (active_window == NULL) return;
  717.     if (left_drag && selected)
  718.         selected->SetRotation(active_window->scene, D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0), D3DVAL(0.0));
  719.     if (right_drag && selected)
  720.         selected->SetVelocity(active_window->scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0), FALSE);
  721. }
  722.  
  723. static int FillModeToMenuItem(D3DRMFILLMODE fm)
  724. {
  725.     switch (fm) {
  726.     case D3DRMFILL_POINTS:
  727.         return 2;
  728.  
  729.     case D3DRMFILL_WIREFRAME:
  730.         return 3;
  731.  
  732.     case D3DRMFILL_SOLID:
  733.         return 4;
  734.     }
  735.     return -1;
  736. }
  737.  
  738. static int ShadeModeToMenuItem(D3DRMSHADEMODE sm)
  739. {
  740.     switch (sm) {
  741.     case D3DRMSHADE_FLAT:
  742.         return 6;
  743.  
  744.     case D3DRMSHADE_GOURAUD:
  745.         return 7;
  746.  
  747.     case D3DRMSHADE_PHONG:
  748.         return 8;
  749.     }
  750.     return -1;
  751. }
  752.  
  753. static BOOL ToggleLighting(HWND win, AppInfo* info)
  754. {
  755.     HMENU menu;
  756.     D3DRMRENDERQUALITY quality = info->dev->GetQuality();
  757.     D3DRMLIGHTMODE mode = (D3DRMLIGHTMODE)(quality & D3DRMLIGHT_MASK);
  758.     HRESULT rval;
  759.  
  760.     if (mode == D3DRMLIGHT_ON)
  761.         mode = D3DRMLIGHT_OFF;
  762.     else
  763.         mode = D3DRMLIGHT_ON;
  764.  
  765.     menu = GetMenu(win);
  766.     menu = GetSubMenu(menu, 2);
  767.     CheckMenuItem(menu, 0, (MF_BYPOSITION
  768.                             | (mode == D3DRMLIGHT_ON
  769.                                ? MF_CHECKED
  770.                                : MF_UNCHECKED)));
  771.  
  772.     quality = (quality & ~D3DRMLIGHT_MASK) | mode;
  773.     rval = info->dev->SetQuality(quality);
  774.     if (rval != D3DRM_OK) {
  775.         Msg("Setting the render quality while toggling lighting failed.\n%s", MyErrorToString(rval));
  776.         return FALSE;
  777.     }
  778.     return TRUE;
  779. }
  780.  
  781. static BOOL SetFillMode(HWND win, AppInfo* info, D3DRMFILLMODE fm)
  782. {
  783.     HMENU menu;
  784.     D3DRMRENDERQUALITY quality = info->dev->GetQuality();
  785.     D3DRMFILLMODE oldfm = (D3DRMFILLMODE)(quality & D3DRMFILL_MASK);
  786.     HRESULT rval;
  787.  
  788.     menu = GetMenu(win);
  789.     menu = GetSubMenu(menu, 2);
  790.     CheckMenuItem(menu, FillModeToMenuItem(oldfm),
  791.                   MF_BYPOSITION|MF_UNCHECKED);
  792.     CheckMenuItem(menu, FillModeToMenuItem(fm),
  793.                   MF_BYPOSITION|MF_CHECKED);
  794.  
  795.     quality = (quality & ~D3DRMFILL_MASK) | fm;
  796.     rval = info->dev->SetQuality(quality);
  797.     if (rval != D3DRM_OK) {
  798.         Msg("Setting the render quality while changing the fill mode failed.\n%s", MyErrorToString(rval));
  799.         return FALSE;
  800.     }
  801.     return TRUE;
  802. }
  803.  
  804. static BOOL SetShadeMode(HWND win, AppInfo* info, D3DRMSHADEMODE sm)
  805. {
  806.     HMENU menu;
  807.     D3DRMRENDERQUALITY quality = info->dev->GetQuality();
  808.     D3DRMSHADEMODE oldsm = (D3DRMSHADEMODE)(quality & D3DRMSHADE_MASK);
  809.     HRESULT rval;
  810.  
  811.     menu = GetMenu(win);
  812.     menu = GetSubMenu(menu, 2);
  813.     CheckMenuItem(menu, ShadeModeToMenuItem(oldsm),
  814.                   MF_BYPOSITION|MF_UNCHECKED);
  815.     CheckMenuItem(menu, ShadeModeToMenuItem(sm),
  816.                   MF_BYPOSITION|MF_CHECKED);
  817.  
  818.     quality = (quality & ~D3DRMSHADE_MASK) | sm;
  819.     rval = info->dev->SetQuality(quality);
  820.     if (rval != D3DRM_OK) {
  821.         Msg("Setting the render quality while changing the shade mode failed.\n%s", MyErrorToString(rval));
  822.         return FALSE;
  823.     }
  824.     return TRUE;
  825. }
  826.  
  827. static BOOL SetModel(HWND win, AppInfo* info, D3DRMCOLORMODEL model)
  828. {
  829.     HMENU menu;
  830.     D3DRMCOLORMODEL oldModel = info->model;
  831.  
  832.     if (oldModel == model) return TRUE;
  833.  
  834.     info->model = model;
  835.     if (!RebuildDevice(win, info, info->dev->GetWidth(), info->dev->GetHeight()))
  836.         return FALSE;
  837.  
  838.     menu = GetMenu(win);
  839.     menu = GetSubMenu(menu, 2);
  840.     CheckMenuItem(menu, 9 + (int) oldModel, MF_BYPOSITION|MF_UNCHECKED);
  841.     CheckMenuItem(menu, 9 + (int) model, MF_BYPOSITION|MF_CHECKED);
  842.     return TRUE;
  843. }
  844.  
  845. static BOOL ToggleDither(HWND win, AppInfo *info)
  846. {
  847.     HMENU menu;
  848.     int dither = info->dev->GetDither();
  849.     int checked;
  850.     dither = !dither;
  851.     if (FAILED(info->dev->SetDither(dither))) {
  852.         Msg("Toggling dithering failed.\n");
  853.         return FALSE;
  854.     }
  855.     menu = GetMenu(win);
  856.     menu = GetSubMenu(menu, 2);
  857.  
  858.     if (dither) checked = MF_CHECKED;
  859.     else checked = MF_UNCHECKED;
  860.  
  861.     //CheckMenuItem(menu, MENU_DITHER, MF_BYCOMMAND|checked);
  862.     CheckMenuItem(menu, 13, MF_BYPOSITION|checked);
  863.     return TRUE;
  864. }
  865.  
  866. static BOOL ToggleTextureFiltering(HWND win, AppInfo *info)
  867. {
  868.     HMENU menu;
  869.     D3DRMTEXTUREQUALITY tq = info->dev->GetTextureQuality();
  870.     int checked;
  871.     if (tq == D3DRMTEXTURE_NEAREST)
  872.         tq = D3DRMTEXTURE_LINEAR;
  873.     else
  874.         tq = D3DRMTEXTURE_NEAREST;
  875.  
  876.     if (FAILED(info->dev->SetTextureQuality(tq))) {
  877.         Msg("Setting texture quality failed.\n");
  878.         return FALSE;
  879.     }
  880.     menu = GetMenu(win);
  881.     menu = GetSubMenu(menu, 2);
  882.  
  883.     if (tq == D3DRMTEXTURE_LINEAR) checked = MF_CHECKED;
  884.     else checked = MF_UNCHECKED;
  885.  
  886.     CheckMenuItem(menu, 14, MF_BYPOSITION|checked);
  887.     return TRUE;
  888. }
  889.  
  890. static BOOL
  891. CreateLight(WPARAM wparam, AppInfo* info)
  892. {
  893.     LPDIRECT3DRMMESHBUILDER builder = NULL;
  894.     LPDIRECT3DRMLIGHT light = NULL;
  895.     LPDIRECT3DRMFRAME frame = NULL;
  896.     HRESULT rval;
  897.  
  898.     if (FAILED(lpD3DRM->CreateMeshBuilder(&builder)))
  899.         goto generic_error;
  900.  
  901.     if (wparam == MENU_LIGHT_DIRECTIONAL) {
  902.         rval = builder->Load("camera.x", NULL, D3DRMLOAD_FROMFILE,
  903.                              NULL, NULL);
  904.         if (rval != D3DRM_OK) {
  905.             Msg("Failed to load camera.x.\n%s", MyErrorToString(rval));
  906.             goto ret_with_error;
  907.         }
  908.         if (FAILED(builder->SetQuality(D3DRMRENDER_UNLITFLAT)))
  909.             goto generic_error;
  910.         if (FAILED(lpD3DRM->CreateLightRGB
  911.             (D3DRMLIGHT_DIRECTIONAL, D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0), &light)))
  912.             goto generic_error;
  913.     } else if (wparam == MENU_LIGHT_PARALLEL_POINT) {
  914.         rval = builder->Load("sphere2.x", NULL, D3DRMLOAD_FROMFILE,
  915.                              NULL, NULL);
  916.         if (rval != D3DRM_OK) {
  917.             Msg("Failed to load sphere2.x.\n%s", MyErrorToString(rval));
  918.             goto ret_with_error;
  919.         }
  920.         if (FAILED(builder->SetQuality(D3DRMRENDER_UNLITFLAT)))
  921.             goto generic_error;
  922.         if (FAILED(builder->Scale(D3DVAL(0.2), D3DVAL(0.2), D3DVAL(0.2))))
  923.             goto generic_error;
  924.         if (FAILED(lpD3DRM->CreateLightRGB
  925.             (D3DRMLIGHT_PARALLELPOINT, D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0), &light)))
  926.             goto generic_error;
  927.     } else if (wparam == MENU_LIGHT_POINT) {
  928.         rval = builder->Load("sphere2.x", NULL, D3DRMLOAD_FROMFILE,
  929.                              NULL, NULL);
  930.         if (rval != D3DRM_OK) {
  931.             Msg("Failed to load sphere2.x.\n%s", MyErrorToString(rval));
  932.             goto ret_with_error;
  933.         }
  934.         if (FAILED(builder->SetQuality(D3DRMRENDER_UNLITFLAT)))
  935.             goto generic_error;
  936.         if (FAILED(builder->Scale(D3DVAL(0.2), D3DVAL(0.2), D3DVAL(0.2))))
  937.             goto generic_error;
  938.         if (FAILED(lpD3DRM->CreateLightRGB
  939.             (D3DRMLIGHT_POINT, D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0), &light)))
  940.             goto generic_error;
  941.     } else if (wparam == MENU_LIGHT_SPOT) {
  942.         rval = builder->Load("camera.x", NULL, D3DRMLOAD_FROMFILE,
  943.                              NULL, NULL);
  944.         if (rval != D3DRM_OK) {
  945.             Msg("Failed to load camera.x.\n%s", MyErrorToString(rval));
  946.             goto ret_with_error;
  947.         }
  948.         if (FAILED(builder->SetQuality(D3DRMRENDER_UNLITFLAT)))
  949.             goto generic_error;
  950.         if (FAILED(lpD3DRM->CreateLightRGB(D3DRMLIGHT_SPOT, D3DVAL(1.0), D3DVAL(1.0), D3DVAL(1.0), &light)))
  951.             goto generic_error;
  952.     }
  953.     if (FAILED(lpD3DRM->CreateFrame(info->scene, &frame)))
  954.         goto generic_error;
  955.     if (FAILED(frame->SetPosition(info->camera, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(10.0))))
  956.         goto generic_error;
  957.     if (FAILED(frame->AddVisual(builder)))
  958.         goto generic_error;
  959.     if (FAILED(frame->AddLight(light)))
  960.         goto generic_error;
  961.  
  962.     builder->Release(), frame->Release(), light->Release();
  963.     return TRUE;
  964. generic_error:
  965.     Msg("A failure occurred while creating a new light.\n");
  966. ret_with_error:
  967.     RELEASE(builder);
  968.     RELEASE(light);
  969.     RELEASE(frame);
  970.     return FALSE;
  971. }
  972.  
  973. HRESULT loadTextures(char *name, void *arg, LPDIRECT3DRMTEXTURE *tex)
  974. {
  975.     /* IDirect3DRM::LoadTexture checks whether the texture is a PPM or
  976.     BMP which it knows how to load. If you want to load other formats 
  977.     you could add code to the callback to load the code into an D3DRMIMAGE
  978.     structure and call IDirect3DRM::CreateTexture */
  979.     
  980.     return lpD3DRM->LoadTexture(name, tex);
  981. }
  982.  
  983. #define SIGN_EXTEND(w)    ((((int)(w)) << 16) >> 16)
  984.  
  985. /*
  986.  * Handle messages for the main application window
  987.  */
  988. LONG FAR PASCAL WindowProc(HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
  989. {
  990.     static HCURSOR oldCursor = NULL;
  991.     AppInfo *info;
  992.     LPDIRECT3DRMFRAME sFrame = SelectedFrame();
  993.     LPDIRECT3DRMMESHBUILDER sVisual = SelectedVisual();
  994.  
  995.     info = (AppInfo *) GetWindowLong(win, 0);
  996.     active_window = info;
  997.  
  998.     switch (msg)
  999.     {
  1000.     case WM_KEYDOWN:
  1001.         {   D3DVECTOR dir, up, right;
  1002.  
  1003.             info->camera->GetOrientation(info->scene, &dir, &up);
  1004.             D3DRMVectorCrossProduct(&right, &up, &dir);
  1005.             up.x /= D3DVAL(4.0);
  1006.             up.y /= D3DVAL(4.0);
  1007.             up.z /= D3DVAL(4.0);
  1008.             right.x /= D3DVAL(4.0);
  1009.             right.y /= D3DVAL(4.0);
  1010.             right.z /= D3DVAL(4.0);
  1011.  
  1012.             switch (wparam)
  1013.             {
  1014.             case 'T':
  1015.                 info->camera->SetVelocity(info->scene, dir.x, dir.y, dir.z, FALSE);
  1016.                 break;
  1017.  
  1018.             case 'Y':
  1019.                 info->camera->SetVelocity(info->scene, D3DVAL(-100.0) * dir.x,
  1020.                                           D3DVAL(-100.0) * dir.y, 
  1021.                                           D3DVAL(-100.0) * dir.z, FALSE);
  1022.  
  1023.             case 'R':
  1024.                 info->camera->SetVelocity(info->scene, -dir.x, -dir.y, -dir.z, FALSE);
  1025.                 break;
  1026.                  
  1027.             case 'E':
  1028.                 info->camera->SetVelocity(info->scene, D3DVAL(100.0) * dir.x,
  1029.                                           D3DVAL(100.0) * dir.y, 
  1030.                                           D3DVAL(100.0) * dir.z, FALSE);
  1031.             case VK_UP:
  1032.                 info->camera->SetVelocity(info->scene, up.x, up.y, up.z, FALSE);
  1033.                 break;
  1034.  
  1035.             case VK_DOWN:
  1036.                 info->camera->SetVelocity(info->scene, -up.x, -up.y, -up.z, FALSE);
  1037.                 break;
  1038.  
  1039.             case VK_RIGHT:
  1040.                 info->camera->SetVelocity(info->scene, right.x, right.y, right.z, FALSE);
  1041.                 break;
  1042.  
  1043.             case VK_LEFT:
  1044.                 info->camera->SetVelocity(info->scene, -right.x, -right.y, -right.z, FALSE);
  1045.                 break;
  1046.  
  1047.             case 'X':
  1048.                 if (sFrame)
  1049.                     sFrame->SetVelocity(info->scene, dir.x, dir.y, dir.z, FALSE);
  1050.                 break;
  1051.  
  1052.             case 'Z':
  1053.                 if (sFrame)
  1054.                     sFrame->SetVelocity(info->scene, -dir.x, -dir.y, -dir.z, FALSE);
  1055.                 break;
  1056.  
  1057.             case VK_SUBTRACT:
  1058.                 if (sFrame)
  1059.                 {   sVisual->Scale(D3DVAL(0.9), D3DVAL(0.9), D3DVAL(0.9));
  1060.                     SelectVisual(sVisual, sFrame);
  1061.                 }
  1062.                 break;
  1063.  
  1064.             case VK_ADD:
  1065.                 if (sFrame)
  1066.                 {   sVisual->Scale(D3DVAL(1.1), D3DVAL(1.1), D3DVAL(1.1));
  1067.                     SelectVisual(sVisual, sFrame);
  1068.                 }
  1069.                 break;
  1070.             }
  1071.         }
  1072.         break;
  1073.  
  1074.     case WM_KEYUP:
  1075.         switch (wparam)
  1076.         {
  1077.         case 'T':
  1078.         case 'R':
  1079.         case VK_UP:
  1080.         case VK_DOWN:
  1081.         case VK_LEFT:
  1082.         case VK_RIGHT:
  1083.             info->camera->SetVelocity(info->scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0), FALSE);
  1084.             break;
  1085.  
  1086.         case 'Z':
  1087.         case 'X':
  1088.             if (sFrame)
  1089.                 sFrame->SetVelocity(info->scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0), FALSE);
  1090.             break;
  1091.         }
  1092.         break;
  1093.  
  1094.     case WM_LBUTTONDOWN:
  1095.         {   HCURSOR hCur;
  1096.             int x = LOWORD(lparam);
  1097.             int y = HIWORD(lparam);
  1098.             last_x = x;
  1099.             last_y = y;
  1100.             FindAndSelectVisual(info->view, x, y);
  1101.             left_drag = TRUE;
  1102.             SetCapture(win);
  1103.             /* change to a groovy cursor */
  1104.             hCur = LoadCursor(NULL, IDC_ARROW);
  1105.             if (hCur) oldCursor = SetCursor(hCur);
  1106.             else oldCursor = NULL;
  1107.         }
  1108.         break;
  1109.  
  1110.     case WM_LBUTTONUP:
  1111.         ReleaseCapture();
  1112.         left_drag = FALSE;
  1113.         if (oldCursor) SetCursor(oldCursor);
  1114.         break;
  1115.  
  1116.     case WM_RBUTTONDOWN:
  1117.         {
  1118.             HCURSOR hCur;
  1119.             int x = LOWORD(lparam);
  1120.             int y = HIWORD(lparam);
  1121.             last_x = x;
  1122.             last_y = y;
  1123.             FindAndSelectVisual(info->view, x, y);
  1124.             right_drag = TRUE;
  1125.             SetCapture(win);
  1126.             /* change to a groovy cursor */
  1127.             hCur = LoadCursor(NULL, IDC_ARROW);
  1128.             if (hCur) oldCursor = SetCursor(hCur);
  1129.             else oldCursor = NULL;
  1130.         }
  1131.         break;
  1132.  
  1133.     case WM_RBUTTONUP:
  1134.         right_drag = FALSE;
  1135.         ReleaseCapture();
  1136.         if (oldCursor) SetCursor(oldCursor);
  1137.         break;
  1138.  
  1139.     case WM_MOUSEMOVE:
  1140.         if ((wparam & MK_LBUTTON) && sFrame && left_drag)
  1141.         {   double delta_x, delta_y;
  1142.             delta_x = SIGN_EXTEND(LOWORD(lparam)) - last_x;
  1143.             delta_y = -SIGN_EXTEND((HIWORD(lparam)) - last_y);
  1144.             last_x = SIGN_EXTEND(LOWORD(lparam));
  1145.             last_y = SIGN_EXTEND(HIWORD(lparam));
  1146.             {
  1147.                 double delta_r = sqrt(delta_x * delta_x + delta_y * delta_y);
  1148.                 double radius = 50;
  1149.                 double denom;
  1150.  
  1151.                 denom = sqrt(radius * radius + delta_r * delta_r);
  1152.  
  1153.                 if (delta_r == 0 || denom == 0) break;
  1154.                 sFrame->SetRotation
  1155.                 (   info->camera,
  1156.                     D3DDivide(D3DVAL((float) delta_y), D3DVAL((float) delta_r)),
  1157.                     D3DDivide(D3DVAL((float) -delta_x), D3DVAL((float) delta_r)),
  1158.                     D3DVAL(0.0),
  1159.                     D3DDivide(D3DVAL((float) delta_r), D3DVAL((float) denom))
  1160.                 );
  1161.             }
  1162.         }
  1163.         else if ((wparam & MK_RBUTTON) && sFrame && right_drag)
  1164.         {   double delta_x, delta_y;
  1165.             D3DVECTOR p1;
  1166.             D3DRMVECTOR4D p2;
  1167.  
  1168.             delta_x = SIGN_EXTEND(LOWORD(lparam)) - last_x;
  1169.             delta_y = SIGN_EXTEND(HIWORD(lparam)) - last_y;
  1170.             last_x = SIGN_EXTEND(LOWORD(lparam));
  1171.             last_y = SIGN_EXTEND(HIWORD(lparam));
  1172.             sFrame->GetPosition(info->scene, &p1);
  1173.             info->view->Transform(&p2, &p1);
  1174.             p2.x += D3DMultiply(D3DVAL((float)delta_x), p2.w);
  1175.             p2.y += D3DMultiply(D3DVAL((float)delta_y), p2.w);
  1176.             info->view->InverseTransform(&p1, &p2);
  1177.             sFrame->SetPosition(info->scene, p1.x, p1.y, p1.z);
  1178.         }
  1179.         break;
  1180.  
  1181.     case WM_COMMAND:
  1182.         switch( wparam & 0xffff )
  1183.         {
  1184.         case MENU_FILE_ABOUT:
  1185.             DialogBox((HINSTANCE) GetWindowLong(win, GWL_HINSTANCE),"AboutBox", win, (DLGPROC)AboutDlgProc);
  1186.             break;
  1187.  
  1188.         case MENU_FILE_OPEN:
  1189.           {  LPDIRECT3DRMMESHBUILDER builder;
  1190.              HRESULT rval;
  1191.              char *file = OpenNewFile(win, "Open a Mesh file");
  1192.              if (file)
  1193.                {
  1194.                  if (FAILED(lpD3DRM->CreateMeshBuilder(&builder))) {
  1195.                    Msg("Failed the create a builder for the new mesh.\n");
  1196.                    break;
  1197.                  }
  1198.                  rval = builder->Load(file, NULL, D3DRMLOAD_FROMFILE,
  1199.                                       loadTextures, NULL);
  1200.                  if (rval != D3DRM_OK) {
  1201.                    Msg("Loading %s failed.\n%s", file, MyErrorToString(rval));
  1202.                    builder->Release();
  1203.                    break;
  1204.                  }
  1205.                  if (!PlaceMesh(builder, info)) {
  1206.                    Msg("Placing the mesh in the scene failed.\n");
  1207.                    builder->Release();
  1208.                    break;
  1209.                  }
  1210.                  builder->Release();
  1211.                }
  1212.  
  1213.              /*LoadAnimationSet(file, info);*/
  1214.              break;
  1215.           }
  1216.  
  1217.         case MENU_FILE_OPEN_ANIMSET:
  1218.           {
  1219.             char *file = OpenNewFile(win, "Open Animation ...");
  1220.             if (file) {
  1221.               if (LoadAnimationSet(file, info) == FALSE) {
  1222.                 Msg("Loading and placing of %s failed.\n", file);
  1223.               }
  1224.             }
  1225.             break;
  1226.           }
  1227.  
  1228.         case MENU_FILE_OPEN_FRAME:
  1229.             {
  1230.               char *file = OpenNewFile(win, "Open Frame ...");
  1231.               if (file) {
  1232.                 if (LoadFrameHierarchy(file, info) == FALSE) {
  1233.                   Msg("Loading and placing of %s failed.\n", file);
  1234.                 }
  1235.               }
  1236.               break;
  1237.             }
  1238.  
  1239.         case MENU_FILE_EXIT:
  1240.             PostQuitMessage(0);
  1241.             break;
  1242.  
  1243.         case MENU_EDIT_CUT:
  1244.             CutVisual();
  1245.             break;
  1246.  
  1247.         case MENU_EDIT_COPY:
  1248.             CopyVisual();
  1249.             break;
  1250.  
  1251.         case MENU_EDIT_PASTE:
  1252.             PasteVisual(info->scene);
  1253.             break;
  1254.  
  1255.         case MENU_EDIT_DELETE:
  1256.             DeleteVisual();
  1257.             break;
  1258.  
  1259.         case MENU_EDIT_COLOR:
  1260.             if (sFrame)
  1261.             {
  1262.                 LPDIRECT3DRMMESHBUILDER mesh;
  1263.                 HRESULT rval;
  1264.  
  1265.                 if (FAILED(sVisual->QueryInterface(IID_IDirect3DRMMeshBuilder,
  1266.                                                    (void**) &mesh)))
  1267.                     break;
  1268.  
  1269.                 if (SelectedLight())
  1270.                 {
  1271.                     D3DCOLOR c = SelectedLight()->GetColor();
  1272.  
  1273.                     if (ChooseNewColor(win, &c)) {
  1274.                         mesh->SetColor(c);
  1275.                         SelectedLight()->SetColor(c);
  1276.                     }
  1277.                 } else {
  1278.                     D3DCOLOR c;
  1279.  
  1280.                     if (mesh->GetFaceCount()) {
  1281.                         LPDIRECT3DRMFACEARRAY faces;
  1282.                         LPDIRECT3DRMFACE face;
  1283.                         mesh->GetFaces(&faces);
  1284.                         faces->GetElement(0, &face);
  1285.                         c = face->GetColor();
  1286.                         RELEASE(face);
  1287.                         RELEASE(faces);
  1288.                     } else
  1289.                         c = D3DRMCreateColorRGB(D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0));
  1290.  
  1291.                     if (ChooseNewColor(win, &c)) {
  1292.                         rval = mesh->SetColor(c);
  1293.                         if (rval != D3DRM_OK)
  1294.                             Msg("Failed to set the mesh's color.\n%s", MyErrorToString(rval));
  1295.                     }
  1296.                 }
  1297.  
  1298.                 RELEASE(mesh);
  1299.             }
  1300.             break;
  1301.  
  1302.         case MENU_EDIT_BOXES:
  1303.             {
  1304.                 HMENU menu;
  1305.                 int checked = ToggleBoxes() ? MF_CHECKED : MF_UNCHECKED;
  1306.                 menu = GetMenu(win);
  1307.                 menu = GetSubMenu(menu, 1);
  1308.                 CheckMenuItem(menu, MENU_EDIT_BOXES, MF_BYCOMMAND|checked);
  1309.             }
  1310.             break;
  1311.  
  1312.         case MENU_QUALITY_LIGHTING:
  1313.             ToggleLighting(win, info);
  1314.             break;
  1315.  
  1316.         case MENU_QUALITY_POINTS:
  1317.             SetFillMode(win, info, D3DRMFILL_POINTS);
  1318.             break;
  1319.  
  1320.         case MENU_QUALITY_WIREFRAME:
  1321.             SetFillMode(win, info, D3DRMFILL_WIREFRAME);
  1322.             break;
  1323.  
  1324.         case MENU_QUALITY_SOLID:
  1325.             SetFillMode(win, info, D3DRMFILL_SOLID);
  1326.             break;
  1327.  
  1328.         case MENU_QUALITY_FLAT:
  1329.             SetShadeMode(win, info, D3DRMSHADE_FLAT);
  1330.             break;
  1331.  
  1332.         case MENU_QUALITY_GOURAUD:
  1333.             SetShadeMode(win, info, D3DRMSHADE_GOURAUD);
  1334.             break;
  1335.  
  1336.         case MENU_QUALITY_PHONG:
  1337.             SetShadeMode(win, info, D3DRMSHADE_PHONG);
  1338.             break;
  1339.  
  1340.         case MENU_MODEL_MONO:
  1341.             if (!SetModel(win, info, D3DCOLOR_MONO))
  1342.                 bQuit = TRUE;
  1343.             break;
  1344.  
  1345.         case MENU_MODEL_RGB:
  1346.             if (!SetModel(win, info, D3DCOLOR_RGB))
  1347.                 bQuit = TRUE;
  1348.             break;
  1349.  
  1350.         case MENU_DITHER:
  1351.             ToggleDither(win, info);
  1352.             break;
  1353.  
  1354.         case MENU_TEXTURE_FILTERING:
  1355.             ToggleTextureFiltering(win, info);
  1356.             break;
  1357.  
  1358.         case MENU_LIGHT_DIRECTIONAL:
  1359.         case MENU_LIGHT_PARALLEL_POINT:
  1360.         case MENU_LIGHT_POINT:
  1361.         case MENU_LIGHT_SPOT:
  1362.             {
  1363.                 CreateLight(wparam, info);
  1364.             }
  1365.             break;
  1366.         }
  1367.         break;
  1368.  
  1369.     case WM_DESTROY:
  1370.         PostQuitMessage( 0 );
  1371.         bQuit = TRUE;
  1372.         break;
  1373.  
  1374.     case WM_SIZE:
  1375.         {
  1376.             int width = LOWORD(lparam);
  1377.             int height = HIWORD(lparam);
  1378.                 if (width && height) {
  1379.                         if (!ResizeViewport(win, info, width, height)) {
  1380.                             bQuit = TRUE;
  1381.                             break;
  1382.                         }   
  1383.                         active_window->bMinimized = FALSE;
  1384.                 } else {
  1385.                         active_window->bMinimized = TRUE;
  1386.                 }
  1387.         }
  1388.         break;
  1389.  
  1390.     case WM_ACTIVATE:
  1391.         {   LPDIRECT3DRMWINDEVICE windev;
  1392.             if (!info || !info->dev) break;
  1393.             if (SUCCEEDED(info->dev->QueryInterface(IID_IDirect3DRMWinDevice, (void **) &windev)))
  1394.             {   if (FAILED(windev->HandleActivate(wparam)))
  1395.                     Msg("Failed to handle WM_ACTIVATE.\n");
  1396.                 windev->Release();
  1397.             } else {
  1398.                 Msg("Failed to create Windows device to handle WM_ACTIVATE.\n");
  1399.             }
  1400.         }
  1401.         break;
  1402.  
  1403.     case WM_PAINT:
  1404.         if (info)
  1405.         {   RECT r;
  1406.             PAINTSTRUCT ps;
  1407.             LPDIRECT3DRMWINDEVICE windev;
  1408.  
  1409.             if (!info || !info->dev) break;
  1410.             if (GetUpdateRect(win, &r, FALSE))
  1411.             {   BeginPaint(win, &ps);
  1412.                 if (SUCCEEDED(info->dev->QueryInterface(IID_IDirect3DRMWinDevice, (void **) &windev)))
  1413.                 {   if (FAILED(windev->HandlePaint(ps.hdc)))
  1414.                         Msg("Failed to handle WM_PAINT.\n");
  1415.                     windev->Release();
  1416.                 } else {
  1417.                     Msg("Failed to create Windows device to handle WM_PAINT.\n");
  1418.                 }
  1419.                 EndPaint(win, &ps);
  1420.             }
  1421.         }
  1422.  
  1423.         else return DefWindowProc(win, msg, wparam, lparam);
  1424.  
  1425.     default:
  1426.         return DefWindowProc(win, msg, wparam, lparam);
  1427.     }
  1428.     return 0L;
  1429. }
  1430.  
  1431. /*
  1432.  * MyErrorToString
  1433.  * Returns a pointer to a string describing the given error code.
  1434.  */
  1435. char*
  1436. MyErrorToString(HRESULT error)
  1437. {
  1438.     switch(error) {
  1439.         case DD_OK:
  1440.             /* Also includes D3D_OK and D3DRM_OK */
  1441.             return "No error.\0";
  1442.         case DDERR_ALREADYINITIALIZED:
  1443.             return "This object is already initialized.\0";
  1444.         case DDERR_BLTFASTCANTCLIP:
  1445.             return "Return if a clipper object is attached to the source surface passed into a BltFast call.\0";
  1446.         case DDERR_CANNOTATTACHSURFACE:
  1447.             return "This surface can not be attached to the requested surface.\0";
  1448.         case DDERR_CANNOTDETACHSURFACE:
  1449.             return "This surface can not be detached from the requested surface.\0";
  1450.         case DDERR_CANTCREATEDC:
  1451.             return "Windows can not create any more DCs.\0";
  1452.         case DDERR_CANTDUPLICATE:
  1453.             return "Can't duplicate primary & 3D surfaces, or surfaces that are implicitly created.\0";
  1454.         case DDERR_CLIPPERISUSINGHWND:
  1455.             return "An attempt was made to set a cliplist for a clipper object that is already monitoring an hwnd.\0";
  1456.         case DDERR_COLORKEYNOTSET:
  1457.             return "No src color key specified for this operation.\0";
  1458.         case DDERR_CURRENTLYNOTAVAIL:
  1459.             return "Support is currently not available.\0";
  1460.         case DDERR_DIRECTDRAWALREADYCREATED:
  1461.             return "A DirectDraw object representing this driver has already been created for this process.\0";
  1462.         case DDERR_EXCEPTION:
  1463.             return "An exception was encountered while performing the requested operation.\0";
  1464.         case DDERR_EXCLUSIVEMODEALREADYSET:
  1465.             return "An attempt was made to set the cooperative level when it was already set to exclusive.\0";
  1466.         case DDERR_GENERIC:
  1467.             return "Generic failure.\0";
  1468.         case DDERR_HEIGHTALIGN:
  1469.             return "Height of rectangle provided is not a multiple of reqd alignment.\0";
  1470.         case DDERR_HWNDALREADYSET:
  1471.             return "The CooperativeLevel HWND has already been set. It can not be reset while the process has surfaces or palettes created.\0";
  1472.         case DDERR_HWNDSUBCLASSED:
  1473.             return "HWND used by DirectDraw CooperativeLevel has been subclassed, this prevents DirectDraw from restoring state.\0";
  1474.         case DDERR_IMPLICITLYCREATED:
  1475.             return "This surface can not be restored because it is an implicitly created surface.\0";
  1476.         case DDERR_INCOMPATIBLEPRIMARY:
  1477.             return "Unable to match primary surface creation request with existing primary surface.\0";
  1478.         case DDERR_INVALIDCAPS:
  1479.             return "One or more of the caps bits passed to the callback are incorrect.\0";
  1480.         case DDERR_INVALIDCLIPLIST:
  1481.             return "DirectDraw does not support the provided cliplist.\0";
  1482.         case DDERR_INVALIDDIRECTDRAWGUID:
  1483.             return "The GUID passed to DirectDrawCreate is not a valid DirectDraw driver identifier.\0";
  1484.         case DDERR_INVALIDMODE:
  1485.             return "DirectDraw does not support the requested mode.\0";
  1486.         case DDERR_INVALIDOBJECT:
  1487.             return "DirectDraw received a pointer that was an invalid DIRECTDRAW object.\0";
  1488.         case DDERR_INVALIDPARAMS:
  1489.             return "One or more of the parameters passed to the function are incorrect.\0";
  1490.         case DDERR_INVALIDPIXELFORMAT:
  1491.             return "The pixel format was invalid as specified.\0";
  1492.         case DDERR_INVALIDPOSITION:
  1493.             return "Returned when the position of the overlay on the destination is no longer legal for that destination.\0";
  1494.         case DDERR_INVALIDRECT:
  1495.             return "Rectangle provided was invalid.\0";
  1496.         case DDERR_LOCKEDSURFACES:
  1497.             return "Operation could not be carried out because one or more surfaces are locked.\0";
  1498.         case DDERR_NO3D:
  1499.             return "There is no 3D present.\0";
  1500.         case DDERR_NOALPHAHW:
  1501.             return "Operation could not be carried out because there is no alpha accleration hardware present or available.\0";
  1502.         case DDERR_NOBLTHW:
  1503.             return "No blitter hardware present.\0";
  1504.         case DDERR_NOCLIPLIST:
  1505.             return "No cliplist available.\0";
  1506.         case DDERR_NOCLIPPERATTACHED:
  1507.             return "No clipper object attached to surface object.\0";
  1508.         case DDERR_NOCOLORCONVHW:
  1509.             return "Operation could not be carried out because there is no color conversion hardware present or available.\0";
  1510.         case DDERR_NOCOLORKEY:
  1511.             return "Surface doesn't currently have a color key\0";
  1512.         case DDERR_NOCOLORKEYHW:
  1513.             return "Operation could not be carried out because there is no hardware support of the destination color key.\0";
  1514.         case DDERR_NOCOOPERATIVELEVELSET:
  1515.             return "Create function called without DirectDraw object method SetCooperativeLevel being called.\0";
  1516.         case DDERR_NODC:
  1517.             return "No DC was ever created for this surface.\0";
  1518.         case DDERR_NODDROPSHW:
  1519.             return "No DirectDraw ROP hardware.\0";
  1520.         case DDERR_NODIRECTDRAWHW:
  1521.             return "A hardware-only DirectDraw object creation was attempted but the driver did not support any hardware.\0";
  1522.         case DDERR_NOEMULATION:
  1523.             return "Software emulation not available.\0";
  1524.         case DDERR_NOEXCLUSIVEMODE:
  1525.             return "Operation requires the application to have exclusive mode but the application does not have exclusive mode.\0";
  1526.         case DDERR_NOFLIPHW:
  1527.             return "Flipping visible surfaces is not supported.\0";
  1528.         case DDERR_NOGDI:
  1529.             return "There is no GDI present.\0";
  1530.         case DDERR_NOHWND:
  1531.             return "Clipper notification requires an HWND or no HWND has previously been set as the CooperativeLevel HWND.\0";
  1532.         case DDERR_NOMIRRORHW:
  1533.             return "Operation could not be carried out because there is no hardware present or available.\0";
  1534.         case DDERR_NOOVERLAYDEST:
  1535.             return "Returned when GetOverlayPosition is called on an overlay that UpdateOverlay has never been called on to establish a destination.\0";
  1536.         case DDERR_NOOVERLAYHW:
  1537.             return "Operation could not be carried out because there is no overlay hardware present or available.\0";
  1538.         case DDERR_NOPALETTEATTACHED:
  1539.             return "No palette object attached to this surface.\0";
  1540.         case DDERR_NOPALETTEHW:
  1541.             return "No hardware support for 16 or 256 color palettes.\0";
  1542.         case DDERR_NORASTEROPHW:
  1543.             return "Operation could not be carried out because there is no appropriate raster op hardware present or available.\0";
  1544.         case DDERR_NOROTATIONHW:
  1545.             return "Operation could not be carried out because there is no rotation hardware present or available.\0";
  1546.         case DDERR_NOSTRETCHHW:
  1547.             return "Operation could not be carried out because there is no hardware support for stretching.\0";
  1548.         case DDERR_NOT4BITCOLOR:
  1549.             return "DirectDrawSurface is not in 4 bit color palette and the requested operation requires 4 bit color palette.\0";
  1550.         case DDERR_NOT4BITCOLORINDEX:
  1551.             return "DirectDrawSurface is not in 4 bit color index palette and the requested operation requires 4 bit color index palette.\0";
  1552.         case DDERR_NOT8BITCOLOR:
  1553.             return "DirectDrawSurface is not in 8 bit color mode and the requested operation requires 8 bit color.\0";
  1554.         case DDERR_NOTAOVERLAYSURFACE:
  1555.             return "Returned when an overlay member is called for a non-overlay surface.\0";
  1556.         case DDERR_NOTEXTUREHW:
  1557.             return "Operation could not be carried out because there is no texture mapping hardware present or available.\0";
  1558.         case DDERR_NOTFLIPPABLE:
  1559.             return "An attempt has been made to flip a surface that is not flippable.\0";
  1560.         case DDERR_NOTFOUND:
  1561.             return "Requested item was not found.\0";
  1562.         case DDERR_NOTLOCKED:
  1563.             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";
  1564.         case DDERR_NOTPALETTIZED:
  1565.             return "The surface being used is not a palette-based surface.\0";
  1566.         case DDERR_NOVSYNCHW:
  1567.             return "Operation could not be carried out because there is no hardware support for vertical blank synchronized operations.\0";
  1568.         case DDERR_NOZBUFFERHW:
  1569.             return "Operation could not be carried out because there is no hardware support for zbuffer blitting.\0";
  1570.         case DDERR_NOZOVERLAYHW:
  1571.             return "Overlay surfaces could not be z layered based on their BltOrder because the hardware does not support z layering of overlays.\0";
  1572.         case DDERR_OUTOFCAPS:
  1573.             return "The hardware needed for the requested operation has already been allocated.\0";
  1574.         case DDERR_OUTOFMEMORY:
  1575.             return "DirectDraw does not have enough memory to perform the operation.\0";
  1576.         case DDERR_OUTOFVIDEOMEMORY:
  1577.             return "DirectDraw does not have enough memory to perform the operation.\0";
  1578.         case DDERR_OVERLAYCANTCLIP:
  1579.             return "The hardware does not support clipped overlays.\0";
  1580.         case DDERR_OVERLAYCOLORKEYONLYONEACTIVE:
  1581.             return "Can only have ony color key active at one time for overlays.\0";
  1582.         case DDERR_OVERLAYNOTVISIBLE:
  1583.             return "Returned when GetOverlayPosition is called on a hidden overlay.\0";
  1584.         case DDERR_PALETTEBUSY:
  1585.             return "Access to this palette is being refused because the palette is already locked by another thread.\0";
  1586.         case DDERR_PRIMARYSURFACEALREADYEXISTS:
  1587.             return "This process already has created a primary surface.\0";
  1588.         case DDERR_REGIONTOOSMALL:
  1589.             return "Region passed to Clipper::GetClipList is too small.\0";
  1590.         case DDERR_SURFACEALREADYATTACHED:
  1591.             return "This surface is already attached to the surface it is being attached to.\0";
  1592.         case DDERR_SURFACEALREADYDEPENDENT:
  1593.             return "This surface is already a dependency of the surface it is being made a dependency of.\0";
  1594.         case DDERR_SURFACEBUSY:
  1595.             return "Access to this surface is being refused because the surface is already locked by another thread.\0";
  1596.         case DDERR_SURFACEISOBSCURED:
  1597.             return "Access to surface refused because the surface is obscured.\0";
  1598.         case DDERR_SURFACELOST:
  1599.             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";
  1600.         case DDERR_SURFACENOTATTACHED:
  1601.             return "The requested surface is not attached.\0";
  1602.         case DDERR_TOOBIGHEIGHT:
  1603.             return "Height requested by DirectDraw is too large.\0";
  1604.         case DDERR_TOOBIGSIZE:
  1605.             return "Size requested by DirectDraw is too large, but the individual height and width are OK.\0";
  1606.         case DDERR_TOOBIGWIDTH:
  1607.             return "Width requested by DirectDraw is too large.\0";
  1608.         case DDERR_UNSUPPORTED:
  1609.             return "Action not supported.\0";
  1610.         case DDERR_UNSUPPORTEDFORMAT:
  1611.             return "FOURCC format requested is unsupported by DirectDraw.\0";
  1612.         case DDERR_UNSUPPORTEDMASK:
  1613.             return "Bitmask in the pixel format requested is unsupported by DirectDraw.\0";
  1614.         case DDERR_VERTICALBLANKINPROGRESS:
  1615.             return "Vertical blank is in progress.\0";
  1616.         case DDERR_WASSTILLDRAWING:
  1617.             return "Informs DirectDraw that the previous Blt which is transfering information to or from this Surface is incomplete.\0";
  1618.         case DDERR_WRONGMODE:
  1619.             return "This surface can not be restored because it was created in a different mode.\0";
  1620.         case DDERR_XALIGN:
  1621.             return "Rectangle provided was not horizontally aligned on required boundary.\0";
  1622.         case D3DERR_BADMAJORVERSION:
  1623.             return "D3DERR_BADMAJORVERSION\0";
  1624.         case D3DERR_BADMINORVERSION:
  1625.             return "D3DERR_BADMINORVERSION\0";
  1626.         case D3DERR_EXECUTE_LOCKED:
  1627.             return "D3DERR_EXECUTE_LOCKED\0";
  1628.         case D3DERR_EXECUTE_NOT_LOCKED:
  1629.             return "D3DERR_EXECUTE_NOT_LOCKED\0";
  1630.         case D3DERR_EXECUTE_CREATE_FAILED:
  1631.             return "D3DERR_EXECUTE_CREATE_FAILED\0";
  1632.         case D3DERR_EXECUTE_DESTROY_FAILED:
  1633.             return "D3DERR_EXECUTE_DESTROY_FAILED\0";
  1634.         case D3DERR_EXECUTE_LOCK_FAILED:
  1635.             return "D3DERR_EXECUTE_LOCK_FAILED\0";
  1636.         case D3DERR_EXECUTE_UNLOCK_FAILED:
  1637.             return "D3DERR_EXECUTE_UNLOCK_FAILED\0";
  1638.         case D3DERR_EXECUTE_FAILED:
  1639.             return "D3DERR_EXECUTE_FAILED\0";
  1640.         case D3DERR_EXECUTE_CLIPPED_FAILED:
  1641.             return "D3DERR_EXECUTE_CLIPPED_FAILED\0";
  1642.         case D3DERR_TEXTURE_NO_SUPPORT:
  1643.             return "D3DERR_TEXTURE_NO_SUPPORT\0";
  1644.         case D3DERR_TEXTURE_NOT_LOCKED:
  1645.             return "D3DERR_TEXTURE_NOT_LOCKED\0";
  1646.         case D3DERR_TEXTURE_LOCKED:
  1647.             return "D3DERR_TEXTURELOCKED\0";
  1648.         case D3DERR_TEXTURE_CREATE_FAILED:
  1649.             return "D3DERR_TEXTURE_CREATE_FAILED\0";
  1650.         case D3DERR_TEXTURE_DESTROY_FAILED:
  1651.             return "D3DERR_TEXTURE_DESTROY_FAILED\0";
  1652.         case D3DERR_TEXTURE_LOCK_FAILED:
  1653.             return "D3DERR_TEXTURE_LOCK_FAILED\0";
  1654.         case D3DERR_TEXTURE_UNLOCK_FAILED:
  1655.             return "D3DERR_TEXTURE_UNLOCK_FAILED\0";
  1656.         case D3DERR_TEXTURE_LOAD_FAILED:
  1657.             return "D3DERR_TEXTURE_LOAD_FAILED\0";
  1658.         case D3DERR_MATRIX_CREATE_FAILED:
  1659.             return "D3DERR_MATRIX_CREATE_FAILED\0";
  1660.         case D3DERR_MATRIX_DESTROY_FAILED:
  1661.             return "D3DERR_MATRIX_DESTROY_FAILED\0";
  1662.         case D3DERR_MATRIX_SETDATA_FAILED:
  1663.             return "D3DERR_MATRIX_SETDATA_FAILED\0";
  1664.         case D3DERR_SETVIEWPORTDATA_FAILED:
  1665.             return "D3DERR_SETVIEWPORTDATA_FAILED\0";
  1666.         case D3DERR_MATERIAL_CREATE_FAILED:
  1667.             return "D3DERR_MATERIAL_CREATE_FAILED\0";
  1668.         case D3DERR_MATERIAL_DESTROY_FAILED:
  1669.             return "D3DERR_MATERIAL_DESTROY_FAILED\0";
  1670.         case D3DERR_MATERIAL_SETDATA_FAILED:
  1671.             return "D3DERR_MATERIAL_SETDATA_FAILED\0";
  1672.         case D3DERR_LIGHT_SET_FAILED:
  1673.             return "D3DERR_LIGHT_SET_FAILED\0";
  1674.         case D3DRMERR_BADOBJECT:
  1675.             return "D3DRMERR_BADOBJECT\0";
  1676.         case D3DRMERR_BADTYPE:
  1677.             return "D3DRMERR_BADTYPE\0";
  1678.         case D3DRMERR_BADALLOC:
  1679.             return "D3DRMERR_BADALLOC\0";
  1680.         case D3DRMERR_FACEUSED:
  1681.             return "D3DRMERR_FACEUSED\0";
  1682.         case D3DRMERR_NOTFOUND:
  1683.             return "D3DRMERR_NOTFOUND\0";
  1684.         case D3DRMERR_NOTDONEYET:
  1685.             return "D3DRMERR_NOTDONEYET\0";
  1686.         case D3DRMERR_FILENOTFOUND:
  1687.             return "The file was not found.\0";
  1688.         case D3DRMERR_BADFILE:
  1689.             return "D3DRMERR_BADFILE\0";
  1690.         case D3DRMERR_BADDEVICE:
  1691.             return "D3DRMERR_BADDEVICE\0";
  1692.         case D3DRMERR_BADVALUE:
  1693.             return "D3DRMERR_BADVALUE\0";
  1694.         case D3DRMERR_BADMAJORVERSION:
  1695.             return "D3DRMERR_BADMAJORVERSION\0";
  1696.         case D3DRMERR_BADMINORVERSION:
  1697.             return "D3DRMERR_BADMINORVERSION\0";
  1698.         case D3DRMERR_UNABLETOEXECUTE:
  1699.             return "D3DRMERR_UNABLETOEXECUTE\0";
  1700.         default:
  1701.             return "Unrecognized error value.\0";
  1702.     }
  1703. }
  1704.